From 93526dc0ed6e3437e03abc5dd8b435fca9f4923e Mon Sep 17 00:00:00 2001 From: ashurrafiev Date: Wed, 10 Apr 2019 15:56:12 +0100 Subject: [PATCH] Implemented #2 --- mdtool/readme.md | 27 +- .../xrbpowered/jparsedown/mdtool/MDTool.java | 251 +++++++++++++----- mdtool/templates/md_ghlike.css | 6 +- .../templates/preview/cheatsheet_ghlike.html | 6 +- readme.md | 9 +- 5 files changed, 221 insertions(+), 78 deletions(-) diff --git a/mdtool/readme.md b/mdtool/readme.md index c429f8d..4520609 100644 --- a/mdtool/readme.md +++ b/mdtool/readme.md @@ -1,23 +1,34 @@ -# MDTool +# MD Tool -**MDTool** is a JParsedown-based command line tool for converting Markdown files into HTML pages. +**MD** tool is a JParsedown-based command line tool for converting Markdown files into HTML pages. ## Download -**JAR file:** [mdtool.jar](https://github.com/ashurrafiev/JParsedown/releases/download/1.0.1/mdtool.jar) (53.8KB) +**JAR file:** [md.jar](https://github.com/ashurrafiev/JParsedown/releases/download/1.0.2/md.jar) (55.5KB) + +**Templates and styles** [templates.zip](https://github.com/ashurrafiev/JParsedown/releases/download/1.0.2/templates.zip) (19.9KB) + ## Usage ``` -java -jar mdtool.jar inputfile [-o outputfile] [options] +java -jar md.jar sourcefile [-o outputfile] [options] +``` + +Recursive mode: + +``` +java -jar md.jar -r sourcedir [-o outputdir] [options] ``` | option | description | | :--- | :--- | -| **-o** filename | Optional: output file name. By default, the output file name is derived from input file name by replacing the extension with `html`. | -| **-t** filename | Optional: HTML template file name. By default, the output will contain only the body of HTML. | -| **-s** filename | Optional: CSS stylesheet file name. By default, no stylesheet is linked or embedded. | +| **-o** path | Optional: output path or filename. If **-o** is directory or not specified, the output file name is derived from source file name by replacing the extension with `html` or template's extension. | +| **-t** filename | Optional: HTML template file name. If not specified, the output will contain only the body of HTML. | +| **-s** filename | Optional: CSS stylesheet file name. If not specified, no stylesheet is linked or embedded. | | **-e** | Optional: embed stylesheet within a `"; + styleTime = cssFile.lastModified(); + } + } + else + style = ""; } - - JParsedown parsedown = new JParsedown(); - String body = parsedown.text(source); + vars.put("style", style); - String output = body; + template = null; + templateTime = -1L; + templateExt = ".html"; if(templatePath!=null) { - HashMap vars = new HashMap<>(); - - vars.put("body", body); - vars.put("title", parsedown.title); - - String style = null; - if(stylePath!=null) { - if(embedStyle) { - String css = loadFile(stylePath); - if(css==null) - System.err.println("Cannot load stylesheet."); - else - style = ""; - } - else - style = ""; - } - vars.put("style", style); - - String template = loadFile(templatePath); + File templateFile = new File(templatePath); + template = loadFile(templateFile); if(template==null) { - System.err.println("Cannot load template file."); + System.err.println("*Error* Cannot load template file."); System.exit(1); } - - output = processTemplate(template, vars); + else { + templateTime = templateFile.lastModified(); + } } - - if(!saveFile(outputPath, output)) { - System.err.println("Cannot save output file."); + + errors = false; + if(recursiveMode) { + File srcDir = new File(sourcePath); + if(!srcDir.isDirectory()) { + System.err.println("*Error* Source path is not a directory"); + System.exit(1); + } + File outDir = new File(sourcePath); + if(outputPath!=null) { + outDir = new File(outputPath); + if(!outDir.isDirectory()) { + System.err.println("*Error* Output path is not a directory"); + System.exit(1); + } + } + scanFolder(srcDir, outDir, checkTime); + } + else { + if(outputPath!=null) { + File outDir = new File(outputPath); + if(!outDir.isDirectory()) + processFile(new File(sourcePath), outDir, checkTime); + else + processFile(new File(sourcePath), outDir, checkTime, false); + } + else + processFile(new File(sourcePath), null, checkTime, false); + } + + if(errors) { + System.out.println("Done with errors"); System.exit(1); } - - System.out.println("Done"); + else + System.out.println("Done"); } } diff --git a/mdtool/templates/md_ghlike.css b/mdtool/templates/md_ghlike.css index 3c86d0b..dc3f302 100644 --- a/mdtool/templates/md_ghlike.css +++ b/mdtool/templates/md_ghlike.css @@ -26,11 +26,11 @@ div.page { padding: 48px; box-sizing: border-box; } -@media (min-width:900px) { +@media (min-width:1000px) { div.body { - width: 880px; + width: 980px; left: 50%; - margin-left: -440px; + margin-left: -490px; } div.page { border: 1px solid #d0d5da; diff --git a/mdtool/templates/preview/cheatsheet_ghlike.html b/mdtool/templates/preview/cheatsheet_ghlike.html index e94471b..d710961 100644 --- a/mdtool/templates/preview/cheatsheet_ghlike.html +++ b/mdtool/templates/preview/cheatsheet_ghlike.html @@ -32,11 +32,11 @@ padding: 48px; box-sizing: border-box; } -@media (min-width:900px) { +@media (min-width:1000px) { div.body { - width: 880px; + width: 980px; left: 50%; - margin-left: -440px; + margin-left: -490px; } div.page { border: 1px solid #d0d5da; diff --git a/readme.md b/readme.md index 505a7e6..ab88454 100644 --- a/readme.md +++ b/readme.md @@ -21,11 +21,12 @@ Additinoal features of JParsedown that are not (yet) available in the original P * Github-compatible [Header IDs](#header-ids) * [Page title detection](#page-title-detection) + ### Download **Source file:** [JParsedown.java](src/com/xrbpowered/jparsedown/JParsedown.java) -**JAR file:** [jparsedown-1.0.1.jar](https://github.com/ashurrafiev/JParsedown/releases/download/1.0.1/jparsedown-1.0.1.jar) (50.4 KB) +**JAR file:** [jparsedown-1.0.2.jar](https://github.com/ashurrafiev/JParsedown/releases/download/1.0.2/jparsedown-1.0.2.jar) (50.4 KB) ### Usage @@ -107,8 +108,8 @@ The benchmarking does not consider saving and loading times. Only `text()` funct > Speedup against the origial Parsedown is due to Java vs PHP performance difference. > Also note how JIT really helps Java with large batches of work. -## MDTool +## MD Tool -**MDTool** is a JParsedown-based command line tool for converting Markdown files into HTML pages. +**MD** tool is a JParsedown-based command line tool for converting Markdown files into HTML pages. -See [MDTool Readme](mdtool/readme.md) +See [MD Tool Readme](mdtool/readme.md)