Class Webgen::Tag::TikZ
In: lib/webgen/tag/tikz.rb
Parent: Object
Error RenderError CommandNotFoundError LoadError NodeCreationError ::Rake::TaskLib WebgenTask Node Context\n[lib/webgen/context.rb\nlib/webgen/context/nodes.rb\nlib/webgen/context/render.rb\nlib/webgen/context/tags.rb] Tree FileSystem Sitemap Feed Copy Virtual Sitemap Directory Page Fragment Template Memory Metainfo Coderay Sitemap IncludeFile BreadcrumbTrail Langbar TikZ Menu Tags Fragments Resource Website Tidy Head Less Kramdown Xmllint Blocks Helpers Configuration Comparable Language Path StandardError CmdParse::CommandParser CommandParser CmdParse::Command RunCommand CreateCommand WebguiCommand ApplyCommand WebsiteAccess Main Loggable OutputPathHelpers ExecuteCommand Link Date Relocatable Metainfo ::Kramdown::Converter::Html KramdownHtmlConverter Cache Blackboard WebsiteManager Logger Page ProxyNode Utils Scss RDoc Sass Erb RDiscount Erubis Haml Maruku Builder RedCloth AccessHash TarArchive Stacked FileSystem lib/webgen/cache.rb lib/webgen/error.rb lib/webgen/languages.rb lib/webgen/website.rb lib/webgen/blackboard.rb lib/webgen/tree.rb lib/webgen/websitemanager.rb lib/webgen/logger.rb lib/webgen/context/tags.rb lib/webgen/configuration.rb lib/webgen/path.rb lib/webgen/webgentask.rb lib/webgen/page.rb lib/webgen/node.rb lib/webgen/cli/run_command.rb lib/webgen/cli/utils.rb lib/webgen/cli/apply_command.rb lib/webgen/cli/webgui_command.rb lib/webgen/cli.rb lib/webgen/cli/create_command.rb Color CLI ClassMethods WebsiteAccess LanguageManager lib/webgen/output/filesystem.rb Output lib/webgen/common/sitemap.rb Common lib/webgen/sourcehandler/metainfo.rb lib/webgen/sourcehandler/memory.rb lib/webgen/sourcehandler/copy.rb lib/webgen/sourcehandler/directory.rb lib/webgen/sourcehandler.rb lib/webgen/sourcehandler/page.rb lib/webgen/sourcehandler/template.rb lib/webgen/sourcehandler/fragment.rb lib/webgen/sourcehandler/sitemap.rb lib/webgen/sourcehandler/virtual.rb lib/webgen/sourcehandler/feed.rb OutputPathHelpers Base SourceHandler lib/webgen/tag/coderay.rb lib/webgen/tag/relocatable.rb lib/webgen/tag/menu.rb lib/webgen/tag/langbar.rb lib/webgen/tag/executecommand.rb lib/webgen/tag/breadcrumbtrail.rb lib/webgen/tag/metainfo.rb lib/webgen/tag/link.rb lib/webgen/tag/includefile.rb lib/webgen/tag/date.rb lib/webgen/tag/tikz.rb lib/webgen/tag/sitemap.rb Base Tag lib/webgen/contentprocessor/less.rb lib/webgen/contentprocessor/scss.rb lib/webgen/contentprocessor/blocks.rb lib/webgen/contentprocessor/rdoc.rb lib/webgen/contentprocessor/sass.rb lib/webgen/contentprocessor/erb.rb lib/webgen/contentprocessor/rdiscount.rb lib/webgen/contentprocessor/tags.rb lib/webgen/contentprocessor/erubis.rb lib/webgen/contentprocessor/kramdown/html.rb lib/webgen/contentprocessor/haml.rb lib/webgen/contentprocessor/maruku.rb lib/webgen/contentprocessor/xmllint.rb lib/webgen/contentprocessor/kramdown.rb lib/webgen/contentprocessor/head.rb lib/webgen/contentprocessor/builder.rb lib/webgen/contentprocessor/tidy.rb lib/webgen/contentprocessor/redcloth.rb lib/webgen/contentprocessor/fragments.rb lib/webgen/contentprocessor.rb ContentProcessor lib/webgen/source/tararchive.rb lib/webgen/source/stacked.rb lib/webgen/source/resource.rb lib/webgen/source/filesystem.rb Source Loggable Webgen dot/m_81_0.png

This tag allows the creation and inclusion of complex graphics using the PGF/TikZ library of LaTeX. You will need a current LaTeX distribution and the convert utility from ImageMagick.

Methods

call   compile   run_command  

Included Modules

Webgen::Tag::Base Webgen::WebsiteAccess

Constants

LATEX_TEMPLATE = <<EOF \\nonstopmode \\documentclass{article} \\usepackage{tikz} \\pagestyle{empty} <% if param('tag.tikz.libraries') %> \\usetikzlibrary{<%= param('tag.tikz.libraries').join(',') %>} <% end %> \\begin{document} <% if param('tag.tikz.opts') %> \\begin{tikzpicture}[<%= param('tag.tikz.opts') %>] <% else %> \\begin{tikzpicture} <% end %> <%= body %> \\end{tikzpicture} \\end{document} EOF

Public Instance methods

Create a graphic from the commands in the body of the tag.

[Source]

    # File lib/webgen/tag/tikz.rb, line 34
34:     def call(tag, body, context)
35:       path = param('tag.tikz.path')
36:       path = Webgen::Path.make_absolute(context.ref_node.parent.alcn, path)
37: 
38:       mem_handler = website.cache.instance('Webgen::SourceHandler::Memory')
39:       src_path = context.ref_node.node_info[:src]
40:       parent = website.blackboard.invoke(:create_directories, context.ref_node.tree.root, File.dirname(path), src_path)
41:       params = @params
42: 
43:       node = website.blackboard.invoke(:create_nodes, Webgen::Path.new(path, src_path), mem_handler) do |node_path|
44:         mem_handler.create_node(node_path, context.ref_node.alcn) do |pic_node|
45:           set_params(params)
46:           document = ERB.new(LATEX_TEMPLATE).result(binding)
47:           pic_path = compile(document, File.extname(path), context)
48:           set_params(nil)
49:           if pic_path
50:             io = Webgen::Path::SourceIO.new { File.open(pic_path, 'rb') }
51:           else
52:             pic_node.flag(:dirty)
53:             nil
54:           end
55:         end
56:       end.first
57:       attrs = {'alt' => ''}.merge(param('tag.tikz.img_attr')).collect {|name,value| "#{name.to_s}=\"#{value}\"" }.sort.unshift('').join(' ')
58:       "<img src=\"#{context.dest_node.route_to(node)}\"#{attrs} />"
59:     end

Private Instance methods

Compile the LaTeX document and convert the resulting PDF to the correct output image format specified by ext (the extension needs to include the dot).

[Source]

     # File lib/webgen/tag/tikz.rb, line 67
 67:     def compile(document, ext, context)
 68:       file = Tempfile.new('webgen-tikz')
 69:       file.write(document)
 70:       file.close
 71: 
 72:       FileUtils.mv(file.path, file.path + '.tex')
 73:       cmd_prefix = "cd #{File.dirname(file.path)}; "
 74:       output = `#{cmd_prefix} pdflatex --shell-escape -interaction=batchmode #{File.basename(file.path)}.tex`
 75:       if $?.exitstatus != 0
 76:         errors = output.scan(/^!(.*\n.*)/).join("\n")
 77:         raise Webgen::RenderError.new("Error while creating a TikZ picture: #{errors}",
 78:                                       self.class.name, context.dest_node, context.ref_node)
 79:       else
 80:         cmd = cmd_prefix + "pdfcrop #{File.basename(file.path)}.pdf #{File.basename(file.path)}.pdf; "
 81:         return unless run_command(cmd, context)
 82: 
 83:         render_res, output_res = param('tag.tikz.resolution').split(' ')
 84:         if param('tag.tikz.transparent') && ext =~ /\.png/i
 85:           cmd = cmd_prefix +
 86:             "gs -dSAFER -dBATCH -dNOPAUSE -r#{render_res} -sDEVICE=pngalpha -dGraphicsAlphaBits=4 -dTextAlphaBits=4 " +
 87:             "-sOutputFile=#{File.basename(file.path)}#{ext} #{File.basename(file.path)}.pdf"
 88:         else
 89:           cmd = cmd_prefix + "convert -density #{render_res} #{File.basename(file.path)}.pdf #{File.basename(file.path)}#{ext}"
 90:         end
 91:         return unless run_command(cmd, context)
 92: 
 93:         if render_res != output_res
 94:           cmd = cmd_prefix + "identify #{File.basename(file.path)}#{ext}"
 95:           return unless (output = run_command(cmd, context))
 96:           width, height = output.scan(/\s\d+x\d+\s/).first.strip.split('x').collect {|s| s.to_f * output_res.to_f / render_res.to_f }
 97:           cmd = cmd_prefix + "convert -resize #{width}x#{height} #{File.basename(file.path)}#{ext} #{File.basename(file.path)}#{ext}"
 98:           return unless run_command(cmd, context)
 99:         end
100:         file.path + ext
101:       end
102:     end

Runs the command cmd and returns it‘s output if successful or nil otherwise.

[Source]

     # File lib/webgen/tag/tikz.rb, line 105
105:     def run_command(cmd, context)
106:       output = `#{cmd}`
107:       if $?.exitstatus != 0
108:         raise Webgen::RenderError.new("Error while running a command for a TikZ picture: #{output}",
109:                                       self.class.name, context.dest_node, context.ref_node)
110:       else
111:         output
112:       end
113:     end

[Validate]