ydocx 1.1.4 → 1.1.5

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/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 1.1.5 / 08.05.2012
2
+
3
+ * Updated output files directory for fi/pi format
4
+
1
5
  === 1.1.4 / 08.05.2012
2
6
 
3
7
  * Updated img src as relative path
data/lib/ydocx/builder.rb CHANGED
@@ -108,8 +108,7 @@ module YDocx
108
108
  attributes.each_pair do |key, value|
109
109
  next if mode == :xml and key.to_s =~ /(id|style|colspan)/u
110
110
  if tag == :img and key == :src
111
- src = @files.join value.to_s
112
- _attributes << " src=\"#{src}\""
111
+ _attributes << " src=\"#{resolve_path(value.to_s)}\""
113
112
  else
114
113
  _attributes << " #{key.to_s}=\"#{value.to_s}\""
115
114
  end
@@ -147,5 +146,8 @@ td {
147
146
  CSS
148
147
  style.gsub(/\s\s+|\n/, ' ')
149
148
  end
149
+ def resolve_path(path)
150
+ @files.join path
151
+ end
150
152
  end
151
153
  end
data/lib/ydocx/command.rb CHANGED
@@ -14,6 +14,9 @@ module YDocx
14
14
  puts "see `#{self.command} --help`"
15
15
  exit
16
16
  end
17
+ def extname(action)
18
+ action == :to_html ? '.html': '.xml'
19
+ end
17
20
  def command
18
21
  File.basename $0
19
22
  end
@@ -28,10 +31,7 @@ Usage: #{self.command} file [options]
28
31
  exit
29
32
  end
30
33
  def report(action, path)
31
- dir = File.dirname path
32
- base = File.basename path, '.docx'
33
- ext = (action == :to_xml) ? '.xml' : '.html'
34
- puts "#{self.command}: generated #{dir}/#{base}#{ext}"
34
+ puts "#{self.command}: generated #{File.expand_path(path)}"
35
35
  exit
36
36
  end
37
37
  def run(action=:to_html)
@@ -82,8 +82,9 @@ Usage: #{self.command} file [options]
82
82
  require 'ydocx/templates/fachinfo'
83
83
  options.merge!({:style => :frame}) if action == :to_html
84
84
  end
85
- YDocx::Document.open(path).send(action, path, options)
86
- self.report action, path
85
+ doc = YDocx::Document.open(path)
86
+ doc.send(action, path, options)
87
+ self.report action, doc.output_file(self.extname(action))
87
88
  end
88
89
  end
89
90
  end
@@ -13,16 +13,19 @@ require 'ydocx/builder'
13
13
 
14
14
  module YDocx
15
15
  class Document
16
- attr_reader :contents, :indecies, :images
16
+ attr_reader :builder,:contents, :images, :indecies,
17
+ :parser, :path
17
18
  def self.open(file, options={})
18
19
  self.new(file, options)
19
20
  end
20
21
  def initialize(file, options={})
22
+ @parser = nil
23
+ @builder = nil
21
24
  @contents = nil
22
25
  @indecies = nil
23
26
  @images = []
24
27
  @options = options
25
- @path = nil
28
+ @path = Pathname.new('.')
26
29
  @files = nil
27
30
  @zip = nil
28
31
  init
@@ -30,13 +33,19 @@ module YDocx
30
33
  end
31
34
  def init
32
35
  end
36
+ def output_directory
37
+ @files ||= @path.dirname.join(@path.basename('.docx').to_s + '_files')
38
+ end
39
+ def output_file(ext)
40
+ @path.sub_ext(".#{ext.to_s}")
41
+ end
33
42
  def to_html(file='', options={})
34
43
  html = ''
35
44
  options = @options.merge(options)
36
- @files = @path.dirname.join(@path.basename('.docx').to_s + '_files')
37
- Builder.new(@contents) do |builder|
45
+ files = output_directory
46
+ @builder = Builder.new(@contents) do |builder|
38
47
  builder.title = @path.basename
39
- builder.files = @files.relative_path_from(@files.dirname)
48
+ builder.files = files.relative_path_from(files.dirname)
40
49
  builder.style = options[:style] if options.has_key?(:style)
41
50
  if @indecies
42
51
  builder.indecies = @indecies
@@ -45,7 +54,7 @@ module YDocx
45
54
  end
46
55
  unless file.empty?
47
56
  create_files if has_image?
48
- html_file = @path.sub_ext('.html')
57
+ html_file = output_file(:html)
49
58
  File.open(html_file, 'w:utf-8') do |f|
50
59
  f.puts html
51
60
  end
@@ -61,7 +70,8 @@ module YDocx
61
70
  xml = builder.build_xml
62
71
  end
63
72
  unless file.empty?
64
- xml_file = @path.sub_ext('.xml')
73
+ xml_file = output_file(:xml)
74
+ mkdir xml_file.parent
65
75
  File.open(xml_file, 'w:utf-8') do |f|
66
76
  f.puts xml
67
77
  end
@@ -71,13 +81,14 @@ module YDocx
71
81
  end
72
82
  private
73
83
  def create_files
74
- FileUtils.mkdir @files unless @files.exist?
84
+ files_dir = output_directory
85
+ mkdir Pathname.new(files_dir) unless files_dir.exist?
75
86
  @zip = Zip::ZipFile.open(@path.realpath)
76
87
  @images.each do |image|
77
88
  origin_path = Pathname.new image[:origin] # media/filename.ext
78
89
  source_path = Pathname.new image[:source] # images/filename.ext
79
- dir = @files.join source_path.dirname
80
- FileUtils.mkdir dir unless dir.exist?
90
+ image_dir = files_dir.join source_path.dirname
91
+ FileUtils.mkdir image_dir unless image_dir.exist?
81
92
  organize_image(origin_path, source_path)
82
93
  end
83
94
  @zip.close
@@ -88,16 +99,16 @@ module YDocx
88
99
  if defined? Magick::Image
89
100
  image = Magick::Image.from_blob(binary.read).first
90
101
  image.format = source_path.extname[1..-1].upcase
91
- @files.join(source_path).open('wb') do |f|
102
+ output_directory.join(source_path).open('wb') do |f|
92
103
  f.puts image.to_blob
93
104
  end
94
105
  else # copy original image
95
- @files.join(dir, origin_path.basename).open('wb') do |f|
106
+ output_directory.join(dir, origin_path.basename).open('wb') do |f|
96
107
  f.puts binary.read
97
108
  end
98
109
  end
99
110
  else
100
- @files.join(source_path).open('wb') do |f|
111
+ output_directory.join(source_path).open('wb') do |f|
101
112
  f.puts binary.read
102
113
  end
103
114
  end
@@ -110,12 +121,18 @@ module YDocx
110
121
  @zip = Zip::ZipFile.open(@path.realpath)
111
122
  doc = @zip.find_entry('word/document.xml').get_input_stream
112
123
  rel = @zip.find_entry('word/_rels/document.xml.rels').get_input_stream
113
- Parser.new(doc, rel) do |parser|
124
+ @parser = Parser.new(doc, rel) do |parser|
114
125
  @contents = parser.parse
115
126
  @indecies = parser.indecies
116
127
  @images = parser.images
117
128
  end
118
129
  @zip.close
119
130
  end
131
+ def mkdir(path)
132
+ return if path.exist?
133
+ parent = path.parent
134
+ mkdir(parent)
135
+ FileUtils.mkdir(path)
136
+ end
120
137
  end
121
138
  end
@@ -5,16 +5,13 @@ require 'cgi'
5
5
 
6
6
  module YDocx
7
7
  class Parser
8
+ attr_reader :code
8
9
  def init
9
- @image_path = 'fi_images'
10
+ @image_path = 'image'
11
+ @code = nil
10
12
  end
11
13
  private
12
- def escape_id(text)
13
- CGI.escape(text.gsub(/&(.)uml;/, '\1e').gsub(/\s*\/\s*|\s+|\/|\-/, '_').gsub(/\./, '').downcase)
14
- end
15
- def parse_block(node)
16
- text = node.inner_text.strip
17
- text = optional_escape text
14
+ def chapters
18
15
  # TODO
19
16
  # Franzoesisch
20
17
  chapters = {
@@ -38,13 +35,30 @@ module YDocx
38
35
  'Packungen' => /^Packungen($|\s*\(\s*mit\s+Angabe\s+der\s+Abgabekategorie\s*\)$)/u, # 18
39
36
  'Reg.Inhaber' => /^Zulassungsinhaberin($|\s*\(\s*Firma\s+und\s+Sitz\s+gem&auml;ss\s*Handelsregisterauszug\s*\))/u, # 19
40
37
  'Stand d. Info.' => /^Stand\s+der\s+Information$|^Mise\s+.\s+jour$/iu, # 20
41
- }.each_pair do |chapter, regexp|
38
+ }
39
+ end
40
+ def escape_id(text)
41
+ CGI.escape(text.gsub(/&(.)uml;/, '\1e').gsub(/\s*\/\s*|\s+|\/|\-/, '_').gsub(/\./, '').downcase)
42
+ end
43
+ def parse_code(text)
44
+ if text =~ /^\s*(\d\d)(&lsquo;|&rsquo;|`|')(\d\d\d)\s*\(\s*[Ss]wiss\s*medic\s*\)\s*$/
45
+ @code = "%5d" % ($1 + $3)
46
+ else
47
+ nil
48
+ end
49
+ end
50
+ def parse_block(node)
51
+ text = node.inner_text.strip
52
+ text = optional_escape text
53
+ chapters.each_pair do |chapter, regexp|
42
54
  if text =~ regexp
43
55
  # allow without line break
44
56
  # next if !node.previous.inner_text.empty? and !node.next.inner_text.empty?
45
57
  id = escape_id(chapter)
46
58
  @indecies << {:text => chapter, :id => id}
47
59
  return markup(:h3, text, {:id => id})
60
+ elsif parse_code(text)
61
+ return nil
48
62
  end
49
63
  end
50
64
  if node.parent.previous.nil? and @indecies.empty?
@@ -149,6 +163,9 @@ div#container {
149
163
  end
150
164
  style.gsub(/\s\s+|\n/, ' ')
151
165
  end
166
+ def resolve_path(path)
167
+ path
168
+ end
152
169
  end
153
170
  # == Document
154
171
  # Image reference option
@@ -157,14 +174,27 @@ div#container {
157
174
  # $ docx2html example.docx --format fachinfo refence1.png refenece2.png
158
175
  class Document
159
176
  def init
177
+ @directory = 'fi'
160
178
  @references = []
161
- ARGV.reverse.each do |arg|
162
- if arg.downcase =~ /\.(jpeg|jpg|png|gif)$/
163
- path = Pathname.new(arg).realpath
164
- @references << path if path.exist?
179
+ prepare_reference
180
+ end
181
+ def output_directory
182
+ unless @files
183
+ if @parser.code
184
+ @files = Pathname.new(@directory + '/' + @parser.code)
185
+ else
186
+ @files = @path.dirname.join(@path.basename('.docx').to_s + '_files')
165
187
  end
166
188
  end
167
- @references.reverse unless @references.empty?
189
+ @files
190
+ end
191
+ def output_file(ext)
192
+ if @parser.code
193
+ filename = @parser.code
194
+ output_directory.join "#{filename}.#{ext.to_s}"
195
+ else # default
196
+ @path.sub_ext(".#{ext.to_s}")
197
+ end
168
198
  end
169
199
  private
170
200
  alias :copy_or_convert :organize_image
@@ -176,5 +206,14 @@ div#container {
176
206
  copy_or_convert(origin_path, source_path)
177
207
  end
178
208
  end
209
+ def prepare_reference
210
+ ARGV.reverse.each do |arg|
211
+ if arg.downcase =~ /\.(jpeg|jpg|png|gif)$/
212
+ path = Pathname.new(arg).realpath
213
+ @references << path if path.exist?
214
+ end
215
+ end
216
+ @references.reverse unless @references.empty?
217
+ end
179
218
  end
180
219
  end
@@ -5,13 +5,8 @@ require 'ydocx/templates/fachinfo'
5
5
 
6
6
  module YDocx
7
7
  class Parser
8
- def init
9
- @image_path = 'pi_images'
10
- end
11
8
  private
12
- def parse_block(node)
13
- text = node.inner_text.strip
14
- text = optional_escape text
9
+ def chapters
15
10
  # TODO
16
11
  # Franzoesisch
17
12
  chapters = {
@@ -31,21 +26,14 @@ module YDocx
31
26
  'Verteiler' => /^Zulassungsinhaberin$/u, # 14
32
27
  'Hersteller' => /^Herstellerin$/u, # 15
33
28
  'Stand d. Info.' => /^Diese\sPackungsbeilage\s+wurde\s+im\s+[\.A-z\s0-9]+(\s+|\s*\/\s*\w+\s+\(Monat\s*\/\s*Jahr\)\s*)letztmals\s+durch\s+die\s+Arzneimittelbeh&ouml;rde\s*\(\s*Swissmedic\s*\)\s*gepr&uuml;ft.?$/u, # 16
34
- }.each_pair do |chapter, regexp|
35
- if text =~ regexp
36
- # allow without line break
37
- #next if !node.previous.inner_text.empty? and !node.next.inner_text.empty?
38
- id = escape_id(chapter)
39
- @indecies << {:text => chapter, :id => id}
40
- return markup(:h3, text, {:id => id})
41
- end
42
- end
43
- if node.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
29
+ }
30
+ end
31
+ end
32
+ class Document
33
+ def init
34
+ @directory = 'pi'
35
+ @references = []
36
+ prepare_reference
49
37
  end
50
38
  end
51
39
  end
data/lib/ydocx.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  require 'ydocx/document'
5
5
 
6
6
  module YDocx
7
- VERSION = '1.1.4'
7
+ VERSION = '1.1.5'
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ydocx
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
16
- requirement: &14374900 !ruby/object:Gem::Requirement
16
+ requirement: &10569500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.10'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *14374900
24
+ version_requirements: *10569500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
27
- requirement: &14374480 !ruby/object:Gem::Requirement
27
+ requirement: &10569080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '2.13'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *14374480
35
+ version_requirements: *10569080
36
36
  description: ''
37
37
  email:
38
38
  - yasaka@ywesee.com, zdavatz@ywesee.com