Module: ToAsciiLatex

Defined in:
lib/to_ascii_latex.rb,
lib/to_ascii_latex/version.rb

Constant Summary collapse

NBSP =
"\u00A0"
MAPPING =
{ "\u2014"  => "--",
  "\u2018"  => "`",
  "\u2019"  => "'",
  "\u201E"  => "``",
  "\u201C"  => "``",
  "\u201D"  => "''",
  "\u00A0"  => ' ',
  "\u00E6"  => '\ae{}',
  "\u00E0"  => '\\\\`{a}',
  "\u0097"  => '--',
  "\u0092"  => "'",
  "\u2026"  => '{}\ldots{}',
  "\uFB01"  => 'fi',
  "\u2013"  => '-',
  "\u25A0"  => '\ensuremath{\blacksquare}',
  "\u2022"  => '\ensuremath{\bullet}',
  "\u2248"  => '\ensuremath{\approx}',
  "\u0B0C"  => '?',
  "\u00B0"  => '\ensuremath{^{\ccirc}}',
  "\u00A7"  => '\S{}',
  "\u20AC"  => '\euro{}',
  "\u00A2"  => '\textcent{}'
}
MAPPING_RE =
/#{MAPPING.keys.join('|')}/
VERSION =
"0.0.15"
@@smartquotes =
false
@@replace_nbsp =
false

Class Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Add a hook - whenever a class or module calls ‘extend ToAsciiLatex`, run this code



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/to_ascii_latex.rb', line 52

def self.extended(base)

  # In the context of the extending module or class 
  # (in this case, it will be ToLatex), do the following
  base.class_eval do

    # Define this new method
    def self.new_escape(s)
      s = s.gsub(NBSP, ' ') if @@replace_nbsp
      x = old_escape(s).gsub(MAPPING_RE){|c| MAPPING[c]}.gsub('\backslash{}', '\ensuremath{\backslash}')
      warn "Unicode in  #{x.inspect}" if x.delete("^\u{0000}-\u{007F}") != x

      if @@smartquotes
        i = 0
        x.gsub!('"'){|q| i += 1; (i % 2 == 0) ? "''" : "``"}
      end

      return x
    end

    # Set up aliases. 
    # We're already in the context of the class, but there's no
    # `self.alias`, so we need to call `alias` inside this block
    class << self

      # We can call the original `escape` method with `old_escape`
      alias :old_escape :escape

      # If we call `escape`, now we'll get our `new_escape` method
      alias :escape :new_escape
    end
  end

end

.replace_nbspObject



15
16
17
# File 'lib/to_ascii_latex.rb', line 15

def self.replace_nbsp
  @@replace_nbsp
end

.replace_nbsp=(v) ⇒ Object



18
19
20
21
# File 'lib/to_ascii_latex.rb', line 18

def self.replace_nbsp=(v)
  raise "Unexpected value #{v.inspect} for replace_nbsp" unless [true, false].include?(v)
  @@replace_nbsp = v
end

.smartquotesObject



8
9
10
# File 'lib/to_ascii_latex.rb', line 8

def self.smartquotes
  @@smartquotes
end

.smartquotes=(v) ⇒ Object



11
12
13
14
# File 'lib/to_ascii_latex.rb', line 11

def self.smartquotes=(v)
  raise "Unexpected value #{v.inspect} for smartquotes" unless [true, false].include?(v)
  @@smartquotes = v
end