Class: RubyDocx::Document
- Inherits:
-
Object
- Object
- RubyDocx::Document
- Defined in:
- lib/ruby_docx/document.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#rels ⇒ Object
readonly
Returns the value of attribute rels.
-
#styles ⇒ Object
readonly
Returns the value of attribute styles.
-
#styles_xml ⇒ Object
readonly
Returns the value of attribute styles_xml.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
-
#zip ⇒ Object
readonly
Returns the value of attribute zip.
Class Method Summary collapse
Instance Method Summary collapse
- #find_relation_by_id(relation_id) ⇒ Object
- #images ⇒ Object
-
#initialize(path) ⇒ Document
constructor
A new instance of Document.
- #inspect ⇒ Object
- #paragraphs ⇒ Object
- #relations ⇒ Object
- #setup_drawing(drawing) ⇒ Object
- #setup_image(img) ⇒ Object
- #setup_object(obj) ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(path) ⇒ Document
Returns a new instance of Document.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby_docx/document.rb', line 16 def initialize(path) @path = path @zip = Zip::File.open(path) @xml = @zip.read('word/document.xml') rels_xml = @zip.read('word/_rels/document.xml.rels') @rels = Nokogiri::XML(rels_xml) @rels.remove_namespaces! @doc = Nokogiri::XML(@xml) @styles_xml = @zip.read('word/styles.xml') @styles = Nokogiri::XML(@styles_xml) end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
10 11 12 |
# File 'lib/ruby_docx/document.rb', line 10 def doc @doc end |
#rels ⇒ Object (readonly)
Returns the value of attribute rels.
10 11 12 |
# File 'lib/ruby_docx/document.rb', line 10 def rels @rels end |
#styles ⇒ Object (readonly)
Returns the value of attribute styles.
10 11 12 |
# File 'lib/ruby_docx/document.rb', line 10 def styles @styles end |
#styles_xml ⇒ Object (readonly)
Returns the value of attribute styles_xml.
10 11 12 |
# File 'lib/ruby_docx/document.rb', line 10 def styles_xml @styles_xml end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
10 11 12 |
# File 'lib/ruby_docx/document.rb', line 10 def xml @xml end |
#zip ⇒ Object (readonly)
Returns the value of attribute zip.
10 11 12 |
# File 'lib/ruby_docx/document.rb', line 10 def zip @zip end |
Class Method Details
.open(path) ⇒ Object
12 13 14 |
# File 'lib/ruby_docx/document.rb', line 12 def self.open(path) self.new(path) end |
Instance Method Details
#find_relation_by_id(relation_id) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby_docx/document.rb', line 91 def find_relation_by_id(relation_id) return nil unless relation_id self.relations.map do |relation| if relation.relation_id.to_s == relation_id return relation end end nil end |
#images ⇒ Object
79 80 81 82 83 |
# File 'lib/ruby_docx/document.rb', line 79 def images @doc.xpath('//w:pict').map do |node| RubyDocx::Elements::Image.new self, node end end |
#inspect ⇒ Object
103 104 105 |
# File 'lib/ruby_docx/document.rb', line 103 def inspect "#<#{self.class}:0x#{(doc.object_id * 2).to_s(16).rjust(14, "0")} @doc=#{@path}>" end |
#paragraphs ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ruby_docx/document.rb', line 28 def paragraphs @paragraphs ||= @doc.xpath('//w:document/w:body/w:p|//w:document/w:body/w:tbl').map { |node| if node.name.to_s == "tbl" RubyDocx::Elements::Table.new self, node else RubyDocx::Elements::Paragraph.new self, node end } end |
#relations ⇒ Object
85 86 87 88 89 |
# File 'lib/ruby_docx/document.rb', line 85 def relations @rels.xpath("//Relationship").map do |node| RubyDocx::Relation.new node end end |
#setup_drawing(drawing) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby_docx/document.rb', line 43 def setup_drawing(drawing) drawing.zip = @zip if drawing.node.name.to_s == "drawing" element = drawing.node.xpath(".//a:blip", 'a' => "http://schemas.openxmlformats.org/drawingml/2006/picture").first # p element, element.attributes["name"].value # drawing.path = "media/#{element.attributes["name"].value}" relation = self.find_relation_by_id(drawing.relation_id) # p relation drawing.path = relation.value if relation # p drawing.path end drawing end |
#setup_image(img) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/ruby_docx/document.rb', line 61 def setup_image(img) img.zip = @zip relation = self.find_relation_by_id(img.relation_id) img.path = relation.value if relation img end |
#setup_object(obj) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/ruby_docx/document.rb', line 70 def setup_object(obj) obj.zip = @zip relation = self.find_relation_by_id(obj.relation_id) obj.path = relation.value if relation obj end |
#to_html ⇒ Object
39 40 41 |
# File 'lib/ruby_docx/document.rb', line 39 def to_html self.paragraphs.map(&:to_html).join end |