/
DocBookService.java
154 lines (121 loc) · 6.09 KB
/
DocBookService.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.kodcu.service.convert;
import com.kodcu.other.Current;
import com.kodcu.other.IOHelper;
import org.joox.Match;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xml.sax.InputSource;
import java.io.StringReader;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.joox.JOOX.$;
/**
* Created by usta on 19.07.2014.
*/
@Component
public class DocBookService {
private final Pattern ascIncludeRegex = Pattern.compile("(?<=include::)(?<path>.*?)(?=\\[(.*?)\\])");
private final Pattern mdIncludeRegex = Pattern.compile("\\[.*?\\]\\((?<path>.*\\.(md|markdown|asc|adoc|asciidoc|ad|txt))\\)");
private final RenderService docConverter;
private final Current current;
@Autowired
public DocBookService(final RenderService docConverter, final Current current) {
this.docConverter = docConverter;
this.current = current;
}
private void traverseLines(List<String> lines, StringBuffer buffer, Path rootPath) {
for (String line : lines) {
Matcher ascMatcher = ascIncludeRegex.matcher(line);
Matcher markdownMatcher = mdIncludeRegex.matcher(line);
if (ascMatcher.find()) {
String chapterPath = ascMatcher.group("path");
Path chapterFile = rootPath.resolve(chapterPath);
String chapterContent = IOHelper.readFile(chapterFile);
traverseLines(Arrays.asList(chapterContent.split("\\r?\\n")), buffer, chapterFile.getParent());
} else if (markdownMatcher.find()) {
String chapterPath = markdownMatcher.group("path");
Path chapterFile = rootPath.resolve(chapterPath);
String chapterContent = IOHelper.readFile(chapterFile);
traverseLines(Arrays.asList(chapterContent.split("\\r?\\n")), buffer, chapterFile.getParent());
} else
traverseLine(line, buffer);
}
}
private void traverseLine(String line, StringBuffer buffer) {
if (line.matches("^=+ +.*:.*"))
line = line.replace(":", "00HEADER00COLON00");
buffer.append(line + "\n");
}
public void generateDocbook(Consumer<String> step) {
StringBuffer outputBuffer = new StringBuffer();
Path currentTabPath = current.currentPath().get();
Path currentTabPathDir = currentTabPath.getParent();
StringBuffer stringBuffer = new StringBuffer();
traverseLines(Arrays.asList(current.currentEditorValue().split("\\r?\\n")), stringBuffer, currentTabPathDir);
String text = stringBuffer.toString();
docConverter.convertDocbook(text, true, docBookHeaderContent -> {
docBookHeaderContent = docBookHeaderContent.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
StringReader bookReader = new StringReader(docBookHeaderContent);
Match rootDocument = IOHelper.$(new InputSource(bookReader));
bookReader.close();
// // makes figure centering
rootDocument.find("figure").find("imagedata").attr("align", "center");
// remove callout's duplicated refs and pick last
rootDocument.find("callout").forEach(elem -> {
String arearefs = $(elem).attr("arearefs");
String[] cos = arearefs.split(" ");
if (cos.length > 1)
$(elem).attr("arearefs", cos[cos.length - 1]);
});
outputBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outputBuffer.append("\n");
outputBuffer.append("<?asciidoc-toc?>");
outputBuffer.append("\n");
outputBuffer.append("<?asciidoc-numbered?>");
outputBuffer.append("\n");
outputBuffer.append(rootDocument.content());
String result = outputBuffer.toString();
result = result.replace("00HEADER00COLON00", ":");
step.accept(result);
});
}
public void generateDocbookArticle(Consumer<String> step) {
StringBuilder outputBuffer = new StringBuilder();
Path currentTabPath = current.currentPath().get();
Path currentTabPathDir = currentTabPath.getParent();
StringBuffer stringBuffer = new StringBuffer();
// traverseLines(Arrays.asList(current.currentEditorValue().split("\\r?\\n")), stringBuffer, currentTabPathDir);
// String input = stringBuffer.toString();
docConverter.convertDocbookArticle(current.currentEditorValue(), docbook -> {
docbook = docbook.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
StringReader bookReader = new StringReader(docbook);
Match rootDocument = IOHelper.$(new InputSource(bookReader));
bookReader.close();
// changes formalpara to figure bug fix
rootDocument.find("imageobject").parents("formalpara").each((context) -> {
$(context).rename("figure");
});
// makes figure centering
rootDocument.find("figure").find("imagedata").attr("align", "center");
// remove callout's duplicated refs and pick last
rootDocument.find("callout").forEach(elem -> {
String arearefs = $(elem).attr("arearefs");
String[] cos = arearefs.split(" ");
if (cos.length > 1)
$(elem).attr("arearefs", cos[cos.length - 1]);
});
outputBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outputBuffer.append("\n");
outputBuffer.append("<?asciidoc-toc?>\n");
outputBuffer.append("<?asciidoc-numbered?>\n");
outputBuffer.append(rootDocument.content());
String result = outputBuffer.toString();
result = result.replace("00HEADER00COLON00", ":");
step.accept(result);
});
}
}