| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.util; |
| 21 |
|
|
| 22 |
|
import java.util.HashMap; |
| 23 |
|
import java.util.LinkedHashMap; |
| 24 |
|
import java.util.Map; |
| 25 |
|
import java.util.regex.Matcher; |
| 26 |
|
import java.util.regex.Pattern; |
| 27 |
|
|
| 28 |
|
import org.xwiki.rendering.util.IdGenerator; |
| 29 |
|
|
| 30 |
|
import com.xpn.xwiki.XWikiContext; |
| 31 |
|
|
| |
|
| 11.9% |
Uncovered Elements: 59 (67) |
Complexity: 14 |
Complexity Density: 0.33 |
|
| 32 |
|
public class TOCGenerator |
| 33 |
|
{ |
| 34 |
|
public static final String TOC_DATA_NUMBERING = "numbering"; |
| 35 |
|
|
| 36 |
|
public static final String TOC_DATA_LEVEL = "level"; |
| 37 |
|
|
| 38 |
|
public static final String TOC_DATA_TEXT = "text"; |
| 39 |
|
|
| |
|
| 0% |
Uncovered Elements: 58 (58) |
Complexity: 12 |
Complexity Density: 0.32 |
|
| 40 |
0 |
public static Map<String, Map<String, Object>> generateTOC(String content, int init, int max, boolean numbered,... |
| 41 |
|
XWikiContext context) |
| 42 |
|
{ |
| 43 |
0 |
IdGenerator idGenerator = new IdGenerator(); |
| 44 |
|
|
| 45 |
0 |
LinkedHashMap<String, Map<String, Object>> tocData = new LinkedHashMap<String, Map<String, Object>>(); |
| 46 |
|
|
| 47 |
0 |
int previousNumbers[] = { 0, 0, 0, 0, 0, 0, 0 }; |
| 48 |
|
|
| 49 |
0 |
Pattern pattern = Pattern.compile("(?-s)^[ \\t]*+(1(\\.1){0,5}+)[ \\t]++(.++)$", Pattern.MULTILINE); |
| 50 |
0 |
Matcher matcher = pattern.matcher(content); |
| 51 |
0 |
while (matcher.find()) { |
| 52 |
0 |
int level = (matcher.group(1).lastIndexOf("1") + 2) / 2; |
| 53 |
0 |
String text = matcher.group(3); |
| 54 |
0 |
text = context.getWiki().parseContent(text, context); |
| 55 |
|
|
| 56 |
0 |
String id = idGenerator.generateUniqueId("H", text); |
| 57 |
|
|
| 58 |
0 |
Map<String, Object> tocEntry = new HashMap<String, Object>(); |
| 59 |
0 |
tocEntry.put(TOC_DATA_LEVEL, level); |
| 60 |
0 |
tocEntry.put(TOC_DATA_TEXT, text); |
| 61 |
|
|
| 62 |
0 |
if (level >= init && level <= max) { |
| 63 |
0 |
if (numbered) { |
| 64 |
0 |
String number = ""; |
| 65 |
0 |
int currentNumber = 0; |
| 66 |
0 |
for (int i = previousNumbers.length - 1; i >= init; i--) { |
| 67 |
0 |
int num = 0; |
| 68 |
0 |
int previousNumber = previousNumbers[i]; |
| 69 |
|
|
| 70 |
0 |
if (previousNumber > 0) { |
| 71 |
|
|
| 72 |
0 |
num = previousNumber; |
| 73 |
0 |
if (i == level) { |
| 74 |
|
|
| 75 |
|
|
| 76 |
0 |
num = previousNumber + 1; |
| 77 |
0 |
} else if (i > level) { |
| 78 |
|
|
| 79 |
0 |
previousNumbers[i] = 0; |
| 80 |
|
} |
| 81 |
|
} else { |
| 82 |
0 |
num = 1; |
| 83 |
|
|
| 84 |
|
|
| 85 |
0 |
if (i < level) { |
| 86 |
0 |
previousNumbers[i] = previousNumbers[i] + 1; |
| 87 |
|
} |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
0 |
if (i <= level) { |
| 92 |
0 |
if ((number.length()) == 0) { |
| 93 |
|
|
| 94 |
0 |
number = num + number; |
| 95 |
0 |
currentNumber = num; |
| 96 |
|
} else { |
| 97 |
|
|
| 98 |
0 |
number = num + "." + number; |
| 99 |
|
} |
| 100 |
|
} |
| 101 |
|
} |
| 102 |
|
|
| 103 |
0 |
previousNumbers[level] = currentNumber; |
| 104 |
|
|
| 105 |
0 |
tocEntry.put(TOC_DATA_NUMBERING, number); |
| 106 |
|
} |
| 107 |
0 |
tocData.put(id, tocEntry); |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
0 |
return tocData; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
@deprecated@link |
| 115 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 116 |
3 |
@Deprecated... |
| 117 |
|
public static String makeHeadingID(String text, int occurence, XWikiContext context) |
| 118 |
|
{ |
| 119 |
3 |
text = "H" + text; |
| 120 |
3 |
text = text.replaceAll("[^a-zA-Z0-9]", ""); |
| 121 |
|
|
| 122 |
3 |
if (occurence > 0) { |
| 123 |
1 |
return text + "-" + occurence; |
| 124 |
|
} else { |
| 125 |
2 |
return text; |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
} |