all repos — hastyscribe @ 8f18d2a6aaf338da237fa1f3e1db57cfe1a70d01

A professional markdown compiler.

Don't split whole document into lines just to check first 3 lines
kaminski kaminski@naew.nato.int
Sun, 04 Apr 2021 22:57:11 +0200
commit

8f18d2a6aaf338da237fa1f3e1db57cfe1a70d01

parent

1e0eceae8930bc6daca948c19c847a1d25ff692d

1 files changed, 19 insertions(+), 13 deletions(-)

jump to
M src/hastyscribepkg/markdown.nimsrc/hastyscribepkg/markdown.nim

@@ -112,7 +112,9 @@ MKD_EMBED* = MKD_NOLINKS or MKD_NOIMAGE or MKD_TAGTEXT

## High Level API -import strutils +import + strutils, + pegs const DefaultFlags = MKD_TOC or MKD_1_COMPAT or MKD_EXTRA_FOOTNOTE or MKD_DLEXTRA or MKD_FENCEDCODE or MKD_GITHUBTAGS or MKD_HTML5ANCHOR or MKD_LATEX

@@ -145,19 +147,23 @@ if (f == 0):

flags = DefaultFlags else: flags = uint32(f) - # Check if metadata is present - var lns = s.splitLines + # Check if Pandoc style metadata is present var valid_metadata = false - var offset = 0 - if lns[0].startsWith('%') and lns[1].startsWith('%') and lns[2].startsWith('%'): - valid_metadata = true - else: - valid_metadata = false - if lns[0].startsWith('%'): - offset = 2 - if lns[1].startsWith('%'): - offset = 3 - var str = cstring(lns[offset..lns.len-1].join("\n")) + var contents = s + let peg_pandoc = peg""" + definition <- ^{line} {line}? {line}? + line <- '\%' @ \n + """ + var matches: array[0..2, string] + let (s, e) = contents.findBounds(peg_pandoc, matches) + # the pattern must start at the beginning of the file + if s == 0: + if matches[0] != "" and matches[1] != "" and matches[2] != "": + valid_metadata = true + else: + # incomplete metadata, remove the whole pandoc section to not confuse discount + contents = contents[e-1 .. ^1] + var str = cstring(contents) var mmiot = mkd_string(str, cint(str.len-1), flags) if valid_metadata: data.title = $mkd_doc_title(mmiot)