feat: complete :)

This commit is contained in:
Anish Lakhwara 2024-01-17 21:07:41 -08:00
parent cd5d7afc96
commit 457cfa9763
3 changed files with 47 additions and 31 deletions

View file

@ -42,7 +42,8 @@
{:name "Paragraph"
:regex ".-\n"}])
(fn block-lexer [contents grammar]
(fn block-lexer [contents grammar ?debug]
(var d (or ?debug false))
(var current 1)
(var blocks [])
(var current-block-type nil)
@ -53,31 +54,36 @@
(let [start (string.find contents "\n" current)
current-str (string.sub contents current start)]
(each [_ g (ipairs grammar) &until matched]
(if d
(do
(print (.. "Current: " current))
(print (.. "Start: " start))
(print (.. "Current-str: " current-str))
(print (.. "checking regex: " (if (not capture-mode)
(. g :regex)
(tostring (. g :mode-regex)))))
(tostring (. g :mode-regex)))))))
(let [m (if (not capture-mode)
(string.match current-str (. g :regex)) ; We're not in previous capture mode
(if (= (. g :name) current-block-type) ; And the regex we're checking is the one we want
(string.match current-str (. g :mode-regex)) ; Use the mode specific regex
nil))] ; Or don't check
(print (.. "M: " (tostring m)))
(if d
(print (.. "M: " (tostring m))))
(if (not (= nil m)) ; This is a match or move on
(do
(set matched true)
(if
capture-mode ; We matched and it's capture-mode, so we end capture mode
(do
(print "Capture-mode completed")
(set current-block (.. current-block current-str "\n"))
(table.insert blocks {:type current-block-type
:block current-block})
(print (.. "Inserted into table: " current-block))
(set current-block "")
(print (.. "New current-block: " current-block))
(if d
(do
(print "Capture-mode completed")
(print (.. "Inserted into table: " current-block))
(print (.. "New current-block: " current-block))))
(set capture-mode nil)
(set current-block-type nil))
(or ; We append to the current block
@ -88,29 +94,35 @@
(set current-block-type (. g :name))
(set capture-mode (. g :capture-mode))
(set current-block (.. current-block current-str "\n"))
(print (.. "Set current-block: " current-block)))
(if d
(print (.. "Set current-block: " current-block))))
(do ; Else we start a new block, and insert our accumulation
(table.insert blocks {:type current-block-type
:block current-block})
(print (.. "Inserted into table: " current-block))
(set current-block current-str)
(print (.. "New current-block: " current-block))
(set capture-mode (. g :capture-mode))
(print (.. "New Capture-mode: " (tostring (. g :capture-mode))))
(if d
(do
(print (.. "Inserted into table: " current-block))
(print (.. "New current-block: " current-block))
(print (.. "New Capture-mode: " (tostring (. g :capture-mode))))))
(set current-block-type (. g :name))))
(set current (+ 1 start)))
(if (and ; We didn't match, BUT
capture-mode ; Or we're in capture mode
(= (. g :name) current-block-type)) ; And are testing the right regex (should be implicitly true if we reached here)
(do
(print (.. "Didnt match capture-mode regex, appending anyway"))
(set current-block (.. current-block current-str "\n"))
(print (.. "New current-block: " current-block))
(set matched true) ; We don't need to check the others
(set current (+ 1 start)))))))))
(set current (+ 1 start))
(if d
(do
(print (.. "Didnt match capture-mode regex, appending anyway"))
(print (.. "New current-block: " current-block)))))))))))
(if current-block-type
(do
(print "End of file reached, unloading current")
(if d
(print "End of file reached, unloading current"))
(table.insert blocks {:type current-block-type
:block current-block})))
blocks)

View file

@ -5,17 +5,21 @@
(local inline (require :inline))
(local render (require :render))
(local test-input "= Big Test File =\n%title Some Title\n:note:\n*This* is a paragraph.\n- This\n- List\n- Should\n- Be One\n-----\n= A new Section Title =\n![Has an Image](file:///link-to-image.png)\n> With some quoted matieral\n>Across multiple lines\n > and different indents\n\n{{{python \n def file(arg, other):\n rturn rg + other\n}}}\n\nMore Paragraphs\n-----\n\nThis has a footnote [#]_ \n\n:: \n\n- This is the first note\n-This is the second\n::\n")
(fn write-file [text]
(_G.pp text)
(let [f (assert (io.open "./test.html" :w))]
(f:write text)
(f:close)))
(let [inline-blocks (-> test-input
(blocks.lexer blocks.grammar)
(inline.inline-lexer inline.inline-grammar))
(fn render-to-html [input]
(let [b-blocks (-> input
(blocks.lexer blocks.grammar))
inline-blocks (inline.inline-lexer b-blocks inline.inline-grammar)
(html metadata) (render.render inline-blocks render.render-grammar)]
(_G.pp html)
(write-file html))
; (_G.pp b-blocks)
; (_G.pp inline-blocks)
; (_G.pp metadata)
(values html metadata)))
{: write-file
: render-to-html}

View file

@ -4,7 +4,7 @@
(var footnote-counter 1)
(fn render-contents [contents]
(var rendered "<p>\n")
(var rendered "<p>")
(each [_ line (ipairs contents)]
(_G.pp line)
(match (. line :type)
@ -15,9 +15,9 @@
"Monospace" (set rendered (.. rendered "<code>" line.text "</code>\n"))
"Link" (set rendered (.. rendered "<a href=\"" line.destination "\" class=\"" line.class "\">" line.title "</a>\n"))
"Footnote-Link" (do
(set rendered (.. rendered "<sup>\n<a href=fn:" (tostring footnote-counter) " id=fnref:" (tostring footnote-counter) ">note " (tostring footnote-counter) "</a>\n</sup>\n"))
(set rendered (.. rendered "<sup>\n<a href=fn:" (tostring footnote-counter) " id=fnref:" (tostring footnote-counter) ">[" (tostring footnote-counter) "]</a>\n</sup>\n"))
(set footnote-counter (+ footnote-counter 1)))))
(.. rendered "</p>\n"))
(.. rendered "</p>"))
(local render-grammar
{:Header (fn render-header [{: contents : size}]
@ -86,8 +86,8 @@
(each [_ element (ipairs elements)]
(let [contents-render (render-contents element.contents)]
(set rendered (.. rendered "<li id=fn:" count ">\n"))
(set rendered (.. rendered "<a href=\"#fnref:" count "\">Back</a>\n"))
(set rendered (.. rendered contents-render "</li>\n"))
(set rendered (.. rendered "<a href=\"#fnref:" count "\">Back</a>\n"))
(set count (+ count 1))))
(.. rendered "</ol>\n"))})