miti.sh/plugins/pygments.moon

62 lines
2.0 KiB
Plaintext

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?"