Skip to content

Commit 1772c5e

Browse files
committed
cleaned up readfile functionality and made pseudocode
1 parent 3caa3e6 commit 1772c5e

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

lesson_02/quiz_java/src/Main.java

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,65 @@
1-
// Main.java (pseudocode)
2-
3-
/*
4-
Goal: Read a Markdown (.md) file, convert it to HTML, wrap it with GitHub-style
5-
CSS, and write out output.html — using the codedifferently-instructional library.
6-
*/
1+
// Main.java
2+
import java.nio.file.Files;
3+
import java.nio.file.Paths;
4+
import java.nio.charset.StandardCharsets;
5+
import java.io.IOException;
76

87
public class Main {
98
public static void main(String[] args) {
10-
// ── 0) Gather Inputs ───────────────────────────────────────────────────────
11-
// Input A: path to markdown file (default: "README.md")
12-
// Input B: output file path (default: "output.html")
13-
// Input C (optional): use CDN CSS or local CSS; include code highlighting?
14-
9+
// 0) Inputs
1510
String mdPath = args.length > 0 ? args[0] : "README.md";
1611
String outputPath = args.length > 1 ? args[1] : "output.html";
17-
boolean useCdnCss = true; // tweak as needed
18-
boolean highlight = false; // tweak as needed
12+
boolean useCdnCss = true;
13+
boolean highlight = false;
1914

20-
// ── 1) Read .md file ───────────────────────────────────────────────────────
21-
// read all text from mdPath into a String: markdownText
22-
// if file not found, print friendly message and exit.
15+
// 1) Read .md file
16+
String markdownText = readFile(mdPath); // real call
2317

24-
String markdownText = /* readFile(mdPath) */ null; // TODO: implement
18+
// My empty-file check should go here, inside of main, after the readFile functionality
19+
if (markdownText.isEmpty()) {
20+
System.out.println("Warning: The file appears to be empty.");
21+
// decide: continue or bail
22+
// System.exit(1);
23+
}
2524

26-
// ── 2) Convert Markdown → raw HTML ─────────────────────────────────────────
27-
// Use the codedifferently-instructional library here.
28-
// ex: MarkdownConverter.convert(markdownText) (adjust to real class/method)
29-
String htmlBody = /* CodedDifferentlyMarkdown.convert(markdownText) */ null; // TODO
25+
// TEMP: prove it works
26+
System.out.println(markdownText);
3027

31-
// ── 3) Wrap HTML text inside a full HTML document ─────────────────────────
32-
// Build a template string:
33-
// - <!doctype html>, <html>, <head>, <meta charset>, <meta viewport>
34-
// - <link> to GitHub Markdown CSS (CDN or local)
35-
// - <article class="markdown-body"> {htmlBody} </article>
36-
// - (optional) include highlight.js CSS/JS if highlight == true
28+
// 2) Convert Markdown → HTML (TODO: replace with real converter call)
29+
String htmlBody = /* CodedDifferentlyMarkdown.convert(markdownText) */ null;
3730

31+
// 3) Wrap HTML in full page (TODO)
3832
String pageHtml = buildPage(htmlBody, useCdnCss, highlight);
3933

40-
// ── 4) Write page → output file ───────────────────────────────────────────
41-
// write pageHtml to outputPath (UTF-8). On success, print where it is.
42-
43-
/* writeFile(outputPath, pageHtml) */ // TODO
34+
// 4) Write output (TODO)
35+
/* writeFile(outputPath, pageHtml); */
4436

45-
// ── 5) Friendly done message ──────────────────────────────────────────────
46-
// "Wrote output.html — open it in your browser!"
37+
// 5) Done message (after you implement writeFile)
38+
// System.out.println("Wrote " + outputPath + " — open it in your browser!");
4739
}
4840

49-
// ───────────────────── Helper methods (pseudocode) ──────────────────────────
50-
51-
// readFile(path): String
52-
// - open file at 'path'
53-
// - read all bytes as UTF-8
54-
// - return text
55-
// - throw/handle exceptions with a clear message
56-
static String readFile(String path) { /* ... */ return null; }
41+
// ---------- Helper Functions for Converter ----------
42+
static String readFile(String path) {
43+
try {
44+
return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
45+
} catch (IOException e) {
46+
System.err.println("Sorry, the file couldn't be read: " + path);
47+
System.err.println("Reason: " + e.getMessage());
48+
System.exit(1);
49+
return null; // required by compiler
50+
}
51+
}
5752

58-
// buildPage(bodyHtml, useCdnCss, highlight): String
59-
// - choose cssLink = CDN or local path based on useCdnCss
60-
// - start template with <!doctype html>, <head> (meta + link rel="stylesheet" href=cssLink)
61-
// - if highlight == true, add highlight.js CSS/JS tags
62-
// - wrap bodyHtml with <article class="markdown-body">...</article>
63-
// - return the full HTML page as a String
6453
static String buildPage(String bodyHtml, boolean useCdnCss, boolean highlight) {
6554
String cssLink = useCdnCss
6655
? "https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css"
67-
: "file:./github-markdown.min.css"; // adjust if you keep a local copy
68-
/* build and return the page string */
56+
: "file:./github-markdown.min.css";
57+
58+
// TODO: build and return the page string
6959
return null;
7060
}
7161

72-
// writeFile(path, contents): void
73-
// - create parent folders if needed
74-
// - write UTF-8 text to 'path'
75-
// - handle errors cleanly
76-
static void writeFile(String path, String contents) { /* ... */ }
62+
static void writeFile(String path, String contents) {
63+
// TODO: write UTF-8 text to path
64+
}
7765
}

0 commit comments

Comments
 (0)