class UltraVioletFilter < Nanoc3::Filter identifier :code def protect(str) str.gsub(%r{<([^>]*)>}) do "<#{$1}>" end end def run(content, params={}) require 'rubygems' code_rule = %r{(\n?(.+?))}m new_content=content.gsub(code_rule) do |full| @full, @lang, @filename, @code = $1, $2, $4, $5 @codeprefix='' if not @filename.nil? and @filename != "" create_file_for_code end @codeprefix+"\n\n
"+protect(@code)+"
\n\n" end return new_content end private def create_file_for_code # Create a plaintext file version for download. webpath = @item.path url = webpath + 'code/' + @filename @codeprefix=%{
➥ #{@filename}
\n} code_path = "output#{webpath}code" FileUtils.mkdir_p code_path File.open(%{#{code_path}/#{@filename}}, 'w'){|f|f.write(@code)} end end