to_ascii_latex 0.0.19 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Rakefile +9 -0
- data/lib/to_ascii_latex/version.rb +1 -1
- data/lib/to_ascii_latex.rb +23 -29
- data/lib/unicode.xml +44303 -0
- data/test/test_ascii_latex.rb +9 -0
- data/to_ascii_latex.gemspec +1 -0
- metadata +23 -4
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/lib/to_ascii_latex.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "to_ascii_latex/version"
|
2
2
|
require 'to_latex'
|
3
|
+
require 'nokogiri'
|
3
4
|
|
4
5
|
module ToAsciiLatex
|
5
6
|
@@smartquotes = false
|
@@ -23,35 +24,28 @@ module ToAsciiLatex
|
|
23
24
|
# http://www.fileformat.info/info/unicode/char/fb01/index.htm
|
24
25
|
# http://www.w3.org/Math/characters/unicode.xml
|
25
26
|
# http://www.johndcook.com/unicode_latex.html
|
27
|
+
|
26
28
|
NBSP = "\u00A0"
|
27
|
-
MAPPING = {
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
"\u00A7" => '\S{}',
|
48
|
-
"\u20AC" => '\euro{}',
|
49
|
-
"\u00A2" => '\textcent{}',
|
50
|
-
"\u2227" => '\ensuremath{\land}',
|
51
|
-
"\u00AC" => '\ensuremath{\neg}',
|
52
|
-
"\u00F6" => '\"{o}',
|
53
|
-
}
|
54
|
-
MAPPING_RE = /#{MAPPING.keys.join('|')}/
|
29
|
+
MAPPING = {}
|
30
|
+
RE_SPECIALS = %w{. | ( ) [ ] { } \ ^ $ + * ?}
|
31
|
+
|
32
|
+
mapping = Nokogiri::XML(open(File.join(File.dirname(__FILE__), 'unicode.xml')))
|
33
|
+
mapping.xpath('//character[@id and @mode and latex]').each{|char|
|
34
|
+
id = char['id'].to_s
|
35
|
+
next if id =~ /^U[-0-9A-F]+$/ && id =~ /-/
|
36
|
+
raise "Unexpected char #{id.inspect}" unless id =~ /^U[0-9A-F]+$/i
|
37
|
+
id.gsub!(/^U/, '')
|
38
|
+
id.gsub!(/^0/, '') if id.size == 5 && id =~ /^0/
|
39
|
+
|
40
|
+
key = [id.gsub(/^U/i, '').hex].pack('U')
|
41
|
+
value = char.at('.//latex').inner_text.strip
|
42
|
+
next if key == value || value == "\\space"
|
43
|
+
|
44
|
+
value = "\\ensuremath{#{value}}" if char['mode'] == 'math'
|
45
|
+
value = "{#{value}}" if value !~ /^\\/ || value !~ /}$/
|
46
|
+
MAPPING[key] = value
|
47
|
+
}
|
48
|
+
RE = /#{MAPPING.keys.collect{|c| RE_SPECIALS.include?(c) ? "\\#{c}" : c}.join('|')}/
|
55
49
|
|
56
50
|
# Add a hook - whenever a class or module calls `extend ToAsciiLatex`,
|
57
51
|
# run this code
|
@@ -64,7 +58,7 @@ module ToAsciiLatex
|
|
64
58
|
# Define this new method
|
65
59
|
def self.new_escape(s)
|
66
60
|
s = s.gsub(NBSP, ' ') if @@replace_nbsp
|
67
|
-
x =
|
61
|
+
x = s.gsub(RE){|c| MAPPING[c]} # .gsub('\backslash{}', '\ensuremath{\backslash}')
|
68
62
|
warn "Unicode in #{x.inspect}" if x.delete("^\u{0000}-\u{007F}") != x
|
69
63
|
|
70
64
|
if @@smartquotes
|