#!/usr/bin/env janet (import ./grammar/code/php :as php) (import ./grammar/code/janet :as janet) (import ./grammar/page/markdown :as markdown-page) (defn read-from-file [file-path] "Read a file from string filepath" (let [f (file/open file-path :r) content (file/read f :all)] (file/close f) content)) (defn write-to-file [file-path content] "Append to file from string filepath" (let [f (file/open file-path :w)] (file/write f content) (file/flush f) (file/close f) content)) (defn htmlspecialchars "Convert html special chars from a string" [value] (string/replace-all "\"" """ (string/replace-all "'" "'" (string/replace-all "<" "<" (string/replace-all ">" ">" (string/replace-all "&" "&" value)))))) (defn code-ast-to-html [ast] (string/join (map |(match $0 [:single-line-comment value] (string "" (htmlspecialchars value) "") [:multi-line-comment value] (string "" (htmlspecialchars value) "") [:string value] (string "" (htmlspecialchars value) "") [:variable value] (string "" (htmlspecialchars value) "") [:return-type value] (string "" (htmlspecialchars value) "") [:keyword value] (string "" (htmlspecialchars value) "") [:other value] (htmlspecialchars value) _ (error (string "Invalid symbol: " (get $0 0)))) ast) "")) (defn page-ast-to-html "Convert an AST to html" [ast] (defn to-codeblock [lang value] (string "
" (code-ast-to-html (peg/match (eval-string (string lang "/to-ast")) value)) "
")) (defn to-codeline [value] (string "" (page-ast-to-html value) "")) (defn to-heading [level value] (string "" (htmlspecialchars value) "")) (defn to-quote [value] (string "
" (page-ast-to-html value) "
")) (defn to-line [value] (string "

" (page-ast-to-html value) "

")) (defn to-italic [value] (string "" (page-ast-to-html value) "")) (defn to-bold [value] (string "" (page-ast-to-html value) "")) (defn to-stroke [value] (string "" (page-ast-to-html value) "")) (string/join (map |(match $0 [:code-block lang value] (to-codeblock lang value) [:heading level value] (to-heading level value) [:quote value] (to-quote value) [:hr] "
" [:line value] (to-line value) [:text value] (htmlspecialchars value) [:italic value] (to-italic value) [:stroke value] (to-stroke value) [:bold value] (to-bold value) [:code-line value] (to-codeline 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] (if ast (do (print "[") (map |(printf "\t%q" $0) ast) (print "]")))) (defn main [bin & args] (write-to-file "test4.html" (string ``` Meow
``` (page-ast-to-html (peg/match markdown-page/to-ast (read-from-file "raw/~blog/blog.md"))) ```
```)))