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

View file

@ -5,17 +5,21 @@
(local inline (require :inline)) (local inline (require :inline))
(local render (require :render)) (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] (fn write-file [text]
(_G.pp text) (_G.pp text)
(let [f (assert (io.open "./test.html" :w))] (let [f (assert (io.open "./test.html" :w))]
(f:write text) (f:write text)
(f:close))) (f:close)))
(let [inline-blocks (-> test-input (fn render-to-html [input]
(blocks.lexer blocks.grammar) (let [b-blocks (-> input
(inline.inline-lexer inline.inline-grammar)) (blocks.lexer blocks.grammar))
inline-blocks (inline.inline-lexer b-blocks inline.inline-grammar)
(html metadata) (render.render inline-blocks render.render-grammar)] (html metadata) (render.render inline-blocks render.render-grammar)]
(_G.pp html) ; (_G.pp b-blocks)
(write-file html)) ; (_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) (var footnote-counter 1)
(fn render-contents [contents] (fn render-contents [contents]
(var rendered "<p>\n") (var rendered "<p>")
(each [_ line (ipairs contents)] (each [_ line (ipairs contents)]
(_G.pp line) (_G.pp line)
(match (. line :type) (match (. line :type)
@ -15,9 +15,9 @@
"Monospace" (set rendered (.. rendered "<code>" line.text "</code>\n")) "Monospace" (set rendered (.. rendered "<code>" line.text "</code>\n"))
"Link" (set rendered (.. rendered "<a href=\"" line.destination "\" class=\"" line.class "\">" line.title "</a>\n")) "Link" (set rendered (.. rendered "<a href=\"" line.destination "\" class=\"" line.class "\">" line.title "</a>\n"))
"Footnote-Link" (do "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))))) (set footnote-counter (+ footnote-counter 1)))))
(.. rendered "</p>\n")) (.. rendered "</p>"))
(local render-grammar (local render-grammar
{:Header (fn render-header [{: contents : size}] {:Header (fn render-header [{: contents : size}]
@ -86,8 +86,8 @@
(each [_ element (ipairs elements)] (each [_ element (ipairs elements)]
(let [contents-render (render-contents element.contents)] (let [contents-render (render-contents element.contents)]
(set rendered (.. rendered "<li id=fn:" count ">\n")) (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 contents-render "</li>\n"))
(set rendered (.. rendered "<a href=\"#fnref:" count "\">Back</a>\n"))
(set count (+ count 1)))) (set count (+ count 1))))
(.. rendered "</ol>\n"))}) (.. rendered "</ol>\n"))})