Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip unmatched array #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-05-03 18:43:13 +0300 using RuboCop version 0.48.1.
# on 2017-05-29 13:33:52 +0300 using RuboCop version 0.49.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent
Layout/IndentHeredoc:
Exclude:
- 'spec/template_processor_spec.rb'

# Offense count: 2
Metrics/AbcSize:
Max: 54

# Offense count: 5
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 177
Max: 191

# Offense count: 23
# Offense count: 26
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 255
Max: 211

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 35
Max: 38

# Offense count: 3
Style/Documentation:
Expand All @@ -34,11 +42,3 @@ Style/Documentation:
- 'lib/docx_templater.rb'
- 'lib/docx_templater/docx_creator.rb'
- 'lib/docx_templater/template_processor.rb'

# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent
Style/IndentHeredoc:
Exclude:
- 'spec/template_processor_spec.rb'
8 changes: 5 additions & 3 deletions lib/docx_templater/docx_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module DocxTemplater
class DocxCreator
attr_reader :template_path, :template_processor

def initialize(template_path, data, escape_html = true)
def initialize(template_path, data, escape_html = true, skip_unmatched: false)
@template_path = template_path
@template_processor = TemplateProcessor.new(data, escape_html)
@template_processor = TemplateProcessor.new(data, escape_html, skip_unmatched: skip_unmatched)
end

def generate_docx_file(file_name = "output_#{Time.now.strftime('%Y-%m-%d_%H%M')}.docx")
Expand All @@ -20,7 +20,9 @@ def generate_docx_bytes
Zip::File.open(template_path).each do |entry|
entry_name = entry.name
out.put_next_entry(entry_name)
out.write(copy_or_template(entry_name, entry.get_input_stream.read))
unless entry.directory?
out.write(copy_or_template(entry_name, entry.get_input_stream.read))
end
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/docx_templater/template_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

module DocxTemplater
class TemplateProcessor
attr_reader :data, :escape_html
attr_reader :data, :escape_html, :skip_unmatched

# data is expected to be a hash of symbols => string or arrays of hashes.
def initialize(data, escape_html = true)
def initialize(data, escape_html = true, skip_unmatched: false)
@data = data
@escape_html = escape_html
@skip_unmatched = skip_unmatched
end

def render(document)
Expand Down Expand Up @@ -51,7 +52,10 @@ def enter_multiple_values(document, key)
end_row_template = xml.xpath("//w:tr[contains(., '#{end_row}')]", xml.root.namespaces).first
DocxTemplater.log("begin_row_template: #{begin_row_template}")
DocxTemplater.log("end_row_template: #{end_row_template}")
raise "unmatched template markers: #{begin_row} nil: #{begin_row_template.nil?}, #{end_row} nil: #{end_row_template.nil?}. This could be because word broke up tags with it's own xml entries. See README." unless begin_row_template && end_row_template
unless begin_row_template && end_row_template
return document if @skip_unmatched
raise "unmatched template markers: #{begin_row} nil: #{begin_row_template.nil?}, #{end_row} nil: #{end_row_template.nil?}. This could be because word broke up tags with it's own xml entries. See README."
end

row_templates = []
row = begin_row_template.next_sibling
Expand Down
16 changes: 16 additions & 0 deletions spec/template_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ module TestData
expect(out).to include('23rd <p>&amp;</p> #1 floor')
end
end
context 'when unmatched array' do
let(:unmatched_data) do
data.merge(unmatched_array: ['Some data'])
end

it 'raised' do
parser = DocxTemplater::TemplateProcessor.new(unmatched_data)
expect { parser.render(xml) }.to raise_error(RuntimeError)
end

it 'skipped' do
parser = DocxTemplater::TemplateProcessor.new(unmatched_data, skip_unmatched: true)
out = parser.render(xml)
expect(Nokogiri::XML.parse(out)).to be_xml
end
end
end

context 'unmatched begin and end row templates' do
Expand Down