texml 0.4.0 → 0.5.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.
Files changed (3) hide show
  1. data/bin/texml +24 -6
  2. data/lib/texml.rb +29 -44
  3. metadata +21 -6
data/bin/texml CHANGED
@@ -1,12 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'texml'
2
+ require 'optparse'
4
3
 
5
- xml = ARGF.read
6
4
  begin
7
- print TeXML.convert(xml)
8
- rescue XMLParserError
9
- puts "#{$0}: #{$!}"
5
+ require 'rubygems'
6
+ require 'texml'
7
+ rescue LoadError
8
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'texml.rb')
9
+ end
10
+
11
+ opts = OptionParser.new do |opts|
12
+ opts.banner = "texml: a TeXML to TeX converter"
13
+ opts.define_head "Usage: texml [options] <path>"
14
+ opts.separator ""
15
+ opts.separator "Options:"
16
+
17
+ opts.on("-h", "--help", "Show this message") do
18
+ puts opts
19
+ exit
20
+ end
21
+ end
22
+ opts.parse!
23
+
24
+ path = ARGV.shift
25
+ if path.to_s.strip.empty?
26
+ puts opts
10
27
  exit 1
11
28
  end
12
29
 
30
+ print TeXML.convert(IO.read(path))
data/lib/texml.rb CHANGED
@@ -1,23 +1,14 @@
1
- ########################################
2
- # texml.rb
3
- # Author: Pierre-Charles David (pcdavid@gmail.com)
4
- # Version: 0.4
5
- # Web page: http://github.com/pcdavid/ruby-texml
6
- # Depends on: xmlparser (available on RAA)
7
- # License: WTFPL: http://sam.zoy.org/wtfpl/COPYING
8
-
9
- # Based of Douglas Lovell's paper:
10
- # "TeXML: Typesetting with TeX", Douglas Lovell, IBM Research
11
- # in TUGboat, Volume 20 (1999), No. 3
12
- #
13
- # Original implementation in Java by D. Lovell, available on IBM
14
- # alphaWorks: http://www.alphaworks.ibm.com/tech/texml
15
- #
16
- # Usage: % texml.rb < input.xml > output.tex
17
-
18
- require "xmltreebuilder"
1
+ # -*- ruby -*-
2
+
3
+ require 'nokogiri'
19
4
 
20
5
  module TeXML
6
+ # Converts a TeXML document, passed as a raw XML string, into the
7
+ # corresponding (La)TeX document.
8
+ def TeXML.convert(xml)
9
+ document = Nokogiri::XML(xml)
10
+ TeXML::Node.create(document.root).to_tex
11
+ end
21
12
 
22
13
  # Escaping sequences for LaTeX special characters
23
14
  SPECIAL_CHAR_ESCAPES = {
@@ -36,14 +27,6 @@ module TeXML
36
27
  '\\'[0] => '$\\backslash${}'#'
37
28
  }
38
29
 
39
- # Converts a TeXML document, passed as a raw XML string, into the
40
- # corresponding (La)TeX document.
41
- def TeXML.convert(xml)
42
- builder = XML::SimpleTreeBuilder.new
43
- tree = builder.parse(xml)
44
- TeXML::Node.create(tree.documentElement).to_tex
45
- end
46
-
47
30
  # Given a raw string, returns a copy with all (La)TeX special
48
31
  # characters properly quoted.
49
32
  def TeXML.quote(str)
@@ -63,10 +46,12 @@ module TeXML
63
46
  # Creates a node handler object appropriate for the specified XML
64
47
  # node, based on the name of the node (uses information from
65
48
  # NODE_HANDLERS).
66
- def Node.create(domNode)
67
- handlerClass = NODE_HANDLERS[domNode.nodeName]
49
+ def Node.create(node)
50
+ kind = node.name
51
+ kind = '#text' if kind == 'text'
52
+ handlerClass = NODE_HANDLERS[kind]
68
53
  if !handlerClass.nil?
69
- handlerClass.new(domNode)
54
+ handlerClass.new(node)
70
55
  else
71
56
  nil
72
57
  end
@@ -81,8 +66,8 @@ module TeXML
81
66
  # of the children.
82
67
  def childrenValue(*childTypes)
83
68
  tex = ''
84
- @node.childNodes do |kid|
85
- if childTypes.include?(kid.nodeName)
69
+ @node.children.each do |kid|
70
+ if childTypes.include?(kid.name) || (kid.text? && childTypes.include?('#text'))
86
71
  node = Node.create(kid)
87
72
  tex << node.to_tex unless node.nil?
88
73
  end
@@ -103,9 +88,9 @@ module TeXML
103
88
  NODE_HANDLERS['cmd'] = CmdNode
104
89
 
105
90
  def to_tex
106
- name = @node.getAttribute('name')
107
- nl_before = (@node.getAttribute('nl1') == '1') ? "\n" : ''
108
- nl_after = (@node.getAttribute('nl2') == '1') ? "\n" : ''
91
+ name = @node['name']
92
+ nl_before = (@node['nl1'] == '1') ? "\n" : ''
93
+ nl_after = (@node['nl2'] == '1') ? "\n" : ''
109
94
  return nl_before + "\\#{name}" + childrenValue('opt') + childrenValue('parm') + ' ' + nl_after
110
95
  end
111
96
  end
@@ -114,10 +99,10 @@ module TeXML
114
99
  NODE_HANDLERS['env'] = EnvNode
115
100
 
116
101
  def to_tex
117
- name = @node.getAttribute('name')
118
- start = @node.getAttribute('begin')
102
+ name = @node['name']
103
+ start = @node['begin']
119
104
  start = 'begin' if start == ''
120
- stop = @node.getAttribute('end')
105
+ stop = @node['end']
121
106
  stop = 'end' if stop == ''
122
107
  return "\\#{start}{#{name}}\n" +
123
108
  childrenValue('cmd', 'env', 'ctrl', 'spec', '#text') +
@@ -145,9 +130,9 @@ module TeXML
145
130
  NODE_HANDLERS['ctrl'] = CtrlNode
146
131
 
147
132
  def to_tex
148
- ch = @node.getAttribute('ch')
133
+ ch = @node['ch']
149
134
  unless ch.nil?
150
- return ch & 0x9F # Control version of ch
135
+ return ch[0] & 0x9F # Control version of ch
151
136
  else
152
137
  nil
153
138
  end
@@ -179,7 +164,7 @@ module TeXML
179
164
  }
180
165
 
181
166
  def to_tex
182
- cat = @node.getAttribute('cat')
167
+ cat = @node['cat']
183
168
  return (SPECIAL_MAP[cat] or '')
184
169
  end
185
170
  end
@@ -188,11 +173,11 @@ module TeXML
188
173
  NODE_HANDLERS['#text'] = TextNode
189
174
 
190
175
  def to_tex
191
- parent = @node.parentNode
192
- if parent.nodeName == 'env' && parent.getAttribute('name') == 'verbatim'
193
- return @node.nodeValue # TODO: is there /some/ quoting to do?
176
+ parent = @node.parent
177
+ if parent.name == 'env' && parent['name'] == 'verbatim'
178
+ return @node.to_s
194
179
  else
195
- return TeXML.quote(@node.nodeValue)
180
+ return TeXML.quote(@node.to_s)
196
181
  end
197
182
  end
198
183
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: texml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pierre-Charles David
@@ -15,10 +15,25 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-21 00:00:00 +01:00
18
+ date: 2010-12-14 00:00:00 +01:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: nokogiri
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 1
34
+ version: 1.4.1
35
+ type: :runtime
36
+ version_requirements: *id001
22
37
  description: "This program converts an XML document conforming to the TeXML syntax into the corresponding (La)TeX document, ready to be typeset. It is based on Douglas Lovell's paper: \"TeXML: Typesetting with TeX\", Douglas Lovell, IBM Research in TUGboat, Volume 20 (1999), No. 3"
23
38
  email: pcdavid@gmail.com
24
39
  executables: