all repos — hastyscribe @ 417338940a8f3dd985c0fa07052e2f0009a0977e

A professional markdown compiler.

Merge branch 'output-file' of https://github.com/philip-wernersbach/hastyscribe
h3rald h3rald@h3rald.com
Fri, 01 May 2015 18:09:42 +0200
commit

417338940a8f3dd985c0fa07052e2f0009a0977e

parent

828a5a2d623077bbf623073943230a2c9274a8ac

1 files changed, 18 insertions(+), 6 deletions(-)

jump to
M hastyscribe.nimhastyscribe.nim

@@ -13,10 +13,13 @@ Arguments:

markdown_file_or_glob The markdown (or glob expression) file to compile into HTML. Options: --notoc Do not generate a Table of Contents. - --user-css=<file> Insert contents of <file> as a CSS stylesheet.""" + --user-css=<file> Insert contents of <file> as a CSS stylesheet. + --output-file=<file> Write output to <file>. + (Use "--output-file=-" to output to stdout.)""" var generate_toc* = true +var output_file*: string = nil var user_css*: string = nil const stylesheet* = "assets/styles/hastyscribe.css".slurp const hastyscribe_font* = "assets/fonts/hastyscribe.woff".slurp

@@ -59,7 +62,7 @@ if (file.existsFile):

let contents = file.readFile return encode_image(contents, format) else: - echo("Warning: image '"& file &"' not found.") + stderr.writeln("Warning: image '"& file &"' not found.") return file proc encode_font*(font, format): string =

@@ -153,7 +156,7 @@ var matches:TSnippet

discard snippet.match(peg_snippet, matches) var id = matches[0].strip if snippets[id] == nil: - echo "Warning: Snippet '" & id & "' not defined." + stderr.writeln "Warning: Snippet '" & id & "' not defined." doc = doc.replace(snippet, "") else: doc = doc.replace(snippet, snippets[id])

@@ -163,7 +166,9 @@ proc compile*(input_file: string) =

let inputsplit = input_file.splitFile # Output file name - let output_file = inputsplit.dir/inputsplit.name & ".htm" + if output_file == nil: + output_file = inputsplit.dir/inputsplit.name & ".htm" + var source = input_file.readFile # Parse snippets

@@ -232,8 +237,11 @@ </body>""" % ["title_tag", title_tag, "header_tag", header_tag, "author", metadata.author, "author_footer", author_footer, "date", timeinfo.format("MMMM d, yyyy"), "toc", toc, "main_css_tag", main_css_tag, "user_css_tag", user_css_tag, "headings", headings, "body", body,

"fonts_css_tag", embed_fonts()] document = embed_images(document, inputsplit.dir) document = add_jump_to_top_links(document) - output_file.writeFile(document) + if output_file != "-": + output_file.writeFile(document) + else: + stdout.write(document) ### MAIN

@@ -254,11 +262,15 @@ if key == "notoc":

generate_toc = false elif key == "user-css": user_css = val + elif key == "output-file": + output_file = val else: discard if input == "": quit(usage, 1) elif user_css == "": + quit(usage, 4) + elif output_file == "": quit(usage, 4) for file in walkFiles(input):

@@ -269,7 +281,7 @@ quit("Error: \"$1\" does not match any file" % [input], 2)

else: try: for file in files: - compile(file) + compile(file) except IOError: let msg = getCurrentExceptionMsg() quit("Error: $1" % [msg], 3)