Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions sitegen/dispatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ do
local callbacks = self.callbacks
for _index_0 = 1, #parts do
local p = parts[_index_0]
local _update_0 = p
callbacks[_update_0] = callbacks[_update_0] or { }
callbacks[p] = callbacks[p] or { }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this is about. It changed to this after I ran moonc sitegen, so I just left it in.

callbacks = callbacks[p]
end
return table.insert(callbacks, callback)
Expand Down
14 changes: 12 additions & 2 deletions sitegen/plugins/pygments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ do
local _base_0 = {
custom_highlighters = { },
disable_indent_detect = false,
ignore_missing_lexer = true,
highlight = function(self, lang, code)
local fname = os.tmpname()
local fname, errfname = os.tmpname(), os.tmpname()
do
local _with_0 = io.open(fname, "w")
_with_0:write(code)
_with_0:close()
end
local p = io.popen(("pygmentize -f html -l %s %s"):format(lang, fname))
local p = io.popen(("pygmentize -f html -l %s %s 2>%s"):format(lang, fname, errfname))
local out = p:read("*a")
local e = io.open(errfname, "r")
local errout = e:read("*a")
e:close()
if self.ignore_missing_lexer and errout:match("Error: no lexer for alias") then
if self.site then
self.site.logger:warn("Failed to find syntax highlighter for: " .. tostring(lang))
end
return html.escape(code)
end
return assert(out:match('^<div class="highlight"><pre>(.-)\n?</pre></div>'), "Failed to parse pygmentize result, is pygments installed?")
end,
_highlight = function(self, lang, code, page)
Expand Down
15 changes: 13 additions & 2 deletions sitegen/plugins/pygments.moon
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@ import Plugin from require "sitegen.plugin"
class PygmentsPlugin extends Plugin
custom_highlighters: {}
disable_indent_detect: false
ignore_missing_lexer: true

-- highlight code with pygments
highlight: (lang, code) =>
fname = os.tmpname!
fname, errfname = os.tmpname!, os.tmpname!
with io.open fname, "w"
\write code
\close!

p = io.popen ("pygmentize -f html -l %s %s")\format lang, fname
p = io.popen ("pygmentize -f html -l %s %s 2>%s")\format lang, fname, errfname
out = p\read"*a"

e = io.open errfname, "r"
errout = e\read"*a"
e\close!

if @ignore_missing_lexer and errout\match "Error: no lexer for alias"
if @site
@site.logger\warn "Failed to find syntax highlighter for: #{lang}"

return html.escape code

-- 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?"
Expand Down
48 changes: 48 additions & 0 deletions spec/plugins_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,51 @@ $hello{[[
print thing
```
]]}]=], out


describe "sitegen.plugins.pygments", ->
local site, py

before_each ->
site = factory.Site {}
py = require("sitegen.plugins.pygments")

it "syntax highlights some code", ->
plugin = py site
out = flatten_html plugin\filter [[
```lua
print("hello world")
```]]

assert.same [[<pre class="highlight lang_lua"><code><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;hello world&quot;</span><span class="p">)</span></code></pre>]], out

it "doesnt highlight code inside of a cosmo template", ->
plugin = py site
out = flatten_html plugin\filter [=[
```lua
print 5
```

$hello{[[
```lua
print thing
```
]]}]=]

assert.same [=[<pre class="highlight lang_lua"><code><span></span><span class="nb">print</span><span class="mi">5</span></code></pre>

$hello{[[
```lua
print thing
```
]]}]=], out

it "doesn't highlight unrecognized code languages", ->
plugin = py site
out = flatten_html plugin\filter [[
```snickerdoodle
this code block has an unknown label
```]]

assert.same [[<pre class="highlight lang_snickerdoodle"><code>this code block has an unknown label
</code></pre>]], out