feat: started render work

This commit is contained in:
Anish Lakhwara 2023-12-07 20:27:45 -08:00
parent 00db423384
commit 38806c4d03

54
render.fnl Normal file
View file

@ -0,0 +1,54 @@
(local fennel (require :fennel))
(fn _G.pp [x] (print (fennel.view x)))
(fn render-contents [contents])
(local renderer
{:Header (fn render-header [{: contents : size}]
(let [header-type (tostring size)
header (.. "h" header-type)
contents-render (render-contents contents)]
(.. "<" header ">\n" contents-render "</" header ">\n")))
:List (fn render-list [{: elements}]
(var list-str "<ul>\n")
(var depth 1)
(each [_ element (ipairs elements)]
(let [contents-render (render-contents (. element :contents))
indent-level (. element :indent-level)]
(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
(do
(set list-str (.. list-str "<ul>\n")) ; Add another layer in the render
(set depth (+ depth 1))) ; Increase our depth level
(do ; We need to decrease depth
(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))
(set list-str (.. list-str "</ul>\n"))
(set depth (- depth 1)))
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")))
:Image (fn render-image [{: link : contents}]
(.. "<img\n src=" link "\n alt=" contents " />"))
; 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 ""
; 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 ""})