Skip to content

Commit 9fb1ae1

Browse files
authored
Merge pull request #308 from shuzijun/gradle
Optimized view
2 parents 6844470 + 1daac2a commit 9fb1ae1

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/main/java/com/shuzijun/leetcode/plugin/utils/CommentUtils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
44
import org.apache.commons.lang.StringUtils;
55
import org.jsoup.Jsoup;
66

7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
710
/**
811
* @author shuzijun
912
*/
1013
public class CommentUtils {
1114

15+
private static final Pattern subPattern = Pattern.compile("<sup>(<span.*>?)?([0-9abcdeghijklmnoprstuvwxyz\\+\\-\\*=\\(\\)\\.\\/]+)(</span>)?</sup>?");
16+
1217
public static String createComment(String html, CodeTypeEnum codeTypeEnum) {
13-
String body = codeTypeEnum.getComment() + Jsoup.parse(html.replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\n")).text().replaceAll("\\\\n", "\n" + codeTypeEnum.getComment());
18+
Matcher subMatcher = subPattern.matcher(html);
19+
while (subMatcher.find()) {
20+
String subStr = SuperscriptUtils.getSup(subMatcher.group(2));
21+
html = html.replace(subMatcher.group(), "<sup>" + subStr + "</sup>");
22+
}
23+
html = html.replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\n").replaceAll(" "," ");
24+
String body = codeTypeEnum.getComment() + Jsoup.parse(html).text().replaceAll("\\\\n", "\n" + codeTypeEnum.getComment());
1425
String[] lines = body.split("\n");
1526
StringBuilder sb = new StringBuilder();
1627
for (String line : lines) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.shuzijun.leetcode.plugin.utils;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* @author shuzijun
10+
*/
11+
public class SuperscriptUtils {
12+
13+
public static Map<Character, String> superscriptMap = new HashMap<>();
14+
15+
static {
16+
superscriptMap.put('0', "\u2070");
17+
superscriptMap.put('1', "\u00B9");
18+
superscriptMap.put('2', "\u00B2");
19+
superscriptMap.put('3', "\u00B3");
20+
superscriptMap.put('4', "\u2074");
21+
superscriptMap.put('5', "\u2075");
22+
superscriptMap.put('6', "\u2076");
23+
superscriptMap.put('7', "\u2077");
24+
superscriptMap.put('8', "\u2078");
25+
superscriptMap.put('9', "\u2079");
26+
superscriptMap.put('+', "\u207A");
27+
superscriptMap.put('-', "\u207B");
28+
superscriptMap.put('=', "\u207C");
29+
superscriptMap.put('(', "\u207D");
30+
superscriptMap.put(')', "\u207E");
31+
superscriptMap.put('.', "\u02D9");
32+
superscriptMap.put('/', "/");
33+
superscriptMap.put('a', "\u1D43");
34+
superscriptMap.put('b', "\u1D47");
35+
superscriptMap.put('c', "\u1D9C");
36+
superscriptMap.put('d', "\u1D48");
37+
superscriptMap.put('e', "\u1D49");
38+
superscriptMap.put('g', "\u1D4D");
39+
superscriptMap.put('h', "\u02B0");
40+
superscriptMap.put('i', "\u2071");
41+
superscriptMap.put('j', "\u02B2");
42+
superscriptMap.put('k', "\u1D4F");
43+
superscriptMap.put('l', "\u02E1");
44+
superscriptMap.put('m', "\u1D50");
45+
superscriptMap.put('n', "\u207F");
46+
superscriptMap.put('o', "\u1D52");
47+
superscriptMap.put('p', "\u1D56");
48+
superscriptMap.put('r', "\u02B3");
49+
superscriptMap.put('s', "\u02E2");
50+
superscriptMap.put('t', "\u1D57");
51+
superscriptMap.put('u', "\u1D58");
52+
superscriptMap.put('v', "\u1D5B");
53+
superscriptMap.put('w', "\u02B7");
54+
superscriptMap.put('x', "\u02E3");
55+
superscriptMap.put('y', "\u02B8");
56+
superscriptMap.put('z', "\u1646");
57+
58+
}
59+
60+
public static String getSup(String str) {
61+
if (StringUtils.isBlank(str)) {
62+
return str;
63+
}
64+
StringBuilder supSb = new StringBuilder();
65+
for (int i = 0; i < str.length(); i++) {
66+
Character c = str.charAt(i);
67+
if (superscriptMap.containsKey(str.charAt(i))) {
68+
supSb.append(superscriptMap.get(c));
69+
} else {
70+
supSb.append(c);
71+
}
72+
73+
}
74+
return supSb.toString();
75+
}
76+
77+
}

0 commit comments

Comments
 (0)