Class: YDocx::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/ydocx/document.rb,
lib/ydocx/templates/patinfo.rb,
lib/ydocx/templates/fachinfo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Document

Returns a new instance of Document.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



16
17
18
# File 'lib/ydocx/document.rb', line 16

def builder
  @builder
end

#contentsObject (readonly)

Returns the value of attribute contents.



16
17
18
# File 'lib/ydocx/document.rb', line 16

def contents
  @contents
end

#imagesObject (readonly)

Returns the value of attribute images.



16
17
18
# File 'lib/ydocx/document.rb', line 16

def images
  @images
end

#indeciesObject (readonly)

Returns the value of attribute indecies.



16
17
18
# File 'lib/ydocx/document.rb', line 16

def indecies
  @indecies
end

#parserObject (readonly)

Returns the value of attribute parser.



16
17
18
# File 'lib/ydocx/document.rb', line 16

def parser
  @parser
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/ydocx/document.rb', line 16

def path
  @path
end

Class Method Details

.open(file, options = {}) ⇒ Object



18
19
20
# File 'lib/ydocx/document.rb', line 18

def self.open(file, options={})
  self.new(file, options)
end

Instance Method Details

#initObject



35
36
# File 'lib/ydocx/document.rb', line 35

def init
end

#output_directoryObject



37
38
39
# File 'lib/ydocx/document.rb', line 37

def output_directory
  @files ||= @path.dirname.join(@path.basename('.docx').to_s + '_files')
end

#output_file(ext) ⇒ Object

html



258
259
260
# File 'lib/ydocx/templates/fachinfo.rb', line 258

def output_file(ext)
  @path.sub_ext(".#{ext.to_s}")
end

#to_html(output = false, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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 = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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