import java.io.*; import java.util.regex.*; import java.util.*; /** HTML Maker - takes a simple input file (index.html) and outputs HTML (also index.html) See: http://tinybrain.blog.de/2015/04/14/an-example-for-the-three-levels-20236978/ http://tinybrain.blog.de/2015/04/14/javax-better-programming-20236764/ */ public class main { static List
sections = new ArrayList
(); public static void main(String[] args) { String s = readTextFile("input/index.html", ""); System.out.println("Processing input:\n" + s); for (String line : toLines(s)) { int idx = line.indexOf(':'); if (idx >= 0) { String left = line.substring(0, idx), right = line.substring(idx+1).trim(); Matcher matcher = Pattern.compile("[^,]+").matcher(left); while (matcher.find()) { String leftpart = matcher.group().trim(); makeSection(leftpart, right); } } } String html = renderSections(); saveTextFile("output/index.html", html); System.out.println("\nHTML output:\n" + html); } public static void makeSection(String left, String right) { sections.add(new Section(left, right)); } public static String renderSections() { StringBuffer head = new StringBuffer(), body = new StringBuffer(); String title = findSection("title"); if (title == null) title = "Untitled"; for (Section section : sections) { if (section.tag.equals("title")) continue; else if (section.tag.equals("text")) body.append("

" + section.content + "

\n"); else if (section.tag.equals("link")) body.append("

" + section.content + "

\n"); else if (section.tag.equals("mail-link")) body.append("

" + section.content + "

\n"); else body.append("<" + section.tag + ">" + section.content + "\n"); } return "\n" + "" + "" + title + "" + "\n" + "\n" + body + "\n"; } public static String readTextFile(String fileName, String defaultContents) { try { if (!new java.io.File(fileName).exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } catch (IOException e) { throw new RuntimeException(e); } } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) builder.append(line).append('\n'); } finally { reader.close(); } return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); } /** writes safely (to temp file, then rename) */ public static void saveTextFile(String fileName, String contents) { try { java.io.File file = new java.io.File(fileName); java.io.File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new java.io.File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } catch (IOException e) { throw new RuntimeException(e); } } public static List toLines(String s) { List lines = new ArrayList(); int start = 0; while (true) { int i = toLines_nextLineBreak(s, start); if (i < 0) { if (s.length() > start) lines.add(s.substring(start)); break; } lines.add(s.substring(start, i)); if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') i += 2; else ++i; start = i; } return lines; } private static int toLines_nextLineBreak(String s, int start) { for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == '\r' || c == '\n') return i; } return -1; } static class Section { String tag, content; Section(String tag, String content) { this.tag = tag; this.content = content; } } static String findSection(String tag) { for (Section section : sections) if (section.tag.equals(tag)) return section.content; return null; } }