| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package org.devacfr.maven.skins.reflow; |
| 20 |
|
|
| 21 |
|
import javax.annotation.Nonnull; |
| 22 |
|
import javax.annotation.Nullable; |
| 23 |
|
|
| 24 |
|
import java.text.Normalizer; |
| 25 |
|
import java.text.Normalizer.Form; |
| 26 |
|
import java.util.ArrayList; |
| 27 |
|
import java.util.Arrays; |
| 28 |
|
import java.util.Collection; |
| 29 |
|
import java.util.Collections; |
| 30 |
|
import java.util.HashSet; |
| 31 |
|
import java.util.List; |
| 32 |
|
import java.util.Locale; |
| 33 |
|
import java.util.Map; |
| 34 |
|
import java.util.Map.Entry; |
| 35 |
|
import java.util.Set; |
| 36 |
|
import java.util.Stack; |
| 37 |
|
import java.util.regex.Pattern; |
| 38 |
|
|
| 39 |
|
import com.google.common.base.Strings; |
| 40 |
|
import com.google.common.collect.Lists; |
| 41 |
|
import org.apache.velocity.tools.ToolContext; |
| 42 |
|
import org.apache.velocity.tools.config.DefaultKey; |
| 43 |
|
import org.apache.velocity.tools.generic.SafeConfig; |
| 44 |
|
import org.apache.velocity.tools.generic.ValueParser; |
| 45 |
|
import org.jsoup.Jsoup; |
| 46 |
|
import org.jsoup.internal.StringUtil; |
| 47 |
|
import org.jsoup.nodes.Document; |
| 48 |
|
import org.jsoup.nodes.Element; |
| 49 |
|
import org.jsoup.nodes.Node; |
| 50 |
|
import org.jsoup.parser.Tag; |
| 51 |
|
|
| 52 |
|
import static java.util.Collections.emptyList; |
| 53 |
|
import static java.util.Objects.requireNonNull; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@author |
| 64 |
|
@author |
| 65 |
|
@since |
| 66 |
|
@see |
| 67 |
|
@see |
| 68 |
|
|
| 69 |
|
@DefaultKey("htmlTool") |
| |
|
| 84,1% |
Uncovered Elements: 74 (465) |
Complexity: 97 |
Complexity Density: 0,3 |
|
| 70 |
|
public class HtmlTool extends SafeConfig { |
| 71 |
|
|
| 72 |
|
private static final int SLUG_SIZE = 50; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
public static final String DEFAULT_SLUG_SEPARATOR = "-"; |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
private static final String SEPARATOR_TOC = "_toc_"; |
| 79 |
|
|
| 80 |
|
|
| 81 |
|
private static final List<String> HEADINGS = Collections |
| 82 |
|
.unmodifiableList(Arrays.asList("h1", "h2", "h3", "h4", "h5", "h6")); |
| 83 |
|
|
| 84 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 85 |
|
public enum JoinSeparator { |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
AFTER, |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
BEFORE, |
| 94 |
|
|
| 95 |
|
NO |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
private String outputEncoding = "UTF-8"; |
| 100 |
|
|
| 101 |
|
private boolean prettyPrint = true; |
| 102 |
|
|
| 103 |
|
|
| 104 |
|
@inheritDoc |
| 105 |
|
|
| 106 |
|
@see |
| 107 |
|
|
| |
|
| 62,5% |
Uncovered Elements: 6 (16) |
Complexity: 4 |
Complexity Density: 0,4 |
|
| 108 |
22 |
@Override... |
| 109 |
|
protected void configure(final ValueParser values) { |
| 110 |
|
|
| 111 |
|
|
| 112 |
22 |
final Object velocityContext = values.get("velocityContext"); |
| 113 |
|
|
| 114 |
22 |
if (!(velocityContext instanceof ToolContext)) { |
| 115 |
0 |
return; |
| 116 |
|
} |
| 117 |
|
|
| 118 |
22 |
final ToolContext ctxt = (ToolContext) velocityContext; |
| 119 |
|
|
| 120 |
|
|
| 121 |
22 |
final Object outputEncodingObj = ctxt.get("outputEncoding"); |
| 122 |
22 |
if (outputEncodingObj instanceof String) { |
| 123 |
0 |
this.outputEncoding = (String) outputEncodingObj; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
22 |
final Object prettyPrint = ctxt.get("prettyPrint"); |
| 127 |
22 |
if (prettyPrint instanceof Boolean) { |
| 128 |
0 |
this.prettyPrint = (Boolean) prettyPrint; |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
@param |
| 137 |
|
|
| 138 |
|
@return |
| 139 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 140 |
0 |
@Nullable public String normaliseWhitespace(@Nullable final String html) {... |
| 141 |
0 |
if (Strings.isNullOrEmpty(html)) { |
| 142 |
0 |
return null; |
| 143 |
|
} |
| 144 |
0 |
return StringUtil.normaliseWhitespace(html); |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
@param |
| 152 |
|
|
| 153 |
|
@param |
| 154 |
|
|
| 155 |
|
@return |
| 156 |
|
@since |
| 157 |
|
@see |
| 158 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 159 |
4 |
public List<String> split(@Nonnull final String content, @Nonnull final String separatorCssSelector) {... |
| 160 |
4 |
return split(content, separatorCssSelector, JoinSeparator.NO); |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
@param |
| 172 |
|
|
| 173 |
|
@param |
| 174 |
|
|
| 175 |
|
@return |
| 176 |
|
|
| 177 |
|
@since |
| 178 |
|
@see |
| 179 |
|
|
| |
|
| 66,7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0,75 |
|
| 180 |
1 |
public List<String> splitOnStarts(final @Nonnull String content, final @Nonnull String separatorCssSelector) {... |
| 181 |
|
|
| 182 |
1 |
final List<String> result = split(content, separatorCssSelector, JoinSeparator.AFTER); |
| 183 |
|
|
| 184 |
1 |
if (result == null || result.size() <= 1) { |
| 185 |
|
|
| 186 |
0 |
return result; |
| 187 |
|
} |
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
1 |
return result.subList(1, result.size()); |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
@param |
| 200 |
|
|
| 201 |
|
@param |
| 202 |
|
|
| 203 |
|
@param |
| 204 |
|
|
| 205 |
|
@return |
| 206 |
|
@since |
| 207 |
|
@see |
| 208 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
| 209 |
3 |
public List<String> split(final @Nonnull String content,... |
| 210 |
|
final @Nonnull String separatorCssSelector, |
| 211 |
|
final String separatorStrategy) { |
| 212 |
|
|
| 213 |
3 |
JoinSeparator sepStrategy; |
| 214 |
3 |
if ("before".equals(separatorStrategy)) { |
| 215 |
1 |
sepStrategy = JoinSeparator.BEFORE; |
| 216 |
2 |
} else if ("after".equals(separatorStrategy)) { |
| 217 |
1 |
sepStrategy = JoinSeparator.AFTER; |
| 218 |
|
} else { |
| 219 |
1 |
sepStrategy = JoinSeparator.NO; |
| 220 |
|
} |
| 221 |
|
|
| 222 |
3 |
return split(content, separatorCssSelector, sepStrategy); |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
@param |
| 234 |
|
|
| 235 |
|
@param |
| 236 |
|
|
| 237 |
|
@param |
| 238 |
|
|
| 239 |
|
@return |
| 240 |
|
|
| 241 |
|
@since |
| 242 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 3 |
Complexity Density: 0,25 |
|
| 243 |
11 |
public List<String> split(@Nonnull final String content,... |
| 244 |
|
@Nonnull final String separatorCssSelector, |
| 245 |
|
@Nonnull final JoinSeparator separatorStrategy) { |
| 246 |
|
|
| 247 |
11 |
requireNonNull(separatorStrategy); |
| 248 |
11 |
final Element body = parse(content).body(); |
| 249 |
|
|
| 250 |
11 |
final List<Element> separators = body.select(separatorCssSelector); |
| 251 |
11 |
if (separators.size() > 0) { |
| 252 |
9 |
final List<List<Element>> partitions = split(separators, separatorStrategy, body); |
| 253 |
|
|
| 254 |
9 |
final List<String> sectionHtml = new ArrayList<>(); |
| 255 |
|
|
| 256 |
9 |
for (final List<Element> partition : partitions) { |
| 257 |
19 |
final String html = outerHtml(partition); |
| 258 |
19 |
if (!Strings.isNullOrEmpty(html)) { |
| 259 |
18 |
sectionHtml.add(outerHtml(partition)); |
| 260 |
|
} |
| 261 |
|
} |
| 262 |
|
|
| 263 |
9 |
return sectionHtml; |
| 264 |
|
} else { |
| 265 |
|
|
| 266 |
2 |
return Collections.singletonList(content); |
| 267 |
|
} |
| 268 |
|
} |
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
@param |
| 276 |
|
@param |
| 277 |
|
@param |
| 278 |
|
@return |
| 279 |
|
|
| 280 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 5 |
Complexity Density: 0,24 |
|
| 281 |
28 |
private static List<List<Element>> split(final Collection<Element> separators,... |
| 282 |
|
final JoinSeparator separatorStrategy, |
| 283 |
|
final Element parent) { |
| 284 |
|
|
| 285 |
28 |
final List<List<Element>> partitions = Lists.newLinkedList(); |
| 286 |
|
|
| 287 |
28 |
for (final Element child : parent.children()) { |
| 288 |
|
|
| 289 |
29 |
if (separators.contains(child)) { |
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
10 |
getLastPartition(partitions); |
| 295 |
|
|
| 296 |
10 |
if (separatorStrategy == JoinSeparator.BEFORE) { |
| 297 |
|
|
| 298 |
2 |
getLastPartition(partitions).add(child); |
| 299 |
|
} |
| 300 |
|
|
| 301 |
|
|
| 302 |
10 |
final List<Element> newPartition = Lists.newLinkedList(); |
| 303 |
10 |
partitions.add(newPartition); |
| 304 |
|
|
| 305 |
10 |
if (separatorStrategy == JoinSeparator.AFTER) { |
| 306 |
|
|
| 307 |
3 |
newPartition.add(child); |
| 308 |
|
} |
| 309 |
|
|
| 310 |
|
} else { |
| 311 |
|
|
| 312 |
19 |
final List<List<Element>> childPartitions = split(separators, separatorStrategy, child); |
| 313 |
|
|
| 314 |
|
|
| 315 |
19 |
getLastPartition(partitions).add(child); |
| 316 |
|
|
| 317 |
19 |
if (childPartitions.size() > 1) { |
| 318 |
|
|
| 319 |
|
|
| 320 |
|
|
| 321 |
|
|
| 322 |
2 |
final List<Element> allChildren = child.children(); |
| 323 |
2 |
final List<Element> firstPartition = childPartitions.get(0); |
| 324 |
|
|
| 325 |
2 |
allChildren.removeAll(firstPartition); |
| 326 |
2 |
for (final Element removeChild : allChildren) { |
| 327 |
2 |
removeChild.remove(); |
| 328 |
|
} |
| 329 |
|
|
| 330 |
|
|
| 331 |
2 |
for (final List<Element> nextPartition : childPartitions.subList(1, childPartitions.size())) { |
| 332 |
2 |
partitions.add(nextPartition); |
| 333 |
|
} |
| 334 |
|
} |
| 335 |
|
} |
| 336 |
|
} |
| 337 |
|
|
| 338 |
28 |
return partitions; |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
|
| 342 |
|
|
| 343 |
|
|
| 344 |
|
@param |
| 345 |
|
@return |
| 346 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
| 347 |
31 |
private static List<Element> getLastPartition(final List<List<Element>> partitions) {... |
| 348 |
31 |
if (partitions.isEmpty()) { |
| 349 |
11 |
final List<Element> newPartition = Lists.newLinkedList(); |
| 350 |
11 |
partitions.add(newPartition); |
| 351 |
11 |
return newPartition; |
| 352 |
|
} else { |
| 353 |
20 |
return partitions.get(partitions.size() - 1); |
| 354 |
|
} |
| 355 |
|
} |
| 356 |
|
|
| 357 |
|
|
| 358 |
|
|
| 359 |
|
|
| 360 |
|
@param |
| 361 |
|
@return |
| 362 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0,3 |
|
| 363 |
37 |
private static String outerHtml(final List<Element> elements) {... |
| 364 |
|
|
| 365 |
37 |
switch (elements.size()) { |
| 366 |
1 |
case 0: |
| 367 |
1 |
return ""; |
| 368 |
|
|
| 369 |
24 |
case 1: |
| 370 |
24 |
return elements.get(0).outerHtml(); |
| 371 |
|
|
| 372 |
12 |
default: |
| 373 |
|
|
| 374 |
|
|
| 375 |
12 |
final Element root = new Element(Tag.valueOf("div"), ""); |
| 376 |
12 |
for (final Element elem : elements) { |
| 377 |
24 |
root.appendChild(elem); |
| 378 |
|
} |
| 379 |
|
|
| 380 |
12 |
return root.html(); |
| 381 |
|
} |
| 382 |
|
} |
| 383 |
|
|
| 384 |
|
|
| 385 |
|
|
| 386 |
|
|
| 387 |
|
|
| 388 |
|
@param |
| 389 |
|
|
| 390 |
|
@param |
| 391 |
|
|
| 392 |
|
@param |
| 393 |
|
|
| 394 |
|
@return |
| 395 |
|
@since |
| 396 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 397 |
0 |
public String reorderToTop(final String content, final String selector, final int amount) {... |
| 398 |
0 |
return reorderToTop(content, selector, amount, null); |
| 399 |
|
} |
| 400 |
|
|
| 401 |
|
|
| 402 |
|
|
| 403 |
|
|
| 404 |
|
|
| 405 |
|
@param |
| 406 |
|
|
| 407 |
|
@param |
| 408 |
|
|
| 409 |
|
@param |
| 410 |
|
|
| 411 |
|
@param |
| 412 |
|
|
| 413 |
|
@return |
| 414 |
|
@since |
| 415 |
|
|
| |
|
| 81,2% |
Uncovered Elements: 3 (16) |
Complexity: 4 |
Complexity Density: 0,4 |
|
| 416 |
1 |
public String reorderToTop(final String content,... |
| 417 |
|
final String selector, |
| 418 |
|
final int amount, |
| 419 |
|
final String wrapRemaining) { |
| 420 |
|
|
| 421 |
|
|
| 422 |
1 |
final List<Element> extracted = extractElements(content, selector, amount); |
| 423 |
|
|
| 424 |
1 |
if (extracted.size() > 1) { |
| 425 |
|
|
| 426 |
1 |
final Element body = extracted.get(0); |
| 427 |
|
|
| 428 |
1 |
if (wrapRemaining != null) { |
| 429 |
1 |
wrapInner(body, wrapRemaining); |
| 430 |
|
} |
| 431 |
|
|
| 432 |
1 |
final List<Element> elements = extracted.subList(1, extracted.size()); |
| 433 |
|
|
| 434 |
|
|
| 435 |
|
|
| 436 |
2 |
for (int index = elements.size() - 1; index >= 0; index--) { |
| 437 |
1 |
body.prependChild(elements.get(index)); |
| 438 |
|
} |
| 439 |
|
|
| 440 |
1 |
return body.html(); |
| 441 |
|
} else { |
| 442 |
|
|
| 443 |
0 |
return content; |
| 444 |
|
} |
| 445 |
|
} |
| 446 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0,12 |
|
| 447 |
1 |
private static Element wrapInner(final Element element, final String html) {... |
| 448 |
|
|
| 449 |
|
|
| 450 |
|
|
| 451 |
1 |
final Element topDiv = new Element(Tag.valueOf("div"), ""); |
| 452 |
1 |
for (final Element topElem : element.children()) { |
| 453 |
|
|
| 454 |
1 |
topElem.remove(); |
| 455 |
1 |
topDiv.appendChild(topElem); |
| 456 |
|
} |
| 457 |
|
|
| 458 |
|
|
| 459 |
1 |
element.appendChild(topDiv); |
| 460 |
|
|
| 461 |
|
|
| 462 |
1 |
topDiv.wrap(html); |
| 463 |
|
|
| 464 |
1 |
topDiv.unwrap(); |
| 465 |
|
|
| 466 |
1 |
return element; |
| 467 |
|
} |
| 468 |
|
|
| 469 |
|
|
| 470 |
|
|
| 471 |
|
|
| 472 |
|
@param |
| 473 |
|
@param |
| 474 |
|
@param |
| 475 |
|
@return |
| 476 |
|
|
| 477 |
|
|
| |
|
| 87,5% |
Uncovered Elements: 2 (16) |
Complexity: 3 |
Complexity Density: 0,25 |
|
| 478 |
2 |
private List<Element> extractElements(final String content, final String selector, final int amount) {... |
| 479 |
|
|
| 480 |
2 |
final Element body = parse(content).body(); |
| 481 |
|
|
| 482 |
2 |
List<Element> elements = body.select(selector); |
| 483 |
2 |
if (elements.size() > 0) { |
| 484 |
|
|
| 485 |
2 |
elements = filterParents(elements); |
| 486 |
|
|
| 487 |
2 |
if (amount >= 0) { |
| 488 |
|
|
| 489 |
2 |
elements = elements.subList(0, Math.min(amount, elements.size())); |
| 490 |
|
} |
| 491 |
|
|
| 492 |
|
|
| 493 |
2 |
for (final Element element : elements) { |
| 494 |
4 |
element.remove(); |
| 495 |
|
} |
| 496 |
|
} |
| 497 |
|
|
| 498 |
2 |
final List<Element> results = new ArrayList<>(); |
| 499 |
|
|
| 500 |
2 |
results.add(body); |
| 501 |
2 |
results.addAll(elements); |
| 502 |
2 |
return results; |
| 503 |
|
} |
| 504 |
|
|
| 505 |
|
|
| 506 |
|
|
| 507 |
|
|
| 508 |
|
|
| 509 |
|
@param |
| 510 |
|
@return |
| 511 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
| 512 |
2 |
private static List<Element> filterParents(final List<Element> elements) {... |
| 513 |
2 |
final List<Element> filtered = new ArrayList<>(); |
| 514 |
2 |
for (final Element element : elements) { |
| 515 |
|
|
| 516 |
6 |
final List<Element> parentsInter = element.parents(); |
| 517 |
6 |
parentsInter.retainAll(elements); |
| 518 |
6 |
if (parentsInter.isEmpty()) { |
| 519 |
|
|
| 520 |
4 |
filtered.add(element); |
| 521 |
|
} |
| 522 |
|
} |
| 523 |
|
|
| 524 |
2 |
return filtered; |
| 525 |
|
} |
| 526 |
|
|
| 527 |
|
|
| 528 |
|
|
| 529 |
|
|
| 530 |
|
|
| 531 |
|
|
| 532 |
|
@param |
| 533 |
|
|
| 534 |
|
@param |
| 535 |
|
|
| 536 |
|
@param |
| 537 |
|
|
| 538 |
|
@return |
| 539 |
|
|
| 540 |
|
@since |
| 541 |
|
|
| |
|
| 81,8% |
Uncovered Elements: 2 (11) |
Complexity: 2 |
Complexity Density: 0,22 |
|
| 542 |
1 |
@Nonnull... |
| 543 |
|
public ExtractResult extract(final String content, final String selector, final int amount) { |
| 544 |
|
|
| 545 |
1 |
final List<Element> extracted = extractElements(content, selector, amount); |
| 546 |
|
|
| 547 |
1 |
if (extracted.size() > 1) { |
| 548 |
|
|
| 549 |
|
|
| 550 |
1 |
final Element body = extracted.get(0); |
| 551 |
1 |
final List<Element> elements = extracted.subList(1, extracted.size()); |
| 552 |
|
|
| 553 |
|
|
| 554 |
1 |
final List<String> elementStr = new ArrayList<>(); |
| 555 |
1 |
for (final Element el : elements) { |
| 556 |
3 |
elementStr.add(el.outerHtml()); |
| 557 |
|
} |
| 558 |
|
|
| 559 |
1 |
return new DefaultExtractResult(elementStr, body.html()); |
| 560 |
|
} else { |
| 561 |
|
|
| 562 |
0 |
return new DefaultExtractResult(Collections.<String> emptyList(), content); |
| 563 |
|
} |
| 564 |
|
} |
| 565 |
|
|
| 566 |
|
|
| 567 |
|
|
| 568 |
|
|
| 569 |
|
|
| 570 |
|
@author |
| 571 |
|
@since |
| 572 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 573 |
|
public interface ExtractResult { |
| 574 |
|
|
| 575 |
|
|
| 576 |
|
|
| 577 |
|
|
| 578 |
|
@return |
| 579 |
|
|
| 580 |
|
List<String> getExtracted(); |
| 581 |
|
|
| 582 |
|
|
| 583 |
|
|
| 584 |
|
|
| 585 |
|
@return |
| 586 |
|
|
| 587 |
|
String getRemainder(); |
| 588 |
|
} |
| 589 |
|
|
| 590 |
|
|
| 591 |
|
@author |
| 592 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0,75 |
|
| 593 |
|
private static final class DefaultExtractResult implements ExtractResult { |
| 594 |
|
|
| 595 |
|
|
| 596 |
|
private final List<String> extracted; |
| 597 |
|
|
| 598 |
|
|
| 599 |
|
private final String remainder; |
| 600 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 601 |
1 |
private DefaultExtractResult(final List<String> extracted, final String remainder) {... |
| 602 |
1 |
this.extracted = extracted; |
| 603 |
1 |
this.remainder = remainder; |
| 604 |
|
} |
| 605 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 606 |
1 |
@Override... |
| 607 |
|
public List<String> getExtracted() { |
| 608 |
1 |
return Collections.unmodifiableList(extracted); |
| 609 |
|
} |
| 610 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 611 |
1 |
@Override... |
| 612 |
|
public String getRemainder() { |
| 613 |
1 |
return remainder; |
| 614 |
|
} |
| 615 |
|
} |
| 616 |
|
|
| 617 |
|
|
| 618 |
|
|
| 619 |
|
|
| 620 |
|
@param |
| 621 |
|
|
| 622 |
|
@param |
| 623 |
|
|
| 624 |
|
@param |
| 625 |
|
|
| 626 |
|
@param |
| 627 |
|
|
| 628 |
|
@return |
| 629 |
|
@since |
| 630 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
| 631 |
6 |
public String setAttr(final String content, final String selector, final String attributeKey, final String value) {... |
| 632 |
|
|
| 633 |
6 |
final Element body = parse(content).body(); |
| 634 |
|
|
| 635 |
6 |
final List<Element> elements = body.select(selector); |
| 636 |
6 |
if (elements.size() > 0) { |
| 637 |
|
|
| 638 |
2 |
for (final Element element : elements) { |
| 639 |
2 |
element.attr(attributeKey, value); |
| 640 |
|
} |
| 641 |
|
|
| 642 |
2 |
return body.html(); |
| 643 |
|
} else { |
| 644 |
|
|
| 645 |
4 |
return content; |
| 646 |
|
} |
| 647 |
|
} |
| 648 |
|
|
| 649 |
|
|
| 650 |
|
|
| 651 |
|
|
| 652 |
|
@param |
| 653 |
|
|
| 654 |
|
@return |
| 655 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 656 |
74 |
public Document parse(@Nonnull final String content) {... |
| 657 |
74 |
final Document doc = Jsoup.parseBodyFragment(content); |
| 658 |
74 |
doc.outputSettings().charset(outputEncoding).prettyPrint(prettyPrint); |
| 659 |
74 |
return doc; |
| 660 |
|
} |
| 661 |
|
|
| 662 |
|
|
| 663 |
|
|
| 664 |
|
|
| 665 |
|
|
| 666 |
|
@param |
| 667 |
|
|
| 668 |
|
@param |
| 669 |
|
|
| 670 |
|
@param |
| 671 |
|
|
| 672 |
|
@return |
| 673 |
|
@since |
| 674 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0,14 |
|
| 675 |
1 |
public List<String> getAttr(final String content, final String selector, final String attributeKey) {... |
| 676 |
|
|
| 677 |
1 |
final Element body = parse(content).body(); |
| 678 |
|
|
| 679 |
1 |
final List<Element> elements = body.select(selector); |
| 680 |
1 |
final List<String> attrs = new ArrayList<>(); |
| 681 |
|
|
| 682 |
1 |
for (final Element element : elements) { |
| 683 |
1 |
final String attrValue = element.attr(attributeKey); |
| 684 |
1 |
attrs.add(attrValue); |
| 685 |
|
} |
| 686 |
|
|
| 687 |
1 |
return attrs; |
| 688 |
|
} |
| 689 |
|
|
| 690 |
|
|
| 691 |
|
|
| 692 |
|
|
| 693 |
|
@param |
| 694 |
|
|
| 695 |
|
@param |
| 696 |
|
|
| 697 |
|
@param |
| 698 |
|
|
| 699 |
|
@param |
| 700 |
|
|
| 701 |
|
@return |
| 702 |
|
@since |
| 703 |
|
|
| |
|
| 85,7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0,3 |
|
| 704 |
11 |
public String addClass(final String content,... |
| 705 |
|
final String selector, |
| 706 |
|
final List<String> classNames, |
| 707 |
|
final int amount) { |
| 708 |
|
|
| 709 |
11 |
final Element body = parse(content).body(); |
| 710 |
|
|
| 711 |
11 |
List<Element> elements = body.select(selector); |
| 712 |
11 |
if (amount >= 0) { |
| 713 |
|
|
| 714 |
0 |
elements = elements.subList(0, Math.min(amount, elements.size())); |
| 715 |
|
} |
| 716 |
|
|
| 717 |
11 |
if (elements.size() > 0) { |
| 718 |
|
|
| 719 |
4 |
for (final Element element : elements) { |
| 720 |
12 |
for (final String className : classNames) { |
| 721 |
14 |
element.addClass(className); |
| 722 |
|
} |
| 723 |
|
} |
| 724 |
|
|
| 725 |
4 |
return body.html(); |
| 726 |
|
} else { |
| 727 |
|
|
| 728 |
7 |
return content; |
| 729 |
|
} |
| 730 |
|
} |
| 731 |
|
|
| 732 |
|
|
| 733 |
|
|
| 734 |
|
|
| 735 |
|
@param |
| 736 |
|
|
| 737 |
|
@param |
| 738 |
|
|
| 739 |
|
@param |
| 740 |
|
|
| 741 |
|
@return |
| 742 |
|
@since |
| 743 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 744 |
11 |
public String addClass(final String content, final String selector, final List<String> classNames) {... |
| 745 |
11 |
return addClass(content, selector, classNames, -1); |
| 746 |
|
} |
| 747 |
|
|
| 748 |
|
|
| 749 |
|
|
| 750 |
|
|
| 751 |
|
@param |
| 752 |
|
|
| 753 |
|
@param |
| 754 |
|
|
| 755 |
|
@param |
| 756 |
|
|
| 757 |
|
@return |
| 758 |
|
@since |
| 759 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 760 |
1 |
public String addClass(final String content, final String selector, final String className) {... |
| 761 |
1 |
return addClass(content, selector, Collections.singletonList(className)); |
| 762 |
|
} |
| 763 |
|
|
| 764 |
|
|
| 765 |
|
|
| 766 |
|
|
| 767 |
|
@param |
| 768 |
|
|
| 769 |
|
@param |
| 770 |
|
|
| 771 |
|
@param |
| 772 |
|
|
| 773 |
|
@param |
| 774 |
|
|
| 775 |
|
@return |
| 776 |
|
@since |
| 777 |
|
|
| |
|
| 76,9% |
Uncovered Elements: 3 (13) |
Complexity: 3 |
Complexity Density: 0,33 |
|
| 778 |
1 |
public String wrap(final String content, final String selector, final String wrapHtml, final int amount) {... |
| 779 |
|
|
| 780 |
1 |
final Element body = parse(content).body(); |
| 781 |
|
|
| 782 |
1 |
List<Element> elements = body.select(selector); |
| 783 |
1 |
if (amount >= 0) { |
| 784 |
|
|
| 785 |
1 |
elements = elements.subList(0, Math.min(amount, elements.size())); |
| 786 |
|
} |
| 787 |
|
|
| 788 |
1 |
if (elements.size() > 0) { |
| 789 |
|
|
| 790 |
1 |
for (final Element element : elements) { |
| 791 |
1 |
element.wrap(wrapHtml); |
| 792 |
|
} |
| 793 |
|
|
| 794 |
1 |
return body.html(); |
| 795 |
|
} else { |
| 796 |
|
|
| 797 |
0 |
return content; |
| 798 |
|
} |
| 799 |
|
} |
| 800 |
|
|
| 801 |
|
|
| 802 |
|
|
| 803 |
|
|
| 804 |
|
@param |
| 805 |
|
|
| 806 |
|
@param |
| 807 |
|
|
| 808 |
|
@return |
| 809 |
|
@since |
| 810 |
|
|
| |
|
| 77,8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
| 811 |
1 |
public String remove(final String content, final String selector) {... |
| 812 |
|
|
| 813 |
1 |
final Element body = parse(content).body(); |
| 814 |
|
|
| 815 |
1 |
final List<Element> elements = body.select(selector); |
| 816 |
1 |
if (elements.size() > 0) { |
| 817 |
1 |
for (final Element element : elements) { |
| 818 |
1 |
element.remove(); |
| 819 |
|
} |
| 820 |
|
|
| 821 |
1 |
return body.html(); |
| 822 |
|
} else { |
| 823 |
|
|
| 824 |
0 |
return content; |
| 825 |
|
} |
| 826 |
|
} |
| 827 |
|
|
| 828 |
|
|
| 829 |
|
|
| 830 |
|
|
| 831 |
|
@param |
| 832 |
|
|
| 833 |
|
@param |
| 834 |
|
|
| 835 |
|
@param |
| 836 |
|
|
| 837 |
|
@return |
| 838 |
|
@since |
| 839 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 840 |
1 |
public String replace(final String content, final String selector, final String replacement) {... |
| 841 |
1 |
return replaceAll(content, Collections.singletonMap(selector, replacement)); |
| 842 |
|
} |
| 843 |
|
|
| 844 |
|
|
| 845 |
|
|
| 846 |
|
|
| 847 |
|
@param |
| 848 |
|
|
| 849 |
|
@param |
| 850 |
|
|
| 851 |
|
|
| 852 |
|
@return |
| 853 |
|
@since |
| 854 |
|
|
| |
|
| 95,2% |
Uncovered Elements: 1 (21) |
Complexity: 4 |
Complexity Density: 0,27 |
|
| 855 |
6 |
public String replaceAll(final String content, final Map<String, String> replacements) {... |
| 856 |
|
|
| 857 |
6 |
final Element body = parse(content).body(); |
| 858 |
|
|
| 859 |
6 |
boolean modified = false; |
| 860 |
6 |
for (final Entry<String, String> replacementEntry : replacements.entrySet()) { |
| 861 |
46 |
final String selector = replacementEntry.getKey(); |
| 862 |
46 |
final String replacement = replacementEntry.getValue(); |
| 863 |
|
|
| 864 |
46 |
final List<Element> elements = body.select(selector); |
| 865 |
46 |
if (elements.size() > 0) { |
| 866 |
|
|
| 867 |
|
|
| 868 |
10 |
final Element replacementElem = parse(replacement).body().child(0); |
| 869 |
|
|
| 870 |
10 |
if (replacementElem != null) { |
| 871 |
10 |
for (final Element element : elements) { |
| 872 |
10 |
element.replaceWith(replacementElem.clone()); |
| 873 |
|
} |
| 874 |
|
|
| 875 |
10 |
modified = true; |
| 876 |
|
} |
| 877 |
|
} |
| 878 |
|
} |
| 879 |
|
|
| 880 |
6 |
if (modified) { |
| 881 |
2 |
return body.html(); |
| 882 |
|
} else { |
| 883 |
|
|
| 884 |
4 |
return content; |
| 885 |
|
} |
| 886 |
|
} |
| 887 |
|
|
| 888 |
|
|
| 889 |
|
|
| 890 |
|
|
| 891 |
|
|
| 892 |
|
@param |
| 893 |
|
|
| 894 |
|
@param |
| 895 |
|
|
| 896 |
|
@param |
| 897 |
|
|
| 898 |
|
@return |
| 899 |
|
@since |
| 900 |
|
|
| |
|
| 95,5% |
Uncovered Elements: 1 (22) |
Complexity: 4 |
Complexity Density: 0,25 |
|
| 901 |
6 |
public String replaceWith(final String content, final String selector, final String newElement) {... |
| 902 |
|
|
| 903 |
6 |
final Element body = parse(content).body(); |
| 904 |
|
|
| 905 |
6 |
boolean modified = false; |
| 906 |
6 |
final List<Element> elements = body.select(selector); |
| 907 |
6 |
if (elements.size() > 0) { |
| 908 |
|
|
| 909 |
|
|
| 910 |
2 |
final Element replacementElem = parse(newElement).body().child(0); |
| 911 |
|
|
| 912 |
2 |
if (replacementElem != null) { |
| 913 |
2 |
for (final Element element : elements) { |
| 914 |
2 |
final List<Node> children = element.childNodes(); |
| 915 |
2 |
final Element el = replacementElem.clone(); |
| 916 |
2 |
for (final Node child : children) { |
| 917 |
2 |
el.appendChild(child.clone()); |
| 918 |
|
} |
| 919 |
2 |
element.replaceWith(el); |
| 920 |
|
} |
| 921 |
|
|
| 922 |
2 |
modified = true; |
| 923 |
|
} |
| 924 |
|
} |
| 925 |
|
|
| 926 |
6 |
if (modified) { |
| 927 |
2 |
return body.html(); |
| 928 |
|
} else { |
| 929 |
|
|
| 930 |
4 |
return content; |
| 931 |
|
} |
| 932 |
|
} |
| 933 |
|
|
| 934 |
|
|
| 935 |
|
|
| 936 |
|
|
| 937 |
|
|
| 938 |
|
@param |
| 939 |
|
|
| 940 |
|
@param |
| 941 |
|
|
| 942 |
|
@return |
| 943 |
|
@since |
| 944 |
|
|
| |
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 2 |
Complexity Density: 0,25 |
|
| 945 |
1 |
public List<String> text(@Nullable final String content, @Nonnull final String selector) {... |
| 946 |
1 |
if (Strings.isNullOrEmpty(content)) { |
| 947 |
0 |
return emptyList(); |
| 948 |
|
} |
| 949 |
1 |
final Element body = parse(content).body(); |
| 950 |
|
|
| 951 |
1 |
final List<Element> elements = body.select(selector); |
| 952 |
1 |
final List<String> texts = new ArrayList<>(); |
| 953 |
|
|
| 954 |
1 |
for (final Element element : elements) { |
| 955 |
1 |
texts.add(element.text()); |
| 956 |
|
} |
| 957 |
|
|
| 958 |
1 |
return texts; |
| 959 |
|
} |
| 960 |
|
|
| 961 |
|
|
| 962 |
|
|
| 963 |
|
|
| 964 |
|
|
| 965 |
|
|
| 966 |
|
|
| 967 |
|
|
| 968 |
|
|
| 969 |
|
|
| 970 |
|
|
| 971 |
|
|
| 972 |
|
|
| 973 |
|
|
| 974 |
|
|
| 975 |
|
|
| 976 |
|
|
| 977 |
|
@param |
| 978 |
|
|
| 979 |
|
@return |
| 980 |
|
|
| 981 |
|
@since |
| 982 |
|
|
| |
|
| 60,6% |
Uncovered Elements: 13 (33) |
Complexity: 5 |
Complexity Density: 0,2 |
|
| 983 |
6 |
public String headingAnchorToId(final String content) {... |
| 984 |
|
|
| 985 |
6 |
final Element body = parse(content).body(); |
| 986 |
|
|
| 987 |
|
|
| 988 |
6 |
final List<String> headNoIds = concat(HEADINGS, ":not([id])", true); |
| 989 |
|
|
| 990 |
|
|
| 991 |
6 |
final String nameA = "a[name]:not([href])"; |
| 992 |
|
|
| 993 |
|
|
| 994 |
6 |
final List<Element> headingsInnerA = body |
| 995 |
|
.select(String.join(", ", concat(headNoIds, ":has(" + nameA + ")", true))); |
| 996 |
|
|
| 997 |
6 |
boolean modified = false; |
| 998 |
6 |
for (final Element heading : headingsInnerA) { |
| 999 |
9 |
final List<Element> anchors = heading.select(nameA); |
| 1000 |
|
|
| 1001 |
9 |
if (!anchors.isEmpty()) { |
| 1002 |
9 |
anchorToId(heading, anchors.get(0)); |
| 1003 |
9 |
modified = true; |
| 1004 |
|
} |
| 1005 |
|
} |
| 1006 |
|
|
| 1007 |
|
|
| 1008 |
6 |
final List<Element> headingsPreA = body.select(String.join(", ", concat(headNoIds, nameA + " + ", false))); |
| 1009 |
|
|
| 1010 |
6 |
for (final Element heading : headingsPreA) { |
| 1011 |
0 |
final Element anchor = heading.previousElementSibling(); |
| 1012 |
0 |
if (anchor != null) { |
| 1013 |
0 |
anchorToId(heading, anchor); |
| 1014 |
0 |
modified = true; |
| 1015 |
|
} |
| 1016 |
|
} |
| 1017 |
|
|
| 1018 |
|
|
| 1019 |
|
|
| 1020 |
|
|
| 1021 |
6 |
final List<Element> anchorsPreH = body.select(String.join(", ", concat(headNoIds, " + " + nameA, true))); |
| 1022 |
|
|
| 1023 |
6 |
for (final Element anchor : anchorsPreH) { |
| 1024 |
0 |
final Element heading = anchor.previousElementSibling(); |
| 1025 |
0 |
if (heading != null) { |
| 1026 |
0 |
anchorToId(heading, anchor); |
| 1027 |
0 |
modified = true; |
| 1028 |
|
} |
| 1029 |
|
} |
| 1030 |
|
|
| 1031 |
6 |
if (modified) { |
| 1032 |
4 |
return body.html(); |
| 1033 |
|
} else { |
| 1034 |
|
|
| 1035 |
2 |
return content; |
| 1036 |
|
} |
| 1037 |
|
} |
| 1038 |
|
|
| 1039 |
|
|
| 1040 |
|
|
| 1041 |
|
|
| 1042 |
|
@param |
| 1043 |
|
@param |
| 1044 |
|
|
| |
|
| 77,8% |
Uncovered Elements: 2 (9) |
Complexity: 4 |
Complexity Density: 0,8 |
|
| 1045 |
9 |
private static void anchorToId(final Element heading, final Element anchor) {... |
| 1046 |
|
|
| 1047 |
9 |
if ("a".equals(anchor.tagName()) && heading.id().isEmpty()) { |
| 1048 |
9 |
final String aName = anchor.attr("name"); |
| 1049 |
9 |
if (!aName.isEmpty()) { |
| 1050 |
|
|
| 1051 |
9 |
heading.attr("id", aName); |
| 1052 |
|
|
| 1053 |
|
|
| 1054 |
9 |
anchor.remove(); |
| 1055 |
|
} |
| 1056 |
|
} |
| 1057 |
|
} |
| 1058 |
|
|
| 1059 |
|
|
| 1060 |
|
|
| 1061 |
|
|
| 1062 |
|
@param |
| 1063 |
|
|
| 1064 |
|
@param |
| 1065 |
|
|
| 1066 |
|
@param |
| 1067 |
|
|
| 1068 |
|
@return |
| 1069 |
|
@since |
| 1070 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 1071 |
31 |
public static List<String> concat(final List<String> elements, final String text, final boolean append) {... |
| 1072 |
31 |
final List<String> concats = new ArrayList<>(); |
| 1073 |
|
|
| 1074 |
31 |
for (final String element : elements) { |
| 1075 |
186 |
concats.add(append ? element + text : text + element); |
| 1076 |
|
} |
| 1077 |
|
|
| 1078 |
31 |
return concats; |
| 1079 |
|
} |
| 1080 |
|
|
| 1081 |
|
|
| 1082 |
|
|
| 1083 |
|
|
| 1084 |
|
|
| 1085 |
|
|
| 1086 |
|
|
| 1087 |
|
|
| 1088 |
|
|
| 1089 |
|
|
| 1090 |
|
|
| 1091 |
|
|
| 1092 |
|
@param |
| 1093 |
|
|
| 1094 |
|
@param |
| 1095 |
|
|
| 1096 |
|
@param |
| 1097 |
|
|
| 1098 |
|
@param |
| 1099 |
|
|
| 1100 |
|
@return@link |
| 1101 |
|
|
| 1102 |
|
@since |
| 1103 |
|
|
| |
|
| 57,5% |
Uncovered Elements: 17 (40) |
Complexity: 6 |
Complexity Density: 0,19 |
|
| 1104 |
3 |
public String ensureHeadingIds(final String pageType,... |
| 1105 |
|
final String currentPage, |
| 1106 |
|
final String content, |
| 1107 |
|
final String idSeparator) { |
| 1108 |
3 |
final List<String> excludedPages = Arrays.asList("checkstyle-aggregate", "checkstyle"); |
| 1109 |
|
|
| 1110 |
3 |
final Element body = parse(content).body(); |
| 1111 |
|
|
| 1112 |
|
|
| 1113 |
3 |
if (excludedPages.contains(currentPage)) { |
| 1114 |
0 |
return content; |
| 1115 |
|
} |
| 1116 |
|
|
| 1117 |
|
|
| 1118 |
3 |
final List<Element> idElems = body.select("*[id]"); |
| 1119 |
|
|
| 1120 |
3 |
final Set<String> ids = new HashSet<>(); |
| 1121 |
3 |
boolean modified = false; |
| 1122 |
3 |
for (final Element idElem : idElems) { |
| 1123 |
|
|
| 1124 |
|
|
| 1125 |
0 |
final String id = idElem.id(); |
| 1126 |
0 |
idElem.attr("id", slug(id, idSeparator)); |
| 1127 |
0 |
modified = true; |
| 1128 |
|
|
| 1129 |
0 |
ids.add(idElem.id()); |
| 1130 |
|
} |
| 1131 |
|
|
| 1132 |
|
|
| 1133 |
3 |
final List<String> headIds = concat(HEADINGS, "[id]", true); |
| 1134 |
|
|
| 1135 |
3 |
final List<Element> headingIds = body.select(String.join(", ", headIds)); |
| 1136 |
|
|
| 1137 |
3 |
for (final Element heading : headingIds) { |
| 1138 |
0 |
final String headingText = heading.text(); |
| 1139 |
0 |
String headingSlug = slug(headingText, idSeparator); |
| 1140 |
|
|
| 1141 |
0 |
if (headingSlug.length() > SLUG_SIZE) { |
| 1142 |
0 |
headingSlug = headingSlug.substring(0, SLUG_SIZE); |
| 1143 |
|
} |
| 1144 |
0 |
final String headingId = generateUniqueId(pageType, currentPage, ids, headingSlug); |
| 1145 |
|
|
| 1146 |
0 |
heading.attr("id", headingId); |
| 1147 |
|
} |
| 1148 |
|
|
| 1149 |
3 |
final List<String> headNoIds = concat(HEADINGS, ":not([id])", true); |
| 1150 |
|
|
| 1151 |
|
|
| 1152 |
3 |
final List<Element> headingsNoId = body.select(String.join(", ", headNoIds)); |
| 1153 |
|
|
| 1154 |
3 |
if (!headingsNoId.isEmpty() || modified) { |
| 1155 |
3 |
for (final Element heading : headingsNoId) { |
| 1156 |
|
|
| 1157 |
9 |
final String headingText = heading.text(); |
| 1158 |
9 |
String headingSlug = slug(headingText, idSeparator); |
| 1159 |
|
|
| 1160 |
9 |
if (headingSlug.length() > SLUG_SIZE) { |
| 1161 |
0 |
headingSlug = headingSlug.substring(0, SLUG_SIZE); |
| 1162 |
|
} |
| 1163 |
9 |
final String headingId = generateUniqueId(pageType, currentPage, ids, headingSlug); |
| 1164 |
|
|
| 1165 |
9 |
heading.attr("id", headingId); |
| 1166 |
|
} |
| 1167 |
|
} |
| 1168 |
|
|
| 1169 |
3 |
return body.html(); |
| 1170 |
|
} |
| 1171 |
|
|
| 1172 |
|
|
| 1173 |
|
|
| 1174 |
|
|
| 1175 |
|
@param |
| 1176 |
|
|
| 1177 |
|
@param |
| 1178 |
|
|
| 1179 |
|
@param |
| 1180 |
|
|
| 1181 |
|
@param |
| 1182 |
|
|
| 1183 |
|
@return@link |
| 1184 |
|
|
| |
|
| 83,3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0,38 |
|
| 1185 |
9 |
private static String generateUniqueId(final String pageType,... |
| 1186 |
|
final String currentPage, |
| 1187 |
|
final Set<String> ids, |
| 1188 |
|
final String idBase) { |
| 1189 |
9 |
String id = idBase; |
| 1190 |
9 |
int counter = 1; |
| 1191 |
9 |
while (ids.contains(id)) { |
| 1192 |
0 |
id = idBase + String.valueOf(counter++); |
| 1193 |
|
} |
| 1194 |
|
|
| 1195 |
|
|
| 1196 |
9 |
ids.add(id); |
| 1197 |
9 |
if ("frame".equals(pageType)) { |
| 1198 |
3 |
id = currentPage + SEPARATOR_TOC + id; |
| 1199 |
|
} |
| 1200 |
9 |
return id; |
| 1201 |
|
} |
| 1202 |
|
|
| 1203 |
|
|
| 1204 |
|
|
| 1205 |
|
|
| 1206 |
|
|
| 1207 |
|
|
| 1208 |
|
|
| 1209 |
|
@param |
| 1210 |
|
|
| 1211 |
|
@return |
| 1212 |
|
@since |
| 1213 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0,18 |
|
| 1214 |
6 |
public String fixTableHeads(final String content) {... |
| 1215 |
|
|
| 1216 |
6 |
final Element body = parse(content).body(); |
| 1217 |
|
|
| 1218 |
6 |
final List<Element> tables = body.select("table"); |
| 1219 |
|
|
| 1220 |
6 |
for (final Element table : tables) { |
| 1221 |
|
|
| 1222 |
7 |
final List<Element> tableHeadRows = table.select("tbody > tr:has(th)"); |
| 1223 |
|
|
| 1224 |
7 |
if (tableHeadRows.size() == 1) { |
| 1225 |
|
|
| 1226 |
4 |
for (final Element row : tableHeadRows) { |
| 1227 |
|
|
| 1228 |
|
|
| 1229 |
4 |
row.remove(); |
| 1230 |
|
|
| 1231 |
|
|
| 1232 |
4 |
final Element thead = new Element(Tag.valueOf("thead"), ""); |
| 1233 |
4 |
thead.appendChild(row); |
| 1234 |
|
|
| 1235 |
4 |
table.prependChild(thead); |
| 1236 |
|
} |
| 1237 |
|
} |
| 1238 |
|
} |
| 1239 |
6 |
return body.html(); |
| 1240 |
|
} |
| 1241 |
|
|
| 1242 |
|
|
| 1243 |
|
private static final Pattern NONLATIN = Pattern.compile("[^\\w-]"); |
| 1244 |
|
|
| 1245 |
|
|
| 1246 |
|
private static final Pattern WHITESPACE = Pattern.compile("[\\s]"); |
| 1247 |
|
|
| 1248 |
|
|
| 1249 |
|
|
| 1250 |
|
|
| 1251 |
|
|
| 1252 |
|
@param |
| 1253 |
|
|
| 1254 |
|
@return |
| 1255 |
|
@since |
| 1256 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 1257 |
36 |
public static String slug(final String input) {... |
| 1258 |
36 |
return slug(input, DEFAULT_SLUG_SEPARATOR); |
| 1259 |
|
} |
| 1260 |
|
|
| 1261 |
|
|
| 1262 |
|
|
| 1263 |
|
|
| 1264 |
|
@param |
| 1265 |
|
|
| 1266 |
|
@param |
| 1267 |
|
|
| 1268 |
|
@return |
| 1269 |
|
@since |
| 1270 |
|
@see |
| 1271 |
|
|
| 1272 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 1273 |
45 |
private static String slug(final String input, final String separator) {... |
| 1274 |
45 |
final String nowhitespace = WHITESPACE.matcher(input).replaceAll(separator); |
| 1275 |
45 |
final String normalized = Normalizer.normalize(nowhitespace, Form.NFD); |
| 1276 |
45 |
return NONLATIN.matcher(normalized).replaceAll("").toLowerCase(Locale.ENGLISH); |
| 1277 |
|
} |
| 1278 |
|
|
| 1279 |
|
|
| 1280 |
|
|
| 1281 |
|
|
| 1282 |
|
|
| 1283 |
|
|
| 1284 |
|
|
| 1285 |
|
|
| 1286 |
|
|
| 1287 |
|
@param |
| 1288 |
|
|
| 1289 |
|
@param |
| 1290 |
|
|
| 1291 |
|
@return |
| 1292 |
|
|
| 1293 |
|
@since |
| 1294 |
|
|
| |
|
| 90% |
Uncovered Elements: 3 (30) |
Complexity: 6 |
Complexity Density: 0,27 |
|
| 1295 |
1 |
public List<? extends IdElement> headingTree(final String content, final List<String> sections) {... |
| 1296 |
|
|
| 1297 |
1 |
final List<String> sectionContents = this.split(content, "hr"); |
| 1298 |
1 |
final List<String> headIds = concat(HEADINGS, "[id]:not(.no-anchor)", true); |
| 1299 |
1 |
final List<HeadingItem> headingItems = new ArrayList<>(); |
| 1300 |
|
|
| 1301 |
1 |
int index = 0; |
| 1302 |
1 |
for (final String sectionContent : sectionContents) { |
| 1303 |
1 |
final String sectionType = index < sections.size() ? sections.get(index++) : ""; |
| 1304 |
|
|
| 1305 |
|
|
| 1306 |
1 |
if ("carousel".equals(sectionType)) { |
| 1307 |
0 |
continue; |
| 1308 |
|
} |
| 1309 |
1 |
final Element body = parse(sectionContent).body(); |
| 1310 |
|
|
| 1311 |
1 |
final List<Element> headings = body.select(String.join(", ", headIds)); |
| 1312 |
1 |
for (final Element heading : headings) { |
| 1313 |
3 |
headingItems |
| 1314 |
|
.add(new HeadingItem(heading.id(), heading.nodeName(), heading.text(), headingIndex(heading))); |
| 1315 |
|
} |
| 1316 |
|
} |
| 1317 |
|
|
| 1318 |
1 |
final List<HeadingItem> topHeadings = new ArrayList<>(); |
| 1319 |
1 |
final Stack<HeadingItem> parentHeadings = new Stack<>(); |
| 1320 |
|
|
| 1321 |
1 |
for (final HeadingItem heading : headingItems) { |
| 1322 |
|
|
| 1323 |
4 |
while (!parentHeadings.isEmpty() && parentHeadings.peek().headingLevel >= heading.headingLevel) { |
| 1324 |
1 |
parentHeadings.pop(); |
| 1325 |
|
} |
| 1326 |
|
|
| 1327 |
3 |
if (parentHeadings.isEmpty()) { |
| 1328 |
|
|
| 1329 |
1 |
topHeadings.add(heading); |
| 1330 |
|
} else { |
| 1331 |
|
|
| 1332 |
2 |
parentHeadings.peek().children.add(heading); |
| 1333 |
|
} |
| 1334 |
|
|
| 1335 |
|
|
| 1336 |
3 |
parentHeadings.push(heading); |
| 1337 |
|
} |
| 1338 |
|
|
| 1339 |
1 |
return topHeadings; |
| 1340 |
|
} |
| 1341 |
|
|
| 1342 |
|
|
| 1343 |
|
|
| 1344 |
|
|
| 1345 |
|
@param |
| 1346 |
|
@return |
| 1347 |
|
|
| |
|
| 62,5% |
Uncovered Elements: 3 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
|
| 1348 |
3 |
private static int headingIndex(final Element element) {... |
| 1349 |
3 |
final String tagName = element.tagName(); |
| 1350 |
3 |
if (tagName.startsWith("h")) { |
| 1351 |
3 |
try { |
| 1352 |
3 |
return Integer.parseInt(tagName.substring(1)); |
| 1353 |
|
} catch (final Exception ex) { |
| 1354 |
0 |
throw new IllegalArgumentException("Must be a header tag: " + tagName, ex); |
| 1355 |
|
} |
| 1356 |
|
} else { |
| 1357 |
0 |
throw new IllegalArgumentException("Must be a header tag: " + tagName); |
| 1358 |
|
} |
| 1359 |
|
} |
| 1360 |
|
|
| 1361 |
|
|
| 1362 |
|
@author |
| 1363 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 6 |
Complexity Density: 0,67 |
|
| 1364 |
|
private static final class HeadingItem implements IdElement { |
| 1365 |
|
|
| 1366 |
|
|
| 1367 |
|
private final String id; |
| 1368 |
|
|
| 1369 |
|
|
| 1370 |
|
private final String tagName; |
| 1371 |
|
|
| 1372 |
|
|
| 1373 |
|
private final String text; |
| 1374 |
|
|
| 1375 |
|
|
| 1376 |
|
private final int headingLevel; |
| 1377 |
|
|
| 1378 |
|
|
| 1379 |
|
private final List<HeadingItem> children = new ArrayList<>(); |
| 1380 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
| 1381 |
3 |
private HeadingItem(final String id, final String tagName, final String text, final int headingLevel) {... |
| 1382 |
3 |
this.id = id; |
| 1383 |
3 |
this.tagName = tagName; |
| 1384 |
3 |
this.text = text; |
| 1385 |
3 |
this.headingLevel = headingLevel; |
| 1386 |
|
} |
| 1387 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 1388 |
3 |
@Override... |
| 1389 |
|
public String getId() { |
| 1390 |
3 |
return id; |
| 1391 |
|
} |
| 1392 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 1393 |
1 |
@Override... |
| 1394 |
|
public String getTagName() { |
| 1395 |
1 |
return tagName; |
| 1396 |
|
} |
| 1397 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 1398 |
1 |
@Override... |
| 1399 |
|
public String getText() { |
| 1400 |
1 |
return text; |
| 1401 |
|
} |
| 1402 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 1403 |
2 |
@Override... |
| 1404 |
|
public List<HeadingItem> getItems() { |
| 1405 |
2 |
return Collections.unmodifiableList(children); |
| 1406 |
|
} |
| 1407 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 1408 |
3 |
@Override... |
| 1409 |
|
public int getHeadingLevel() { |
| 1410 |
3 |
return headingLevel; |
| 1411 |
|
} |
| 1412 |
|
} |
| 1413 |
|
|
| 1414 |
|
|
| 1415 |
|
|
| 1416 |
|
|
| 1417 |
|
@author |
| 1418 |
|
@since |
| 1419 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 1420 |
|
public interface IdElement { |
| 1421 |
|
|
| 1422 |
|
|
| 1423 |
|
|
| 1424 |
|
|
| 1425 |
|
@return |
| 1426 |
|
|
| 1427 |
|
String getId(); |
| 1428 |
|
|
| 1429 |
|
|
| 1430 |
|
@return |
| 1431 |
|
|
| 1432 |
|
String getTagName(); |
| 1433 |
|
|
| 1434 |
|
|
| 1435 |
|
|
| 1436 |
|
|
| 1437 |
|
@return |
| 1438 |
|
|
| 1439 |
|
String getText(); |
| 1440 |
|
|
| 1441 |
|
|
| 1442 |
|
@return |
| 1443 |
|
|
| 1444 |
|
int getHeadingLevel(); |
| 1445 |
|
|
| 1446 |
|
|
| 1447 |
|
|
| 1448 |
|
|
| 1449 |
|
@return |
| 1450 |
|
|
| 1451 |
|
List<? extends IdElement> getItems(); |
| 1452 |
|
} |
| 1453 |
|
} |