Module: ReVIEW::LaTeXUtils

Included in:
LATEXBuilder, PDFMaker
Defined in:
lib/review/latexutils.rb

Constant Summary collapse

METACHARS =
{
  '#' => '\#',
  "$" => '\textdollar{}',
  '%' => '\%',
  '&' => '\&',
  '{' => '\{',
  '}' => '\}',
  '_' => '\textunderscore{}',
  '^' => '\textasciicircum{}',
  '~' => '\textasciitilde{}',
  '|' => '\textbar{}',
  '<' => '\textless{}',
  '>' => '\textgreater{}',
  "\\" => '\reviewbackslash{}',
  "-" => '{-}',

  '' => '\UTF{24EA}',
  '' => '\UTF{2460}',
  '' => '\UTF{2461}',
  '' => '\UTF{2462}',
  '' => '\UTF{2463}',
  '' => '\UTF{2464}',
  '' => '\UTF{2465}',
  '' => '\UTF{2466}',
  '' => '\UTF{2467}',
  '' => '\UTF{2468}',
  '' => '\UTF{2469}',
  '' => '\UTF{246A}',
  '' => '\UTF{246B}',
  '' => '\UTF{246C}',
  '' => '\UTF{246D}',
  '' => '\UTF{246E}',
  '' => '\UTF{246F}',
}
METACHARS_RE =
/[#{Regexp.escape(METACHARS.keys.join(''))}]/u
METACHARS_INVERT =
METACHARS.invert

Instance Method Summary collapse

Instance Method Details

#escape_index(str) ⇒ Object



81
82
83
# File 'lib/review/latexutils.rb', line 81

def escape_index(str)
  str.gsub(/[@!|"]/) {|s| '"' + s }
end

#escape_latex(str) ⇒ Object Also known as: escape



64
65
66
67
68
# File 'lib/review/latexutils.rb', line 64

def escape_latex(str)
  str.gsub(METACHARS_RE) {|s|
    METACHARS[s] or raise "unknown trans char: #{s}"
  }
end

#escape_url(str) ⇒ Object



85
86
87
# File 'lib/review/latexutils.rb', line 85

def escape_url(str)
  str.gsub(/[\#%]/) {|s| '\\'+s }
end

#macro(name, *args) ⇒ Object



89
90
91
# File 'lib/review/latexutils.rb', line 89

def macro(name, *args)
  "\\#{name}" + args.map {|a| "{#{a}}" }.join('')
end

#unescape_latex(str) ⇒ Object Also known as: unescape



72
73
74
75
76
77
# File 'lib/review/latexutils.rb', line 72

def unescape_latex(str)
  metachars_invert_re = Regexp.new(METACHARS_INVERT.keys.collect{|key| Regexp.escape(key)}.join('|'))
  str.gsub(metachars_invert_re) {|s|
    METACHARS_INVERT[s] or raise "unknown trans char: #{s}"
  }
end