ydocx 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 1.0.9 / 04.05.2012
2
+
3
+ * Added image reference option
4
+
1
5
  === 1.0.8 / 04.05.2012
2
6
 
3
7
  * Removed unnecessary when
data/lib/ydocx/builder.rb CHANGED
@@ -9,11 +9,12 @@ module YDocx
9
9
  class Builder
10
10
  include MarkupMethod
11
11
  attr_accessor :contents, :container, :indecies,
12
- :files, :style, :title
12
+ :block, :files, :style, :title
13
13
  def initialize(contents)
14
14
  @contents = contents
15
15
  @container = {}
16
16
  @indecies = []
17
+ @block = :div
17
18
  @files = Pathname.new('.')
18
19
  @style = false
19
20
  @title = ''
@@ -59,25 +60,20 @@ module YDocx
59
60
  end
60
61
  private
61
62
  def compile(contents, mode)
62
- if mode == :xml
63
- block_tag = :chapter
64
- else
65
- block_tag = :div
66
- end
67
63
  result = ''
68
64
  headings = 0
69
65
  contents.each do |element|
70
66
  if element[:tag].to_s =~ /^h[1-9]$/ # block
71
67
  if headings == 0
72
- result << "<#{block_tag}>"
68
+ result << "<#{@block}>"
73
69
  else
74
- result << "</#{block_tag}><#{block_tag}>"
70
+ result << "</#{@block}><#{@block}>"
75
71
  end
76
72
  headings += 1
77
73
  end
78
74
  result << build_tag(element[:tag], element[:content], element[:attributes], mode)
79
75
  end
80
- result << "</#{block_tag}>"
76
+ result << "</#{@block}>"
81
77
  end
82
78
  def build_after_content
83
79
  nil
data/lib/ydocx/command.rb CHANGED
@@ -68,7 +68,8 @@ Usage: #{self.command} file [options]
68
68
  elsif option =~ @@help
69
69
  self.help
70
70
  else
71
- self.error "#{self.command}: exit with #{option}: Unknown option"
71
+ # added image reference support for fachinfo format
72
+ #self.error "#{self.command}: exit with #{option}: Unknown option"
72
73
  end
73
74
  else
74
75
  # default fachinfo
@@ -14,20 +14,25 @@ require 'ydocx/builder'
14
14
  module YDocx
15
15
  class Document
16
16
  attr_reader :contents, :indecies, :pictures
17
- def self.open(file)
18
- self.new(file)
17
+ def self.open(file, options={})
18
+ self.new(file, options)
19
19
  end
20
- def initialize(file)
20
+ def initialize(file, options={})
21
21
  @contents = nil
22
22
  @indecies = nil
23
23
  @pictures = []
24
+ @options = options
24
25
  @path = nil
25
26
  @files = nil
26
27
  @zip = nil
28
+ init
27
29
  read(file)
28
30
  end
31
+ def init
32
+ end
29
33
  def to_html(file='', options={})
30
34
  html = ''
35
+ options = @options.merge(options)
31
36
  @files = @path.dirname.join(@path.basename('.docx').to_s + '_files')
32
37
  Builder.new(@contents) do |builder|
33
38
  builder.title = @path.basename
@@ -50,7 +55,9 @@ module YDocx
50
55
  end
51
56
  def to_xml(file='', options={})
52
57
  xml = ''
58
+ options = @options.merge(options)
53
59
  Builder.new(@contents) do |builder|
60
+ builder.block = options.has_key?(:block) ? options[:block] : :chapter
54
61
  xml = builder.build_xml
55
62
  end
56
63
  unless file.empty?
@@ -63,9 +70,6 @@ module YDocx
63
70
  end
64
71
  end
65
72
  private
66
- def has_picture?
67
- !@pictures.empty?
68
- end
69
73
  def create_files
70
74
  FileUtils.mkdir @files unless @files.exist?
71
75
  @zip = Zip::ZipFile.open(@path.realpath)
@@ -74,26 +78,32 @@ module YDocx
74
78
  source_path = Pathname.new pic[:source] # id/filename.ext
75
79
  dir = @files.join source_path.dirname
76
80
  FileUtils.mkdir dir unless dir.exist?
77
- binary = @zip.find_entry("word/#{origin_path}").get_input_stream
78
- if source_path.extname != origin_path.extname # convert
79
- if defined? Magick::Image
80
- image = Magick::Image.from_blob(binary.read).first
81
- image.format = source_path.extname[1..-1].upcase
82
- @files.join(source_path).open('w') do |f|
83
- f.puts image.to_blob
84
- end
85
- else # copy original image
86
- @files.join(dir, origin_path.basename).open('w') do |f|
87
- f.puts binary.read
88
- end
89
- end
90
- else
81
+ organize_image(origin_path, source_path)
82
+ end
83
+ @zip.close
84
+ end
85
+ def organize_image(origin_path, source_path)
86
+ binary = @zip.find_entry("word/#{origin_path}").get_input_stream
87
+ if source_path.extname != origin_path.extname # convert
88
+ if defined? Magick::Image
89
+ image = Magick::Image.from_blob(binary.read).first
90
+ image.format = source_path.extname[1..-1].upcase
91
91
  @files.join(source_path).open('w') do |f|
92
+ f.puts image.to_blob
93
+ end
94
+ else # copy original image
95
+ @files.join(dir, origin_path.basename).open('w') do |f|
92
96
  f.puts binary.read
93
97
  end
94
98
  end
99
+ else
100
+ @files.join(source_path).open('w') do |f|
101
+ f.puts binary.read
102
+ end
95
103
  end
96
- @zip.close
104
+ end
105
+ def has_picture?
106
+ !@pictures.empty?
97
107
  end
98
108
  def read(file)
99
109
  @path = Pathname.new file
@@ -147,4 +147,31 @@ div#container {
147
147
  style.gsub(/\s\s+|\n/, ' ')
148
148
  end
149
149
  end
150
+ # == Document
151
+ # Image reference option
152
+ # Currently, this supports only all images or first one reference.
153
+ #
154
+ # $ docx2html example.docx --format fachinfo refence1.png refenece2.png
155
+ class Document
156
+ def init
157
+ @references = []
158
+ ARGV.reverse.each do |arg|
159
+ if arg =~ /\.(jpg|png|gif)$/
160
+ path = Pathname.new(arg).realpath
161
+ @references << path if path.exist?
162
+ end
163
+ end
164
+ @references.reverse unless @references.empty?
165
+ end
166
+ private
167
+ alias :copy_or_convert :organize_image
168
+ def organize_image(origin_path, source_path)
169
+ if reference = @references.shift and
170
+ File.extname(reference) == source_path.extname
171
+ FileUtils.copy reference, @files.join(source_path)
172
+ else
173
+ copy_or_convert(origin_path, source_path)
174
+ end
175
+ end
176
+ end
150
177
  end
data/lib/ydocx.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  require 'ydocx/document'
5
5
 
6
6
  module YDocx
7
- VERSION = '1.0.8'
7
+ VERSION = '1.0.9'
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.0.8
4
+ version: 1.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
16
- requirement: &25506240 !ruby/object:Gem::Requirement
16
+ requirement: &24082040 !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: *25506240
24
+ version_requirements: *24082040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
27
- requirement: &25505820 !ruby/object:Gem::Requirement
27
+ requirement: &24081620 !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: *25505820
35
+ version_requirements: *24081620
36
36
  description: ''
37
37
  email:
38
38
  - yasaka@ywesee.com, zdavatz@ywesee.com