86 lines
2.8 KiB
Plaintext
86 lines
2.8 KiB
Plaintext
#!/usr/bin/env janet
|
|
|
|
(defn read-from-file [file-path]
|
|
(let [f (file/open file-path :r)
|
|
content (file/read f :all)]
|
|
(file/close f)
|
|
content))
|
|
|
|
(def md-to-ast
|
|
"A custom markdown flavored grammar"
|
|
~{
|
|
# Symbole
|
|
:end-of-line (* (? "\r") "\n")
|
|
:space "\u0020"
|
|
:code-block-tag "```"
|
|
:list-item-starter (+ "*" "-")
|
|
|
|
:paragraph (some (if-not :end-of-line 1))
|
|
|
|
# Block
|
|
:heading (cmt (* (<- (between 1 6 "#")) :space (<- :paragraph)),|[:heading (length $0) $1])
|
|
:hr (cmt (at-least 3 (+ "-" "_" "*")),|[:hr])
|
|
:code-block (cmt (* :code-block-tag (? (<- :paragraph)) :end-of-line (<- (some (if-not :code-block-tag 1))) :code-block-tag),|[:code-block $0 $1])
|
|
:quote (cmt (* ">" :space (<- :paragraph)),|[:quote $0])
|
|
:list-item (cmt (* :list-item-starter :space (<- :paragraph)),|[:list-item $0])
|
|
:list (cmt (some (* :list-item (? :end-of-line))),|[:list $&])
|
|
|
|
# :table-cel ""
|
|
:table-row (* "|" (cmt (some (* (<- (some (if-not "|" 1))) "|")),|[:table-row (map string/trim $&)]) :end-of-line)
|
|
:table-delimiter (* "|" (cmt (some (* (any :space) (at-least 3 "-") (any :space) "|")),|[:delimiter]) :end-of-line) # Delimiter Row <https://github.github.com/gfm/#delimiter-row>
|
|
:table # <https://github.github.com/gfm/#table>
|
|
(cmt (*
|
|
:table-row
|
|
:table-delimiter
|
|
(some :table-row)),|[:table $&])
|
|
|
|
:line (cmt (<- :paragraph),|[:line $0])
|
|
|
|
:block (+ :heading
|
|
:hr
|
|
:code-block
|
|
:quote
|
|
:list
|
|
:table
|
|
:table-row
|
|
:line
|
|
:end-of-line)
|
|
|
|
:main (some (* :block (? :end-of-line)))})
|
|
|
|
(defn to-html
|
|
"Convert AST to HTML"
|
|
[ast]
|
|
(if ast
|
|
(string/join
|
|
(map
|
|
|(match $0
|
|
[:heading level value]
|
|
(string "<h" level ">" value "</h" level ">")
|
|
[:quote value]
|
|
(string "<blockquote>" value "</blockquote>")
|
|
[:list items]
|
|
(string/join @["<ul>"
|
|
;(map |(string "<li>" $0 "</li>") (get items 0))
|
|
"</ul>"] "\n")
|
|
[:line value]
|
|
(string "<p>" value "</p>")
|
|
[:code-block lang value]
|
|
(string "<pre><code>" value "</code></pre>")
|
|
_
|
|
(error (string "Invalid symbol: " (get $0 0))))
|
|
ast) "\n" )))
|
|
|
|
(defn pp-ast
|
|
"Pretty prints the Mardown AST (as provided by (peg/match md-to-ast))"
|
|
[ast]
|
|
(if ast
|
|
(do
|
|
(print "[")
|
|
(map |(printf "\t%q" $0) ast)
|
|
(print "]"))))
|
|
|
|
(defn main [bin & args]
|
|
(pp-ast (peg/match md-to-ast (read-from-file "raw/~notes/blog.md"))))
|
|
# (print (to-html (peg/match md-to-ast (read-from-file "raw/~notes/blog.md")))))
|