| 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 |
15
|
public TocMacro()... |
| 88 |
|
{ |
| 89 |
15
|
super("Table Of Contents", DESCRIPTION, TocMacroParameters.class); |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
15
|
setPriority(2000); |
| 95 |
15
|
setDefaultCategory(DEFAULT_CATEGORY_NAVIGATION); |
| 96 |
|
} |
| 97 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 98 |
15
|
@Override... |
| 99 |
|
public void initialize() throws InitializationException |
| 100 |
|
{ |
| 101 |
15
|
super.initialize(); |
| 102 |
|
|
| 103 |
15
|
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 |
15
|
@Override... |
| 113 |
|
public List<Block> execute(TocMacroParameters parameters, String content, MacroTransformationContext context) |
| 114 |
|
throws MacroExecutionException |
| 115 |
|
{ |
| 116 |
15
|
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 |
15
|
int start = parameters.getStart(); |
| 139 |
15
|
int depth = parameters.getDepth(); |
| 140 |
|
|
| 141 |
15
|
Block root; |
| 142 |
|
|
| 143 |
15
|
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 |
11
|
root = context.getXDOM(); |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
|
| 158 |
15
|
List<HeaderBlock> headers = root.getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT); |
| 159 |
|
|
| 160 |
|
|
| 161 |
15
|
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 |
15
|
Block tocBlock = generateTree(headers, start, depth, parameters.isNumbered()); |
| 171 |
15
|
if (tocBlock != null) { |
| 172 |
13
|
result = Arrays.asList(tocBlock); |
| 173 |
|
} else { |
| 174 |
2
|
result = Collections.emptyList(); |
| 175 |
|
} |
| 176 |
|
|
| 177 |
15
|
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 |
15
|
private Block generateTree(List<HeaderBlock> headers, int start, int depth, boolean numbered)... |
| 190 |
|
{ |
| 191 |
15
|
Block tocBlock = null; |
| 192 |
|
|
| 193 |
15
|
int currentLevel = start - 1; |
| 194 |
15
|
Block currentBlock = null; |
| 195 |
15
|
for (HeaderBlock headerBlock : headers) { |
| 196 |
43
|
int headerLevel = headerBlock.getLevel().getAsInt(); |
| 197 |
|
|
| 198 |
43
|
if (headerLevel >= start && headerLevel <= depth) { |
| 199 |
|
|
| 200 |
|
|
| 201 |
42
|
if (currentLevel < headerLevel) { |
| 202 |
77
|
while (currentLevel < headerLevel) { |
| 203 |
44
|
if (currentBlock instanceof ListBLock) { |
| 204 |
11
|
currentBlock = addItemBlock(currentBlock, null); |
| 205 |
|
} |
| 206 |
|
|
| 207 |
44
|
currentBlock = createChildListBlock(numbered, currentBlock); |
| 208 |
44
|
++currentLevel; |
| 209 |
|
} |
| 210 |
|
} else { |
| 211 |
22
|
while (currentLevel > headerLevel) { |
| 212 |
13
|
currentBlock = currentBlock.getParent().getParent(); |
| 213 |
13
|
--currentLevel; |
| 214 |
|
} |
| 215 |
9
|
currentBlock = currentBlock.getParent(); |
| 216 |
|
} |
| 217 |
|
|
| 218 |
42
|
currentBlock = addItemBlock(currentBlock, headerBlock); |
| 219 |
|
} |
| 220 |
|
} |
| 221 |
|
|
| 222 |
15
|
if (currentBlock != null) { |
| 223 |
13
|
tocBlock = currentBlock.getRoot(); |
| 224 |
|
} |
| 225 |
|
|
| 226 |
15
|
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 |
53
|
private Block addItemBlock(Block currentBlock, HeaderBlock headerBlock)... |
| 237 |
|
{ |
| 238 |
53
|
ListItemBlock itemBlock = headerBlock == null ? createEmptyTocEntry() : createTocEntry(headerBlock); |
| 239 |
|
|
| 240 |
53
|
currentBlock.addChild(itemBlock); |
| 241 |
|
|
| 242 |
53
|
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 |
42
|
private ListItemBlock createTocEntry(HeaderBlock headerBlock)... |
| 261 |
|
{ |
| 262 |
|
|
| 263 |
42
|
DocumentResourceReference reference = new DocumentResourceReference(null); |
| 264 |
42
|
reference.setAnchor(headerBlock.getId()); |
| 265 |
42
|
LinkBlock linkBlock = new LinkBlock(this.tocBlockFilter.generateLabel(headerBlock), reference, false); |
| 266 |
|
|
| 267 |
42
|
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 |
44
|
private ListBLock createChildListBlock(boolean numbered, Block parentBlock)... |
| 278 |
|
{ |
| 279 |
44
|
ListBLock childListBlock = |
| 280 |
44
|
numbered ? new NumberedListBlock(Collections.<Block> emptyList()) : new BulletedListBlock( |
| 281 |
|
Collections.<Block> emptyList()); |
| 282 |
|
|
| 283 |
44
|
if (parentBlock != null) { |
| 284 |
31
|
parentBlock.addChild(childListBlock); |
| 285 |
|
} |
| 286 |
|
|
| 287 |
44
|
return childListBlock; |
| 288 |
|
} |
| 289 |
|
} |