SigPlace/sigPlace.java

136 lines
5.3 KiB
Java
Raw Normal View History

2022-05-02 15:45:53 +00:00
import java.io.IOException;
2022-05-02 17:44:55 +00:00
import java.nio.charset.Charset;
2022-05-02 15:45:53 +00:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
2022-05-02 17:44:55 +00:00
import java.nio.file.StandardOpenOption;
2022-05-02 15:45:53 +00:00
import java.util.AbstractMap;
import java.util.HashMap;
2022-05-02 17:44:55 +00:00
import java.util.List;
2022-05-02 15:45:53 +00:00
import java.util.Map;
import java.util.Set;
2022-05-02 18:16:03 +00:00
import java.util.regex.Pattern;
2022-05-02 15:45:53 +00:00
import java.util.stream.Collectors;
public class sigPlace {
2022-05-02 17:44:55 +00:00
final static String ROOTDIR = "sitefiles";
2022-05-02 18:16:03 +00:00
final static String REFDIR = "ref";
2022-05-02 17:44:55 +00:00
final static String OUTDIR = "out";
static int PORT = 8080;
2022-05-02 17:44:55 +00:00
2022-05-02 15:45:53 +00:00
final static HashMap<String,String> map = new HashMap<>(Map.ofEntries(
2022-05-02 21:03:16 -05:00
new AbstractMap.SimpleEntry<>("$SITENAME", "SigPlace"),
2022-05-03 14:47:49 +00:00
new AbstractMap.SimpleEntry<>("$SITE_BACKCOL", "#111"),
new AbstractMap.SimpleEntry<>("$TITLE_CONTENT_START", "<div class=\"contentWrapper\"><h1>"),
2022-05-03 18:07:44 +00:00
new AbstractMap.SimpleEntry<>("$TITLE_CONTENT_END", "</h1><div class=\"content\" %ID%>"),
2022-05-03 15:29:19 +00:00
new AbstractMap.SimpleEntry<>("$CONTENT_END", "</div>"),
2022-05-03 17:48:17 +00:00
new AbstractMap.SimpleEntry<>("$DATE_CONTENT_START", "<div class=\"datebar\"></div><div class=\"date\">")
2022-05-02 15:45:53 +00:00
));
2022-05-02 18:16:03 +00:00
final static HashMap<String,Path> ops = new HashMap<>(Map.ofEntries(
2022-05-02 17:44:55 +00:00
new AbstractMap.SimpleEntry<>(
2022-05-02 18:16:03 +00:00
"%DEFAULT", Paths.get(REFDIR,"DEFAULT.html")),
new AbstractMap.SimpleEntry<>(
"%FOOTER", Paths.get(REFDIR,"FOOTER.html"))
2022-05-02 17:44:55 +00:00
));
2022-05-02 15:45:53 +00:00
public static void main(String[] args) {
if (args.length>0&&args.length%2==0) {
int i=0;
while (i<args.length) {
String arg1=args[i];
String arg2=args[i+1];
i+=2;
if (arg1.equals("-p")) {
PORT=Integer.parseInt(arg2);
System.out.println("Port set to "+PORT+".");
} else {
System.err.println("Invalid argument \""+arg1+"\".");
return;
}
}
}
2022-05-02 17:44:55 +00:00
Set<Path> files = GetFilesInDir(ROOTDIR);
2022-05-02 15:45:53 +00:00
for (Path f : files) {
2022-05-02 17:44:55 +00:00
2022-05-02 18:16:03 +00:00
System.out.println(" Found "+f.getFileName());
2022-05-02 17:44:55 +00:00
try {
2022-05-02 18:16:03 +00:00
System.out.println(" Preparing "+f.getFileName());
2022-05-02 17:44:55 +00:00
List<String> content = Files.readAllLines(f);
2022-05-03 14:47:49 +00:00
if (isHTMLFile(f)) {
2022-05-02 21:03:16 -05:00
content.addAll(0,Files.readAllLines(ops.get("%DEFAULT")));
content.addAll(Files.readAllLines(ops.get("%FOOTER")));
}
2022-05-02 18:16:03 +00:00
System.out.println(" Parsing "+f.getFileName());
for (int i=0;i<content.size();i++) {
String s = content.get(i);
2022-05-03 14:48:37 +00:00
if (s.length()>0&&isHTMLFile(f)) {
2022-05-03 14:47:49 +00:00
//Check for markdown pieces.
if (s.charAt(0)=='-') {
//Start of a title piece.
s=s.replace("-",map.get("$TITLE_CONTENT_START"));
2022-05-03 18:07:44 +00:00
s=s+map.get("$TITLE_CONTENT_END").replace("%ID%","id=\"content_"+i+"\"");
2022-05-04 13:44:01 +00:00
s=s+"<div class=\"arrow\" onClick=\"expand("+i+")\">&#x2908;</div>";
2022-05-03 18:07:44 +00:00
//Use ⤈ if there's more text to be shown than can fit.
2022-05-03 14:47:49 +00:00
} else
if (s.contains("===")) {
2022-05-03 15:34:33 +00:00
s=map.get("$CONTENT_END")+map.get("$DATE_CONTENT_START")+s.replace("===","")+map.get("$CONTENT_END")+map.get("$CONTENT_END");
2022-05-03 14:47:49 +00:00
}
}
2022-05-02 18:16:03 +00:00
for (String key : map.keySet()) {
s=s.replaceAll(Pattern.quote(key),map.get(key));
}
content.set(i,s);
}
2022-05-02 17:44:55 +00:00
Path newf = Paths.get(OUTDIR,f.getFileName().toString());
2022-05-02 18:16:03 +00:00
System.out.println(" Writing to "+newf);
2022-05-02 17:44:55 +00:00
2022-05-02 18:16:03 +00:00
Files.write(newf, content, Charset.defaultCharset(),StandardOpenOption.CREATE,StandardOpenOption.TRUNCATE_EXISTING,StandardOpenOption.WRITE);
2022-05-02 17:44:55 +00:00
2022-05-02 18:16:03 +00:00
System.out.println(" "+newf.getFileName() + " conversion complete!");
2022-05-02 17:44:55 +00:00
} catch (IOException e) {
e.printStackTrace();
}
2022-05-02 15:45:53 +00:00
}
2022-05-02 18:16:03 +00:00
System.out.println("Site has been built into the "+OUTDIR+" directory.");
2022-05-02 21:03:16 -05:00
ExportCodeFile();
2022-05-02 19:07:04 +00:00
System.out.println("\nStarting web server...");
new sigServer();
2022-05-02 15:45:53 +00:00
}
2022-05-03 14:47:49 +00:00
private static boolean isHTMLFile(Path f) {
return f.getFileName().toString().contains(".html");
}
2022-05-02 21:03:16 -05:00
private static void ExportCodeFile() {
try {
Path file = Paths.get("sigServer.java");
List<String> data = Files.readAllLines(file);
int i=0;
while (!data.get(i++).contains("sigServer()")&&i<data.size());
if (i<data.size()) {
Files.write(Paths.get(OUTDIR,"codeBackground"),data.subList(i, Math.min(i+40,data.size())),Charset.defaultCharset(),StandardOpenOption.CREATE,StandardOpenOption.TRUNCATE_EXISTING,StandardOpenOption.WRITE);
}
} catch (IOException e) {
e.printStackTrace();
}
}
2022-05-02 15:45:53 +00:00
private static Set<Path> GetFilesInDir(String directory) {
Path dir = Paths.get(directory);
try {
return Files.list(dir).collect(Collectors.toSet());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}