all repos — hastyscribe @ a8279a1b045f727e27f9339804e7979330dd079d

A professional markdown compiler.

Now embedding local images inside output file.
h3rald h3rald@h3rald.com
Sun, 06 Oct 2013 16:40:33 +0200
commit

a8279a1b045f727e27f9339804e7979330dd079d

parent

693a70628087fda774bd3dfd3b3b53c5fa4e3fa9

3 files changed, 26 insertions(+), 4 deletions(-)

jump to
M hastyscribe.nimhastyscribe.nim

@@ -1,4 +1,4 @@

-import os, parseopt, strutils, times, markdown +import os, parseopt, strutils, times, base64, markdown # Source const src_css = "assets/hastyscribe.css".slurp

@@ -26,9 +26,31 @@ let output_file = inputsplit.dir/inputsplit.name & ".htm"

let source = input_file.readFile +proc encode_image(file, format): string = + let contents = file.readFile + let enc_contents = contents.encode(contents.len*3) + return "data:image/$format;base64,$enc_contents" % ["format", format, "enc_contents", enc_contents] + +# URL callback +proc callback(url: cstring, size: cint, p: pointer): cstring = + let str_url = $url + var target = str_url[0..size-1] + let file = inputsplit.dir/target + if file.existsFile: + let filesplit = target.splitFile + case filesplit.ext + of ".png": + target = encode_image(file, "png") + of ".jpg": + target = encode_image(file, "jpeg") + of ".gif": + target = encode_image(file, "gif") + else: nil + return target + # Document Variables var metadata = TMDMetaData(title:"", author:"", date:"") -let body = source.md(MKD_DOTOC or MKD_EXTRA_FOOTNOTE, metadata) +let body = source.md(MKD_DOTOC or MKD_EXTRA_FOOTNOTE, metadata, callback) # TODO handle invalid date errors
M markdown.nimmarkdown.nim

@@ -141,13 +141,14 @@ result = cstringArrayToSeq(res)[0]

mkd_cleanup(mmiot) return -proc md*(s: string, f = 0, data: var TMDMetadata): string = +proc md*(s: string, f = 0, data: var TMDMetadata, callback: mkd_callback_t): string = var flags = uint32(f) var str = cstring(s) var mmiot = mkd_string(str, cint(str.len-1), flags) data.title = $mkd_doc_title(mmiot) data.author = $mkd_doc_author(mmiot) data.date = $mkd_doc_date(mmiot) + mkd_e_url(mmiot, callback) discard mkd_compile(mmiot, flags) if (int(flags) and MKD_DOTOC) == MKD_DOTOC: var toc = allocCStringArray([""])

@@ -158,4 +159,3 @@ discard mkd_document(mmiot, res)

result = cstringArrayToSeq(res)[0] mkd_cleanup(mmiot) return -