ast-to-htmk

Signed-off-by: Superkooka <aymeric.gueracague@gmail.com>
This commit is contained in:
Aymeric GUERACAGUE 2023-12-19 18:02:44 +01:00
parent 7fa5a89722
commit e77693ea96
1 changed files with 40 additions and 2 deletions

View File

@ -6,6 +6,14 @@
(file/close f)
content))
(defn htmlspecialchars
[value]
(string/replace-all "\"" "&quot;"
(string/replace-all "'" "&apos;"
(string/replace-all "<" "&lt;"
(string/replace-all ">" "&gt;"
(string/replace-all "&" "&amp;" value))))))
(def md-to-ast
"A custom markdown flavored grammar"
~{
@ -52,6 +60,36 @@
:main (some (* :block (? :end-of-line)))})
(defn ast-to-html
[ast]
(string/join
(map
|(match $0
[:code-block lang value]
(string "<pre><code>" (htmlspecialchars value) "</code></pre>")
[:heading level value]
(string "<h" level ">" (htmlspecialchars value) "</h" level ">")
[:quote value]
(string "<blockquote>" (ast-to-html value) "</blockquote>")
[:hr]
"<hr/>"
[:line value]
(string "<p>" (ast-to-html value) "</p>")
[:text value]
(htmlspecialchars value)
[:italic value]
(string "<em>" (ast-to-html value) "</em>")
[:stroke value]
(string "<stroke>" (ast-to-html value) "</stroke>")
[:bold value]
(string "<strong>" (ast-to-html value) "</strong>")
[:code-line value]
(string "<code>" (ast-to-html value) "</code>")
_
(error (string "Invalid symbol: " (get $0 0))))
ast)
""))
(defn pp-ast
"Pretty prints the Mardown AST (as provided by (peg/match md-to-ast))"
[ast]
@ -62,5 +100,5 @@
(print "]"))))
(defn main [bin & args]
(pp-ast (peg/match md-to-ast (read-from-file "raw/~blog/blog.md"))))
# (print (to-html (peg/match md-to-ast (read-from-file "raw/~notes/blog.md")))))
# (pp-ast (peg/match md-to-ast (read-from-file "raw/~blog/blog.md")))
(print (ast-to-html (peg/match md-to-ast (read-from-file "raw/~blog/blog.md")))))