File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ def properties
1010 @node . at_xpath ( "./#{ @properties_tag } " )
1111 end
1212
13+ # TODO: Maybe merge and then clear so there is only one text node left.
1314 def blank!
1415 @node . xpath ( ".//w:t" ) . each { |t | t . content = '' }
1516 end
Original file line number Diff line number Diff line change @@ -23,6 +23,27 @@ def insert_text_after(text)
2323 text_run . text = "#{ text_run . text } #{ text } "
2424 end
2525
26+ def insert_multiple_lines ( text_array )
27+ # Hold paragraphs to be inserted into, corresponding to the index of the strings in the text array
28+ paragraphs = [ ]
29+ paragraph = self . parent_paragraph
30+ # Remove text from paragraph
31+ paragraph . blank!
32+ paragraphs << paragraph
33+ for i in 0 ..( text_array . size - 1 )
34+ # Copy previous paragraph
35+ new_p = paragraphs [ i ] . copy
36+ # Insert as sibling of previous paragraph
37+ new_p . insert_after ( paragraphs [ i ] )
38+ paragraphs << new_p
39+ end
40+
41+ # Insert text into corresponding newly created paragraphs
42+ paragraphs . each_index do |index |
43+ paragraphs [ index ] . text = text_array [ index ]
44+ end
45+ end
46+
2647 def get_run_before
2748 # at_xpath returns the first match found and preceding-sibling returns siblings in the
2849 # order they appear in the document not the order as they appear when moving out from
Original file line number Diff line number Diff line change 11require 'nokogiri'
2+ require 'docx/elements'
3+ require 'docx/containers'
24
35module Docx
46 module Elements
@@ -19,8 +21,8 @@ def parent(type = '*')
1921 end
2022
2123 # TODO: Should create a docx paragraph from this
22- def paragraph
23- parent ( 'w:p' )
24+ def parent_paragraph
25+ Elements :: Containers :: Paragraph . new ( parent ( 'w:p' ) )
2426 end
2527
2628 # Insertion methods
Original file line number Diff line number Diff line change @@ -40,6 +40,24 @@ def test_inserting_text_after_bookmark
4040 assert_equal 'test textbar' , @doc . paragraphs . first . text
4141 end
4242
43+ def test_inserting_multiple_lines_at_bookmark
44+ assert_equal '' , @doc . paragraphs . last . text
45+ new_lines = [ 'replacement test' , 'second paragraph test' , 'and a third paragraph test' ]
46+ @doc . bookmarks [ 'isolated_bookmark' ] . insert_multiple_lines ( new_lines )
47+ new_lines . each_index do |line |
48+ assert_equal new_lines [ line ] , @doc . paragraphs [ line + 2 ] . text
49+ end
50+ end
51+
52+ def test_inserting_multiple_lines_at_bookmark_with_replacement
53+ assert_equal 'placeholder text' , @doc . paragraphs [ 1 ] . text
54+ new_lines = [ 'replacement test' , 'second paragraph test' , 'and a third paragraph test' ]
55+ @doc . bookmarks [ 'word_splitting_bookmark' ] . insert_multiple_lines ( new_lines )
56+ new_lines . each_index do |line |
57+ assert_equal new_lines [ line ] , @doc . paragraphs [ line + 1 ] . text
58+ end
59+ end
60+
4361 # Insert text intelligently around bookmark
4462 def test_inserting_text_around_bookmark
4563
You can’t perform that action at this time.
0 commit comments