Use Pandoc lua-filter instead of Pygments plugin

for syntax highlights
This commit is contained in:
Catalin Constantin Mititiuc 2025-05-27 19:28:17 -07:00
parent c72580f717
commit e06d75f82f
3 changed files with 4 additions and 146 deletions

View File

@ -1,61 +0,0 @@
html = require "sitegen.html"
class CustomPygments extends require "sitegen.plugins.pygments"
filter: (text, page) =>
-- require("moon").p text
-- super(text, page)
text -- pandoc output
super(text, page) -- plugin output
highlight: (lang, code) =>
-- print "HIGHLIGHT CALLED"
-- require("moon").p code
fname = os.tmpname!
with io.open fname, "w"
\write code
\close!
-- p = io.popen ("pygmentize -f html -l %s %s")\format lang, fname
-- out = p\read"*a"
--
-- -- get rid of the div and pre inserted by pygments
-- assert out\match('^<div class="highlight"><pre>(.-)\n?</pre></div>'),
-- "Failed to parse pygmentize result, is pygments installed?"
cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
if lang == "moon" or lang == "moonscript"
p = io.popen (cmd .. " -x")\format "moonscript.py", fname
-- -- p = io.popen (cmd)\format "moonscript", fname
out = p\read"*a"
p\close!
-- out
-- get rid of the div and pre inserted by pygments
assert out\match('^<div class="highlight"><pre>(.-)\n?</pre></div>'),
"Failed to parse pygmentize result, is pygments installed?"
else
if lang == nil or lang == "heex"
-- if block.classes[1] == nil
-- cb = pandoc.CodeBlock(block.text, class: "sourceCode")
-- pandoc.Div cb, class: "sourceCode"
-- else
-- block
-- "code(nil or heex): #{code}"
code
else
status, handle_or_error = pcall ->
io.popen (cmd)\format lang, fname
sout = handle_or_error\read"*a"
handle_or_error\close!
if sout\len! == 0
-- "code: #{code}"
-- raw code
code
else
-- get rid of the div and pre inserted by pygments
assert sout\match('^<div class="highlight"><pre>(.-)\n?</pre></div>'),
"Failed to parse pygmentize result, is pygments installed?"

View File

@ -17,80 +17,17 @@ Site.config.out_dir = "html/"
-- Compile lua-filter used by Pandoc to highlight MoonScript syntax
os.execute "moonc pygments.moon"
try_compile = (text, options=implicitly_return_root: false) ->
to_lua text, options
-- PygmentsPlugin.highlight = (lang, code) =>
-- fname = os.tmpname!
-- with io.open fname, "w"
-- \write code
-- \close!
--
-- p = io.popen ("pygmentize -f html -l %s %s")\format lang, fname
-- out = p\read"*a"
--
-- -- get rid of the div and pre inserted by pygments
-- assert out\match('^<div class="highlight"><pre>(.-)\n?</pre></div>'),
-- "Failed to parse pygmentize result, is pygments installed?"
-- print "HIGHLIGHT CALLED"
--
-- fname = os.tmpname!
-- with io.open fname, "w"
-- \write block.text
-- \close!
--
-- cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
--
-- if lang == "moon" or lang == "moonscript"
-- -- p = io.popen ("pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l moonscript.py %s -x")\format fname
-- p = io.popen (cmd .. " -x")\format "moonscript.py", fname
-- -- p = io.popen (cmd)\format "moonscript", fname
--
-- -- p = io.open fname
-- out = p\read"*a"
-- p\close!
-- out
-- -- pandoc.RawBlock "html", "moonscript"
-- else
-- if lang == nil or lang == "heex"
-- -- if block.classes[1] == nil
-- -- cb = pandoc.CodeBlock(block.text, class: "sourceCode")
-- -- pandoc.Div cb, class: "sourceCode"
-- -- else
-- -- block
--
-- "code: #{code}"
-- else
-- status, handle_or_error = pcall ->
-- io.popen (cmd)\format lang, fname
-- -- io.open fname
--
-- sout = handle_or_error\read"*a"
-- -- handle_or_error\close!
-- if sout\len! == 0
-- -- cb = pandoc.CodeBlock(block.text, class: "sourceCode")
-- -- pandoc.Div cb, class: "sourceCode"
-- "code: #{code}"
-- else
-- -- pandoc.RawBlock "html", sout
-- sout
-- Configure the command our custom renderer uses to convert markdown to html
rend = "renderers.markdown"
-- require(rend).cmd = "pandoc --mathjax --no-highlight >"
require(rend).cmd = "pandoc --mathjax >"
-- require(rend).cmd = "pandoc --mathjax --lua-filter pygments.lua >"
require(rend).cmd = "pandoc --mathjax --lua-filter pygments.lua >"
-- Insert custom renderer in the first position so it will be preferred
table.insert Site.default_renderers, 1, rend
-- Remove "pygments" plugin
-- Remove Pygments plugin
Site.default_plugins = for v in *Site.default_plugins
if v\find "pygments" then continue else v
table.insert Site.default_plugins, 1, "plugins.pygments"
-- from https://github.com/leafo/sitegen/blob/v0.2/spec/sitegen_spec.moon#L9-L18
get_files = (path, prefix=path) ->
files = Path.read_exec "find", path, "-type", "f"
@ -195,23 +132,6 @@ sitegen.create =>
publish_date: format_date date true
-- add_all files_from "docs"
-- with PygmentsPlugin.custom_highlighters
-- .moon = (code_text, page, options) =>
-- lua_text, err = try_compile code_text, options
-- return err if not lua_text
--
-- html.build ->
-- tag.span "moon custom highlighter"
--
-- .heex = (code_text, page, options) =>
-- lua_text, err = try_compile code_text, options
-- return err if not lua_text
--
-- html.build ->
-- tag.span "heex custom highlighter"
copy "app.css"
copy "pygments.css"
copy "moonscript.css"

View File

@ -11,9 +11,8 @@
type="text/css"
media="screen"
/>
<link rel="stylesheet" href="$root/pandoc.css?$generate_date" />
<link rel="stylesheet" href="$root/moonscript.css?$generate_date" />
<link rel="stylesheet" href="$root/app.css?$generate_date" />
$each{{"pandoc", "pygments", "app"}, "stylesheet"}[[
<link rel="stylesheet" href="$root/$stylesheet.css?$generate_date" />]]
</head>
<body class="bg-white">
<header>