Class: LMDocstache::Document
- Inherits:
-
Object
- Object
- LMDocstache::Document
- 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
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
- #errors? ⇒ Boolean
- #fix_errors ⇒ Object
-
#initialize(*paths) ⇒ Document
constructor
A new instance of Document.
- #render_file(output, data = {}, render_options = {}) ⇒ Object
- #render_replace(output, text) ⇒ Object
- #render_stream(data = {}) ⇒ Object
- #render_xml(data = {}, render_options = {}) ⇒ Object
- #save(path = @path) ⇒ Object
- #tags ⇒ Object
- #unusable_tags ⇒ Object
- #usable_role_tags ⇒ Object
- #usable_tag_names ⇒ Object
- #usable_tags ⇒ Object
Constructor Details
#initialize(*paths) ⇒ Document
Returns a new instance of Document.
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
#document ⇒ Object (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
79 80 81 |
# File 'lib/lm_docstache/document.rb', line 79 def errors? .length != .length end |
#fix_errors ⇒ Object
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 = {}, = {}) buffer = zip_buffer(render_documents(data, nil, )) 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_documents(data, nil, ) 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 |
#tags ⇒ Object
40 41 42 43 44 45 |
# File 'lib/lm_docstache/document.rb', line 40 def @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_tags ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/lm_docstache/document.rb', line 67 def .reduce() do |, usable_tag| next unless index = .index(usable_tag) .delete_at(index) && end end |
#usable_role_tags ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lm_docstache/document.rb', line 29 def @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_names ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/lm_docstache/document.rb', line 58 def usable_tag_names .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_tags ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lm_docstache/document.rb', line 47 def @documents.values.reduce([]) do |, document| document.css('w|t').reduce() do |, text_node| text = text_node.text .push(*extract_tag_names(text)) .push(*extract_tag_names(text, :start_block)) .push(*extract_tag_names(text, :full_block)) end end end |