class YDocx::Document
Attributes
builder[R]
contents[R]
images[R]
indecies[R]
parser[R]
path[R]
Public Class Methods
new(file, options={})
click to toggle source
# File lib/ydocx/document.rb, line 21 def initialize(file, options={}) @parser = nil @builder = nil @contents = nil @indecies = [] @references = [] @images = [] @options = options @path = Pathname.new('.') @files = nil @zip = nil init read(file) end
open(file, options={})
click to toggle source
# File lib/ydocx/document.rb, line 18 def self.open(file, options={}) self.new(file, options) end
Public Instance Methods
init()
click to toggle source
# File lib/ydocx/document.rb, line 35 def init end
output_directory()
click to toggle source
# File lib/ydocx/document.rb, line 37 def output_directory @files ||= @path.dirname.join(@path.basename('.docx').to_s + '_files') end
output_file(ext)
click to toggle source
# File lib/ydocx/document.rb, line 40 def output_file(ext) @path.sub_ext(".#{ext.to_s}") end
to_html(output=false, options={})
click to toggle source
# File lib/ydocx/document.rb, line 43 def to_html(output=false, options={}) html = '' options = @options.merge(options) files = output_directory @builder = Builder.new(@contents) do |builder| builder.title = @path.basename builder.files = files.relative_path_from(files.dirname) builder.style = options[:style] if options.has_key?(:style) # option builder.indecies = @indecies.dup builder.references = @references.dup html = builder.build_html end if output create_files if has_image? html_file = output_file(:html) File.open(html_file, 'w:utf-8') do |f| f.puts html end end html end
to_xml(output=false, options={})
click to toggle source
# File lib/ydocx/document.rb, line 66 def to_xml(output=false, options={}) xml = '' options = @options.merge(options) Builder.new(@contents) do |builder| builder.block = options.has_key?(:block) ? options[:block] : :chapter xml = builder.build_xml end if output xml_file = output_file(:xml) mkdir xml_file.parent File.open(xml_file, 'w:utf-8') do |f| f.puts xml end end xml end
Private Instance Methods
copy_or_convert(origin_path, source_path)
NOTE Image reference option Currently, this supports only all images or first one reference.
$ docx2html example.docx –format fachinfo refence1.png refenece2.png
Alias for: organize_image
create_files()
click to toggle source
# File lib/ydocx/document.rb, line 83 def create_files files_dir = output_directory mkdir Pathname.new(files_dir) unless files_dir.exist? @zip = Zip::File.open(@path.realpath) @images.each do |image| origin_path = Pathname.new image[:origin] # media/filename.ext source_path = Pathname.new image[:source] # images/filename.ext image_dir = files_dir.join source_path.dirname FileUtils.mkdir image_dir unless image_dir.exist? organize_image(origin_path, source_path) end @zip.close end
has_image?()
click to toggle source
# File lib/ydocx/document.rb, line 116 def has_image? !@images.empty? end
mkdir(path)
click to toggle source
# File lib/ydocx/document.rb, line 132 def mkdir(path) return if path.exist? parent = path.parent mkdir(parent) FileUtils.mkdir(path) end
optional_copy(source_path)
click to toggle source
# File lib/ydocx/templates/fachinfo.rb, line 278 def optional_copy(source_path) end
organize_image(origin_path, source_path)
click to toggle source
# File lib/ydocx/document.rb, line 96 def organize_image(origin_path, source_path) binary = @zip.find_entry("word/#{origin_path}").get_input_stream if source_path.extname != origin_path.extname # convert if defined? Magick::Image image = Magick::Image.from_blob(binary.read).first image.format = source_path.extname[1..-1].upcase output_directory.join(source_path).open('wb') do |f| f.puts image.to_blob end else # copy original image output_directory.join(dir, origin_path.basename).open('wb') do |f| f.puts binary.read end end else output_directory.join(source_path).open('wb') do |f| f.puts binary.read end end end
Also aliased as: copy_or_convert
prepare_reference()
click to toggle source
# File lib/ydocx/templates/fachinfo.rb, line 297 def prepare_reference ARGV.reverse.each do |arg| if arg.downcase =~ /\.(jpeg|jpg|png|gif)$/ path = Pathname.new(arg) @references << path.realpath if path.exist? end end @references.reverse unless @references.empty? end
read(file)
click to toggle source
# File lib/ydocx/document.rb, line 119 def read(file) @path = Pathname.new file @zip = Zip::File.open(@path.realpath) doc = @zip.find_entry('word/document.xml').get_input_stream rel = @zip.find_entry('word/_rels/document.xml.rels').get_input_stream @parser = Parser.new(doc, rel) do |parser| parser.lang = @options[:lang] if @options[:lang] @contents = parser.parse @indecies = parser.indecies @images = parser.images end @zip.close end