Refactor pygments.moon

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 20:18:05 -07:00
parent a4258167df
commit 41baffb445
7 changed files with 52 additions and 59 deletions

View File

@ -7,6 +7,12 @@ run:
build:
docker run --rm -w /opt/app -v $(PWD):/opt/app $(image) sitegen
build-code:
docker run --rm -w /opt/app -v $(PWD):/opt/app $(image) sitegen build code.md
build-pygments:
docker run --rm -w /opt/app -v $(PWD):/opt/app $(image) moonc pygments.moon
image-rm:
docker image rm $(image)
@ -16,5 +22,5 @@ image-build:
lint:
docker run --rm -w /opt/app -v $(PWD):/opt/app $(image) moonc -l .
test:
test: build-pygments
./test.sh

View File

@ -78,10 +78,6 @@ div.sourceCode {
margin: inherit;
}
div.sourceCode, div.highlight {
border-radius: 3px;
}
main * {
text-align: left;
margin-left: auto;
@ -207,6 +203,11 @@ figure figcaption {
pre {
padding: 1em;
overflow: auto;
border-radius: 3px;
/* Dracula theme */
background: #282a36;
color: #f8f8f2;
}
:not(pre) > code {

View File

@ -30,7 +30,7 @@ pre.numberSource code > span > a:first-child::before
pre.numberSource { margin-left: 3em; border-left: 1px solid #7a7c7d; padding-left: 4px; }
div.sourceCode
/* { color: #cfcfc2; background-color: #232629; } */
{ background: #282a36; color: #f8f8f2; }
/* { background: #282a36; color: #f8f8f2; } */
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}

View File

@ -8,7 +8,7 @@ span.linenos { color: #f1fa8c; background-color: #44475a; padding-left: 5px; pad
td.linenos .special { color: #50fa7b; background-color: #6272a4; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #50fa7b; background-color: #6272a4; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #44475a }
.highlight { background: #282a36; color: #f8f8f2 }
/* .highlight { background: #282a36; color: #f8f8f2 } */
.highlight .py-c { color: #6272a4 } /* Comment */
.highlight .py-err { color: #f8f8f2 } /* Error */
.highlight .py-g { color: #f8f8f2 } /* Generic */

View File

@ -1,44 +1,33 @@
return {
CodeBlock = function(block)
local label
label = block.classes[1]
local cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
local fname = os.tmpname()
do
local _with_0 = io.open(fname, "w")
_with_0:write(block.text)
_with_0:close()
end
local cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
if block.classes[1] == "moon" or block.classes[1] == "moonscript" then
local _exp_0 = label
if "moon" == _exp_0 or "moonscript" == _exp_0 then
local p = io.popen((cmd .. " -x"):format("moonscript.py", fname))
local out = p:read("*a")
local out
do
local _with_0 = p:read("*a")
p:close()
out = _with_0
end
return pandoc.RawBlock("html", out)
else
if block.classes[1] == nil or block.classes[1] == "heex" then
if block.classes[1] == nil then
local cb = pandoc.CodeBlock(block.text, {
class = "sourceCode"
})
return pandoc.Div(cb, {
class = "sourceCode"
})
else
return block
end
else
local status, handle_or_error = pcall(function()
return io.popen((cmd):format(block.classes[1], fname))
local status, handle = pcall(function()
return io.popen(cmd:format(label or "text", fname))
end)
local sout = handle_or_error:read("*a")
if sout:len() == 0 then
local cb = pandoc.CodeBlock(block.text, {
class = "sourceCode"
})
return pandoc.Div(cb, {
class = "sourceCode"
})
local out = handle:read("*a")
if out:len() == 0 then
return block
else
return pandoc.RawBlock("html", sout)
end
return pandoc.RawBlock("html", out)
end
end
end

View File

@ -1,30 +1,26 @@
CodeBlock: (block) ->
{ label } = block.classes
cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
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 block.classes[1] == "moon" or block.classes[1] == "moonscript"
switch label
when "moon", "moonscript"
p = io.popen (cmd .. " -x")\format "moonscript.py", fname
out = p\read"*a"
out = with p\read"*a"
p\close!
-- syntax highlight with pygments
pandoc.RawBlock "html", out
else
if block.classes[1] == nil or block.classes[1] == "heex"
if block.classes[1] == nil
cb = pandoc.CodeBlock(block.text, class: "sourceCode")
pandoc.Div cb, class: "sourceCode"
else
status, handle = pcall -> io.popen cmd\format label or "text", fname
out = handle\read"*a"
if out\len! == 0
-- syntax highlight with pandoc
block
else
status, handle_or_error = pcall ->
io.popen (cmd)\format block.classes[1], fname
sout = handle_or_error\read"*a"
if sout\len! == 0
cb = pandoc.CodeBlock(block.text, class: "sourceCode")
pandoc.Div cb, class: "sourceCode"
else
pandoc.RawBlock "html", sout
-- syntax highlight with pygments
pandoc.RawBlock "html", out

View File

@ -67,7 +67,7 @@ $hello{[[
this code block has an unknown label
```]]
assert.same [[<div class="sourceCode"><pre class="sourceCode"><code>this code block has an unknown label</code></pre></div>]], out
assert.same [[<pre class="snickerdoodle"><code>this code block has an unknown label</code></pre>]], out
it "doesn't highlight unlabeled code blocks", ->
out = flatten_html render [[
@ -75,7 +75,8 @@ this code block has an unknown label
this code block has no label
```]]
assert.same [[<div class="sourceCode"><pre class="sourceCode"><code>this code block has no label</code></pre></div>]], out
assert.same [[<div class="highlight"><pre><span></span><code>this code block has no label
</code></pre></div>]], out
it "defaults to pandoc highlighting when pygments fails to recognize code language", ->
out = flatten_html render [[