Now handling TOC and CSS processing properly. * Closes #36.
h3rald h3rald@h3rald.com
Sun, 06 Sep 2015 11:56:43 +0000
2 files changed,
198 insertions(+),
198 deletions(-)
M
doc/HastyScribe_UserGuide.htm
→
doc/HastyScribe_UserGuide.htm
@@ -1202,7 +1202,7 @@
<p style="text-align:center;"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFgAAAAfCAYAAABjyArgAAAGa0lEQVRo3u1aW08bRxTmuSqIV0SlIFWtAKkVtEpfWiVI6Q+gT60qpeIhTXkrSpqoaZ1AkJMobSRkkUahEtAWCVWBYMDcfME22MZ3rw0hrzzwAxC/4FTfrGeYXXa9u8Y0yGGkEeu5nGW+OfOdy2wDETU1NDTQea19JcKf8o/lwBKthlbIH16j0EaQwrEwbSSiFEvFaCuToFQuSel8mrKFDOWULOWKObUqWcoqGcrk02wMxsZTMTY3EgszWf6wn8nGO3xri7SwOk/zy16aW3pJL32zrM4uztDMwou6qRLQR+AGIn5a3wxRNB6hWHKTgZXOpxiAhVKeijsKlV4VaXt3m3ZeqxXPpVcl1ocxWSXL5mAuZEQTESYTsvUge5fnBMgAuE5BbtCCm4gyDUxmk0xbARpAdT9w05Uvr1BjU+Oxo4A29LkfutnYwnaBbQo0Op6OM21e31zXgLy4tkDzK14VZN8RyHdcd6ijo+PYO9CGPruLOwtyBMCgBRlcAJMrZplWekY91Nraapt3MNbz1MPmgkKgzQkBckjQxZLfR4urKsjQ4j/GnlJ7R7uQ09PTQwMDA6zimbdjzOjzUdNFoe+syBEAgydBC0fg5pgm9n7VqwGvubmZ+vr6aGhoiCKRCKt4Rhv65LGYG0/GKF9SQYYmgy5UTl6jleCyoIp/pv+mpqYmdV5vL+3t7ZG+oA19GIOxRouanJo4U3IEwDBo4EvQAjRXDy7AA5AHBwdUqUxOTmqAxu4CZGwYNg7vgOELRgO0tr6qUsXqgjiC2Ci5SNZY8w4uW78grnF6OZ6REfr2629OLMdszWZyBMA4vjBK4FwcbRncrq4uwx00K9gEz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DAFnW5GqujABuvpijZ2PP7H5HYBhk6MfYkWP22yyZ5EROpaSUJcACZEW99AQnO770HOWXnlnb4BoBYbQwJxtVab7djaqBHOMO0IV8bQ/QrK7tcXPBru3xfUQh45gWrNKVTjfK7G8t5VQNMLvf+rSbvIte5sLBT1bEhycl6cOTEmuTPzyZ93np0uVL1XxqdCJqsKKEajS4BnKs/2GA9fjJYwpFQ9KnU+WKT6cKGdaHMdUAe1oafGY5+LzWsBLRO+dAnN7Xlf8BOGLNYHeHmZwAAAAASUVORK5CYII=" alt="Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License" /></p> </div> <div id="footer"> - <p><span class="copy"></span> Fabio Cevasco – August 23, 2015</p> + <p><span class="copy"></span> Fabio Cevasco – September 6, 2015</p> <p><span>Powered by</span> <a href="https://h3rald.com/hastyscribe"><span class="hastyscribe"></span></a></p> </div> </body>
M
markdown.nim
→
markdown.nim
@@ -1,197 +1,197 @@
-import strutils - -type - MMIOT* = int - mkd_flag_t* = uint32 - -# line builder for markdown() -# -{.push importc, cdecl.} -proc mkd_in*(a2: File; a3: mkd_flag_t): ptr MMIOT -# assemble input from a file - -proc mkd_string*(a2: cstring; a3: cint; a4: mkd_flag_t): ptr MMIOT -# assemble input from a buffer -# line builder for github flavoured markdown -# - -proc gfm_in*(a2: File; a3: mkd_flag_t): ptr MMIOT -# assemble input from a file - -proc gfm_string*(a2: cstring; a3: cint; a4: mkd_flag_t): ptr MMIOT -# assemble input from a buffer - -proc mkd_basename*(a2: ptr MMIOT; a3: cstring) -proc mkd_initialize*() -proc mkd_with_html5_tags*() -proc mkd_shlib_destructor*() -# compilation, debugging, cleanup -# - -proc mkd_compile*(a2: ptr MMIOT; a3: mkd_flag_t): cint -proc mkd_cleanup*(a2: ptr MMIOT) -# markup functions -# - -proc mkd_dump*(a2: ptr MMIOT; a3: File; a4: cint; a5: cstring): cint -proc markdown*(a2: ptr MMIOT; a3: File; a4: mkd_flag_t): cint -proc mkd_line*(a2: cstring; a3: cint; a4: cstringArray; a5: mkd_flag_t): cint -type - mkd_sta_function_t* = proc (a2: cint; a3: pointer): cint - -proc mkd_string_to_anchor*(a2: cstring; a3: cint; a4: mkd_sta_function_t; - a5: pointer; a6: cint) -proc mkd_xhtmlpage*(a2: ptr MMIOT; a3: cint; a4: File): cint -# header block access -# - -proc mkd_doc_title*(a2: ptr MMIOT): cstring -proc mkd_doc_author*(a2: ptr MMIOT): cstring -proc mkd_doc_date*(a2: ptr MMIOT): cstring -# compiled data access -# - -proc mkd_document*(a2: ptr MMIOT; a3: cstringArray): cint -proc mkd_toc*(a2: ptr MMIOT; a3: cstringArray): cint -proc mkd_css*(a2: ptr MMIOT; a3: cstringArray): cint -proc mkd_xml*(a2: cstring; a3: cint; a4: cstringArray): cint -# write-to-file functions -# - -proc mkd_generatehtml*(a2: ptr MMIOT; a3: File): cint -proc mkd_generatetoc*(a2: ptr MMIOT; a3: File): cint -proc mkd_generatexml*(a2: cstring; a3: cint; a4: File): cint -proc mkd_generatecss*(a2: ptr MMIOT; a3: File): cint -const - mkd_style* = mkd_generatecss - -proc mkd_generateline*(a2: cstring; a3: cint; a4: File; a5: mkd_flag_t): cint -const - mkd_text* = mkd_generateline - -# url generator callbacks -# - -type - mkd_callback_t* = proc (a2: cstring; a3: cint; a4: pointer): cstring - mkd_free_t* = proc (a2: cstring; a3: pointer) - -proc mkd_e_url*(a2: pointer; a3: mkd_callback_t) -proc mkd_e_flags*(a2: pointer; a3: mkd_callback_t) -proc mkd_e_free*(a2: pointer; a3: mkd_free_t) -proc mkd_e_data*(a2: pointer; a3: pointer) -# version#. -# - -var markdown_version*: ptr char - -proc mkd_mmiot_flags*(a2: File; a3: ptr MMIOT; a4: cint) -proc mkd_flags_are*(a2: File; a3: mkd_flag_t; a4: cint) -proc mkd_ref_prefix*(a2: ptr MMIOT; a3: cstring) -# special flags for markdown() and mkd_text() -# - -{.pop.} - -const - MKD_NOLINKS* = 0x00000001 # don't do link processing, block <a> tags - MKD_NOIMAGE* = 0x00000002 # don't do image processing, block <img> - MKD_NOPANTS* = 0x00000004 # don't run smartypants() - MKD_NOHTML* = 0x00000008 # don't allow raw html through AT ALL - MKD_STRICT* = 0x00000010 # disable SUPERSCRIPT, RELAXED_EMPHASIS - MKD_TAGTEXT* = 0x00000020 # process text inside an html tag; no - # <em>, no <bold>, no html or [] expansion - MKD_NO_EXT* = 0x00000040 # don't allow pseudo-protocols - #MKD_NOEXT* = MKD_NO_EXT # ^^^ (aliased for user convenience) - MKD_CDATA* = 0x00000080 # generate code for xml ![CDATA[...]] - MKD_NOSUPERSCRIPT* = 0x00000100 # no A^B - MKD_NORELAXED* = 0x00000200 # emphasis happens /everywhere/ - MKD_NOTABLES* = 0x00000400 # disallow tables - MKD_NOSTRIKETHROUGH* = 0x00000800 # forbid ~~strikethrough~~ - MKD_DOTOC* = 0x00001000 # do table-of-contents processing - MKD_1_COMPAT* = 0x00002000 # compatibility with MarkdownTest_1.0 - MKD_AUTOLINK* = 0x00004000 # make http://foo.com link even without <>s - MKD_SAFELINK* = 0x00008000 # paranoid check for link protocol - MKD_NOHEADER* = 0x00010000 # don't process header blocks - MKD_TABSTOP* = 0x00020000 # expand tabs to 4 spaces - MKD_NODIVQUOTE* = 0x00040000 # forbid >%class% blocks - MKD_NOALPHALIST* = 0x00080000 # forbid alphabetic lists - MKD_NODLIST* = 0x00100000 # forbid definition lists - MKD_EXTRA_FOOTNOTE* = 0x00200000 # enable markdown extra-style footnotes - MKD_NOSTYLE* = 0x00400000 # don't extract <style> blocks - MKD_EMBED* = MKD_NOLINKS or MKD_NOIMAGE or MKD_TAGTEXT - -# special flags for mkd_in() and mkd_string() - -type TMDMetaData* = object - title*: string - author*: string - date*: string - toc*: string - css*: string - -proc md*(s: string, f = 0): string = - var flags = uint32(f) - var str = cstring(s) - var mmiot = mkd_string(str, cint(str.len-1), flags) - discard mkd_compile(mmiot, flags) - var res = allocCStringArray([""]) - discard mkd_document(mmiot, res) - result = cstringArrayToSeq(res)[0] - mkd_cleanup(mmiot) - return - -proc md*(s: string, f = 0, data: var TMDMetadata): string = - var flags = uint32(f) - # Check if metadata is present - var lns = s.splitLines - var valid_metadata = false - var offset = 0 - if (lns[0][0] == '%') and (lns[1][0] == '%') and (lns[2][0] == '%'): - valid_metadata = true - else: - valid_metadata = false - if lns[0][0] == '%': - offset = 2 - if lns[1][0] == '%': - offset = 3 - var str = cstring(lns[offset..lns.len-1].join("\n")) - var mmiot = mkd_string(str, cint(str.len-1), flags) - if valid_metadata: - data.title = $mkd_doc_title(mmiot) - data.author = $mkd_doc_author(mmiot) - data.date = $mkd_doc_date(mmiot) - discard mkd_compile(mmiot, flags) - # Process TOC - if (int(flags) and MKD_DOTOC) == MKD_DOTOC: - var toc = allocCStringArray([""]) - try: - discard mkd_toc(mmiot, toc) - data.toc = cstringArrayToSeq(toc)[0] - except: - data.toc = "" - # Process CSS - try: - var css = allocCStringArray([""]) - discard mkd_css(mmiot, css) - data.css = cstringArrayToSeq(css)[0] - except: - data.css = "" - # Generate HTML - var res = allocCStringArray([""]) - discard mkd_document(mmiot, res) - result = cstringArrayToSeq(res)[0] - mkd_cleanup(mmiot) - -when defined(macosx): - {.link: "vendor/libmarkdown_macosx_x64.a".} -when defined(windows): - when defined(i386): - {.link: "vendor/libmarkdown_windows_x86.a".} - when defined(amd64): - {.link: "vendor/libmarkdown_windows_x64.a".} -when defined(linux): - when defined(arm): - {.link: "vendor/libmarkdown_linux_arm.a".} - when defined(i386) or defined(amd64): - {.link: "vendor/libmarkdown_linux_x86.a".} +import strutils + +type + MMIOT* = int + mkd_flag_t* = uint32 + +# line builder for markdown() +# +{.push importc, cdecl.} +proc mkd_in*(a2: File; a3: mkd_flag_t): ptr MMIOT +# assemble input from a file + +proc mkd_string*(a2: cstring; a3: cint; a4: mkd_flag_t): ptr MMIOT +# assemble input from a buffer +# line builder for github flavoured markdown +# + +proc gfm_in*(a2: File; a3: mkd_flag_t): ptr MMIOT +# assemble input from a file + +proc gfm_string*(a2: cstring; a3: cint; a4: mkd_flag_t): ptr MMIOT +# assemble input from a buffer + +proc mkd_basename*(a2: ptr MMIOT; a3: cstring) +proc mkd_initialize*() +proc mkd_with_html5_tags*() +proc mkd_shlib_destructor*() +# compilation, debugging, cleanup +# + +proc mkd_compile*(a2: ptr MMIOT; a3: mkd_flag_t): cint +proc mkd_cleanup*(a2: ptr MMIOT) +# markup functions +# + +proc mkd_dump*(a2: ptr MMIOT; a3: File; a4: cint; a5: cstring): cint +proc markdown*(a2: ptr MMIOT; a3: File; a4: mkd_flag_t): cint +proc mkd_line*(a2: cstring; a3: cint; a4: cstringArray; a5: mkd_flag_t): cint +type + mkd_sta_function_t* = proc (a2: cint; a3: pointer): cint + +proc mkd_string_to_anchor*(a2: cstring; a3: cint; a4: mkd_sta_function_t; + a5: pointer; a6: cint) +proc mkd_xhtmlpage*(a2: ptr MMIOT; a3: cint; a4: File): cint +# header block access +# + +proc mkd_doc_title*(a2: ptr MMIOT): cstring +proc mkd_doc_author*(a2: ptr MMIOT): cstring +proc mkd_doc_date*(a2: ptr MMIOT): cstring +# compiled data access +# + +proc mkd_document*(a2: ptr MMIOT; a3: cstringArray): cint +proc mkd_toc*(a2: ptr MMIOT; a3: cstringArray): cint +proc mkd_css*(a2: ptr MMIOT; a3: cstringArray): cint +proc mkd_xml*(a2: cstring; a3: cint; a4: cstringArray): cint +# write-to-file functions +# + +proc mkd_generatehtml*(a2: ptr MMIOT; a3: File): cint +proc mkd_generatetoc*(a2: ptr MMIOT; a3: File): cint +proc mkd_generatexml*(a2: cstring; a3: cint; a4: File): cint +proc mkd_generatecss*(a2: ptr MMIOT; a3: File): cint +const + mkd_style* = mkd_generatecss + +proc mkd_generateline*(a2: cstring; a3: cint; a4: File; a5: mkd_flag_t): cint +const + mkd_text* = mkd_generateline + +# url generator callbacks +# + +type + mkd_callback_t* = proc (a2: cstring; a3: cint; a4: pointer): cstring + mkd_free_t* = proc (a2: cstring; a3: pointer) + +proc mkd_e_url*(a2: pointer; a3: mkd_callback_t) +proc mkd_e_flags*(a2: pointer; a3: mkd_callback_t) +proc mkd_e_free*(a2: pointer; a3: mkd_free_t) +proc mkd_e_data*(a2: pointer; a3: pointer) +# version#. +# + +var markdown_version*: ptr char + +proc mkd_mmiot_flags*(a2: File; a3: ptr MMIOT; a4: cint) +proc mkd_flags_are*(a2: File; a3: mkd_flag_t; a4: cint) +proc mkd_ref_prefix*(a2: ptr MMIOT; a3: cstring) +# special flags for markdown() and mkd_text() +# + +{.pop.} + +const + MKD_NOLINKS* = 0x00000001 # don't do link processing, block <a> tags + MKD_NOIMAGE* = 0x00000002 # don't do image processing, block <img> + MKD_NOPANTS* = 0x00000004 # don't run smartypants() + MKD_NOHTML* = 0x00000008 # don't allow raw html through AT ALL + MKD_STRICT* = 0x00000010 # disable SUPERSCRIPT, RELAXED_EMPHASIS + MKD_TAGTEXT* = 0x00000020 # process text inside an html tag; no + # <em>, no <bold>, no html or [] expansion + MKD_NO_EXT* = 0x00000040 # don't allow pseudo-protocols + #MKD_NOEXT* = MKD_NO_EXT # ^^^ (aliased for user convenience) + MKD_CDATA* = 0x00000080 # generate code for xml ![CDATA[...]] + MKD_NOSUPERSCRIPT* = 0x00000100 # no A^B + MKD_NORELAXED* = 0x00000200 # emphasis happens /everywhere/ + MKD_NOTABLES* = 0x00000400 # disallow tables + MKD_NOSTRIKETHROUGH* = 0x00000800 # forbid ~~strikethrough~~ + MKD_DOTOC* = 0x00001000 # do table-of-contents processing + MKD_1_COMPAT* = 0x00002000 # compatibility with MarkdownTest_1.0 + MKD_AUTOLINK* = 0x00004000 # make http://foo.com link even without <>s + MKD_SAFELINK* = 0x00008000 # paranoid check for link protocol + MKD_NOHEADER* = 0x00010000 # don't process header blocks + MKD_TABSTOP* = 0x00020000 # expand tabs to 4 spaces + MKD_NODIVQUOTE* = 0x00040000 # forbid >%class% blocks + MKD_NOALPHALIST* = 0x00080000 # forbid alphabetic lists + MKD_NODLIST* = 0x00100000 # forbid definition lists + MKD_EXTRA_FOOTNOTE* = 0x00200000 # enable markdown extra-style footnotes + MKD_NOSTYLE* = 0x00400000 # don't extract <style> blocks + MKD_EMBED* = MKD_NOLINKS or MKD_NOIMAGE or MKD_TAGTEXT + +# special flags for mkd_in() and mkd_string() + +type TMDMetaData* = object + title*: string + author*: string + date*: string + toc*: string + css*: string + +proc md*(s: string, f = 0): string = + var flags = uint32(f) + var str = cstring(s) + var mmiot = mkd_string(str, cint(str.len-1), flags) + discard mkd_compile(mmiot, flags) + var res = allocCStringArray([""]) + discard mkd_document(mmiot, res) + result = cstringArrayToSeq(res)[0] + mkd_cleanup(mmiot) + return + +proc md*(s: string, f = 0, data: var TMDMetadata): string = + var flags = uint32(f) + # Check if metadata is present + var lns = s.splitLines + var valid_metadata = false + var offset = 0 + if (lns[0][0] == '%') and (lns[1][0] == '%') and (lns[2][0] == '%'): + valid_metadata = true + else: + valid_metadata = false + if lns[0][0] == '%': + offset = 2 + if lns[1][0] == '%': + offset = 3 + var str = cstring(lns[offset..lns.len-1].join("\n")) + var mmiot = mkd_string(str, cint(str.len-1), flags) + if valid_metadata: + data.title = $mkd_doc_title(mmiot) + data.author = $mkd_doc_author(mmiot) + data.date = $mkd_doc_date(mmiot) + discard mkd_compile(mmiot, flags) + # Process TOC + if (int(flags) and MKD_DOTOC) == MKD_DOTOC: + var toc = allocCStringArray(@[""]) + if mkd_toc(mmiot, toc) > 0: + data.toc = cstringArrayToSeq(toc)[0] + else: + data.toc = "" + # Process CSS + var css = allocCStringArray(newSeq[string](10)) + if mkd_css(mmiot, css) > 0: + data.css = cstringArrayToSeq(css)[0] + else: + data.css = "" + # Generate HTML + var res = allocCStringArray([""]) + if mkd_document(mmiot, res) > 0: + result = cstringArrayToSeq(res)[0] + else: + result = "" + mkd_cleanup(mmiot) + +when defined(macosx): + {.link: "vendor/libmarkdown_macosx_x64.a".} +when defined(windows): + when defined(i386): + {.link: "vendor/libmarkdown_windows_x86.a".} + when defined(amd64): + {.link: "vendor/libmarkdown_windows_x64.a".} +when defined(linux): + when defined(arm): + {.link: "vendor/libmarkdown_linux_arm.a".} + when defined(i386) or defined(amd64): + {.link: "vendor/libmarkdown_linux_x86.a".}