From fe4b5ea4380e28c753a1bff6813fb55caf73793a Mon Sep 17 00:00:00 2001 From: corsair Date: Sat, 11 Dec 2021 16:33:22 +0300 Subject: [PATCH 1/3] Fix new line conversion --- lib/docx/newline_replacer.rb | 13 ++++++++----- spec/functional/newline_conversion_spec.rb | 4 ++-- spec/lib/docx/newline_replacer_spec.rb | 5 ++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/docx/newline_replacer.rb b/lib/docx/newline_replacer.rb index e3f451e..198f2a9 100644 --- a/lib/docx/newline_replacer.rb +++ b/lib/docx/newline_replacer.rb @@ -26,27 +26,30 @@ def replace_text_node_or_continue_walking(node, child) end def replace_text_node(parent, node) + grand_parent = parent.parent list_of_new_nodes(node).reverse.each do |new_node| - parent.insert_after(node, new_node) + grand_parent.insert_after(parent, new_node) end - node.remove + parent.remove end def line_break_node - br = REXML::Element.new('w:br') + REXML::Element.new('w:br') end def list_of_new_nodes(node) node.to_s.split("\n") .map{|str| str_to_text_node(str)} - .flat_map{ |txt| [txt, line_break_node] }[0..-2] + .flat_map{ |txt_node| [txt_node, line_break_node] }[0..-2] end def str_to_text_node(str) respect_whitespace = true parent = nil raw_text = true - REXML::Text.new(str, respect_whitespace, parent, raw_text) + t = REXML::Element.new('w:t') + text = REXML::Text.new(str, respect_whitespace, parent, raw_text) + t.add_text text end end end diff --git a/spec/functional/newline_conversion_spec.rb b/spec/functional/newline_conversion_spec.rb index 0b25b7f..c47c6ce 100644 --- a/spec/functional/newline_conversion_spec.rb +++ b/spec/functional/newline_conversion_spec.rb @@ -17,7 +17,7 @@ let(:options){ {convert_newlines: true} } it "can convert newlines with docx equivalents" do body.should_not include("||quotes||") - body.should include("Be excellent to eachother ~Bill and TedTyping is not the bottlneckDo something awesome.") + body.should include("Be excellent to eachother ~Bill and TedTyping is not the bottlneckDo something awesome.") end it "does not double escape special characters" do @@ -37,7 +37,7 @@ context "default" do let(:options){ {} } it "converts newlines" do - body.should include("Bill and TedTyping") + body.should include("Bill and TedTyping") end end end diff --git a/spec/lib/docx/newline_replacer_spec.rb b/spec/lib/docx/newline_replacer_spec.rb index e043e8c..c617fee 100644 --- a/spec/lib/docx/newline_replacer_spec.rb +++ b/spec/lib/docx/newline_replacer_spec.rb @@ -7,7 +7,7 @@ it "it replaces \\n with " do replacer.replace - xml_doc.to_s.should include("LeslieKnope") + xml_doc.to_s.should include("LeslieKnope") end context "multiple newlines" do @@ -16,8 +16,7 @@ it "replaces all newlines in a single node" do replacer.replace str = xml_doc.to_s - str.should include("LeslieKnope") - str.should include("BenWyatt") + str.should include("LeslieKnopelovesBenWyatt") end end end From a1aaca35adb2a4042ba1ffcb6be60a0ee51679f9 Mon Sep 17 00:00:00 2001 From: corsair Date: Sat, 11 Dec 2021 16:47:37 +0300 Subject: [PATCH 2/3] Process only nodes with "\n" --- lib/docx/newline_replacer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/docx/newline_replacer.rb b/lib/docx/newline_replacer.rb index 198f2a9..971822d 100644 --- a/lib/docx/newline_replacer.rb +++ b/lib/docx/newline_replacer.rb @@ -19,7 +19,7 @@ def walk(node) def replace_text_node_or_continue_walking(node, child) if child.node_type == :text - replace_text_node(node, child) + replace_text_node(node, child) if child.to_s.include?("\n") else walk(child) end From d8095b88515b09be183510f91d1c05bac1fb0172 Mon Sep 17 00:00:00 2001 From: corsair Date: Wed, 27 Apr 2022 10:12:03 +0300 Subject: [PATCH 3/3] replace new lines in word text nodes (``````) only --- lib/docx/newline_replacer.rb | 4 +++- spec/lib/docx/newline_replacer_spec.rb | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/docx/newline_replacer.rb b/lib/docx/newline_replacer.rb index 971822d..1aa45e5 100644 --- a/lib/docx/newline_replacer.rb +++ b/lib/docx/newline_replacer.rb @@ -19,7 +19,9 @@ def walk(node) def replace_text_node_or_continue_walking(node, child) if child.node_type == :text - replace_text_node(node, child) if child.to_s.include?("\n") + if (node.node_type == :element && node.expanded_name == 'w:t') && child.to_s.include?("\n") + replace_text_node(node, child) + end else walk(child) end diff --git a/spec/lib/docx/newline_replacer_spec.rb b/spec/lib/docx/newline_replacer_spec.rb index c617fee..dcfa31f 100644 --- a/spec/lib/docx/newline_replacer_spec.rb +++ b/spec/lib/docx/newline_replacer_spec.rb @@ -1,15 +1,21 @@ require 'spec_helper' +require 'spec_helper' describe Docx::NewlineReplacer do - let(:xml_str){ "Leslie\nKnope" } + let(:xml_str){ "Leslie\nKnope\n " } let(:xml_doc){ REXML::Document.new(xml_str) } let(:replacer){ Docx::NewlineReplacer.new(xml_doc) } - it "it replaces \\n with " do + it "it replaces \\n with in text nodes" do replacer.replace xml_doc.to_s.should include("LeslieKnope") end + it "it does not replace \\n with in not text nodes" do + replacer.replace + xml_doc.to_s.should include("\n ") + end + context "multiple newlines" do let(:xml_str){ "Leslie\nKnope\nloves\nBen\nWyatt" }