Module: ToAsciiLatex

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

Constant Summary collapse

NBSP =
"\u00A0"
MAPPING =
{}
RE_SPECIALS =
%w{. | ( ) [ ] { } \ ^ $ + * ?}
RE =
/#{MAPPING.keys.collect{|c| RE_SPECIALS.include?(c) ? "\\#{c}" : c}.join('|')}/
VERSION =
"0.1.1"
@@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



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
86
87
88
89
90
# File 'lib/to_ascii_latex.rb', line 57

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 = s.gsub(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



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

def self.replace_nbsp
  @@replace_nbsp
end

.replace_nbsp=(v) ⇒ Object



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

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

.smartquotesObject



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

def self.smartquotes
  @@smartquotes
end

.smartquotes=(v) ⇒ Object



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

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