1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.toc; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.component.phase.InitializationException; |
32 |
|
import org.xwiki.rendering.block.Block; |
33 |
|
import org.xwiki.rendering.block.BulletedListBlock; |
34 |
|
import org.xwiki.rendering.block.HeaderBlock; |
35 |
|
import org.xwiki.rendering.block.LinkBlock; |
36 |
|
import org.xwiki.rendering.block.ListBLock; |
37 |
|
import org.xwiki.rendering.block.ListItemBlock; |
38 |
|
import org.xwiki.rendering.block.NumberedListBlock; |
39 |
|
import org.xwiki.rendering.block.SectionBlock; |
40 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
41 |
|
import org.xwiki.rendering.listener.reference.DocumentResourceReference; |
42 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
43 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
44 |
|
import org.xwiki.rendering.macro.toc.TocMacroParameters; |
45 |
|
import org.xwiki.rendering.macro.toc.TocMacroParameters.Scope; |
46 |
|
import org.xwiki.rendering.parser.Parser; |
47 |
|
import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator; |
48 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@Component |
57 |
|
@Named("toc") |
58 |
|
@Singleton |
|
|
| 96% |
Uncovered Elements: 4 (99) |
Complexity: 25 |
Complexity Density: 0.42 |
|
59 |
|
public class TocMacro extends AbstractMacro<TocMacroParameters> |
60 |
|
{ |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private static final String DESCRIPTION = "Generates a Table Of Contents."; |
65 |
|
|
66 |
|
|
67 |
|
@link |
68 |
|
|
69 |
|
private TocBlockFilter tocBlockFilter; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@Inject |
75 |
|
@Named("plain/1.0") |
76 |
|
private Parser plainTextParser; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@Inject |
82 |
|
private LinkLabelGenerator linkLabelGenerator; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
87 |
19 |
public TocMacro()... |
88 |
|
{ |
89 |
19 |
super("Table Of Contents", DESCRIPTION, TocMacroParameters.class); |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
19 |
setPriority(2000); |
95 |
19 |
setDefaultCategory(DEFAULT_CATEGORY_NAVIGATION); |
96 |
|
} |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
98 |
19 |
@Override... |
99 |
|
public void initialize() throws InitializationException |
100 |
|
{ |
101 |
19 |
super.initialize(); |
102 |
|
|
103 |
19 |
this.tocBlockFilter = new TocBlockFilter(this.plainTextParser, this.linkLabelGenerator); |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
0 |
@Override... |
107 |
|
public boolean supportsInlineMode() |
108 |
|
{ |
109 |
0 |
return false; |
110 |
|
} |
111 |
|
|
|
|
| 94.1% |
Uncovered Elements: 2 (34) |
Complexity: 7 |
Complexity Density: 0.32 |
|
112 |
25 |
@Override... |
113 |
|
public List<Block> execute(TocMacroParameters parameters, String content, MacroTransformationContext context) |
114 |
|
throws MacroExecutionException |
115 |
|
{ |
116 |
25 |
List<Block> result; |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
25 |
int start = parameters.getStart(); |
139 |
25 |
int depth = parameters.getDepth(); |
140 |
|
|
141 |
25 |
Block root; |
142 |
|
|
143 |
25 |
if (parameters.getScope() == Scope.LOCAL) { |
144 |
4 |
root = context.getCurrentMacroBlock().getParent(); |
145 |
4 |
if (!parameters.isCustomStart()) { |
146 |
1 |
SectionBlock rootSection = context.getCurrentMacroBlock().getFirstBlock( |
147 |
|
new ClassBlockMatcher(SectionBlock.class), Block.Axes.ANCESTOR); |
148 |
1 |
HeaderBlock header = rootSection.getHeaderBlock(); |
149 |
1 |
if (header != null) { |
150 |
1 |
start = header.getLevel().getAsInt() + 1; |
151 |
|
} |
152 |
|
} |
153 |
|
} else { |
154 |
21 |
root = context.getXDOM(); |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
25 |
List<HeaderBlock> headers = root.getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT); |
159 |
|
|
160 |
|
|
161 |
25 |
if (root instanceof SectionBlock) { |
162 |
4 |
Block block = root.getChildren().get(0); |
163 |
|
|
164 |
4 |
if (block instanceof HeaderBlock) { |
165 |
4 |
headers.remove(block); |
166 |
|
} |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
25 |
Block tocBlock = generateTree(headers, start, depth, parameters.isNumbered()); |
171 |
25 |
if (tocBlock != null) { |
172 |
23 |
result = Arrays.asList(tocBlock); |
173 |
|
} else { |
174 |
2 |
result = Collections.emptyList(); |
175 |
|
} |
176 |
|
|
177 |
25 |
return result; |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
@param |
184 |
|
@param |
185 |
|
@param |
186 |
|
@param |
187 |
|
@return |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (32) |
Complexity: 8 |
Complexity Density: 0.4 |
|
189 |
25 |
private Block generateTree(List<HeaderBlock> headers, int start, int depth, boolean numbered)... |
190 |
|
{ |
191 |
25 |
Block tocBlock = null; |
192 |
|
|
193 |
25 |
int currentLevel = start - 1; |
194 |
25 |
Block currentBlock = null; |
195 |
25 |
for (HeaderBlock headerBlock : headers) { |
196 |
70 |
int headerLevel = headerBlock.getLevel().getAsInt(); |
197 |
|
|
198 |
70 |
if (headerLevel >= start && headerLevel <= depth) { |
199 |
|
|
200 |
|
|
201 |
69 |
if (currentLevel < headerLevel) { |
202 |
101 |
while (currentLevel < headerLevel) { |
203 |
56 |
if (currentBlock instanceof ListBLock) { |
204 |
11 |
currentBlock = addItemBlock(currentBlock, null); |
205 |
|
} |
206 |
|
|
207 |
56 |
currentBlock = createChildListBlock(numbered, currentBlock); |
208 |
56 |
++currentLevel; |
209 |
|
} |
210 |
|
} else { |
211 |
38 |
while (currentLevel > headerLevel) { |
212 |
14 |
currentBlock = currentBlock.getParent().getParent(); |
213 |
14 |
--currentLevel; |
214 |
|
} |
215 |
24 |
currentBlock = currentBlock.getParent(); |
216 |
|
} |
217 |
|
|
218 |
69 |
currentBlock = addItemBlock(currentBlock, headerBlock); |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
25 |
if (currentBlock != null) { |
223 |
23 |
tocBlock = currentBlock.getRoot(); |
224 |
|
} |
225 |
|
|
226 |
25 |
return tocBlock; |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
@link@link |
231 |
|
|
232 |
|
@param |
233 |
|
@param@link |
234 |
|
@return@link |
235 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
236 |
80 |
private Block addItemBlock(Block currentBlock, HeaderBlock headerBlock)... |
237 |
|
{ |
238 |
80 |
ListItemBlock itemBlock = headerBlock == null ? createEmptyTocEntry() : createTocEntry(headerBlock); |
239 |
|
|
240 |
80 |
currentBlock.addChild(itemBlock); |
241 |
|
|
242 |
80 |
return itemBlock; |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
@return |
247 |
|
@since |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
249 |
11 |
private ListItemBlock createEmptyTocEntry()... |
250 |
|
{ |
251 |
11 |
return new ListItemBlock(Collections.<Block>emptyList()); |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
@param@link |
258 |
|
@return |
259 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
260 |
69 |
private ListItemBlock createTocEntry(HeaderBlock headerBlock)... |
261 |
|
{ |
262 |
|
|
263 |
69 |
DocumentResourceReference reference = new DocumentResourceReference(null); |
264 |
69 |
reference.setAnchor(headerBlock.getId()); |
265 |
69 |
LinkBlock linkBlock = new LinkBlock(this.tocBlockFilter.generateLabel(headerBlock), reference, false); |
266 |
|
|
267 |
69 |
return new ListItemBlock(Collections.<Block>singletonList(linkBlock)); |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
@param |
274 |
|
@param |
275 |
|
@return |
276 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
277 |
56 |
private ListBLock createChildListBlock(boolean numbered, Block parentBlock)... |
278 |
|
{ |
279 |
56 |
ListBLock childListBlock = |
280 |
56 |
numbered ? new NumberedListBlock(Collections.<Block>emptyList()) : new BulletedListBlock( |
281 |
|
Collections.<Block>emptyList()); |
282 |
|
|
283 |
56 |
if (parentBlock != null) { |
284 |
33 |
parentBlock.addChild(childListBlock); |
285 |
|
} |
286 |
|
|
287 |
56 |
return childListBlock; |
288 |
|
} |
289 |
|
} |