Write Pandoc lua-filter in MoonScript and compile to Lua on build

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 20:18:05 -07:00
parent 46aa6c829f
commit 895ef13d73
4 changed files with 32 additions and 16 deletions

View File

@ -1,12 +1,15 @@
function CodeBlock(block) return {
if block.classes[1] == "moon" or block.classes[1] == "moonscript" then CodeBlock = function(block)
local fname = os.tmpname() if block.classes[1] == "moon" or block.classes[1] == "moonscript" then
local handle = io.open(fname, "w") local fname = os.tmpname()
handle:write(block.text) do
handle:close() local _with_0 = io.open(fname, "w")
_with_0:write(block.text)
local p = io.popen(string.format("pygmentize -f html -l moonscript.py %s -x", fname)) _with_0:close()
local out = p:read("*a") end
return pandoc.RawBlock("html", out) local p = io.popen(("pygmentize -f html -l moonscript.py %s -x"):format(fname))
local out = p:read("*a")
return pandoc.RawBlock("html", out)
end
end end
end }

11
pygments.moon Normal file
View File

@ -0,0 +1,11 @@
CodeBlock: (block) ->
if block.classes[1] == "moon" or block.classes[1] == "moonscript"
fname = os.tmpname!
with io.open fname, "w"
\write block.text
\close!
p = io.popen ("pygmentize -f html -l moonscript.py %s -x")\format fname
out = p\read"*a"
pandoc.RawBlock "html", out

View File

@ -13,6 +13,9 @@ import p from require "moon" -- debug
-- Change output dir to what Openresty prefers -- Change output dir to what Openresty prefers
Site.config.out_dir = "html/" Site.config.out_dir = "html/"
-- Compile lua-filter used by Pandoc to highlight MoonScript syntax
os.execute "moonc pygments.moon"
-- Configure the command our custom renderer uses to convert markdown to html -- Configure the command our custom renderer uses to convert markdown to html
rend = "renderers.markdown" rend = "renderers.markdown"
require(rend).cmd = "pandoc --mathjax --lua-filter pygments.lua >" require(rend).cmd = "pandoc --mathjax --lua-filter pygments.lua >"
@ -120,13 +123,13 @@ sitegen.create =>
add "blog.html", title: "Posts", target: "posts/index", template: "blog" add "blog.html", title: "Posts", target: "posts/index", template: "blog"
add_all files_from "docs" add_all files_from "docs"
copy "app.css"
copy "moonscript.css"
copy "pygments.css"
-- replace post markdown yaml headers with moonscript headers -- replace post markdown yaml headers with moonscript headers
filter "docs", (body) => filter "docs", (body) =>
body\gsub "^%-%-%-.-%.%.%.", (yaml_header) -> body\gsub "^%-%-%-.-%.%.%.", (yaml_header) ->
header = yaml_header\gsub "%-%-%-", "{" header = yaml_header\gsub "%-%-%-", "{"
moonscript_header = header\gsub "%.%.%.", "}" moonscript_header = header\gsub "%.%.%.", "}"
moonscript_header moonscript_header
copy "app.css"
copy "moonscript.css"
copy "pygments.css"

View File

@ -12,7 +12,6 @@
media="screen" media="screen"
/> />
<link rel="stylesheet" href="$root/app.css?$generate_date" /> <link rel="stylesheet" href="$root/app.css?$generate_date" />
<link rel="stylesheet" href="$root/pygments.css?$generate_date" />
<link rel="stylesheet" href="$root/moonscript.css?$generate_date" /> <link rel="stylesheet" href="$root/moonscript.css?$generate_date" />
</head> </head>
<body class="bg-white"> <body class="bg-white">