-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMd2Html.java
129 lines (94 loc) · 4.25 KB
/
Md2Html.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package md2html;
import javafx.util.Pair;
import java.io.*;
import java.util.Arrays;
import java.util.*;
import java.util.regex.*;
import java.util.stream.Collectors;
/**
* Created by nikitos on 18.12.17.
*/
public class Md2Html {
private static final String ENCODING = "utf8";
private static StringBuilder sb = new StringBuilder();
private static final Pattern headingPattern = Pattern.compile("^(#+)\\s+(.*)", Pattern.DOTALL);
private static List<Pair<Pair<String,String>,Pair<String, String>>> tagPair = Arrays.asList(
new Pair<>(new Pair<>("(?<!\\\\)\\*\\*","(?<!\\\\)\\*\\*"), new Pair<>("<strong>", "</strong>")),
new Pair<>(new Pair<>("(?<!\\\\)__", "(?<!\\\\)__"), new Pair<>("<strong>", "</strong>")),
new Pair<>(new Pair<>("(?<!\\\\)\\*", "(?<!\\\\)\\*"), new Pair<>("<em>", "</em>")),
new Pair<>(new Pair<>("(?<!\\\\)_", "(?<!\\\\)_"), new Pair<>("<em>", "</em>")),
new Pair<>(new Pair<>("--", "--"), new Pair<>("<s>", "</s>")),
new Pair<>(new Pair<>("\\+\\+", "\\+\\+"), new Pair<>("<u>", "</u>")),
new Pair<>(new Pair<>("`", "`"), new Pair<>("<code>", "</code>")),
new Pair<>(new Pair<>("~", "~"), new Pair<>("<mark>", "</mark>"))
);
public static void start(String inputFileName, String outputFileName) {
String contents;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFileName), ENCODING))) {
contents = reader.lines().collect(Collectors.joining("\n"));
} catch (FileNotFoundException e) {
System.err.println("Input file not found: " + inputFileName);
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
String[] inputStrings = contents.replaceAll("^\n*", "").replaceAll("\n*$", "").split("\n\\s*\n");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName))) {
int iterator = 0;
for (String now: inputStrings) {
if (isHeading(now)) {
parseHeader(now);
} else {
parseParagraph(now);
}
writer.write(sb.toString());
if (inputStrings.length - 1 != iterator++) writer.write("\n");
sb.setLength(0);
}
} catch (IOException e) {
System.err.println("Failed to write to the output file: " + outputFileName);
}
}
private static void parseHeader(String s) {
Matcher matcher = headingPattern.matcher(s);
boolean matches = matcher.matches();
int headingLevel = matcher.group(1).length();
String rest = matcher.group(2);
sb.append("<h").append(headingLevel).append(">");
checkContent(rest);
sb.append("</h").append(headingLevel).append(">");
}
private static void parseParagraph(String s) {
sb.append("<p>");
checkContent(s);
sb.append("</p>");
}
private static void checkContent(String s) {
sb.append(unescapeSpecialCharacters(replaceTags(escapeSpecialCharacters(s))));
}
private static String replaceTags(String s) {
if (s == null) throw new NullPointerException();
for (Pair<Pair<String,String>,Pair<String,String>> pair: tagPair) {
Pair first = pair.getKey();
Pair second = pair.getValue();
s = s.replaceAll("(?s)" + first.getKey() + "(.*?)" + first.getValue(), second.getKey() + "$1" + second.getValue());
}
return s;
}
private static String escapeSpecialCharacters(String s) {
if (s == null) throw new NullPointerException();
return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
}
private static String unescapeSpecialCharacters(String s) {
if (s == null) throw new NullPointerException();
return s.replaceAll("\\\\\\*", "\\*").replaceAll("\\\\_", "_");
}
private static boolean isHeading(String s) {
if (s == null) throw new NullPointerException();
return headingPattern.matcher(s).matches();
}
public static void main(String[] args) {
new Md2Html().start(args[0], args[1]);
}
}