144 lines
5.4 KiB
Plaintext
144 lines
5.4 KiB
Plaintext
#!/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 "<span class=\"highlighted-comment\">" (htmlspecialchars value) "</span>")
|
|
[:multi-line-comment value] (string "<span class=\"highlighted-comment\">" (htmlspecialchars value) "</span>")
|
|
[:string value] (string "<span class=\"highlighted-string\">" (htmlspecialchars value) "</span>")
|
|
[:variable value] (string "<span class=\"highlighted-variable\">" (htmlspecialchars value) "</span>")
|
|
[:return-type value] (string "<span class=\"highlighted-variable\">" (htmlspecialchars value) "</span>")
|
|
[:keyword value] (string "<span class=\"highlighted-keyword\">" (htmlspecialchars value) "</span>")
|
|
[: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 "<pre class=\"my-4 p-4 highlighted-base\"><code>" (code-ast-to-html (peg/match (eval-string (string lang "/to-ast")) value)) "</code></pre>"))
|
|
(defn to-codeline [value] (string "<code>" (page-ast-to-html value) "</code>"))
|
|
(defn to-heading [level value] (string "<h" level " class=\"my-4\">" (htmlspecialchars value) "</h" level ">"))
|
|
(defn to-quote [value] (string "<blockquote class=\"my-4\">" (page-ast-to-html value) "</blockquote>"))
|
|
(defn to-line [value] (string "<p class=\"my-4\">" (page-ast-to-html value) "</p>"))
|
|
(defn to-italic [value] (string "<em>" (page-ast-to-html value) "</em>"))
|
|
(defn to-bold [value] (string "<strong>" (page-ast-to-html value) "</strong>"))
|
|
(defn to-stroke [value] (string "<stroke>" (page-ast-to-html value) "</stroke>"))
|
|
|
|
(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] "<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
|
|
```
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+3:wght@400&display=swap" rel="stylesheet">
|
|
<link href="public/highlight-style.css" rel="stylesheet">
|
|
<script src="https://cdn.tailwindcss.com/3.3.7"></script>
|
|
<script>
|
|
tailwind.config = {
|
|
theme: {
|
|
extend: {
|
|
fontFamily: {
|
|
'sans': ['"Source Sans 3"'],
|
|
},
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
<title>Meow</title>
|
|
</head>
|
|
|
|
<body class="flex flex-row my-8">
|
|
<nav class="my-8 ml-16 text-xl">
|
|
<div class="flex flex-row">
|
|
<ul class="flex flex-col mr-6">
|
|
<li class="p-1 w-max"><a href="../index.html">Blog</a></li>
|
|
<li class="p-1 w-max"><a href="index.html">Portfolio</a></li>
|
|
<li class="p-1 w-max"><a href="index.html">À Propos de moi</a></li>
|
|
</ul>
|
|
<span class="after:content-[''] after:w-[1px] after:bg-black after:my-2 after:h-4/5 after:block"></span>
|
|
</div>
|
|
</nav>
|
|
<main class="my-8 ml-24 mr-[38rem] w-[100%] p-4">
|
|
```
|
|
(page-ast-to-html (peg/match markdown-page/to-ast (read-from-file "raw/~blog/blog.md")))
|
|
```
|
|
</main>
|
|
<footer class="w-full fixed m-1 left-0 bottom-0 flex justify-center">
|
|
<a class="mx-1 underline" href="#">Flux RSS</a>
|
|
<span class="mx-1">•</span>
|
|
<a class="mx-1 underline" href="#">Inspirer de Sidey</a>
|
|
<span class="mx-1">•</span>
|
|
<a class="mx-1 underline" href="#">GitHub</a>
|
|
<span class="mx-1">•</span>
|
|
<a class="mx-1 underline" href="#">Instagram</a>
|
|
</footer>
|
|
</body>
|
|
</html>
|
|
```)))
|