Sync to lastest Maruku. Tweak to CSS stylesheet.

This commit is contained in:
Jacques Distler 2007-01-22 11:34:51 -06:00
parent b19e1e4f47
commit ceb0931bb3
15 changed files with 290 additions and 98 deletions

View file

@ -0,0 +1,108 @@
require 'tempfile'
require 'fileutils'
require 'digest/md5'
require 'pstore'
module MaRuKu; module Out; module HTML
PNG = Struct.new(:src,:depth,:height)
def convert_to_png_blahtex(kind, tex)
begin
FileUtils::mkdir_p MaRuKu::Globals[:html_png_dir]
# first, we check whether this image has already been processed
md5sum = Digest::MD5.hexdigest(tex+" params: ")
result_file = File.join(MaRuKu::Globals[:html_png_dir], md5sum+".txt")
if not File.exists?(result_file)
tmp_in = Tempfile.new('maruku_blahtex')
f = tmp_in.open
f.write tex
f.close
resolution = get_setting(:html_png_resolution)
options = "--png --use-preview-package --shell-dvipng 'dvipng -D #{resolution}' "
options += ("--png-directory '%s'" % MaRuKu::Globals[:html_png_dir])
cmd = "blahtex #{options} < #{tmp_in.path} > #{result_file}"
$stderr.puts "$ #{cmd}"
system cmd
tmp_in.delete
end
result = nil
f = File.open(result_file)
result = f.read
f.close
doc = Document.new(result, {:respect_whitespace =>:all})
png = doc.root.elements[1]
if png.name != 'png'
maruku_error "Blahtex error: \n#{doc}"
return nil
end
depth = png.elements['depth'] || (raise "No depth element in:\n #{doc}")
height = png.elements['height'] || (raise "No height element in:\n #{doc}")
md5 = png.elements['md5'] || (raise "No md5 element in:\n #{doc}")
depth = depth.text.to_f
height = height.text.to_f # XXX check != 0
md5 = md5.text
dir_url = MaRuKu::Globals[:html_png_url]
return PNG.new("#{dir_url}#{md5}.png", depth, height)
rescue Exception => e
maruku_error "Error: #{e}"
end
nil
end
BlahtexCache = PStore.new("blahtex_cache.pstore")
def convert_to_mathml_blahtex(kind, tex)
begin
BlahtexCache.transaction do
if BlahtexCache[tex].nil?
tmp_in = Tempfile.new('maruku_blahtex')
f = tmp_in.open
f.write tex
f.close
tmp_out = Tempfile.new('maruku_blahtex')
options = "--mathml"
cmd = "blahtex #{options} < #{tmp_in.path} > #{tmp_out.path}"
$stderr.puts "$ #{cmd}"
system cmd
tmp_in.delete
result = nil
File.open(tmp_out.path) do |f| result=f.read end
puts result
BlahtexCache[tex] = result
end
blahtex = BlahtexCache[tex]
doc = Document.new(blahtex, {:respect_whitespace =>:all})
mathml = doc.root.elements['mathml']
if not mathml
maruku_error "Blahtex error: \n#{doc}"
return nil
else
return mathml
end
end
rescue Exception => e
maruku_error "Error: #{e}"
end
nil
end
end end end

View file

@ -1,14 +1,16 @@
module MaRuKu; module Out; module HTML
def convert_to_mathml_itex2mml(tex, method)
def convert_to_mathml_itex2mml(kind, tex)
begin
if not $itex2mml_parser
require 'itextomml'
$itex2mml_parser = Itex2MML::Parser.new
end
mathml = $itex2mml_parser.send(method, tex)
itex_method = {:equation=>:block_filter,:inline=>:inline_filter}
mathml = $itex2mml_parser.send(itex_method[kind], tex)
doc = Document.new(mathml, {:respect_whitespace =>:all}).root
return doc
rescue LoadError => e
@ -23,13 +25,5 @@ module MaRuKu; module Out; module HTML
end
nil
end
def to_html_inline_math_itex2mml
convert_to_mathml_itex2mml(self.math, :inline_filter)
end
def to_html_equation_itex2mml
convert_to_mathml_itex2mml(self.math, :block_filter)
end
end end end

View file

@ -1,20 +1,20 @@
module MaRuKu; module Out; module HTML
def to_html_inline_math_none
def convert_to_mathml_none(kind, tex)
# You can: either return a REXML::Element
# return Element.new 'div'
# or return an empty array on error
# return []
# or have a string parsed by REXML:
tex = self.math
tex.gsub!('&','&amp;')
tex = tex.gsub('&','&amp;')
mathml = "<code>#{tex}</code>"
return Document.new(mathml).root
end
def to_html_equation_none
return to_html_inline_math_none
def convert_to_png_none(kind, tex)
return nil
end
end end end

View file

@ -1,5 +1,6 @@
module MaRuKu; module Out; module HTML
def convert_to_mathml_ritex(tex)
def convert_to_mathml_ritex(kind, tex)
begin
if not $ritex_parser
require 'ritex'
@ -20,15 +21,4 @@ module MaRuKu; module Out; module HTML
nil
end
def to_html_inline_math_ritex
tex = self.math
mathml = convert_to_mathml_ritex(tex)
return mathml || []
end
def to_html_equation_ritex
tex = self.math
mathml = convert_to_mathml_ritex(tex)
return mathml || []
end
end end end