-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAozoraBunkoRuby.java
More file actions
309 lines (262 loc) · 10.4 KB
/
AozoraBunkoRuby.java
File metadata and controls
309 lines (262 loc) · 10.4 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import java.io.*;
import java.nio.charset.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.*;
public class AozoraBunkoRuby {
private String text;
// private int dbgInt = 0;
private List<Integer> liKanjiStart;
private List<Integer> liFuriganaOpening;
private List<Integer> liFuriganaClosing;
private List<Integer> liBoutenOpening;
private List<Integer> liBoutenClosing;
private List<Integer> liKanjiBou;
private HashMap<String, String> tenStyles;
private HashMap<String, String> senStyles;
private static final String BOUTEN = "\u508d\u70b9";
private static final String BOUSEN = "\u7dda";
private static final String SPACING = "\u5B57\u4E0B\u3052";
private static final String FW_INTS = "\uFF10\uFF11\uFF12\uFF13\uFF14\uFF15\uFF16\uFF17\uFF18\uFF19";
public AozoraBunkoRuby(String text) {
this.text = text;
this.liKanjiStart = new ArrayList<>();
this.liFuriganaOpening = new ArrayList<>();
this.liFuriganaClosing = new ArrayList<>();
this.liBoutenOpening = new ArrayList<>();
this.liBoutenClosing = new ArrayList<>();
this.liKanjiBou = new ArrayList<>();
this.tenStyles = new HashMap<>();
this.tenStyles.put("\u306b\u4E38\u508D\u70B9", "●");
this.tenStyles.put("\u306b\u767D\u4E38\u508D\u70B9", "○");
this.tenStyles.put("\u306b\u9ED2\u4E09\u89D2\u508D\u70B9", "▲");
this.tenStyles.put("\u306b\u767D\u4E09\u89D2\u508D\u70B9", "△");
this.tenStyles.put("\u306b\u4E8C\u91CD\u4E38\u508D\u70B9", "◎");
this.tenStyles.put("\u306b\u3070\u3064\u508D\u70B9", "×");
this.senStyles = new HashMap<>();
this.senStyles.put("\u306b\u4e8c\u91cd\u508d\u7dda", "text-decoration-style: double;");
this.senStyles.put("\u306b\u9396\u7dda", "text-decoration-style: dotted;");
this.senStyles.put("\u306b\u7834\u7dda", "text-decoration-style: dashed;");
this.senStyles.put("\u306b\u6ce2\u7dda", "text-decoration-style: wavy;");
}
private void replacements() {
this.text = this.text.replaceAll("\uFF3B\uFF03\u633F\u7D75\uFF08", "<img src=\"");
this.text = this.text.replaceAll("\uFF09\u5165\u308B\uFF3D", "\">");
this.text = this.text.replaceAll("\uFF3B\uFF03\u6539\u9801\uFF3D", "<br>");
}
public String parse() {
this.replacements();
this.getMarkerIdxs();
/*
System.out.printf("%d %d %d %d %d %d\n",
liKanjiStart.size(),
liFuriganaOpening.size(),
liFuriganaClosing.size(),
liBoutenOpening.size(),
liBoutenClosing.size(),
liKanjiBou.size());
int count1 = 0, count2 = 0;
for (int i = 0; i < this.text.length(); i++) {
if (this.text.charAt(i) == '\u300a') {
count1++;
}
else if (this.text.charAt(i) == '\u300b') {
count2++;
}
}
System.out.printf("%d %d", count1, count2);
System.out.println(this.text.substring(133130, 133150));
System.out.println("---------------------------------------------------");
System.out.println(this.text.substring(133733, 133999));
*/
StringBuilder sb = new StringBuilder();
int i = 0, j = 0, curr = 0;
int kssize = this.liKanjiStart.size(), kbsize = this.liKanjiBou.size();
while (i < kssize && j < kbsize) {
// System.out.printf("%d, %d: %d, [%d, %d], [%d, %d]\n", i, j, curr, liKanjiStart.get(i), liFuriganaClosing.get(i), liKanjiBou.get(j), liBoutenClosing.get(j));
if (liKanjiStart.get(i) < liKanjiBou.get(j)) {
sb.append(this.text.substring(curr, liKanjiStart.get(i)));
sb.append(furiganaToRubyTag(liKanjiStart.get(i), liFuriganaOpening.get(i), liFuriganaClosing.get(i)));
curr = liFuriganaClosing.get(i) + 1;
i++;
}
else {
sb.append(this.text.substring(curr, liKanjiBou.get(j)));
sb.append(boutenToRubyTag(liKanjiBou.get(j), liBoutenOpening.get(j), liBoutenClosing.get(j)));
curr = liBoutenClosing.get(j) + 1;
j++;
}
}
while (i < this.liKanjiStart.size()) {
sb.append(this.text.substring(curr, liKanjiStart.get(i)));
sb.append(furiganaToRubyTag(liKanjiStart.get(i), liFuriganaOpening.get(i), liFuriganaClosing.get(i)));
curr = liFuriganaClosing.get(i) + 1;
i++;
}
while (j < this.liKanjiBou.size()) {
// System.out.printf("%d: %d, %d\n", j, curr, liKanjiBou.get(j));
if (curr >= liKanjiBou.get(j))
break;
sb.append(this.text.substring(curr, liKanjiBou.get(j)));
sb.append(boutenToRubyTag(liKanjiBou.get(j), liBoutenOpening.get(j), liBoutenClosing.get(j)));
curr = liBoutenClosing.get(j) + 1;
j++;
}
sb.append(this.text.substring(curr));
return sb.toString().replaceAll("\uff5c", "");
}
public String bookmark(String text) {
StringBuilder sb = new StringBuilder();
int curr = 0, count = 1, idx;
while ((idx = text.indexOf('\u3002', curr)) != -1) {
// System.out.printf("%d %d\n", curr, idx);
sb.append(text.substring(curr, idx));
sb.append("<a name=\"save_" + count + "\" href=\"#save_" + count + "\">。</a>");
curr = idx + 1;
count++;
}
return sb.toString();
}
private void getMarkerIdxs() {
for (int i = 0; i < text.length(); i++) {
// <<
if (this.text.charAt(i) == '\u300a') {
this.liFuriganaOpening.add(i);
int idx;
for (idx = i - 1; isCJKIdeograph(this.text.charAt(idx)) && this.text.charAt(idx) != '\uff5c'; idx--);
if (idx == i - 1) {
for (idx = i - 1; this.text.charAt(idx) != '\uff5c'; idx--);
}
this.liKanjiStart.add(idx + 1);
for (idx = i + 1; this.text.charAt(idx) != '\u300b'; idx++);
this.liFuriganaClosing.add(idx);
i = idx + 1;
}
// // >>
// else if (this.text.charAt(i) == '\u300b') {
// this.liFuriganaClosing.add(i);
// }
// [
else if (this.text.charAt(i) == '\uff3b') {
this.liBoutenOpening.add(i);
int eidx, ws = -1, we = -1;
for (eidx = i + 1; this.text.charAt(eidx) != '\uff3d'; eidx++) {
if (this.text.charAt(eidx) == '\u300c')
ws = eidx;
else if (this.text.charAt(eidx) == '\u300d')
we = eidx;
}
String btext = this.text.substring(i + 1, eidx);
if (btext.endsWith(BOUTEN) || btext.endsWith(BOUSEN))
this.liKanjiBou.add(i - (we-ws-1));
else
this.liKanjiBou.add(i);
this.liBoutenClosing.add(eidx);
i = eidx + 1;
}
}
}
private String furiganaToRubyTag(int kanjiIndex, int startIndex, int endIndex) {
StringBuilder sbFurigana = new StringBuilder();
int i;
sbFurigana.append("<rp>").append(this.text.charAt(startIndex)).append("</rp><rt>");
// System.out.printf("%d %d %d %d\n", dbgInt++, kanjiIndex, startIndex, endIndex);
sbFurigana.append(this.text.substring(startIndex + 1, endIndex));
sbFurigana.append("</rt><rp>").append(this.text.charAt(endIndex)).append("</rp></ruby>");
sbFurigana.insert(0, "</rb>");
sbFurigana.insert(0, this.text.substring(kanjiIndex, startIndex));
sbFurigana.insert(0, "<ruby><rb>");
return sbFurigana.toString();
}
private String boutenToRubyTag(int kanjiIndex, int startIndex, int endIndex) {
String btext = this.text.substring(startIndex+1, endIndex);
int wordStart = btext.indexOf("\u300c");
int wordEnd = btext.indexOf("\u300d");
int wordLength = wordEnd - wordStart - 1;
if (kanjiIndex != startIndex) {
StringBuilder output = new StringBuilder();
if (btext.endsWith(BOUTEN)) {
String stylename = btext.substring(wordEnd + 1);
String style = (this.tenStyles.containsKey(stylename)) ? this.tenStyles.get(stylename) : "﹅";
output.append("<ruby><rb>").append(this.text.substring(kanjiIndex, startIndex));
output.append("<rp>\u300a</rp><rt>");
for (int i = 0; i < wordLength; i++)
output.append(style);
output.append("</rt><rp>\u300b</rp></ruby>");
return output.toString();
}
else if (btext.endsWith(BOUSEN)) {
String stylename = btext.substring(wordEnd + 1);
String style = (this.senStyles.containsKey(stylename)) ? this.senStyles.get(stylename) : "";
output.append("<u style=\"").append(style).append("\">").append(this.text.substring(kanjiIndex, startIndex)).append("</u>");
return output.toString();
}
}
else {
StringBuilder output = new StringBuilder();
if (btext.endsWith(SPACING)) {
int hash = btext.indexOf("\uFF03");
int ji = btext.indexOf("\u5B57");
String space = btext.substring(hash+1, ji);
int value = 0;
for (int i = 0; i < space.length(); i++) {
value = value * 10 + FW_INTS.indexOf(space.charAt(i));
}
for (int i = 0; i < value; i++)
output.append(" ");
return output.toString();
}
}
return this.text.substring(kanjiIndex, startIndex);
}
private boolean isCJKIdeograph(char c) {
return Character.UnicodeBlock.of(c) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || c == '\u3005';
}
/*
public static void main(String[] args) {
String encoding = args[0];
String fileName = args[1];
System.out.println(fileName);
Path input = Paths.get(fileName);
try {
List<String> lines = Files.readAllLines(input, Charset.forName(encoding));
StringBuilder textsb = new StringBuilder();
boolean skip = false;
for (String line : lines) {
if (!skip) {
if (line.startsWith("-"))
skip = true;
else
textsb.append(line).append("\n");
} else {
if (line.startsWith("-"))
skip = false;
}
}
String text = textsb.toString();
AozoraBunkoRuby abp = new AozoraBunkoRuby(text);
StringBuilder sb = new StringBuilder();
sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
sb.append("<head>");
sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
sb.append("<link rel='stylesheet' type='text/css' href='jnf_style.css' />");
sb.append("</head>");
sb.append("<body>");
sb.append(abp.bookmark(abp.parse()));
sb.append("</body>");
sb.append("</html>");
Path output = Paths.get(args[2]);
try (BufferedWriter writer = Files.newBufferedWriter(output, StandardCharsets.UTF_8)) {
writer.append(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}