feat: html rendering (almost)

This commit is contained in:
Anish Lakhwara 2023-12-29 23:34:23 -08:00
parent 8e17a8f94c
commit cd5d7afc96
4 changed files with 91 additions and 41 deletions

View file

@ -33,7 +33,7 @@
:sp-chars ["#" "-"]
:any-space true
:continue-until-no-match true
:regex "[^%[][%-%#].-\n"}
:regex "^[%s%-%#].-\n"}
{:name "Footnote"
:sp-chars ["::\n"]
:regex "%s*::%s-\n"

View file

@ -22,18 +22,20 @@
:Footnote "%[(.-)%]%_"}
(find-char _) (string.find str "%[")
next-char (string.sub str (+ 1 find-char) (+ 1 find-char))]
(_G.pp (.. "find-char " find-char))
(_G.pp (.. "next-char " next-char))
; (_G.pp (.. "find-char " find-char))
; (_G.pp (.. "next-char " next-char))
(if (= next-char "[")
(if (not (= nil (string.match str (. patterns :Named-In-Link) current)))
(let [(start end filename title) (string.find str (. patterns :Named-In-Link) current)]
(values {:type "Link"
:title title
:class "internal"
:destination filename}
end))
(let [(start end filename) (string.find str (. patterns :In-Link) current)]
(values {:type "Link"
:title filename
:class "internal"
:destination filename}
end)))
; If it's not a [[ type link
@ -42,18 +44,20 @@
; if it's ) it's an out-link
(let [(_ close-bracket) (string.find str "%]" current)
next-close-char (string.sub str (+ 1 close-bracket) (+ 1 close-bracket))]
(_G.pp "we think it's not a in-link")
(_G.pp (.. "next-close-char " next-close-char))
; (_G.pp "we think it's not a in-link")
; (_G.pp (.. "next-close-char " next-close-char))
(match next-close-char
"(" (let [(start end title destination) (string.find str (. patterns :Out-Link) current)]
(values {:type "Link"
:class "external"
:title title
:destination destination}
end))
"_" (let [(start end footnote-number) (string.find str (. patterns :Footnote) current)]
(_G.pp "In footnote")
(_G.pp (.. "end " end))
; (_G.pp "In footnote")
; (_G.pp (.. "end " end))
(values {:type "Footnote-Link"
:class "footnote"
:number footnote-number}
end))
_ (values nil next-close-char))))))}) ; Could not parse type of link, just skip it
@ -66,14 +70,14 @@
(var lexed [])
(while (and (not (= nil current)) (> (length contents) current))
(let [found (string.find contents (. grammar :match-first) current)]
(_G.pp (.. "current " current))
(_G.pp (.. "found " found))
; (_G.pp (.. "current " current))
; (_G.pp (.. "found " (tostring found)))
(if (not (= nil found))
(let [char (string.sub contents found found)]
(_G.pp (.. "char " char))
; (_G.pp (.. "char " char))
(if (> found 1)
(table.insert lexed {:type "raw-text"
:contents (string.sub contents current (- found 1))}))
:text (string.sub contents current (- found 1))}))
(match char
; This would be better if we had a reverse lookup table
"_" (let [(block new-end) ((. grammar :Italics) contents current)]
@ -149,9 +153,9 @@
:Footnote {:rule (fn footnote-rule [str ?regex]
(let [regex (or ?regex "::%s-\n%s*(.-)\n::\n")
list (string.match str regex)]
{:type "Footnotes"
:elements (. (list-rule list) :elements)
:is-metadata true}))}
{:type "Footnote"
:elements (. (list-rule list) :elements)}))}
; :is-metadata false}))}
:Tag {:rule (fn tag-rule [str ?regex]
(let [regex (or ?regex "%:(.-):")]
(var tags [])
@ -175,7 +179,7 @@
(var lexed [])
(var section [])
(each [_ block (ipairs blocks)]
(_G.pp block)
; (_G.pp block)
(let [rule-set (. grammar (. block :type))]
(if (= "Section" (. block :type))
(do ; Each section gets it's own list

View file

@ -3,10 +3,19 @@
(local blocks (require :block))
(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")
(-> test-input
(blocks.lexer blocks.grammar)
(inline.inline-lexer inline.inline-grammar)
(_G.pp))
(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))
(html metadata) (render.render inline-blocks render.render-grammar)]
(_G.pp html)
(write-file html))

View file

@ -1,7 +1,23 @@
(local fennel (require :fennel))
(fn _G.pp [x] (print (fennel.view x)))
(fn render-contents [contents])
(var footnote-counter 1)
(fn render-contents [contents]
(var rendered "<p>\n")
(each [_ line (ipairs contents)]
(_G.pp line)
(match (. line :type)
"raw-text" (set rendered (.. rendered line.text "\n"))
"Italics" (set rendered (.. rendered "<em>" line.text "</em>\n"))
"Bold" (set rendered (.. rendered "<b>" line.text "</b>\n"))
"Strikethrough" (set rendered (.. rendered "<del>" line.text "</del>\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"))
"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 footnote-counter (+ footnote-counter 1)))))
(.. rendered "</p>\n"))
(local render-grammar
{:Header (fn render-header [{: contents : size}]
@ -11,15 +27,17 @@
(.. "<" header ">\n" contents-render "</" header ">\n")))
:List (fn render-list [{: elements}]
(var list-str "<ul>\n")
(var depth 1)
(var depth 0)
(each [_ element (ipairs elements)]
(_G.pp element)
(let [contents-render (render-contents (. element :contents))
indent-level (. element :indent-level)]
(_G.pp (.. "indent-level " indent-level " depth: " depth))
(if (= indent-level depth) ; If we're at the right indent level
(set list-str (.. list-str "<li>" contents-render "</li>\n")) ; Just add the string as a list element
(do ; Otherwise
(while (not (= depth indent-level)) ; While our depth level is different from the indent level
(if (< indent-level depth) ; If we need to increase depth
(if (> indent-level depth) ; If we need to increase depth
(do
(set list-str (.. list-str "<ul>\n")) ; Add another layer in the render
(set depth (+ depth 1))) ; Increase our depth level
@ -27,35 +45,51 @@
(set list-str (.. list-str "</ul>\n")) ; Close a layer
(set depth (- depth 1))))) ; Reduce depth level
(set list-str (.. list-str "<li>" contents-render "</li>\n"))))))
(while (not (= depth 1))
(while (not (= depth 0))
(set list-str (.. list-str "</ul>\n"))
(set depth (- depth 1)))
(set depth (- depth 1))
(_G.pp (.. "depth: " depth)))
(set list-str (.. list-str "</ul>\n")) ; Close the first one
list-str)
:Quote (fn render-quote [{: contents}]
(let [contents-render (render-contents contents)]
(.. "<blockquote>\n" contents-render "</blockquote>\n")))
:Paragraph (fn render-paragraph [{: contents}]
(let [contents-render (render-contents contents)]
(.. "<p>\n" contents-render "</p>\n")))
(render-contents contents)) ; Contents render handles the <p> tag?
:Image (fn render-image [{: link : contents}]
(.. "<img\n src=" link "\n alt=" contents " />"))
(.. "<img\n src=" link "\n alt=" (render-contents contents) " />")) ; TODO this fucks up the alt tag
; Tags are a type of metadata
; Not really sure how to render metadata
; I think it actually shouldn't be rendered, and passed back up as... metadata
; The SSG should handle how to render them
:Tag (fn metadata-tag [{: tags}]
(var metadata {}))
:Metadata ""
:Tag (fn metadata-tag [{: tags} metadata]
(if (= metadata.tags nil)
(tset metadata "tags" [tags])
(table.insert metadata.tags tags))
metadata)
:Metadata (fn metadata [{: key : val} metadata]
(if (= metadata.key nil)
(tset metadata key [val])
(table.insert metadata.key val))
metadata)
; Not sure what to do about code either
; Need to look into pygments or whatever see how that works
; Or find some sort of minimal js solution?
; Not really sure
:Code ""
; Also not sure what to do about footnotes
; We need to link footnotes from inline parsing
; (which I'm just realizing I've forgotton to do... )
; And then link them here
:Footnote ""})
:Code (fn code [{: codehint : contents}]
(.. "<code>\n" contents "</code>\n"))
; Each footnote gets an incremental ID
; the contents parser will link to the appropriate footnote
; Don't forget to link to the ref (back link)
; See seirdy's footnotes for an example
; https://seirdy.one/meta/site-design/#fn:1
:Footnote (fn footnotes [{: elements}]
(var count 1)
(var rendered "<ol>\n")
(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 count (+ count 1))))
(.. rendered "</ol>\n"))})
(fn render [blocks grammar]
(var rendered "")
@ -65,10 +99,13 @@
(let [rule-set (. grammar (. block :type))]
(if (. block :is-metadata)
(set metadata (rule-set block metadata)) ; Handle metadata, TODO this doesn't handle sections for metadata semantics
(set rendered (.. rendered (rule-set block)))))) ; Write to the rendered string
(set rendered (.. rendered (rule-set block))))))
;(_G.pp rendered))) ; Write to the rendered string
(set rendered (.. rendered "\n<break/>\n"))) ; TODO Section "rendering"
; Should each section be returned seperately?
; Then we can add metadata to it easily.
; Final section
rendered)
(values rendered metadata))
{: render
: render-grammar}