Class: LMDocstache::Document

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

Constant Summary collapse

WHOLE_BLOCK_START_REGEX =
/^#{Parser::BLOCK_START_PATTERN}$/
GENERAL_TAG_REGEX =
/\{\{[\/#^]?(.+?)(?:(\s((?:==|~=))\s?.+?))?\}\}/
ROLES_REGEXP =
/({{(sig|sigfirm|date|check|text|initial)\|(req|noreq)\|(.+?)}})/
BLOCK_CHILDREN_ELEMENTS =
'w|r,w|hyperlink,w|ins,w|del'
RUN_LIKE_ELEMENTS =
'w|r,w|ins'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ Document

Returns a new instance of Document.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lm_docstache/document.rb', line 11

def initialize(*paths)
  raise ArgumentError if paths.empty?

  @path = paths.shift
  @zip_file = Zip::File.open(@path)
  @document = Nokogiri::XML(unzip_read(@zip_file, "word/document.xml"))
  zip_files = paths.map { |path| Zip::File.open(path) }
  documents = zip_files.map { |f| Nokogiri::XML(unzip_read(f, "word/document.xml")) }

  load_references
  documents.each do |doc|
    @document.css('w|p').last.after(page_break)
    @document.css('w|p').last.after(doc.css('w|body > *:not(w|sectPr)'))
  end

  find_documents_to_interpolate
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



9
10
11
# File 'lib/lm_docstache/document.rb', line 9

def document
  @document
end

Instance Method Details

#errors?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/lm_docstache/document.rb', line 79

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

#fix_errorsObject



75
76
77
# File 'lib/lm_docstache/document.rb', line 75

def fix_errors
  problem_paragraphs.each { |pg| flatten_text_blocks(pg) if pg }
end

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



88
89
90
91
# File 'lib/lm_docstache/document.rb', line 88

def render_file(output, data = {}, render_options = {})
  buffer = zip_buffer(render_documents(data, nil, render_options))
  File.open(output, "w") { |f| f.write buffer.string }
end

#render_replace(output, text) ⇒ Object



93
94
95
96
# File 'lib/lm_docstache/document.rb', line 93

def render_replace(output, text)
  buffer = zip_buffer(render_documents({}, text))
  File.open(output, "w") { |f| f.write buffer.string }
end

#render_stream(data = {}) ⇒ Object



98
99
100
101
102
# File 'lib/lm_docstache/document.rb', line 98

def render_stream(data = {})
  buffer = zip_buffer(render_documents(data))
  buffer.rewind
  buffer.sysread
end

#render_xml(data = {}, render_options = {}) ⇒ Object



104
105
106
# File 'lib/lm_docstache/document.rb', line 104

def render_xml(data = {}, render_options = {})
  render_documents(data, nil, render_options)
end

#save(path = @path) ⇒ Object



83
84
85
86
# File 'lib/lm_docstache/document.rb', line 83

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

#tagsObject



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

def tags
  @documents.values.flat_map do |document|
    document_text = document.text
    extract_tag_names(document_text) + extract_tag_names(document_text, :full_block)
  end
end

#unusable_tagsObject



67
68
69
70
71
72
73
# File 'lib/lm_docstache/document.rb', line 67

def unusable_tags
  usable_tags.reduce(tags) do |broken_tags, usable_tag|
    next broken_tags unless index = broken_tags.index(usable_tag)

    broken_tags.delete_at(index) && broken_tags
  end
end

#usable_role_tagsObject



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

def usable_role_tags
  @documents.values.flat_map do |document|
    document.css('w|t')
      .select { |tag| tag.text =~ ROLES_REGEXP }
      .flat_map { |tag|
        tag.text.scan(ROLES_REGEXP)
          .map {|r| r.first }
      }
  end
end

#usable_tag_namesObject



58
59
60
61
62
63
64
65
# File 'lib/lm_docstache/document.rb', line 58

def usable_tag_names
  usable_tags.reduce([]) do |memo, tag|
    next memo if !tag.is_a?(Regexp) && tag =~ ROLES_REGEXP

    tag = unescape_escaped_start_block(tag.source) if tag.is_a?(Regexp)
    memo << (tag.scan(GENERAL_TAG_REGEX) && $1)
  end.compact.uniq
end

#usable_tagsObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/lm_docstache/document.rb', line 47

def usable_tags
  @documents.values.reduce([]) do |tags, document|
    document.css('w|t').reduce(tags) do |document_tags, text_node|
      text = text_node.text
      document_tags.push(*extract_tag_names(text))
      document_tags.push(*extract_tag_names(text, :start_block))
      document_tags.push(*extract_tag_names(text, :full_block))
    end
  end
end