Class: LMDocstache::Document
- Inherits:
-
Object
- Object
- LMDocstache::Document
- Defined in:
- lib/lm_docstache/document.rb
Instance Method Summary collapse
- #errors? ⇒ Boolean
- #fix_errors ⇒ Object
-
#initialize(*paths) ⇒ Document
constructor
A new instance of Document.
- #render_file(output, data = {}) ⇒ Object
- #render_replace(output, text) ⇒ Object
- #render_stream(data = {}) ⇒ Object
- #render_xml(data = {}) ⇒ Object
- #save(path = @path) ⇒ Object
- #tags ⇒ Object
- #unusable_tags ⇒ Object
- #usable_tag_names ⇒ Object
- #usable_tags ⇒ Object
Constructor Details
#initialize(*paths) ⇒ Document
Returns a new instance of Document.
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
52 53 54 |
# File 'lib/lm_docstache/document.rb', line 52 def errors? .length != .length end |
#fix_errors ⇒ Object
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 |
#tags ⇒ Object
18 19 20 21 22 |
# File 'lib/lm_docstache/document.rb', line 18 def @documents.values.flat_map do |document| document.text.strip.scan(/\{\{.+?\}\}/) end end |
#unusable_tags ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/lm_docstache/document.rb', line 39 def = .each do |usable_tag| index = .index(usable_tag) .delete_at(index) if index end return end |
#usable_tag_names ⇒ Object
32 33 34 35 36 37 |
# File 'lib/lm_docstache/document.rb', line 32 def usable_tag_names self..map do |tag| tag.scan(/\{\{[\/#^]?(.+?)(?:(\s((?:==|~=))\s?.+?))?\}\}/) $1 end.compact.uniq end |
#usable_tags ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/lm_docstache/document.rb', line 24 def @documents.values.flat_map do |document| document.css('w|t') .select { |tag| tag.text =~ /\{\{.+?\}\}/ } .flat_map { |tag| tag.text.scan(/\{\{.+?\}\}/) } end end |