ydocx 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,13 @@
1
+ === 1.0.4 / 02.05.2012
2
+
3
+ * Fixed bug for action to_xml in command
4
+ * Updated help message
5
+ * Added option handling into command
6
+ * Improved heading/chapter detection
7
+ * Added docx2xml/to_xml simple xml format output
8
+ * Changed module and lib structure
9
+ * Updated executable file
10
+
1
11
  === 1.0.3 / 01.05.2012
2
12
 
3
13
  * Fixed blank tag without fachinfo format
data/Manifest.txt CHANGED
@@ -3,11 +3,12 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  bin/docx2html
6
- lib/docx2html/bin/docx2html
7
- lib/docx2html/lib/docx2html.rb
8
- lib/docx2html/lib/docx2html/builder.rb
9
- lib/docx2html/lib/docx2html/document.rb
10
- lib/docx2html/lib/docx2html/html_methods.rb
11
- lib/docx2html/lib/docx2html/parser.rb
12
- lib/fachinfo.rb
6
+ bin/docx2xml
13
7
  lib/version.rb
8
+ lib/ydocx.rb
9
+ lib/ydocx/builder.rb
10
+ lib/ydocx/document.rb
11
+ lib/ydocx/markup_method.rb
12
+ lib/ydocx/parser.rb
13
+ lib/ydocx/command.rb
14
+ lib/ydocx/templates/fachinfo.rb
data/bin/docx2html CHANGED
@@ -4,28 +4,9 @@
4
4
  if $0 == __FILE__
5
5
  require 'pathname'
6
6
  root = Pathname.new(__FILE__).realpath.parent.parent
7
- %w(
8
- lib/docx2html/lib
9
- lib
10
- ).each do |lib|
11
- $:.unshift root.join(lib)
12
- end
13
- require 'docx2html'
14
- else
15
- require 'docx2html/lib/docx2html'
7
+ $:.unshift root.join('lib')
16
8
  end
17
9
 
18
- if file = ARGV.first
19
- unless File.exist?(file)
20
- exit
21
- else
22
- # TODO args handling
23
- if true
24
- require 'fachinfo'
25
- end
26
- options = {
27
- :style => :frame
28
- }
29
- Docx2html::Document.open(file).to_html file, options
30
- end
31
- end
10
+ require 'ydocx/command'
11
+
12
+ YDocx::Command.run(:to_html)
data/bin/docx2xml ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ if $0 == __FILE__
5
+ require 'pathname'
6
+ root = Pathname.new(__FILE__).realpath.parent.parent
7
+ $:.unshift root.join('lib')
8
+ end
9
+
10
+ require 'ydocx/command'
11
+
12
+ YDocx::Command.run(:to_xml)
data/lib/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  module Docx2html
5
- VERSION = "1.0.3"
5
+ VERSION = "1.0.4"
6
6
  end
@@ -0,0 +1,144 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'nokogiri'
5
+ require 'ydocx/markup_method'
6
+
7
+ module YDocx
8
+ class Builder
9
+ include MarkupMethod
10
+ attr_accessor :contents, :container, :indecies,
11
+ :style, :title
12
+ def initialize(contents)
13
+ @contents = contents
14
+ @container = {}
15
+ @indecies = []
16
+ @style = false
17
+ @title = ''
18
+ init
19
+ if block_given?
20
+ yield self
21
+ end
22
+ end
23
+ def init
24
+ end
25
+ def build_html
26
+ contents = @contents
27
+ body = compile(contents, :html)
28
+ if @container.has_key?(:content)
29
+ body = build_tag(@container[:tag], body, @container[:attributes])
30
+ end
31
+ if before = build_before_content
32
+ body = build_tag(before[:tag], before[:content], before[:attributes]) << body
33
+ end
34
+ if after = build_after_content
35
+ body << build_tag(after[:tag], after[:content], after[:attributes])
36
+ end
37
+ builder = Nokogiri::HTML::Builder.new do |doc|
38
+ doc.html {
39
+ doc.head {
40
+ doc.meta :charset => 'utf-8'
41
+ doc.title @title
42
+ doc.style { doc << style } if @style
43
+ }
44
+ doc.body { doc << body }
45
+ }
46
+ end
47
+ builder.to_html.gsub(/\n/, '')
48
+ end
49
+ def build_xml
50
+ chapters = compile(@contents, :xml)
51
+ builder = Nokogiri::XML::Builder.new do |xml|
52
+ xml.document {
53
+ xml.chapters { xml << chapters }
54
+ }
55
+ end
56
+ builder.to_xml(:indent => 0, :encoding => 'utf-8').gsub(/\n/, '')
57
+ end
58
+ private
59
+ def compile(contents, mode)
60
+ if mode == :xml
61
+ block_tag = :chapter
62
+ else
63
+ block_tag = :div
64
+ end
65
+ result = ''
66
+ headings = 0
67
+ contents.each do |element|
68
+ if element[:tag].to_s =~ /^h[1-9]$/ # block
69
+ if headings == 0
70
+ result << "<#{block_tag}>"
71
+ else
72
+ result << "</#{block_tag}><#{block_tag}>"
73
+ end
74
+ headings += 1
75
+ end
76
+ result << build_tag(element[:tag], element[:content], element[:attributes], mode)
77
+ end
78
+ result << "</#{block_tag}>"
79
+ end
80
+ def build_after_content
81
+ nil
82
+ end
83
+ def build_before_content
84
+ nil
85
+ end
86
+ def build_tag(tag, content, attributes, mode=:html)
87
+ if tag == :br and mode != :xml
88
+ return "<br/>"
89
+ elsif content.nil? or content.empty?
90
+ return ''
91
+ end
92
+ _content = ''
93
+ if content.is_a? Array
94
+ content.each do |c|
95
+ next if c.nil? or c.empty?
96
+ if c.is_a? Hash
97
+ _content << build_tag(c[:tag], c[:content], c[:attributes], mode)
98
+ elsif c.is_a? String
99
+ _content << c.chomp.to_s
100
+ end
101
+ end
102
+ elsif content.is_a? Hash
103
+ _content = build_tag(content[:tag], content[:content], content[:attributes], mode)
104
+ elsif content.is_a? String
105
+ _content = content
106
+ end
107
+ _tag = tag.to_s
108
+ _attributes = ''
109
+ unless attributes.empty?
110
+ attributes.each_pair do |key, value|
111
+ next if mode == :xml and key.to_s =~ /(id|style|colspan)/u
112
+ _attributes << " #{key.to_s}=#{value.to_s}"
113
+ end
114
+ end
115
+ if mode == :xml
116
+ case _tag.to_sym
117
+ when :span then _tag = 'underline'
118
+ when :strong then _tag = 'bold'
119
+ when :em then _tag = 'italic'
120
+ when :p then _tag = 'paragraph'
121
+ when :h2, :h3 then _tag = 'heading'
122
+ when :sup then _tag = 'superscript' # text
123
+ when :sub then _tag = 'subscript' # text
124
+ end
125
+ end
126
+ return "<#{_tag}#{_attributes}>#{_content}</#{_tag}>"
127
+ end
128
+ def style
129
+ style = <<-CSS
130
+ table, tr, td {
131
+ border-collapse: collapse;
132
+ border: 1px solid gray;
133
+ }
134
+ table {
135
+ margin: 5px 0 5px 0;
136
+ }
137
+ td {
138
+ padding: 5px 10px;
139
+ }
140
+ CSS
141
+ style.gsub(/\s\s+|\n/, ' ')
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'ydocx/document'
5
+
6
+ module YDocx
7
+ class Command
8
+ class << self
9
+ @@help = /^\-(h|\-help)$/u
10
+ @@format = /^\-(f|\-format)$/u
11
+ def error(message='')
12
+ puts message
13
+ puts "see `#{self.command} --help`"
14
+ exit
15
+ end
16
+ def command
17
+ File.basename $0
18
+ end
19
+ def help
20
+ banner = <<-BANNER
21
+ Usage: #{$0} file [options]
22
+ -f, --format Format of style and chapter {fi|fachinfo}, default none.
23
+ -h, --help Display this help message.
24
+ BANNER
25
+ puts banner
26
+ exit
27
+ end
28
+ def report(action, path)
29
+ dir = File.dirname path
30
+ base = File.basename path, '.docx'
31
+ ext = (action == :to_xml) ? '.xml' : '.html'
32
+ puts "#{self.command}: generated #{dir}/#{base}#{ext}"
33
+ exit
34
+ end
35
+ def run(action=:to_html)
36
+ argv = ARGV.dup
37
+ if argv.empty? or argv[0] =~ @@help
38
+ self.help
39
+ else
40
+ file = argv.shift
41
+ path = File.expand_path(file)
42
+ if !File.exist?(path)
43
+ self.error "#{self.command}: cannot open #{file}: No such file"
44
+ elsif !File.extname(path).match(/^\.docx$/)
45
+ self.error "#{self.command}: cannot open #{file}: Not a docx file"
46
+ else
47
+ options = {}
48
+ if option = argv.shift
49
+ if option =~ @@format
50
+ case argv[0]
51
+ when 'fi', 'fachinfo'
52
+ require 'ydocx/templates/fachinfo'
53
+ # TODO style option?
54
+ options.merge!({:style => :frame}) if action == :to_html
55
+ when 'pi', 'patinfo'
56
+ # pending
57
+ else
58
+ self.error "#{self.command}: exit with #{option}: Invalid argument"
59
+ end
60
+ elsif option =~ @@help
61
+ self.help
62
+ else
63
+ self.error "#{self.command}: exit with #{option}: Unknown option"
64
+ end
65
+ end
66
+ YDocx::Document.open(path).send(action, path, options)
67
+ self.report action, path
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -3,12 +3,12 @@
3
3
 
4
4
  require 'pathname'
5
5
  require 'zip/zip'
6
- require 'docx2html/lib/docx2html/parser'
7
- require 'docx2html/lib/docx2html/builder'
6
+ require 'ydocx/parser'
7
+ require 'ydocx/builder'
8
8
 
9
- module Docx2html
9
+ module YDocx
10
10
  class Document
11
- attr_reader :contents
11
+ attr_reader :contents, :indecies
12
12
  def self.open(file)
13
13
  self.new(file)
14
14
  end
@@ -25,7 +25,7 @@ module Docx2html
25
25
  if @indecies
26
26
  builder.indecies = @indecies
27
27
  end
28
- html = builder.build
28
+ html = builder.build_html
29
29
  end
30
30
  unless file.empty?
31
31
  path = Pathname.new(file).realpath.sub_ext('.html')
@@ -36,6 +36,20 @@ module Docx2html
36
36
  html
37
37
  end
38
38
  end
39
+ def to_xml(file='', options={})
40
+ xml = ''
41
+ Builder.new(@contents) do |builder|
42
+ xml = builder.build_xml
43
+ end
44
+ unless file.empty?
45
+ path = Pathname.new(file).realpath.sub_ext('.xml')
46
+ File.open(path, 'w:utf-8') do |f|
47
+ f.puts xml
48
+ end
49
+ else
50
+ xml
51
+ end
52
+ end
39
53
  private
40
54
  def read(file)
41
55
  @path = File.expand_path(file)
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: utf-8
3
3
 
4
- module Docx2html
5
- module HtmlMethods
6
- def tag(tag, content = [], attributes = {})
4
+ module YDocx
5
+ module MarkupMethod
6
+ def markup(tag, content = [], attributes = {})
7
7
  tag_hash = {
8
8
  :tag => tag,
9
9
  :content => content,
@@ -3,11 +3,11 @@
3
3
 
4
4
  require 'nokogiri'
5
5
  require 'htmlentities'
6
- require 'docx2html/lib/docx2html/html_methods'
6
+ require 'ydocx/markup_method'
7
7
 
8
- module Docx2html
8
+ module YDocx
9
9
  class Parser
10
- include HtmlMethods
10
+ include MarkupMethod
11
11
  attr_accessor :indecies, :result, :space
12
12
  def initialize(stream)
13
13
  @xml = Nokogiri::XML.parse(stream)
@@ -63,9 +63,17 @@ module Docx2html
63
63
  unless rpr.xpath('w:vertAlign').empty?
64
64
  script = rpr.xpath('w:vertAlign').first['val'].to_sym
65
65
  if script == :subscript
66
- text = tag(:sub, text)
66
+ if text =~ /^[0-9]$/
67
+ text = "&sub" + text + ";"
68
+ else
69
+ text = markup(:sub, text)
70
+ end
67
71
  elsif script == :superscript
68
- text = tag(:sup, text)
72
+ if text =~ /^[0-9]$/
73
+ text = "&sup" + text + ";"
74
+ else
75
+ text = markup(:sup, text)
76
+ end
69
77
  end
70
78
  end
71
79
  text
@@ -138,8 +146,7 @@ module Docx2html
138
146
  else
139
147
  #p "code : " + ("&#%s;" % code)
140
148
  #p "hex : " + code.hex.to_s
141
- #p "char : " + @coder.decode("&#%s;" % code)
142
- #@coder.decode("&#%s;" % code.hex.to_s)
149
+ #p "char : " + @coder.decode("&#%s;" % code.hex.to_s)
143
150
  end
144
151
  end
145
152
  def parse_image
@@ -173,7 +180,7 @@ module Docx2html
173
180
  c.is_a?(Hash) and c[:tag].to_s =~ /^h[1-9]/u
174
181
  end.empty?
175
182
  if paragraph
176
- tag :p, content
183
+ markup :p, content
177
184
  else
178
185
  content.first
179
186
  end
@@ -182,9 +189,9 @@ module Docx2html
182
189
  end
183
190
  end
184
191
  def parse_table(node)
185
- table = tag :table
192
+ table = markup :table
186
193
  node.xpath('w:tr').each do |tr|
187
- cells = tag :tr
194
+ cells = markup :tr
188
195
  tr.xpath('w:tc').each do |tc|
189
196
  attributes = {}
190
197
  tc.xpath('w:tcPr').each do |tcpr|
@@ -192,7 +199,7 @@ module Docx2html
192
199
  attributes[:colspan] = span.first['val'] # w:val
193
200
  end
194
201
  end
195
- cell = tag :td, [], attributes
202
+ cell = markup :td, [], attributes
196
203
  tc.xpath('w:p').each do |p|
197
204
  cell[:content] << parse_paragraph(p)
198
205
  end
@@ -214,13 +221,13 @@ module Docx2html
214
221
  text = text.strip
215
222
  text = apply_align(rpr, text)
216
223
  unless rpr.xpath('w:u').empty?
217
- text = tag(:span, text, {:style => "text-decoration:underline;"})
224
+ text = markup(:span, text, {:style => "text-decoration:underline;"})
218
225
  end
219
226
  unless rpr.xpath('w:i').empty?
220
- text = tag(:em, text)
227
+ text = markup(:em, text)
221
228
  end
222
229
  unless rpr.xpath('w:b').empty?
223
- text = tag(:strong, text)
230
+ text = markup(:strong, text)
224
231
  end
225
232
  text
226
233
  end
@@ -3,19 +3,10 @@
3
3
 
4
4
  require 'cgi'
5
5
 
6
- module Docx2html
6
+ module YDocx
7
7
  class Parser
8
8
  private
9
- def escape(text)
10
- CGI.escape(text.gsub(/&(.)uml;/, '\1').gsub(/\s*\/\s*|\/|\s+/, '_').downcase)
11
- end
12
9
  def parse_as_block(r, text)
13
- if r.parent.previous.nil? and @indecies.empty?
14
- # The first line as package name
15
- id = escape('titel')
16
- @indecies << {:text => 'Titel', :id => id}
17
- return tag(:h2, text, {:id => id})
18
- end
19
10
  text = text.strip
20
11
  # TODO
21
12
  # Franzoesisch
@@ -42,18 +33,24 @@ module Docx2html
42
33
  'Zusammens.' => /^Zusammensetzung($|\s*\/\s*(Wirkstoffe|Hilsstoffe)$)/u, # 2
43
34
  }.each_pair do |chapter, regexp|
44
35
  if text =~ regexp
45
- next unless r.next.nil? # without line break
46
- id = escape(text)
36
+ next if !r.next.nil? and # skip matches in paragraph
37
+ r.next.name.downcase != 'bookmarkend'
38
+ id = CGI.escape(text.gsub(/&(.)uml;/, '\1').gsub(/\s*\/\s*|\/|\s+/, '_').downcase)
47
39
  @indecies << {:text => chapter, :id => id}
48
- return tag(:h3, text, {:id => id})
40
+ return markup(:h3, text, {:id => id})
49
41
  end
50
42
  end
51
- nil
43
+ if r.parent.previous.nil? and @indecies.empty?
44
+ # The first line as package name
45
+ @indecies << {:text => 'Titel', :id => 'titel'}
46
+ return markup(:h2, text, {:id => 'titel'})
47
+ end
48
+ return nil
52
49
  end
53
50
  end
54
51
  class Builder
55
52
  def init
56
- @container = tag(:div, [], {:id => 'container'})
53
+ @container = markup(:div, [], {:id => 'container'})
57
54
  end
58
55
  private
59
56
  def build_before_content
@@ -61,11 +58,11 @@ module Docx2html
61
58
  indices = []
62
59
  @indecies.each do |index|
63
60
  if index.has_key?(:id)
64
- link = tag(:a, index[:text], {:href => "#" + index[:id]})
65
- indices << tag(:li, link)
61
+ link = markup(:a, index[:text], {:href => "#" + index[:id]})
62
+ indices << markup(:li, link)
66
63
  end
67
64
  end
68
- tag(:div, tag(:ul, indices), {:id => 'indecies'})
65
+ markup(:div, markup(:ul, indices), {:id => 'indecies'})
69
66
  end
70
67
  end
71
68
  def style
data/lib/ydocx.rb ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'ydocx/document'
5
+
6
+ module YDocx
7
+ end
metadata CHANGED
@@ -1,91 +1,88 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ydocx
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 3
9
- version: 1.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Yasuhiro Asaka, Zeno R.R. Davatz
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-05-01 00:00:00 +02:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: hoe
12
+ date: 2012-05-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &19114300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
22
+ type: :development
22
23
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 2
29
- - 9
30
- - 1
31
- version: 2.9.1
24
+ version_requirements: *19114300
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ requirement: &19113780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.13'
32
33
  type: :development
33
- version_requirements: *id001
34
- description: ""
35
- email:
34
+ prerelease: false
35
+ version_requirements: *19113780
36
+ description: ''
37
+ email:
36
38
  - yasaka@ywesee.com, zdavatz@ywesee.com
37
- executables:
39
+ executables:
38
40
  - docx2html
41
+ - docx2xml
39
42
  extensions: []
40
-
41
- extra_rdoc_files:
43
+ extra_rdoc_files:
42
44
  - History.txt
43
45
  - Manifest.txt
44
46
  - README.txt
45
- files:
47
+ files:
46
48
  - History.txt
47
49
  - Manifest.txt
48
50
  - README.txt
49
51
  - Rakefile
50
52
  - bin/docx2html
51
- - lib/docx2html/bin/docx2html
52
- - lib/docx2html/lib/docx2html.rb
53
- - lib/docx2html/lib/docx2html/builder.rb
54
- - lib/docx2html/lib/docx2html/document.rb
55
- - lib/docx2html/lib/docx2html/html_methods.rb
56
- - lib/docx2html/lib/docx2html/parser.rb
57
- - lib/fachinfo.rb
53
+ - bin/docx2xml
58
54
  - lib/version.rb
59
- has_rdoc: true
55
+ - lib/ydocx.rb
56
+ - lib/ydocx/builder.rb
57
+ - lib/ydocx/document.rb
58
+ - lib/ydocx/markup_method.rb
59
+ - lib/ydocx/parser.rb
60
+ - lib/ydocx/command.rb
61
+ - lib/ydocx/templates/fachinfo.rb
60
62
  homepage: https://github.com/zdavatz/ydocx
61
63
  licenses: []
62
-
63
64
  post_install_message:
64
- rdoc_options:
65
+ rdoc_options:
65
66
  - --main
66
67
  - README.txt
67
- require_paths:
68
+ require_paths:
68
69
  - lib
69
- required_ruby_version: !ruby/object:Gem::Requirement
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- segments:
74
- - 0
75
- version: "0"
76
- required_rubygems_version: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- segments:
81
- - 0
82
- version: "0"
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
83
82
  requirements: []
84
-
85
83
  rubyforge_project: ydocx
86
- rubygems_version: 1.3.6
84
+ rubygems_version: 1.8.15
87
85
  signing_key:
88
86
  specification_version: 3
89
- summary: ""
87
+ summary: ''
90
88
  test_files: []
91
-
@@ -1,16 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- require 'pathname'
5
-
6
- root = Pathname.new(__FILE__).realpath.parent.parent
7
- $:.unshift root.join('lib/docx2html') if $0 == __FILE__
8
- require 'docx2html'
9
-
10
- if file = ARGV.first
11
- unless File.exist?(file)
12
- exit
13
- else
14
- Docx2html::Document.open(file).to_html file, :style => true
15
- end
16
- end
@@ -1,105 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- require 'nokogiri'
5
- require 'docx2html/lib/docx2html/html_methods'
6
-
7
- module Docx2html
8
- class Builder
9
- include HtmlMethods
10
- attr_accessor :body, :indecies, :title,
11
- :frame, :style
12
- def initialize(body)
13
- @body = body
14
- @container = {}
15
- @indecies = []
16
- @style = false
17
- @title = ''
18
- init
19
- if block_given?
20
- yield self
21
- end
22
- end
23
- def init
24
- end
25
- def build
26
- if @container.has_key?(:content)
27
- @container[:content] = @body
28
- @body = [@container]
29
- end
30
- if before_content = build_before_content
31
- @body.unshift before_content
32
- end
33
- if after_content = build_after_content
34
- @body.push after_content
35
- end
36
- body = ''
37
- @body.each do |e|
38
- body << build_tag(e[:tag], e[:content], e[:attributes])
39
- end
40
- builder = Nokogiri::HTML::Builder.new do |doc|
41
- doc.html {
42
- doc.head {
43
- doc.meta :charset => 'utf-8'
44
- doc.title @title
45
- doc.style { doc << style } if @style
46
- }
47
- doc.body { doc << body }
48
- }
49
- end
50
- builder.to_html.gsub(/\n/, '')
51
- end
52
- private
53
- def build_after_content
54
- nil
55
- end
56
- def build_before_content
57
- nil
58
- end
59
- def build_tag(tag, content, attributes)
60
- if tag == :br
61
- return "<br/>"
62
- elsif content.nil? or content.empty?
63
- return ''
64
- end
65
- _content = ''
66
- if content.is_a? Array
67
- content.each do |c|
68
- next if c.nil? or c.empty?
69
- if c.is_a? Hash
70
- _content << build_tag(c[:tag], c[:content], c[:attributes])
71
- elsif c.is_a? String
72
- _content << c.chomp.to_s
73
- end
74
- end
75
- elsif content.is_a? Hash
76
- _content = build_tag(content[:tag], content[:content], content[:attributes])
77
- elsif content.is_a? String
78
- _content = content
79
- end
80
- _tag = tag.to_s
81
- _attributes = ''
82
- unless attributes.empty?
83
- attributes.each_pair do |k, v|
84
- _attributes << " #{k.to_s}=#{v.to_s}"
85
- end
86
- end
87
- return "<#{_tag}#{_attributes}>#{_content}</#{_tag}>"
88
- end
89
- def style
90
- style = <<-CSS
91
- table, tr, td {
92
- border-collapse: collapse;
93
- border: 1px solid gray;
94
- }
95
- table {
96
- margin: 5px 0 5px 0;
97
- }
98
- td {
99
- padding: 5px 10px;
100
- }
101
- CSS
102
- style.gsub(/\s\s+|\n/, ' ')
103
- end
104
- end
105
- end
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- require 'docx2html/lib/docx2html/document'
5
-
6
- module Docx2html
7
- end