Module: ReVIEW::LaTeXUtils

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

Instance Method Summary collapse

Instance Method Details

#escape_index(str) ⇒ Object



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

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

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



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

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

#escape_url(str) ⇒ Object



92
93
94
# File 'lib/review/latexutils.rb', line 92

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

#initialize_metachars(texcommand) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/review/latexutils.rb', line 18

def initialize_metachars(texcommand)
  @metachars = {
    '#' => '\#',
    "$" => '\textdollar{}',
    '%' => '\%',
    '&' => '\&',
    '{' => '\{',
    '}' => '\}',
    '_' => '\textunderscore{}',
    '^' => '\textasciicircum{}',
    '~' => '\textasciitilde{}',
    '|' => '\textbar{}',
    '<' => '\textless{}',
    '>' => '\textgreater{}',
    "\\" => '\reviewbackslash{}',
    "-" => '{-}'
  }

  if File.basename(texcommand, ".*") == "platex"
    @metachars.merge!({
                       '' => '\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}'
                     })

    kanalist = %w{                   
                            ソ                
                                 }
    kanalist.each do |char|
      char_jisx0208 = NKF::nkf('-WwX',char)
      @metachars[char] = "\\aj半角{#{char_jisx0208}}"
    end
  end

  @metachars_re = /[#{Regexp.escape(@metachars.keys.join(''))}]/u

  @metachars_invert = @metachars.invert
end

#macro(name, *args) ⇒ Object



96
97
98
# File 'lib/review/latexutils.rb', line 96

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

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



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

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