doctor_ninja 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ require "doctor_ninja/parsers/base"
2
+
3
+ class DoctorNinja::Parsers::Paragraph < DoctorNinja::Parsers::Base
4
+ @@style_map = {
5
+ /^T[í|i]?t(ulo|le)$/ => "h1",
6
+ /^Subt[í|i]?t(ulo|le)$/ => "h2",
7
+ // => "p"
8
+ }
9
+
10
+ def self.applicable_to?(node)
11
+ node.name == "p"
12
+ end
13
+
14
+ def parse
15
+ @context[:has_text] = false
16
+ content = parse_children
17
+ attrs = {}
18
+ style = {}
19
+ style["text-align"] = "center" unless @context[:has_text]
20
+
21
+ attrs["style"] = style.map{|k,v| "#{k}:#{v};"}.join(" ") if style.length > 0
22
+
23
+ attrs = attrs.map{|k,v| " #{k}=\"#{v}\""}.join("")
24
+ "<#{tag}#{attrs}>#{content}</#{tag}>"
25
+ end
26
+
27
+ def tag
28
+ style == nil ? "p" : @@style_map.select do |k,v|
29
+ k =~ style
30
+ end.first[1]
31
+ end
32
+
33
+ def style
34
+ @node.xpath("./w:pPr/w:pStyle").first.attributes["val"].value
35
+ rescue
36
+ nil
37
+ end
38
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "./base"
2
+
3
+ class DoctorNinja::Parsers::Run < DoctorNinja::Parsers::Base
4
+ @@available_tags = ["b", "u", "i"]
5
+
6
+ def self.applicable_to?(node)
7
+ node.name == "r"
8
+ end
9
+
10
+ def parse
11
+ tag = nil
12
+ if @node.xpath(".//w:rPr/w:b").length > 0
13
+ tag = "b"
14
+ end
15
+
16
+ tags.inject(parse_children){|text,tag| "<#{tag}>#{text}</#{tag}>"}
17
+ end
18
+
19
+ def tags
20
+ @node.xpath("./w:rPr").children
21
+ .map{|n| n.name}
22
+ .select{|n| @@available_tags.include? n}
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "./base"
2
+
3
+ class DoctorNinja::Parsers::Text < DoctorNinja::Parsers::Base
4
+ def self.applicable_to?(node)
5
+ node.text?
6
+ end
7
+
8
+ def parse
9
+ @context[:has_text] = true
10
+ @node.text
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ module DoctorNinja
2
+ class Relationships
3
+ def initialize(doc)
4
+ @doc = doc
5
+ @rels = Nokogiri::XML @doc.read "word/_rels/document.xml.rels"
6
+ end
7
+
8
+ def read id
9
+ @doc.read "word/#{path id}"
10
+ end
11
+
12
+ alias_method :[], :read
13
+
14
+ def path id
15
+ @rels.xpath("//*[@Id=\"#{id}\"]").attribute("Target").value
16
+ rescue
17
+ nil
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module DoctorNinja
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "./minitest_helper"
2
+ require "doctor_ninja/document"
3
+ require "doctor_ninja/errors"
4
+
5
+ class DocumentTest < MiniTest::Test
6
+ def test_valid_file
7
+ @doc = DoctorNinja::Document.new("test/fixtures/limites.docx")
8
+ end
9
+
10
+ def test_invalid_file
11
+ assert_raises(::DoctorNinja::InvalidDocumentError) do
12
+ ::DoctorNinja::Document.new("test/fixtures/invalid_file.docx")
13
+ end
14
+ end
15
+ end
Binary file
File without changes
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+ require "minitest/autorun"
3
+ require "doctor_ninja"
4
+
5
+ class IntegrationTest < MiniTest::Test
6
+ def test_valid_file
7
+ @doc = DoctorNinja::Document.new("test/fixtures/limites.docx")
8
+ @parser = DoctorNinja::Parser.new(@doc)
9
+ @result = @parser.parse
10
+
11
+ assert_includes(@result, "<h1>Introdução aos Limites</h1>")
12
+ assert_includes(@result, "<h2>Introdução</h2>")
13
+ assert_includes(@result, "<b>O limite")
14
+ assert_includes(@result, "<u>Mas a verdade é")
15
+
16
+ math = <<-MATH
17
+ <math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
18
+ <mfrac>
19
+ <mrow>
20
+ <mn>1</mn>
21
+ </mrow>
22
+ <mrow>
23
+ <mn>0</mn>
24
+ </mrow>
25
+ </mfrac>
26
+ <mo>=</mo>
27
+ <mi>∞</mi>
28
+ </math>
29
+ MATH
30
+
31
+ assert_includes(@result.gsub(/[\n ]/,""), math.gsub(/[\n ]/,""))
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/reporters'
2
+ Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
3
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doctor_ninja
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bernardo Amorim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubyzip
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rmagick
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.13'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.13'
111
+ description: DoctorNinja is built by the Responde Aí Team (www.respondeai.com.br/time)
112
+ in an attempt to fill our needs to convert docx files into html that also converts
113
+ Word Forumlas into MathML.
114
+ email:
115
+ - contato@bamorim.com
116
+ executables:
117
+ - ninjadoc
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/ninjadoc
127
+ - doctor_ninja.gemspec
128
+ - lib/doctor_ninja.rb
129
+ - lib/doctor_ninja/document.rb
130
+ - lib/doctor_ninja/errors.rb
131
+ - lib/doctor_ninja/parser.rb
132
+ - lib/doctor_ninja/parsers/base.rb
133
+ - lib/doctor_ninja/parsers/blip_fill.rb
134
+ - lib/doctor_ninja/parsers/drawing.rb
135
+ - lib/doctor_ninja/parsers/math.rb
136
+ - lib/doctor_ninja/parsers/math_para.rb
137
+ - lib/doctor_ninja/parsers/omml2mml.xsl
138
+ - lib/doctor_ninja/parsers/paragraph.rb
139
+ - lib/doctor_ninja/parsers/run.rb
140
+ - lib/doctor_ninja/parsers/text.rb
141
+ - lib/doctor_ninja/relationships.rb
142
+ - lib/doctor_ninja/version.rb
143
+ - test/document_test.rb
144
+ - test/fixtures/img.docx
145
+ - test/fixtures/img_crop.docx
146
+ - test/fixtures/img_rot.docx
147
+ - test/fixtures/img_rot_crop.docx
148
+ - test/fixtures/invalid_file.docx
149
+ - test/fixtures/limites.docx
150
+ - test/integration_test.rb
151
+ - test/minitest_helper.rb
152
+ homepage: https://github.com/bamorim/doctor_ninja
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.2.2
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: DoctorNinja is a library to convert word documents into html files like a
176
+ ninja.
177
+ test_files:
178
+ - test/document_test.rb
179
+ - test/fixtures/img.docx
180
+ - test/fixtures/img_crop.docx
181
+ - test/fixtures/img_rot.docx
182
+ - test/fixtures/img_rot_crop.docx
183
+ - test/fixtures/invalid_file.docx
184
+ - test/fixtures/limites.docx
185
+ - test/integration_test.rb
186
+ - test/minitest_helper.rb