import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; import java.util.AbstractMap; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.regex.Pattern; public class sigPlace { final static String ROOTDIR = "sitefiles"; final static String REFDIR = "ref"; final static String OUTDIR = "out"; final static String DIRECTORYLISTING_FILENAME = "DIRECTORY_LISTING"; static int PORT = 8080; final static HashMap map = new HashMap<>(Map.ofEntries( new AbstractMap.SimpleEntry<>("$SITENAME", "SigPlace"), new AbstractMap.SimpleEntry<>("$SITE_BACKCOL", "#111"), new AbstractMap.SimpleEntry<>("$TITLE_CONTENT_START", "

"), new AbstractMap.SimpleEntry<>("$TITLE_CONTENT_END", "

"), new AbstractMap.SimpleEntry<>("$CONTENT_END", "
"), new AbstractMap.SimpleEntry<>("$DATE_CONTENT_START", "
") )); final static HashMap ops = new HashMap<>(Map.ofEntries( new AbstractMap.SimpleEntry<>( "%DEFAULT", Paths.get(REFDIR,"DEFAULT.html")), new AbstractMap.SimpleEntry<>( "%FOOTER", Paths.get(REFDIR,"FOOTER.html")) )); public static void main(String[] args) { if (args.length>0&&args.length%2==0) { int i=0; while (i items = Files.walk(Paths.get("out")).iterator(); ParseArticleFiles(items); items = Files.walk(Paths.get("out")).iterator(); ConvertArticleReferences(items); }catch (IOException e) { e.printStackTrace(); System.err.println("Copying files over failed!"); return; } System.out.println("Building directory listings..."); try { buildDirectoryListings(); } catch (IOException e) { e.printStackTrace(); System.err.println("Failed to build directory listings!"); return; } System.out.println("Site has been built into the "+OUTDIR+" directory."); ExportCodeFile(); System.out.println("\nStarting web server..."); new sigServer(); } private static void ParseArticleFiles(Iterator items) { while (items.hasNext()) { Path f = items.next(); System.out.println(" Found "+f.getFileName()); if (Files.isRegularFile(f)) { try { System.out.println(" Preparing "+f.getFileName()); List content = Files.readAllLines(f); if (isHTMLFile(f)) { content.addAll(0,Files.readAllLines(ops.get("%DEFAULT"))); content.addAll(Files.readAllLines(ops.get("%FOOTER"))); } System.out.println(" Parsing "+f.getFileName()); for (int i=0;i0&&isArticleFile(f)) { //Check for markdown pieces. if (s.charAt(0)=='-') { //Start of a title piece. s=s.replace("-",map.get("$TITLE_CONTENT_START")); s=s+map.get("$TITLE_CONTENT_END").replace("%ID%","id=\"content_"+i+"\""); //Use ⤈ if there's more text to be shown than can fit. } else if (s.contains("===")) { s=map.get("$CONTENT_END")+map.get("$DATE_CONTENT_START")+s.replace("===","")+map.get("$CONTENT_END")+"




⤈ Click to expand.
"+map.get("$CONTENT_END"); } } for (String key : map.keySet()) { s=s.replaceAll(Pattern.quote(key),map.get(key)); } content.set(i,s); } System.out.println(" Writing to "+f.toAbsolutePath()); Files.write(f, content, Charset.defaultCharset(),StandardOpenOption.CREATE,StandardOpenOption.TRUNCATE_EXISTING,StandardOpenOption.WRITE); System.out.println(" "+f.getFileName() + " conversion complete!"); } catch (IOException e) { e.printStackTrace(); } } } } private static void ConvertArticleReferences(Iterator items) { while (items.hasNext()) { Path f = items.next(); System.out.println(" Looking for Article References..."+f.getFileName()); if (Files.isRegularFile(f)&&isHTMLFile(f)) { System.out.println(" Searching "+f.getFileName()); try { List content = Files.readAllLines(f); for (int i=0;i0&&s.contains("$ARTICLE_PREVIEW")) { String article = s.replace("$ARTICLE_PREVIEW ",""); System.out.println(" Found article preview request in "+f.getFileName()+" for article "+article+"."); Path file = Paths.get(OUTDIR,article); content.remove(i--); } } } catch (IOException e) { e.printStackTrace(); } } } } static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException { Files.walk(Paths.get(sourceDirectoryLocation)) .forEach(source -> { Path destination = Paths.get(destinationDirectoryLocation, source.toString() .substring(sourceDirectoryLocation.length())); try { if (Files.isDirectory(destination)) { Files.createDirectories(destination); } else { Files.copy(source, destination, new CopyOption[]{StandardCopyOption.COPY_ATTRIBUTES,StandardCopyOption.REPLACE_EXISTING}); } } catch (IOException e) { e.printStackTrace(); } }); } static void buildDirectoryListings() throws IOException { String startingPath=Paths.get(sigPlace.OUTDIR).toAbsolutePath().toString(); HashMap> map = new HashMap<>(); Iterator it = Files.walk(Paths.get(sigPlace.OUTDIR)).iterator(); map.put("/",new ArrayList()); while (it.hasNext()) { Path f = it.next(); if (!f.getFileName().toString().equals(OUTDIR)) { String myKey = f.toAbsolutePath().toString().replace(startingPath,"").replace(f.getFileName().toString(),""); //System.out.println(myKey+","+f); map.putIfAbsent(myKey,new ArrayList()); map.get(myKey).add(f); } } System.out.println("Directory structure determined:"); System.out.println(" "+map); for (String key : map.keySet()) { System.out.println("Creating directory listing for "+key+"..."); StringBuilder sb = new StringBuilder(""); List data = Files.readAllLines(ops.get("%DEFAULT")); List data2 = Files.readAllLines(ops.get("%FOOTER")); for (String d : data) { for (String k : sigPlace.map.keySet()) { d=d.replaceAll(Pattern.quote(k),sigPlace.map.get(k)); } sb.append(d).append("\n"); } sb.append("

Directory Listing for "+key+"

"); sb.append(""); for (Path f : map.get(key)) { sb.append("
") .append("") .append((Files.isDirectory(f)?"📁":"🗎")) .append("") .append("") .append(f.getFileName()) .append("\t") .append(Files.getLastModifiedTime(f)) .append("\t") .append(Files.getOwner(f)) .append("\t") .append(Files.size(f)) .append("
\n"); } for (String d : data2) { for (String k : sigPlace.map.keySet()) { d=d.replaceAll(Pattern.quote(k),sigPlace.map.get(k)); } sb.append(d).append("\n"); } Path newf = Files.write(Paths.get(OUTDIR,key,DIRECTORYLISTING_FILENAME),sb.toString().getBytes()); System.out.println(" Added info for ("+map.size()+") files to "+newf.toAbsolutePath()); } } private static boolean isArticleFile(Path f) { return f.getFileName().toString().contains(".article"); } private static boolean isHTMLFile(Path f) { return f.getFileName().toString().contains(".html"); } private static void ExportCodeFile() { try { Path file = Paths.get("sigServer.java"); List data = Files.readAllLines(file); int i=0; while (!data.get(i++).contains("sigServer()")&&i