diff --git a/blog.janet b/blog.janet index afe4502..6ff4a12 100644 --- a/blog.janet +++ b/blog.janet @@ -1,5 +1,8 @@ #!/usr/bin/env janet +(import ./grammar/php :as php) +(import ./grammar/janet :as janet) + (defn read-from-file [file-path] "Read a file from string filepath" (let [f (file/open file-path :r) @@ -7,6 +10,14 @@ (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] @@ -62,10 +73,26 @@ :main (some (* :block (? :end-of-line)))}) +(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 ast-to-html "Convert an AST to html" [ast] - (defn to-codeblock [lang value] (string "
" (htmlspecialchars value) ""))
+ (defn to-codeblock [lang value] (string "" (code-ast-to-html (peg/match (eval-string (string lang "/to-ast")) value)) ""))
(defn to-codeline [value] (string "" (ast-to-html value) ""))
(defn to-heading [level value] (string "" (ast-to-html value) "")) @@ -102,7 +129,8 @@ (print "]")))) (defn main [bin & args] - # (pp-ast (peg/match md-to-ast (read-from-file "raw/~blog/blog.md"))) - (print (read-from-file "partial-template/header.html")) - (print (ast-to-html (peg/match md-to-ast (read-from-file "raw/~blog/blog.md")))) - (print (read-from-file "partial-template/footer.html"))) + (pp-ast (peg/match md-to-ast (read-from-file "raw/~blog/blog.md"))) + (write-to-file "test4.html" (string + (read-from-file "partial-template/header.html") + (ast-to-html (peg/match md-to-ast (read-from-file "raw/~blog/blog.md"))) + (read-from-file "partial-template/footer.html")))) diff --git a/grammar/php.janet b/grammar/php.janet index 874c6ae..bc15029 100644 --- a/grammar/php.janet +++ b/grammar/php.janet @@ -1,43 +1,6 @@ #!/usr/bin/env janet -# need to get comment, link, doctag, string literal/regexp, other literal, keyword identifier? -# based on https://github.com/php/php-langspec/blob/ca697b43031efd70b5306d29b74e1190e7eacac8/spec/19-grammar.md - -(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 pp-ast - "Pretty prints the PHP AST (as provided by (peg/match php-to-ast))" - [ast] - (if ast - (do - (print "[") - (map |(printf "\t%q" $0) ast) - (print "]")))) - -(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)))))) - -(def php-to-ast +(def to-ast "A custom markdown flavored grammar" ~{ :end-of-line (* (? "\r") "\n") @@ -68,24 +31,3 @@ :other (cmt (<- (if-not :comment 1)),|[:other (string/join $&)]) :main (some (+ :comment :string :variable :return-type :keyword :other))}) - -(defn 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 main [bin & args] - # (pp-ast (peg/match php-to-ast (read-from-file "sample.php")))) - (write-to-file "php.html" (string "
" (ast-to-html (peg/match php-to-ast (read-from-file "sample.php"))) "")))
diff --git a/partial-template/header.html b/partial-template/header.html
index 68463eb..662f3ea 100644
--- a/partial-template/header.html
+++ b/partial-template/header.html
@@ -8,6 +8,7 @@
+