Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

154
LINES

< > BotCompany Repo | #578 // HTML Maker (JavaX)

JavaX source code - run with: x30.jar

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<Section> sections = new ArrayList<Section>();
  
  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("<p>" + section.content + "</p>\n");
      else if (section.tag.equals("link"))
        body.append("<p><a href=\"http://" + section.content + "\">" + section.content + "</a></p>\n");
      else if (section.tag.equals("mail-link"))
        body.append("<p><a href=\"mailto:" + section.content + "\">" + section.content + "</a></p>\n");
      else
        body.append("<" + section.tag + ">" + section.content + "</" + section.tag + ">\n");
    }
    return "<html>\n" + "<head>" + "<title>" + title + "</title>" + "</head>\n"
      + "<body>\n" + body + "</body></html>\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<String> toLines(String s) {
    List<String> lines = new ArrayList<String>();
    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;
  }
}
 

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

Comments [hide]

ID Author/Program Comment Date
415 #1000610 (pitcher) 2015-08-18 00:07:07
414 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

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: 853 / 743
Referenced in: [show references]