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.
-
#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
- #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.
9 10 11 12 |
# File 'lib/docxedit/docx.rb', line 9 def initialize(path) @zip_file = Zip::ZipFile.new(path) @xml_document = REXML::Document.new(read_content) end |
Instance Attribute Details
#xml_document ⇒ Object (readonly)
Returns the value of attribute xml_document.
7 8 9 |
# File 'lib/docxedit/docx.rb', line 7 def xml_document @xml_document end |
#zip_file ⇒ Object (readonly)
Returns the value of attribute zip_file.
7 8 9 |
# File 'lib/docxedit/docx.rb', line 7 def zip_file @zip_file end |
Instance Method Details
#commit ⇒ Object
Persist changes in the Zip file
22 23 24 |
# File 'lib/docxedit/docx.rb', line 22 def commit write_content end |
#contains?(text) ⇒ Boolean
14 15 16 17 18 19 |
# File 'lib/docxedit/docx.rb', line 14 def contains?(text) REXML::XPath.each @xml_document, XPATH_ALL_TEXT_NODE do |n| return true if n.text =~ text end return false end |
#find_block_with_content(exact_content_string) ⇒ Object
32 33 34 35 |
# File 'lib/docxedit/docx.rb', line 32 def find_block_with_content(exact_content_string) node = REXML::XPath.first(@xml_document, "//w:p[descendant-or-self::*[text()='#{exact_content_string}']]") return ContentBlock.new(node, exact_content_string) end |
#insert_block(position, anchor_block, new_block) ⇒ Object
insert the xml of a content block :before or :after the anchor_block
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/docxedit/docx.rb', line 38 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
49 50 51 |
# File 'lib/docxedit/docx.rb', line 49 def remove_block(block) block.xml.remove end |
#replace(reg_to_match, replacement) ⇒ Object
26 27 28 29 30 |
# File 'lib/docxedit/docx.rb', line 26 def replace(reg_to_match, replacement) REXML::XPath.each @xml_document, XPATH_ALL_TEXT_NODE do |n| n.text = n.text.gsub(reg_to_match, replacement) end end |