aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorj-james2022-10-17 15:53:19 +0000
committerj-james2022-10-17 15:53:19 +0000
commitec21913201b99edc09eacff300f5424e28fcc4ac (patch)
treed8cb3311a5dda60ffb50fefe6192d84e7680b681
parent9eea270aaec82bc38d297f7aa2282877d48cd3c1 (diff)
Add skeleton of console UI
-rw-r--r--src/main/ui/BrowserApp.java60
-rw-r--r--src/main/ui/Main.java2
2 files changed, 61 insertions, 1 deletions
diff --git a/src/main/ui/BrowserApp.java b/src/main/ui/BrowserApp.java
new file mode 100644
index 0000000..521a28f
--- /dev/null
+++ b/src/main/ui/BrowserApp.java
@@ -0,0 +1,60 @@
+package ui;
+
+import model.html.ElementNode;
+import model.html.HtmlParser;
+import model.html.TextNode;
+import model.util.Node;
+
+import java.nio.file.*;
+import java.util.*;
+
+/**
+ * The console interface to Apus.
+ */
+public class BrowserApp {
+ private Scanner input;
+
+ /**
+ * EFFECTS: Renders an arbitrary HTML page and arbitrary HTML input.
+ */
+ public BrowserApp() {
+ println("apus: currently a barebones html/css renderer");
+ println("please provide a path to a file (examples located in data/*):");
+
+ String pathString = input.next();
+ Path path = Path.of(pathString);
+ try {
+ String file = Files.readString(path);
+ HtmlParser parser = new HtmlParser();
+ renderHtml(parser.parseHtml(file));
+ println("Page rendered. Input raw HTML to add Nodes.");
+ parser = new HtmlParser();
+ String rawHtml = input.next();
+ renderHtml(parser.parseHtml(file + rawHtml));
+ } catch (Exception e) {
+ println("Reading from the file failed with " + e.toString());
+ }
+ }
+
+ /**
+ * EFFECTS: Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text.
+ */
+ private void renderHtml(ArrayList<Node> html) {
+ for (Node node: html) {
+ if (node instanceof TextNode) {
+ println(node.getData());
+ } else {
+ renderHtml(((ElementNode) node).getChildren());
+ }
+ }
+ }
+
+ private void print(String toPrint) {
+ System.out.print(toPrint);
+ }
+
+ private void println(String toPrint) {
+ System.out.println(toPrint);
+ }
+
+}
diff --git a/src/main/ui/Main.java b/src/main/ui/Main.java
index d80be21..841576f 100644
--- a/src/main/ui/Main.java
+++ b/src/main/ui/Main.java
@@ -2,6 +2,6 @@ package ui;
public class Main {
public static void main(String[] args) {
-
+ new BrowserApp();
}
}