Skip to content

Commit

Permalink
google charts math tag fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
nricciar committed Mar 1, 2012
1 parent cf2d737 commit 562da3b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/wikicloth/extensions/math.rb
Expand Up @@ -7,25 +7,30 @@ class MathExtension < Extension
blahtex_png_path = @options[:blahtex_png_path] || '/tmp'
blahtex_options = @options[:blahtex_options] || '--texvc-compatible-commands --mathml-version-1-fonts --disallow-plane-1 --spacing strict'

if File.exists?(blahtex_path)
if File.exists?(blahtex_path) && @options[:math_formatter] != :google
begin
# pass tex markup to blahtex
response = `echo '#{buffer.element_content}' | #{blahtex_path} #{blahtex_options} --png --mathml --png-directory #{blahtex_png_path}`
xml_response = REXML::Document.new(response).root

if @options[:blahtex_html_prefix]
# render as embedded image
file_md5 = xml_response.elements["png/md5"].text
return "<img src=\"#{File.join(@options[:blahtex_html_prefix],"#{file_md5}.png")}\" />"
else
# render as mathml
html = xml_response.elements["mathml/markup"].text
return "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">#{xml_response.elements["mathml/markup"].children.to_s}</math>"
end
rescue => err
# blahtex error
return "<span class=\"error\">#{I18n.t("unable to parse mathml", :error => err)}</span>"
end
else
return "<span class=\"error\">#{I18n.t("blahtex binary not found", :path => blahtex_path)}</span>"
# if blahtex does not exist fallback to google charts api
encoded_string = URI.escape(buffer.element_content, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
return "<img src=\"https://chart.googleapis.com/chart?cht=tx&chl=#{encoded_string}\" />"
end

end

end
Expand Down

0 comments on commit 562da3b

Please sign in to comment.