-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJNovelFormatter.java
More file actions
160 lines (135 loc) · 5 KB
/
JNovelFormatter.java
File metadata and controls
160 lines (135 loc) · 5 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
import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.*;
import java.awt.event.*;
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 JNovelFormatter extends JFrame {
private JPanel panel = new JPanel();
private JButton btnFile;
private JButton btnOutDir;
private JButton btnFormat;
private JTextField tfDir;
private JTextField tfOutDir;
private final String[] strEncodings = {"x-SJIS_0213", "UTF-8", "UTF-16"};
private JComboBox<String> cbEncoding;
private JCheckBox chkbBookmark;
public JNovelFormatter() {
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(new JLabel("Novel File or Directory:"));
btnFile = new JButton("File...");
tfDir = new JTextField();
btnFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
FileNameExtensionFilter filter = new FileNameExtensionFilter("Aozora Bunko text file", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(panel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
tfDir.setText(chooser.getSelectedFile().getCanonicalPath());
} catch(Exception x) {
JOptionPane.showMessageDialog(null, "Error");
x.printStackTrace();
}
}
}
});
panel.add(btnFile);
panel.add(tfDir);
panel.add(new JLabel("Output Directory:"));
btnOutDir = new JButton("Out Dir...");
tfOutDir = new JTextField();
btnOutDir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(panel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
tfOutDir.setText(chooser.getSelectedFile().getCanonicalPath());
} catch(Exception x) {
JOptionPane.showMessageDialog(panel, "Error");
x.printStackTrace();
}
}
}
});
panel.add(btnOutDir);
panel.add(tfOutDir);
panel.add(new JLabel("Encoding:"));
cbEncoding = new JComboBox<>(strEncodings);
panel.add(cbEncoding);
chkbBookmark = new JCheckBox("Add a bookmark anchor to every '.'");
panel.add(chkbBookmark);
btnFormat = new JButton("Format!");
btnFormat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String encoding = cbEncoding.getSelectedItem().toString();
String fileName = tfDir.getText();
// System.out.println(fileName);
Path input = Paths.get(fileName);
try {
java.util.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>");
if (chkbBookmark.isSelected())
sb.append(abp.bookmark(abp.parse()));
else
sb.append(abp.parse());
sb.append("</body>");
sb.append("</html>");
String outfile = input.getFileName().toString();
outfile = (outfile.indexOf('.') > 0) ? outfile.substring(0, outfile.indexOf('.')) : outfile;
Path output = Paths.get(tfOutDir.getText() + "/" + outfile + ".html");
try (BufferedWriter writer = Files.newBufferedWriter(output, StandardCharsets.UTF_8)) {
writer.append(sb.toString());
JOptionPane.showMessageDialog(panel, "Done");
} catch (IOException x) {
JOptionPane.showMessageDialog(panel, "Failed to write file: " + output.toString());
x.printStackTrace();
}
} catch (IOException x) {
JOptionPane.showMessageDialog(panel, "Incorrect encoding");
x.printStackTrace();
}
}
});
panel.add(btnFormat);
add(panel);
setSize(350,350);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new JNovelFormatter();
}
}