From 41baffb4453849e124d019eae65050b14f773817 Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Mon, 16 Jun 2025 20:18:05 -0700 Subject: [PATCH] Refactor pygments.moon --- Makefile | 8 ++++++- app.css | 9 ++++---- pandoc.css | 2 +- pygments.css | 2 +- pygments.lua | 47 +++++++++++++++------------------------- pygments.moon | 38 +++++++++++++++----------------- spec/renderers_spec.moon | 5 +++-- 7 files changed, 52 insertions(+), 59 deletions(-) diff --git a/Makefile b/Makefile index cb2fdab..5ce8014 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/app.css b/app.css index 34a355a..55636c6 100644 --- a/app.css +++ b/app.css @@ -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 { diff --git a/pandoc.css b/pandoc.css index b45e810..ffe6c79 100644 --- a/pandoc.css +++ b/pandoc.css @@ -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; } } diff --git a/pygments.css b/pygments.css index de4141d..732352d 100644 --- a/pygments.css +++ b/pygments.css @@ -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 */ diff --git a/pygments.lua b/pygments.lua index eabe7f1..dfdf250 100644 --- a/pygments.lua +++ b/pygments.lua @@ -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") - p:close() + 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 + local status, handle = pcall(function() + return io.popen(cmd:format(label or "text", fname)) + end) + local out = handle:read("*a") + if out:len() == 0 then + return block else - local status, handle_or_error = pcall(function() - return io.popen((cmd):format(block.classes[1], 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" - }) - else - return pandoc.RawBlock("html", sout) - end + return pandoc.RawBlock("html", out) end end end diff --git a/pygments.moon b/pygments.moon index f9063c9..40cf3ef 100644 --- a/pygments.moon +++ b/pygments.moon @@ -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" - p = io.popen (cmd .. " -x")\format "moonscript.py", fname - out = p\read"*a" - p\close! - 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 - block + switch label + when "moon", "moonscript" + p = io.popen (cmd .. " -x")\format "moonscript.py", fname + out = with p\read"*a" + p\close! + -- syntax highlight with pygments + pandoc.RawBlock "html", out else - status, handle_or_error = pcall -> - io.popen (cmd)\format block.classes[1], fname + status, handle = pcall -> io.popen cmd\format label or "text", fname + out = handle\read"*a" - sout = handle_or_error\read"*a" - if sout\len! == 0 - cb = pandoc.CodeBlock(block.text, class: "sourceCode") - pandoc.Div cb, class: "sourceCode" + if out\len! == 0 + -- syntax highlight with pandoc + block else - pandoc.RawBlock "html", sout + -- syntax highlight with pygments + pandoc.RawBlock "html", out diff --git a/spec/renderers_spec.moon b/spec/renderers_spec.moon index cfd27b4..ca821fa 100644 --- a/spec/renderers_spec.moon +++ b/spec/renderers_spec.moon @@ -67,7 +67,7 @@ $hello{[[ this code block has an unknown label ```]] - assert.same [[
this code block has an unknown label
]], out + assert.same [[
this code block has an unknown label
]], 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 [[
this code block has no label
]], out + assert.same [[
this code block has no label
+
]], out it "defaults to pandoc highlighting when pygments fails to recognize code language", -> out = flatten_html render [[