parse rich text
Signed-off-by: Superkooka <aymeric.gueracague@gmail.com>
This commit is contained in:
parent
8e5880a022
commit
7fa5a89722
67
blog.janet
67
blog.janet
|
|
@ -12,65 +12,46 @@
|
|||
# Symbole
|
||||
:end-of-line (* (? "\r") "\n")
|
||||
:space "\u0020"
|
||||
:tab (+ "\u0009" (4 :space))
|
||||
|
||||
:code-block-tag "```"
|
||||
:list-item-starter (+ "*" "-")
|
||||
:italic-tag "*"
|
||||
:bold-tag "**"
|
||||
:stroke-tag "~~"
|
||||
:code-line-tag "`"
|
||||
|
||||
:paragraph (some (if-not :end-of-line 1))
|
||||
:rich-text-tag (+ :italic-tag :bold-tag :stroke-tag :code-line-tag)
|
||||
:basic-text (some (if-not (+ :end-of-line :code-line-tag :stroke-tag :bold-tag :italic-tag) 1))
|
||||
|
||||
:c-basic-text (cmt (<- :basic-text),|[:text $0])
|
||||
|
||||
:bold (cmt (* :bold-tag :rich-text :bold-tag),|[:bold $0 $&])
|
||||
:italic (cmt (* :italic-tag :rich-text :italic-tag),|[:italic $0 $&])
|
||||
:stroke (cmt (* :stroke-tag :rich-text :stroke-tag),|[:stroke $0 $&])
|
||||
:code-line (cmt (* :code-line-tag (<- :rich-text) :code-line-tag),|[:code-line $0 $&])
|
||||
|
||||
:rich-text (cmt (some (+ :code-line :bold :italic :stroke :c-basic-text)),|(map (fn [arg] [(get arg 0) (get arg 1)]) $&))
|
||||
:line (cmt (* :rich-text :end-of-line),|[:line $0])
|
||||
|
||||
# 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])
|
||||
:heading (cmt (* (<- (between 1 6 "#")) :space (<- :basic-text)),|[:heading (length $0) $1])
|
||||
:hr (cmt (* (at-least 3 (+ "-" "_" "*")) :end-of-line),|[:hr])
|
||||
:code-block (cmt (* :code-block-tag (? (<- :basic-text)) :end-of-line (<- (some (if-not :code-block-tag 1))) :code-block-tag),|[:code-block $0 $1])
|
||||
:quote (cmt (* ">" :space (? :rich-text)),|[:quote $0])
|
||||
:list-item (cmt (* (<- (? (between 0 2 :tab))) :list-item-starter :space :rich-text),|[:list-item (/ (length $0) 4) $1])
|
||||
: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]
|
||||
|
|
@ -81,5 +62,5 @@
|
|||
(print "]"))))
|
||||
|
||||
(defn main [bin & args]
|
||||
(pp-ast (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 (to-html (peg/match md-to-ast (read-from-file "raw/~notes/blog.md")))))
|
||||
|
|
|
|||
Loading…
Reference in New Issue