|
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; |
7 | 6 |
|
8 | 7 | public class Main { |
9 | 8 | 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 |
15 | 10 | String mdPath = args.length > 0 ? args[0] : "README.md"; |
16 | 11 | 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; |
19 | 14 |
|
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 |
23 | 17 |
|
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 | + } |
25 | 24 |
|
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); |
30 | 27 |
|
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; |
37 | 30 |
|
| 31 | + // 3) Wrap HTML in full page (TODO) |
38 | 32 | String pageHtml = buildPage(htmlBody, useCdnCss, highlight); |
39 | 33 |
|
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); */ |
44 | 36 |
|
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!"); |
47 | 39 | } |
48 | 40 |
|
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 | + } |
57 | 52 |
|
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 |
64 | 53 | static String buildPage(String bodyHtml, boolean useCdnCss, boolean highlight) { |
65 | 54 | String cssLink = useCdnCss |
66 | 55 | ? "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 |
69 | 59 | return null; |
70 | 60 | } |
71 | 61 |
|
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 | + } |
77 | 65 | } |
0 commit comments