require 'rexml/document' class String # escapes special characters def to_latex s = self # We escape '{' before, so we can use it in convert_entities s = s.gsub(/([\{\}])/) { |m| "\\#{m}" } # better yet, we should use the catcode for {} s = String.convert_entities(s) s = s.gsub(/([\$\&\%\#\_])/) { |m| "\\#{m}" } OtherGoodies.each do |k, v| s.gsub!(k, v) end s end # other things that are good on the eyes OtherGoodies = { 'LaTeX' => '\\LaTeX', # XXX not if already \latex 'HTML' => '\\textsc{html}', 'PDF' => '\\textsc{pdf}' } def String.convert_entities(s) init_tables r = s.gsub(/&([\w\d]+);/) do |match| replace = @@entity_to_latex[match] if replace # puts "#{match} -> #{replace}" replace else $stderr.puts "Cannot translate #{match}" match end end end include REXML # create hash @@entity_to_latex def String.init_tables ## why can't I use a class variable? if not $conversion_table_inited doc = Document.new XML_TABLE @@entity_to_latex = {} doc.elements.each("//char") do |c| num = c.attributes['num'] name = c.attributes['name'] convert = c.attributes['convertTo'] # add space after commands if convert[0,1] == "\\" then convert += " " end convert.gsub!(/^\$/, "\\begin{math}") convert.gsub!(/\$$/, "\\end{math}") @@entity_to_latex["&#{num};"] = convert @@entity_to_latex["&#{name};"] = convert end # puts @@entity_to_latex.inspect $conversion_table_inited = true end end # The following is a conversion chart for html elements, courtesy of ?? XML_TABLE =< EOF end