Class: DocxEdit::Docx
- Inherits:
-
Object
- Object
- DocxEdit::Docx
- Defined in:
- lib/docxedit/docx.rb
Instance Attribute Summary collapse
-
#xml_document ⇒ Object
readonly
Returns the value of attribute xml_document.
-
#xml_footers ⇒ Object
readonly
Returns the value of attribute xml_footers.
-
#xml_headers ⇒ Object
readonly
Returns the value of attribute xml_headers.
-
#zip_file ⇒ Object
readonly
Returns the value of attribute zip_file.
Instance Method Summary collapse
-
#commit ⇒ Object
Persist changes in the Zip file.
- #contains?(text) ⇒ Boolean
- #files ⇒ Object
- #find_block_with_content(exact_content_string) ⇒ Object
-
#initialize(path) ⇒ Docx
constructor
A new instance of Docx.
-
#insert_block(position, anchor_block, new_block) ⇒ Object
insert the xml of a content block :before or :after the anchor_block.
- #remove_block(block) ⇒ Object
- #replace(reg_to_match, replacement) ⇒ Object
Constructor Details
#initialize(path) ⇒ Docx
Returns a new instance of Docx.
10 11 12 13 |
# File 'lib/docxedit/docx.rb', line 10 def initialize(path) @zip_file = Zip::ZipFile.new(path) bind_contents end |
Instance Attribute Details
#xml_document ⇒ Object (readonly)
Returns the value of attribute xml_document.
8 9 10 |
# File 'lib/docxedit/docx.rb', line 8 def xml_document @xml_document end |
#xml_footers ⇒ Object (readonly)
Returns the value of attribute xml_footers.
8 9 10 |
# File 'lib/docxedit/docx.rb', line 8 def @xml_footers end |
#xml_headers ⇒ Object (readonly)
Returns the value of attribute xml_headers.
8 9 10 |
# File 'lib/docxedit/docx.rb', line 8 def xml_headers @xml_headers end |
#zip_file ⇒ Object (readonly)
Returns the value of attribute zip_file.
8 9 10 |
# File 'lib/docxedit/docx.rb', line 8 def zip_file @zip_file end |
Instance Method Details
#commit ⇒ Object
Persist changes in the Zip file
29 30 31 |
# File 'lib/docxedit/docx.rb', line 29 def commit write_content end |
#contains?(text) ⇒ Boolean
19 20 21 22 23 24 25 26 |
# File 'lib/docxedit/docx.rb', line 19 def contains?(text) files.each do |f| REXML::XPath.each f, XPATH_ALL_TEXT_NODE do |n| return true if n.text =~ text end end return false end |
#files ⇒ Object
15 16 17 |
# File 'lib/docxedit/docx.rb', line 15 def files return @xml_headers + @xml_footers + [@xml_document] end |
#find_block_with_content(exact_content_string) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/docxedit/docx.rb', line 41 def find_block_with_content(exact_content_string) files.each do node = REXML::XPath.first(@xml_document, "//w:p[descendant-or-self::*[text()='#{exact_content_string}']]") return ContentBlock.new(node, exact_content_string) unless node.nil? end end |
#insert_block(position, anchor_block, new_block) ⇒ Object
insert the xml of a content block :before or :after the anchor_block
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/docxedit/docx.rb', line 49 def insert_block(position, anchor_block, new_block) case position when :before anchor_block.xml.previous_sibling = new_block.xml when :after anchor_block.xml.next_sibling = new_block.xml else raise "position argument must be one of :before, :after" end end |
#remove_block(block) ⇒ Object
60 61 62 |
# File 'lib/docxedit/docx.rb', line 60 def remove_block(block) block.xml.remove end |
#replace(reg_to_match, replacement) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/docxedit/docx.rb', line 33 def replace(reg_to_match, replacement) files.each do |f| REXML::XPath.each f, XPATH_ALL_TEXT_NODE do |n| n.text = n.text.gsub(reg_to_match, replacement) end end end |