diff --git a/blog.janet b/blog.janet index 9e83fe6..aa219b0 100644 --- a/blog.janet +++ b/blog.janet @@ -6,6 +6,14 @@ (file/close f) content)) +(defn htmlspecialchars + [value] + (string/replace-all "\"" """ + (string/replace-all "'" "'" + (string/replace-all "<" "<" + (string/replace-all ">" ">" + (string/replace-all "&" "&" 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 "
" (htmlspecialchars value) "
") + [:heading level value] + (string "" (htmlspecialchars value) "") + [:quote value] + (string "
" (ast-to-html value) "
") + [:hr] + "
" + [:line value] + (string "

" (ast-to-html value) "

") + [:text value] + (htmlspecialchars value) + [:italic value] + (string "" (ast-to-html value) "") + [:stroke value] + (string "" (ast-to-html value) "") + [:bold value] + (string "" (ast-to-html value) "") + [:code-line value] + (string "" (ast-to-html value) "") + _ + (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")))))