to_ascii_latex 0.0.19 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -7,3 +7,4 @@ source 'https://rubygems.org'
7
7
  gemspec
8
8
 
9
9
  gem 'to_latex'
10
+ gem 'nokogiri'
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -1,3 +1,3 @@
1
1
  module ToAsciiLatex
2
- VERSION = "0.0.19"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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 = { "\u2014" => "--",
28
- "\u2018" => "`",
29
- "\u2019" => "'",
30
- "\u201E" => "``",
31
- "\u201C" => "``",
32
- "\u201D" => "''",
33
- "\u00A0" => ' ',
34
- "\u00E6" => '\ae{}',
35
- "\u00E0" => '\\\\`{a}',
36
- "\u0097" => '--',
37
- "\u0092" => "'",
38
- "\u2026" => '{}\ldots{}',
39
- "\uFB01" => 'fi',
40
- "\uFB00" => 'ff',
41
- "\u2013" => '-',
42
- "\u25A0" => '\ensuremath{\blacksquare}',
43
- "\u2022" => '\ensuremath{\bullet}',
44
- "\u2248" => '\ensuremath{\approx}',
45
- "\u0B0C" => '?',
46
- "\u00B0" => '\ensuremath{^{\circ}}',
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 = old_escape(s).gsub(MAPPING_RE){|c| MAPPING[c]}.gsub('\backslash{}', '\ensuremath{\backslash}')
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