1 | import java.io.*; |
2 | import java.util.regex.*; |
3 | import java.util.*; |
4 | |
5 | /** HTML Maker - takes a simple input file (index.html) and outputs HTML (also index.html) |
6 | See: |
7 | http://tinybrain.blog.de/2015/04/14/an-example-for-the-three-levels-20236978/ |
8 | http://tinybrain.blog.de/2015/04/14/javax-better-programming-20236764/ |
9 | */ |
10 | |
11 | public class main {
|
12 | static List<Section> sections = new ArrayList<Section>(); |
13 | |
14 | public static void main(String[] args) {
|
15 | String s = readTextFile("input/index.html", "");
|
16 | System.out.println("Processing input:\n" + s);
|
17 | |
18 | for (String line : toLines(s)) {
|
19 | int idx = line.indexOf(':');
|
20 | if (idx >= 0) {
|
21 | String left = line.substring(0, idx), right = line.substring(idx+1).trim(); |
22 | Matcher matcher = Pattern.compile("[^,]+").matcher(left);
|
23 | while (matcher.find()) {
|
24 | String leftpart = matcher.group().trim(); |
25 | makeSection(leftpart, right); |
26 | } |
27 | } |
28 | } |
29 | |
30 | String html = renderSections(); |
31 | saveTextFile("output/index.html", html);
|
32 | System.out.println("\nHTML output:\n" + html);
|
33 | } |
34 | |
35 | public static void makeSection(String left, String right) {
|
36 | sections.add(new Section(left, right)); |
37 | } |
38 | |
39 | public static String renderSections() {
|
40 | StringBuffer head = new StringBuffer(), body = new StringBuffer(); |
41 | String title = findSection("title");
|
42 | if (title == null) title = "Untitled"; |
43 | |
44 | for (Section section : sections) {
|
45 | if (section.tag.equals("title"))
|
46 | continue; |
47 | else if (section.tag.equals("text"))
|
48 | body.append("<p>" + section.content + "</p>\n");
|
49 | else if (section.tag.equals("link"))
|
50 | body.append("<p><a href=\"http://" + section.content + "\">" + section.content + "</a></p>\n");
|
51 | else if (section.tag.equals("mail-link"))
|
52 | body.append("<p><a href=\"mailto:" + section.content + "\">" + section.content + "</a></p>\n");
|
53 | else |
54 | body.append("<" + section.tag + ">" + section.content + "</" + section.tag + ">\n");
|
55 | } |
56 | return "<html>\n" + "<head>" + "<title>" + title + "</title>" + "</head>\n" |
57 | + "<body>\n" + body + "</body></html>\n"; |
58 | } |
59 | |
60 | public static String readTextFile(String fileName, String defaultContents) {
|
61 | try {
|
62 | if (!new java.io.File(fileName).exists()) |
63 | return defaultContents; |
64 | |
65 | FileInputStream fileInputStream = new FileInputStream(fileName); |
66 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); |
67 | return loadTextFile(inputStreamReader); |
68 | } catch (IOException e) {
|
69 | throw new RuntimeException(e); |
70 | } |
71 | } |
72 | |
73 | public static String loadTextFile(Reader reader) throws IOException {
|
74 | StringBuilder builder = new StringBuilder(); |
75 | try {
|
76 | BufferedReader bufferedReader = new BufferedReader(reader); |
77 | String line; |
78 | while ((line = bufferedReader.readLine()) != null) |
79 | builder.append(line).append('\n');
|
80 | } finally {
|
81 | reader.close(); |
82 | } |
83 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
84 | } |
85 | |
86 | /** writes safely (to temp file, then rename) */ |
87 | public static void saveTextFile(String fileName, String contents) {
|
88 | try {
|
89 | java.io.File file = new java.io.File(fileName); |
90 | java.io.File parentFile = file.getParentFile(); |
91 | if (parentFile != null) |
92 | parentFile.mkdirs(); |
93 | String tempFileName = fileName + "_temp"; |
94 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
95 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); |
96 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
97 | printWriter.print(contents); |
98 | printWriter.close(); |
99 | if (file.exists() && !file.delete()) |
100 | throw new IOException("Can't delete " + fileName);
|
101 | |
102 | if (!new java.io.File(tempFileName).renameTo(file)) |
103 | throw new IOException("Can't rename " + tempFileName + " to " + fileName);
|
104 | } catch (IOException e) {
|
105 | throw new RuntimeException(e); |
106 | } |
107 | } |
108 | |
109 | public static List<String> toLines(String s) {
|
110 | List<String> lines = new ArrayList<String>(); |
111 | int start = 0; |
112 | while (true) {
|
113 | int i = toLines_nextLineBreak(s, start); |
114 | if (i < 0) {
|
115 | if (s.length() > start) lines.add(s.substring(start)); |
116 | break; |
117 | } |
118 | |
119 | lines.add(s.substring(start, i)); |
120 | if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') |
121 | i += 2; |
122 | else |
123 | ++i; |
124 | |
125 | start = i; |
126 | } |
127 | return lines; |
128 | } |
129 | |
130 | private static int toLines_nextLineBreak(String s, int start) {
|
131 | for (int i = start; i < s.length(); i++) {
|
132 | char c = s.charAt(i); |
133 | if (c == '\r' || c == '\n') |
134 | return i; |
135 | } |
136 | return -1; |
137 | } |
138 | |
139 | static class Section {
|
140 | String tag, content; |
141 | |
142 | Section(String tag, String content) {
|
143 | this.tag = tag; |
144 | this.content = content; |
145 | } |
146 | } |
147 | |
148 | static String findSection(String tag) {
|
149 | for (Section section : sections) |
150 | if (section.tag.equals(tag)) |
151 | return section.content; |
152 | return null; |
153 | } |
154 | } |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
2 comment(s) hidden. show
| Snippet ID: | #578 |
| Snippet name: | HTML Maker (JavaX) |
| Eternal ID of this version: | #578/1 |
| Text MD5: | 3f4f1cceff767977c9c4e5be04151e3b |
| Author: | stefan |
| Category: | |
| Type: | JavaX source code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-04-14 22:25:35 |
| Source code size: | 5239 bytes / 154 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 1505 / 1364 |
| Referenced in: | [show references] |