Class: LMDocstache::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/lm_docstache/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ Document

Returns a new instance of Document.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/lm_docstache/document.rb', line 3

def initialize(*paths)
  raise ArgumentError if paths.empty?
  @path = paths.shift
  @zip_file = Zip::File.open(@path)
  load_references
  @document = Nokogiri::XML(unzip_read(@zip_file, "word/document.xml"))
  zip_files = paths.map{|p| Zip::File.open(p)}
  documents = zip_files.map{|f| Nokogiri::XML(unzip_read(f, "word/document.xml"))}
  documents.each do |doc|
    @document.css('w|p').last.add_next_sibling(page_break)
    @document.css('w|p').last.add_next_sibling(doc.css('w|body > *:not(w|sectPr)'))
  end
  find_documents_to_interpolate
end

Instance Method Details

#errors?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/lm_docstache/document.rb', line 52

def errors?
  tags.length != usable_tags.length
end

#fix_errorsObject



48
49
50
# File 'lib/lm_docstache/document.rb', line 48

def fix_errors
  problem_paragraphs.each { |p| flatten_paragraph(p) if p.present? }
end

#render_file(output, data = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/lm_docstache/document.rb', line 61

def render_file(output, data={})
  rendered_documents = Hash[
    @documents.map do |(path, document)|
      [path, LMDocstache::Renderer.new(document.dup, data).render]
    end
  ]
  buffer = zip_buffer(rendered_documents)
  File.open(output, "w") { |f| f.write buffer.string }
end

#render_replace(output, text) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/lm_docstache/document.rb', line 71

def render_replace(output, text)
  rendered_documents = Hash[
    @documents.map do |(path, document)|
      [path, LMDocstache::Renderer.new(document.dup, {}).render_replace(text)]
    end
  ]
  buffer = zip_buffer(rendered_documents)
  File.open(output, "w") { |f| f.write buffer.string }
end

#render_stream(data = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/lm_docstache/document.rb', line 81

def render_stream(data={})
  rendered_documents = Hash[
    @documents.map do |(path, document)|
      [path, LMDocstache::Renderer.new(document.dup, data).render]
    end
  ]
  buffer = zip_buffer(rendered_documents)
  buffer.rewind
  return buffer.sysread
end

#render_xml(data = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/lm_docstache/document.rb', line 92

def render_xml(data={})
  rendered_documents = Hash[
    @documents.map do |(path, document)|
      [path, LMDocstache::Renderer.new(document.dup, data).render]
    end
  ]

  rendered_documents
end

#save(path = @path) ⇒ Object



56
57
58
59
# File 'lib/lm_docstache/document.rb', line 56

def save(path = @path)
  buffer = zip_buffer(@documents)
  File.open(path, "w") { |f| f.write buffer.string }
end

#tagsObject



18
19
20
21
22
# File 'lib/lm_docstache/document.rb', line 18

def tags
  @documents.values.flat_map do |document|
    document.text.strip.scan(/\{\{.+?\}\}/)
  end
end

#unusable_tagsObject



39
40
41
42
43
44
45
46
# File 'lib/lm_docstache/document.rb', line 39

def unusable_tags
  unusable_tags = tags
  usable_tags.each do |usable_tag|
    index = unusable_tags.index(usable_tag)
    unusable_tags.delete_at(index) if index
  end
  return unusable_tags
end

#usable_tag_namesObject



32
33
34
35
36
37
# File 'lib/lm_docstache/document.rb', line 32

def usable_tag_names
  self.usable_tags.map do |tag|
    tag.scan(/\{\{[\/#^]?(.+?)(?:(\s((?:==|~=))\s?.+?))?\}\}/)
    $1
  end.compact.uniq
end

#usable_tagsObject



24
25
26
27
28
29
30
# File 'lib/lm_docstache/document.rb', line 24

def usable_tags
  @documents.values.flat_map do |document|
    document.css('w|t')
      .select { |tag| tag.text =~ /\{\{.+?\}\}/ }
      .flat_map { |tag| tag.text.scan(/\{\{.+?\}\}/) }
  end
end