44 lines
1.2 KiB
Ruby
44 lines
1.2 KiB
Ruby
|
module Nanoc
|
||
|
class PygmentizerFilter < Nanoc::Filter
|
||
|
identifier :pygmentizer
|
||
|
type :text
|
||
|
|
||
|
def run(content, params = {})
|
||
|
doc = Nokogiri::HTML::fragment(content, 'UTF-8')
|
||
|
nodes = doc.search("pre>code")
|
||
|
|
||
|
nodes.each do |node|
|
||
|
code = node.inner_html || "[++where is the code?++]"
|
||
|
node.parent.swap(pygmentize(code))
|
||
|
end
|
||
|
|
||
|
doc.to_html
|
||
|
end
|
||
|
|
||
|
private
|
||
|
def pygmentize(string, pattern = /\A:::(\w+)\s*(\n|
)/i)
|
||
|
refs = pattern.match(string) # extract language name
|
||
|
if refs # found a language identifier!
|
||
|
lang = refs[1]
|
||
|
str = unescape_html(string.sub(pattern, ""))
|
||
|
code = ::Pygments.highlight(str, options: {
|
||
|
encoding: 'utf-8',
|
||
|
linenos: 'table',
|
||
|
lexer: lexers.include?(lang) ? lang : nil
|
||
|
})
|
||
|
"<div class='code'>#{code}</div>"
|
||
|
else # No language identifer,
|
||
|
"<pre class='plaincode'>#{string}</pre>"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
private
|
||
|
def lexers
|
||
|
@lexers ||= Pygments.lexers.map { |lex| lex[1][:aliases] }.flatten
|
||
|
end
|
||
|
|
||
|
private
|
||
|
def unescape_html(string)
|
||
|
string.to_s.gsub(/
/i, "\n").gsub("<", '<').gsub(">", '>').gsub("&", '&')
|
||
|
end end
|
||
|
end
|