Class: Docx::Builder::Template
- Inherits:
-
Object
- Object
- Docx::Builder::Template
- Defined in:
- lib/docx/builder/template.rb
Overview
Represents a docx erb template
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
Instance Method Summary collapse
-
#build_erb_template(document) ⇒ Object
Replaces the docx-builder's tags for those ones currently used in erb files.
-
#clean(document) ⇒ Object
Cleans the xml nodes in the middle of the variables.
-
#initialize(path) ⇒ Template
constructor
A new instance of Template.
-
#render(data) ⇒ Object
Renders the hash data in the template xml.
- #render_erb_template(document, data) ⇒ Object
-
#save(path, new_document) ⇒ Object
Saves the rendered template in a file.
-
#save_as_buffer(new_document) ⇒ Object
Saves the rendered template in a memory stream (StringIO).
Constructor Details
#initialize(path) ⇒ Template
Returns a new instance of Template.
9 10 11 12 13 14 15 |
# File 'lib/docx/builder/template.rb', line 9 def initialize(path) @document = Docx::Builder::Decoder.to_xml(path) @sections = ['word/document.xml'] @document.each do |file| @sections.push(file.name) if file.name.include? 'word/header' end end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
7 8 9 |
# File 'lib/docx/builder/template.rb', line 7 def document @document end |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
7 8 9 |
# File 'lib/docx/builder/template.rb', line 7 def sections @sections end |
Instance Method Details
#build_erb_template(document) ⇒ Object
Replaces the docx-builder's tags for those ones currently used in erb files
37 38 39 40 41 |
# File 'lib/docx/builder/template.rb', line 37 def build_erb_template(document) ret = document.gsub(/\{\{%/, '<%=').gsub(/%\}\}/, '%>') ret = ret.gsub(/\{\{t\s(.*)\st\}\}/) { |_| build_dynamic_table(Regexp.last_match(1)) } ret.gsub(/\{\{/, '<%').gsub(/\}\}/, '%>') end |
#clean(document) ⇒ Object
Cleans the xml nodes in the middle of the variables
31 32 33 34 |
# File 'lib/docx/builder/template.rb', line 31 def clean(document) document.force_encoding(Encoding::UTF_8) if document.respond_to?(:force_encoding) Docx::Builder::XmlProcessor.clean_variables(document) end |
#render(data) ⇒ Object
Renders the hash data in the template xml
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/docx/builder/template.rb', line 18 def render(data) @sections.map do |section| Docx::Builder.logger.debug("Cleaning template variables for section #{section}") cleaned_document = clean(@document.read(section)) Docx::Builder.logger.debug("Replacing template tags ('{{%' -> '<%') for section #{section}") erb_template = build_erb_template(cleaned_document) Docx::Builder.logger.debug("Rendering template for section #{section} with data") processed_document = render_erb_template(erb_template, data) [section, processed_document] end.to_h end |
#render_erb_template(document, data) ⇒ Object
43 44 45 46 |
# File 'lib/docx/builder/template.rb', line 43 def render_erb_template(document, data) erb_template = ERB.new(document) erb_template.result_with_hash(data) end |
#save(path, new_document) ⇒ Object
Saves the rendered template in a file
49 50 51 52 53 54 55 56 |
# File 'lib/docx/builder/template.rb', line 49 def save(path, new_document) Docx::Builder.logger.info("Saving output file into path #{path}") File.open(path, 'wb') do |f| buffer = Docx::Builder::Encoder.build_docx_buffer(@document, new_document) f.write(buffer.string) end @document.close end |
#save_as_buffer(new_document) ⇒ Object
Saves the rendered template in a memory stream (StringIO)
59 60 61 62 63 64 |
# File 'lib/docx/builder/template.rb', line 59 def save_as_buffer(new_document) Docx::Builder.logger.info('Saving output file as buffer') buffer = Docx::Builder::Encoder.build_docx_buffer(@document, new_document) @document.close buffer end |