Module: ReVIEW::LaTeXUtils

Included in:
LATEXBuilder
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}',
}
METACHARS_RE =
/[#{Regexp.escape(METACHARS.keys.join(''))}]/u
METACHARS_INVERT =
METACHARS.invert

Instance Method Summary collapse

Instance Method Details

#escape_index(str) ⇒ Object



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

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

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



58
59
60
61
62
# File 'lib/review/latexutils.rb', line 58

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

#escape_url(str) ⇒ Object



77
78
79
# File 'lib/review/latexutils.rb', line 77

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

#macro(name, *args) ⇒ Object



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

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

#unescape_latex(str) ⇒ Object



66
67
68
69
70
71
# File 'lib/review/latexutils.rb', line 66

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