Skip to content

Commit 8557c1f

Browse files
committed
Add generation of toc
1 parent f8fb8e9 commit 8557c1f

File tree

8 files changed

+125
-33
lines changed

8 files changed

+125
-33
lines changed

BbCode/TagRenderer.php

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class TagRenderer
1010
private static int $subsubsecNum = 0;
1111
private static int $parNum = 0;
1212

13+
private static ?string $context = null;
14+
private static ?string $subContext = null;
15+
private static ?int $entityId = null;
16+
1317
private static function getContent($tagChildren, $removeNewLines = true)
1418
{
1519
if (is_string($tagChildren))
@@ -104,8 +108,10 @@ private static function getNumByDepth($depth, $override = -1)
104108

105109
}
106110

107-
private static function renderTag($depth, $tagChildren, $tagOption, $entity)
111+
private static function renderTag($depth, $tagChildren, $tagOption,
112+
$entity, \XF\BbCode\Renderer\AbstractRenderer $renderer)
108113
{
114+
self::resetCountersIfNeeded($entity, $renderer);
109115
$content = self::getContent($tagChildren);
110116
$depth = max($depth, 0);
111117
$depth = min($depth, 4);
@@ -121,10 +127,10 @@ private static function renderTag($depth, $tagChildren, $tagOption, $entity)
121127
$num = '';
122128
if (!empty($options['enumerate']))
123129
$num .= self::getNumByDepth($depth, $options['overrideNum'] ?? -1);
124-
$uniqueId = self::getEntityId($entity) . '-' . str_replace('.', '-', $num);
130+
$uniqueId = self::$entityId . '-' . str_replace('.', '-', $num);
125131
$anchorId = self::getAnchorId($uniqueId, $tagChildren);
126132
if (strlen($num) > 0)
127-
$num .= ' | ';
133+
$num .= '    ';
128134
return "<$tag class=\"$htmlClass\" id=\"$anchorId\">$num$content</$tag>";
129135
}
130136

@@ -161,54 +167,44 @@ public static function renderChapterTag($tagChildren, $tagOption, $tag,
161167
array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
162168
{
163169
return self::renderTag(0, $tagChildren, $tagOption,
164-
$options['entity']);
170+
$options['entity'], $renderer);
165171
}
166172

167173
public static function renderSectionTag($tagChildren, $tagOption, $tag,
168174
array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
169175
{
170176
return self::renderTag(1, $tagChildren, $tagOption,
171-
$options['entity']);
177+
$options['entity'], $renderer);
172178
}
173179

174180
public static function renderSubsectionTag($tagChildren, $tagOption,
175181
$tag, array $options,
176182
\XF\BbCode\Renderer\AbstractRenderer $renderer)
177183
{
178184
return self::renderTag(2, $tagChildren, $tagOption,
179-
$options['entity']);
185+
$options['entity'], $renderer);
180186
}
181187

182188
public static function renderSubsubsectionTag($tagChildren, $tagOption,
183189
$tag, array $options,
184190
\XF\BbCode\Renderer\AbstractRenderer $renderer)
185191
{
186192
return self::renderTag(3, $tagChildren, $tagOption,
187-
$options['entity']);
193+
$options['entity'], $renderer);
188194
}
189195

190196
public static function renderParagraphTag($tagChildren, $tagOption, $tag,
191197
array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
192198
{
193199
return self::renderTag(4, $tagChildren, $tagOption,
194-
$options['entity']);
200+
$options['entity'], $renderer);
195201
}
196202

197203
public static function renderTocTag($tagChildren, $tagOption, $tag,
198204
array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
199205
{
200-
//$renderer->getTemplater()->includeJs([
201-
// 'src' => 'inforge/posttoc/toc-renderer.js',
202-
// 'addon' => 'Inforge/PostTOC',
203-
// 'min' => true,
204-
// ]);
205-
self::resetCounters();
206-
$hideTitle = !empty($tagOption) &&
207-
(strtolower(trim($tagOption)) == 'notitle');
208206
return $renderer->getTemplater()->renderTemplate(
209-
'public:if_toc_bb_code_tag_toc', [
210-
'showTitle' => !$hideTitle,
211-
]);
207+
'public:if_toc_bb_code_tag_toc');
212208
}
213209

214210
private static function resetCounters($depth = 0)
@@ -226,4 +222,30 @@ private static function resetCounters($depth = 0)
226222
self::$parNum = 0;
227223
}
228224
}
225+
226+
private static function resetCountersIfNeeded($entity,
227+
\XF\BbCode\Renderer\AbstractRenderer $renderer)
228+
{
229+
$curId = self::getEntityId($entity);
230+
$curContext = self::getCurrentContext($renderer);
231+
$curSubContext = self::getCurrentSubContext($renderer);
232+
if (self::$entityId !== $curId
233+
|| self::$context !== $curContext
234+
|| self::$subContext !== $curSubContext) {
235+
self::$entityId = $curId;
236+
self::$context = $curContext;
237+
self::$subContext = $curSubContext;
238+
self::resetCounters();
239+
}
240+
}
241+
242+
private static function getCurrentContext(\XF\BbCode\Renderer\AbstractRenderer $renderer)
243+
{
244+
return $renderer->getRules()->getContext();
245+
}
246+
247+
private static function getCurrentSubContext(\XF\BbCode\Renderer\AbstractRenderer $renderer)
248+
{
249+
return $renderer->getRules()->getSubContext();
250+
}
229251
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function getTocEntries(post)
2+
{
3+
var entries = [];
4+
$(post).find('.posttoc-index').each(function() {
5+
var entry = new Object();
6+
entry.txt = $(this).text();
7+
entry.depth = parseInt($(this).prop('tagName').substring(1)) - 2;
8+
entry.anchor = '#' + $(this).prop('id');
9+
entries.push(entry);
10+
});
11+
return entries;
12+
}
13+
14+
function getNameFromDepth(depth)
15+
{
16+
switch (depth) {
17+
case 0:
18+
return 'chapter';
19+
case 2:
20+
return 'subsection';
21+
case 3:
22+
return 'subsubsection';
23+
case 4:
24+
return 'paragraph';
25+
default:
26+
return 'section';
27+
}
28+
}
29+
30+
function fillTableOfContents(entries, idx, depth, toc)
31+
{
32+
if (idx >= entries.length)
33+
return;
34+
for (var i = idx; i < entries.length; i++) {
35+
var entry = entries[i];
36+
if (entry.depth < depth)
37+
return i;
38+
var item = $('<li>');
39+
if (entry.depth > depth) {
40+
var newtoc = $('<ul>');
41+
$(item).append(newtoc);
42+
i = fillTableOfContents(entries, i, depth + 1, newtoc) - 1;
43+
} else {
44+
var link = $('<a>', {
45+
text: entry.txt,
46+
title: 'Go to ' + getNameFromDepth(depth),
47+
href: entry.anchor,
48+
});
49+
$(item).append(link);
50+
}
51+
$(toc).append(item);
52+
}
53+
return entries.length;
54+
}
55+
56+
function createTocForPost(post)
57+
{
58+
$(post).off('DOMSubtreeModified', subtreeModifiedHandler)
59+
$(post).find('div.bbTocBlock-toc').each(function() {
60+
$(this).empty();
61+
var toc = $('<ul>');
62+
$(this).append(toc);
63+
fillTableOfContents(getTocEntries(post), 0, 0, toc);
64+
});
65+
$(post).on('DOMSubtreeModified', subtreeModifiedHandler)
66+
}
67+
68+
function subtreeModifiedHandler()
69+
{
70+
createTocForPost(this);
71+
}
72+
73+
function initTocForEachPost()
74+
{
75+
$('article.message').each(function() {
76+
createTocForPost(this);
77+
});
78+
}
79+
80+
$(document).ready(initTocForEachPost);

_output/bb_codes/_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"hash": "3055cded7ae3c55aee7c4e22b4e78981"
1616
},
1717
"toc.json": {
18-
"hash": "5b1517c235663912f30644bb8b89f0f2"
18+
"hash": "7b6e8a89f99e43e802149162cb367ef9"
1919
}
2020
}

_output/bb_codes/toc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"bb_code_mode": "callback",
3-
"has_option": "optional",
3+
"has_option": "no",
44
"replace_html": "",
55
"replace_html_email": "",
66
"replace_text": "",
77
"callback_class": "Inforge\\PostTOC\\BbCode\\TagRenderer",
88
"callback_method": "renderTocTag",
99
"option_regex": "",
10-
"trim_lines_after": 0,
10+
"trim_lines_after": 1,
1111
"plain_children": true,
1212
"disable_smilies": false,
1313
"disable_nl2br": false,

_output/phrases/_metadata.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,5 @@
142142
"version_id": 1000070,
143143
"version_string": "1.0.0",
144144
"hash": "f61d6c3e3733db355168b7e1aee6780b"
145-
},
146-
"if_toc_table_of_contents.txt": {
147-
"global_cache": false,
148-
"version_id": 1000070,
149-
"version_string": "1.0.0",
150-
"hash": "f61d6c3e3733db355168b7e1aee6780b"
151145
}
152146
}

_output/phrases/if_toc_table_of_contents.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

_output/templates/_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"public/if_toc_bb_code_tag_toc.html": {
33
"version_id": 1000070,
44
"version_string": "1.0.0",
5-
"hash": "1bd72783c8e0a5a351761c28c477861b"
5+
"hash": "1293a7c624f8a134fdeabdc18e558210"
66
}
77
}
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<xf:js addon="Inforge/PostTOC" src="inforge/posttoc/toc-renderer.js" min="1" />
22

33
<div class="bbTocBlock">
4-
<div class="bbTocBlock-title">
5-
{{ phrase('if_toc_table_of_contents') }}
6-
</div>
74
<div class="bbTocBlock-toc">
85
</div>
96
</div>

0 commit comments

Comments
 (0)