all repos — hastyscribe @ 07bae0dedb41edce15b48083f8e4a9eb80d5d913

A professional markdown compiler.

Added validation for metadata and date.
h3rald h3rald@h3rald.com
Sun, 06 Oct 2013 19:13:58 +0200
commit

07bae0dedb41edce15b48083f8e4a9eb80d5d913

parent

00e8f655afbee3ba5824e60c35447f5727087e22

3 files changed, 57 insertions(+), 17 deletions(-)

jump to
M hastyscribe.nimhastyscribe.nim

@@ -52,28 +52,56 @@ # Document Variables

var metadata = TMDMetaData(title:"", author:"", date:"") let body = source.md(MKD_DOTOC or MKD_EXTRA_FOOTNOTE, metadata, callback) -# TODO handle invalid date errors + +# Manage metadata + +if metadata.author != "": + metadata.author = "by <em>" & metadata.author & "</em> &ndash;" + + +var title_tag, header_tag, toc: string -if metadata.date == "": - metadata.date = getDateStr() +if metadata.title != "": + title_tag = "<title>" & metadata.title & "</title>" + header_tag = "<div id=\"header\"><h1>" & metadata.title & "</h1></div>" +else: + title_tag = "" + header_tag = "" -var date = metadata.date.split('-') +if metadata.toc != "": + toc = "<div id=\"toc\">" & metadata.toc & "</div>" +else: + toc = "" -var timeinfo = TTimeInfo(year: date[0].parseInt, month: TMonth(date[1].parseInt-1), monthday: date[2].parseInt) +var timeinfo: TTimeInfo + +proc parse_date(date: string, timeinfo: var TTimeInfo): bool = + var parts = metadata.date.split('-').map(proc(i:string): int = i.parseInt) + try: + timeinfo = TTimeInfo(year: parts[0], month: TMonth(parts[1]-1), monthday: parts[2]) + # Fix invalid dates (e.g. Feb 31st -> Mar 3rd) + timeinfo = getLocalTime(timeinfo.TimeInfoToTime); + return true + except: + return false + +if metadata.date == "": + discard parse_date(getDateStr(), timeinfo) +else: + if parse_date(metadata.date, timeinfo) == false: + discard parse_date(getDateStr(), timeinfo) let document = """<!doctype html> <html lang="en"> <head> - <title>$title</title> + $title_tag <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="author" content="$author"> $css </head> <body> - <div id="header"> - <h1>$title</h1> - </div> + $header_tag <div id="toc"> $toc </div>

@@ -81,8 +109,8 @@ <div id="main">

$body </div> <div id="footer"> - <p>by <em>$author</em> &ndash; Generated with <a href="https://github.com/h3rald/hastyscribe/">HastyScribe</a> on <em>$date</em></p> + <p>$author Generated with <a href="https://github.com/h3rald/hastyscribe/">HastyScribe</a> on <em>$date</em></p> </div> -</body>""" % ["title", metadata.title, "author", metadata.author, "date", timeinfo.format("MMMM d, yyyy"), "toc", metadata.toc, "css", css, "body", body] +</body>""" % ["title_tag", title_tag, "header_tag", header_tag, "author", metadata.author, "date", timeinfo.format("MMMM d, yyyy"), "toc", toc, "css", css, "body", body] output_file.writeFile(document)
M markdown.nimmarkdown.nim

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

-##ifndef _MKDIO_D -##define _MKDIO_D +import strutils type MMIOT* = int

@@ -143,11 +142,24 @@ return

proc md*(s: string, f = 0, data: var TMDMetadata, callback: mkd_callback_t): string = var flags = uint32(f) - var str = cstring(s) + # 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 = 1 + if lns[1][0] == '%': + offset = 2 + var str = cstring(lns[offset..lns.len-1].join("\n")) 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) + if valid_metadata: + 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: