require 'digest/sha1' module Bricks module Math module ViewMethods def math_asset(opts) content = opts[:content] node = opts[:node] if !(content =~ /^\s*\\begin\{(align|equation|itemize|equation)/) pre = '\[' post = '\]' else pre = post = '' end # FIXME: SECURITY LateX filtering: is this enough ? if content =~ /\\input/ return "#{content.gsub(/(\\input\w*)/, "#{$1} (not supported)")}" end if opts[:output] == 'latex' "#{pre}#{content}#{post}" elsif Zena::ENABLE_MATH # Create PNG image # 1. get image path math_id = Digest::SHA1.hexdigest(content)[0..4] filename = math_id + '.png' filepath = node.asset_path(filename) unless File.exist?(filepath) if !Bricks::CONFIG['math']['live'] && opts[:preview] # do not render image during preview tag = content =~ /\n/ ? 'pre' : 'span' return "<#{tag} class='math_preview'>#{content}" else # create image FileUtils::mkpath(File.dirname(filepath)) unless File.exist?(File.dirname(filepath)) begin tempf = Tempfile.new(filename) # TODO: do we need to close this file ? base = tempf.path latex_template = %q{ \documentclass[10pt]{article} \usepackage[utf8]{inputenc} \usepackage{amssymb} \usepackage{amsmath} \usepackage{amsfonts} \usepackage{ulem} % strikethrough (\sout{...}) \usepackage{hyperref} % links % shortcuts \DeclareMathOperator*{\argmin}{arg\,min} \newcommand{\ve}[1]{\boldsymbol{#1}} \newcommand{\ma}[1]{\boldsymbol{#1}} \newenvironment{m}{\begin{bmatrix}}{\end{bmatrix}} \pagestyle{empty} \begin{document} } File.open("#{base}.tex", 'wb') do |f| f.syswrite(latex_template) f.syswrite(pre) f.syswrite(content) f.syswrite(post) f.syswrite("\n\\end{document}\n") end ok, msg, err = Bricks::run(File.dirname(tempf.path), 'latex', '-interaction=batchmode', "#{base}.tex") if ok ok, msg, err = Bricks::run(nil, 'dvips', "#{tempf.path}.dvi", '-E', '-o', "#{base}.ps") end if ok ok, msg, err = Bricks::run(nil, 'convert', '-units', 'PixelsPerInch', '-density', '150', '-matte', '-fuzz', '10%', '-transparent', '#ffffff', "#{base}.ps", filepath) end if !ok Node.logger.error(err) Bricks::run(nil, 'cp', "#{Zena::ROOT}/public/world.png", filepath.inspect) end ensure system("rm -rf #{tempf.path.inspect} #{(tempf.path + '.*').inspect}") end end end "" else # Math not supported "[math]#{content}[/math]" end end end # ViewMethods end # Math end # Bricks