From 50b51e3e1d630d3a6307d8da35f0fefefbf8aaca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D0=B5=D0=B4=D0=BE=D1=81=D0=BE=D0=B2=20=D0=A1=D0=B5?= =?UTF-8?q?=D1=80=D0=B3=D0=B5=D0=B9?= Date: Mon, 29 May 2017 13:34:30 +0300 Subject: [PATCH 1/2] skip unmatched array --- .rubocop_todo.yml | 26 ++++++++++++------------ lib/docx_templater/docx_creator.rb | 8 +++++--- lib/docx_templater/template_processor.rb | 10 ++++++--- spec/template_processor_spec.rb | 16 +++++++++++++++ 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2979906..a5baa9e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,11 +1,19 @@ # 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 @@ -13,18 +21,18 @@ Metrics/AbcSize: # 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: @@ -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' diff --git a/lib/docx_templater/docx_creator.rb b/lib/docx_templater/docx_creator.rb index c4e4c9a..606cbd0 100644 --- a/lib/docx_templater/docx_creator.rb +++ b/lib/docx_templater/docx_creator.rb @@ -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") @@ -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 diff --git a/lib/docx_templater/template_processor.rb b/lib/docx_templater/template_processor.rb index ae12348..633d2fe 100644 --- a/lib/docx_templater/template_processor.rb +++ b/lib/docx_templater/template_processor.rb @@ -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) @@ -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 diff --git a/spec/template_processor_spec.rb b/spec/template_processor_spec.rb index ed20170..b708d3b 100644 --- a/spec/template_processor_spec.rb +++ b/spec/template_processor_spec.rb @@ -75,6 +75,22 @@ module TestData expect(out).to include('23rd

&

#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 + 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 From 044d1ab893345eb22d62153621072110179ebc92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D0=B5=D0=B4=D0=BE=D1=81=D0=BE=D0=B2=20=D0=A1=D0=B5?= =?UTF-8?q?=D1=80=D0=B3=D0=B5=D0=B9?= Date: Wed, 31 May 2017 17:08:48 +0300 Subject: [PATCH 2/2] prevent rspec warning --- spec/template_processor_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/template_processor_spec.rb b/spec/template_processor_spec.rb index b708d3b..00a43d3 100644 --- a/spec/template_processor_spec.rb +++ b/spec/template_processor_spec.rb @@ -82,7 +82,7 @@ module TestData it 'raised' do parser = DocxTemplater::TemplateProcessor.new(unmatched_data) - expect { parser.render(xml) }.to raise_error + expect { parser.render(xml) }.to raise_error(RuntimeError) end it 'skipped' do