2010-03-12 13:22:16 +00:00
|
|
|
class UltraVioletFilter < Nanoc3::Filter
|
|
|
|
identifier :ultraviolet
|
2010-09-21 14:52:25 +00:00
|
|
|
|
2010-03-12 13:22:16 +00:00
|
|
|
def run(content, params={})
|
|
|
|
require 'rubygems'
|
|
|
|
require 'uv'
|
2010-03-12 14:37:42 +00:00
|
|
|
code_rule = %r{(<code class="([^"]+?)"( file="([^"]+?)")?>(.+?)</code>)}m
|
2010-09-21 14:52:25 +00:00
|
|
|
new_content=content.gsub(code_rule) do |full|
|
2010-09-21 19:13:12 +00:00
|
|
|
@lang, @filename, @code = $2, $4, $5
|
|
|
|
if @lang =~ /^(zsh|bash|sh|csh|shell)$/
|
|
|
|
@lang='shell-unix-generic'
|
2010-03-12 13:22:16 +00:00
|
|
|
end
|
2010-09-21 19:13:12 +00:00
|
|
|
@codeprefix=''
|
|
|
|
@codesuffix=''
|
|
|
|
if not @filename.nil? and @filename != ""
|
|
|
|
create_file_for_code
|
|
|
|
end
|
|
|
|
@codeprefix+
|
|
|
|
Uv.parse(@code, "xhtml", @lang, false, @config[:ultraviolet_theme])+
|
|
|
|
@codesuffix
|
2010-09-21 14:52:25 +00:00
|
|
|
end
|
|
|
|
return new_content
|
2010-03-12 13:22:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2010-09-21 19:13:12 +00:00
|
|
|
def create_file_for_code
|
|
|
|
# Create a plaintext file version for download.
|
|
|
|
webpath = @item.path
|
|
|
|
|
|
|
|
url = webpath + 'code/' + @filename
|
|
|
|
@codeprefix=%{<div class="code"><div class="file"><a href="#{url}"> ➥ #{@filename} </a></div><div class="withfile">\n}
|
|
|
|
@codesuffix=%{\n</div></div>}
|
|
|
|
|
|
|
|
code_path = "output#{webpath}code"
|
|
|
|
puts %{\t\tEcriture de #{code_path}/#{@filename}}
|
|
|
|
FileUtils.mkdir_p code_path
|
|
|
|
File.open(%{#{code_path}/#{@filename}}, 'w'){|f|f.write(@code)}
|
2010-03-12 13:22:16 +00:00
|
|
|
end
|
2010-09-21 19:13:12 +00:00
|
|
|
|
2010-03-12 13:22:16 +00:00
|
|
|
end
|