ast-to-htmk
Signed-off-by: Superkooka <aymeric.gueracague@gmail.com>
This commit is contained in:
parent
7fa5a89722
commit
e77693ea96
42
blog.janet
42
blog.janet
|
|
@ -6,6 +6,14 @@
|
||||||
(file/close f)
|
(file/close f)
|
||||||
content))
|
content))
|
||||||
|
|
||||||
|
(defn htmlspecialchars
|
||||||
|
[value]
|
||||||
|
(string/replace-all "\"" """
|
||||||
|
(string/replace-all "'" "'"
|
||||||
|
(string/replace-all "<" "<"
|
||||||
|
(string/replace-all ">" ">"
|
||||||
|
(string/replace-all "&" "&" value))))))
|
||||||
|
|
||||||
(def md-to-ast
|
(def md-to-ast
|
||||||
"A custom markdown flavored grammar"
|
"A custom markdown flavored grammar"
|
||||||
~{
|
~{
|
||||||
|
|
@ -52,6 +60,36 @@
|
||||||
|
|
||||||
:main (some (* :block (? :end-of-line)))})
|
: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
|
(defn pp-ast
|
||||||
"Pretty prints the Mardown AST (as provided by (peg/match md-to-ast))"
|
"Pretty prints the Mardown AST (as provided by (peg/match md-to-ast))"
|
||||||
[ast]
|
[ast]
|
||||||
|
|
@ -62,5 +100,5 @@
|
||||||
(print "]"))))
|
(print "]"))))
|
||||||
|
|
||||||
(defn main [bin & args]
|
(defn main [bin & args]
|
||||||
(pp-ast (peg/match md-to-ast (read-from-file "raw/~blog/blog.md"))))
|
# (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")))))
|
(print (ast-to-html (peg/match md-to-ast (read-from-file "raw/~blog/blog.md")))))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue