Refactor pygments.moon
This commit is contained in:
parent
a4258167df
commit
41baffb445
8
Makefile
8
Makefile
@ -7,6 +7,12 @@ run:
|
|||||||
build:
|
build:
|
||||||
docker run --rm -w /opt/app -v $(PWD):/opt/app $(image) sitegen
|
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:
|
image-rm:
|
||||||
docker image rm $(image)
|
docker image rm $(image)
|
||||||
|
|
||||||
@ -16,5 +22,5 @@ image-build:
|
|||||||
lint:
|
lint:
|
||||||
docker run --rm -w /opt/app -v $(PWD):/opt/app $(image) moonc -l .
|
docker run --rm -w /opt/app -v $(PWD):/opt/app $(image) moonc -l .
|
||||||
|
|
||||||
test:
|
test: build-pygments
|
||||||
./test.sh
|
./test.sh
|
||||||
|
9
app.css
9
app.css
@ -78,10 +78,6 @@ div.sourceCode {
|
|||||||
margin: inherit;
|
margin: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
div.sourceCode, div.highlight {
|
|
||||||
border-radius: 3px;
|
|
||||||
}
|
|
||||||
|
|
||||||
main * {
|
main * {
|
||||||
text-align: left;
|
text-align: left;
|
||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
@ -207,6 +203,11 @@ figure figcaption {
|
|||||||
pre {
|
pre {
|
||||||
padding: 1em;
|
padding: 1em;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
|
border-radius: 3px;
|
||||||
|
|
||||||
|
/* Dracula theme */
|
||||||
|
background: #282a36;
|
||||||
|
color: #f8f8f2;
|
||||||
}
|
}
|
||||||
|
|
||||||
:not(pre) > code {
|
:not(pre) > code {
|
||||||
|
@ -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; }
|
pre.numberSource { margin-left: 3em; border-left: 1px solid #7a7c7d; padding-left: 4px; }
|
||||||
div.sourceCode
|
div.sourceCode
|
||||||
/* { color: #cfcfc2; background-color: #232629; } */
|
/* { color: #cfcfc2; background-color: #232629; } */
|
||||||
{ background: #282a36; color: #f8f8f2; }
|
/* { background: #282a36; color: #f8f8f2; } */
|
||||||
@media screen {
|
@media screen {
|
||||||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||||||
}
|
}
|
||||||
|
@ -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; }
|
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; }
|
span.linenos.special { color: #50fa7b; background-color: #6272a4; padding-left: 5px; padding-right: 5px; }
|
||||||
.highlight .hll { background-color: #44475a }
|
.highlight .hll { background-color: #44475a }
|
||||||
.highlight { background: #282a36; color: #f8f8f2 }
|
/* .highlight { background: #282a36; color: #f8f8f2 } */
|
||||||
.highlight .py-c { color: #6272a4 } /* Comment */
|
.highlight .py-c { color: #6272a4 } /* Comment */
|
||||||
.highlight .py-err { color: #f8f8f2 } /* Error */
|
.highlight .py-err { color: #f8f8f2 } /* Error */
|
||||||
.highlight .py-g { color: #f8f8f2 } /* Generic */
|
.highlight .py-g { color: #f8f8f2 } /* Generic */
|
||||||
|
43
pygments.lua
43
pygments.lua
@ -1,44 +1,33 @@
|
|||||||
return {
|
return {
|
||||||
CodeBlock = function(block)
|
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()
|
local fname = os.tmpname()
|
||||||
do
|
do
|
||||||
local _with_0 = io.open(fname, "w")
|
local _with_0 = io.open(fname, "w")
|
||||||
_with_0:write(block.text)
|
_with_0:write(block.text)
|
||||||
_with_0:close()
|
_with_0:close()
|
||||||
end
|
end
|
||||||
local cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
|
local _exp_0 = label
|
||||||
if block.classes[1] == "moon" or block.classes[1] == "moonscript" then
|
if "moon" == _exp_0 or "moonscript" == _exp_0 then
|
||||||
local p = io.popen((cmd .. " -x"):format("moonscript.py", fname))
|
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()
|
p:close()
|
||||||
|
out = _with_0
|
||||||
|
end
|
||||||
return pandoc.RawBlock("html", out)
|
return pandoc.RawBlock("html", out)
|
||||||
else
|
else
|
||||||
if block.classes[1] == nil or block.classes[1] == "heex" then
|
local status, handle = pcall(function()
|
||||||
if block.classes[1] == nil then
|
return io.popen(cmd:format(label or "text", fname))
|
||||||
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))
|
|
||||||
end)
|
end)
|
||||||
local sout = handle_or_error:read("*a")
|
local out = handle:read("*a")
|
||||||
if sout:len() == 0 then
|
if out:len() == 0 then
|
||||||
local cb = pandoc.CodeBlock(block.text, {
|
return block
|
||||||
class = "sourceCode"
|
|
||||||
})
|
|
||||||
return pandoc.Div(cb, {
|
|
||||||
class = "sourceCode"
|
|
||||||
})
|
|
||||||
else
|
else
|
||||||
return pandoc.RawBlock("html", sout)
|
return pandoc.RawBlock("html", out)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,30 +1,26 @@
|
|||||||
CodeBlock: (block) ->
|
CodeBlock: (block) ->
|
||||||
|
{ label } = block.classes
|
||||||
|
cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
|
||||||
fname = os.tmpname!
|
fname = os.tmpname!
|
||||||
|
|
||||||
with io.open fname, "w"
|
with io.open fname, "w"
|
||||||
\write block.text
|
\write block.text
|
||||||
\close!
|
\close!
|
||||||
|
|
||||||
cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
|
switch label
|
||||||
|
when "moon", "moonscript"
|
||||||
if block.classes[1] == "moon" or block.classes[1] == "moonscript"
|
|
||||||
p = io.popen (cmd .. " -x")\format "moonscript.py", fname
|
p = io.popen (cmd .. " -x")\format "moonscript.py", fname
|
||||||
out = p\read"*a"
|
out = with p\read"*a"
|
||||||
p\close!
|
p\close!
|
||||||
|
-- syntax highlight with pygments
|
||||||
pandoc.RawBlock "html", out
|
pandoc.RawBlock "html", out
|
||||||
else
|
else
|
||||||
if block.classes[1] == nil or block.classes[1] == "heex"
|
status, handle = pcall -> io.popen cmd\format label or "text", fname
|
||||||
if block.classes[1] == nil
|
out = handle\read"*a"
|
||||||
cb = pandoc.CodeBlock(block.text, class: "sourceCode")
|
|
||||||
pandoc.Div cb, class: "sourceCode"
|
if out\len! == 0
|
||||||
else
|
-- syntax highlight with pandoc
|
||||||
block
|
block
|
||||||
else
|
else
|
||||||
status, handle_or_error = pcall ->
|
-- syntax highlight with pygments
|
||||||
io.popen (cmd)\format block.classes[1], fname
|
pandoc.RawBlock "html", out
|
||||||
|
|
||||||
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
|
|
||||||
|
@ -67,7 +67,7 @@ $hello{[[
|
|||||||
this code block has an unknown label
|
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", ->
|
it "doesn't highlight unlabeled code blocks", ->
|
||||||
out = flatten_html render [[
|
out = flatten_html render [[
|
||||||
@ -75,7 +75,8 @@ this code block has an unknown label
|
|||||||
this code block has no 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", ->
|
it "defaults to pandoc highlighting when pygments fails to recognize code language", ->
|
||||||
out = flatten_html render [[
|
out = flatten_html render [[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user