Skip to content

Commit 3e67413

Browse files
committed
Add documentation
1 parent 1423615 commit 3e67413

10 files changed

Lines changed: 370 additions & 332 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.bundle
33
Gemfile.lock
44
pkg/*
5+
doc/

lib/docx/containers/container.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
require 'docx/elements'
2-
3-
module Docx
4-
module Elements
5-
module Containers
6-
module Container
7-
# Relation methods
8-
# TODO: Create a properties object, include Element
9-
def properties
10-
@node.at_xpath("./#{@properties_tag}")
11-
end
12-
13-
# TODO: Maybe merge and then clear so there is only one text node left.
14-
def blank!
15-
@node.xpath(".//w:t").each {|t| t.content = '' }
16-
end
17-
end
18-
end
19-
end
1+
require 'docx/elements'
2+
3+
module Docx
4+
module Elements
5+
module Containers
6+
module Container
7+
# Relation methods
8+
# TODO: Create a properties object, include Element
9+
def properties
10+
@node.at_xpath("./#{@properties_tag}")
11+
end
12+
13+
# Erase text within an element
14+
def blank!
15+
@node.xpath(".//w:t").each {|t| t.content = '' }
16+
end
17+
end
18+
end
19+
end
2020
end

lib/docx/containers/paragraph.rb

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
1-
require 'docx/containers/text_run'
2-
require 'docx/containers/container'
3-
4-
module Docx
5-
module Elements
6-
module Containers
7-
class Paragraph
8-
include Container
9-
include Elements::Element
10-
11-
TAG = 'p'
12-
13-
# Child elements: pPr, r, fldSimple, hlink, subDoc
14-
# http://msdn.microsoft.com/en-us/library/office/ee364458(v=office.11).aspx
15-
def initialize(node)
16-
@node = node
17-
@properties_tag = 'pPr'
18-
end
19-
20-
# Handle direct text insertion into paragraph on some conditions
21-
def text=(content)
22-
if text_runs.size == 1
23-
text_runs.first.text = content
24-
elsif text_runs.size == 0
25-
new_r = TextRun.create_within(self)
26-
new_r.text = content
27-
else
28-
text_runs.each {|r| r.node.remove }
29-
new_r = TextRun.create_within(self)
30-
new_r.text = content
31-
end
32-
end
33-
34-
def to_s
35-
text_runs.map(&:text).join('')
36-
end
37-
38-
def text_runs
39-
@node.xpath('w:r').map {|r_node| Containers::TextRun.new(r_node) }
40-
end
41-
42-
def each_text_run
43-
text_runs.each { |tr| yield(tr) }
44-
end
45-
46-
alias_method :text, :to_s
47-
end
48-
end
49-
end
50-
end
1+
require 'docx/containers/text_run'
2+
require 'docx/containers/container'
3+
4+
module Docx
5+
module Elements
6+
module Containers
7+
class Paragraph
8+
include Container
9+
include Elements::Element
10+
11+
TAG = 'p'
12+
13+
# Child elements: pPr, r, fldSimple, hlink, subDoc
14+
# http://msdn.microsoft.com/en-us/library/office/ee364458(v=office.11).aspx
15+
def initialize(node)
16+
@node = node
17+
@properties_tag = 'pPr'
18+
end
19+
20+
# Set text of paragraph
21+
def text=(content)
22+
if text_runs.size == 1
23+
text_runs.first.text = content
24+
elsif text_runs.size == 0
25+
new_r = TextRun.create_within(self)
26+
new_r.text = content
27+
else
28+
text_runs.each {|r| r.node.remove }
29+
new_r = TextRun.create_within(self)
30+
new_r.text = content
31+
end
32+
end
33+
34+
# Return text of paragraph
35+
def to_s
36+
text_runs.map(&:text).join('')
37+
end
38+
39+
# Array of text runs contained within paragraph
40+
def text_runs
41+
@node.xpath('w:r').map {|r_node| Containers::TextRun.new(r_node) }
42+
end
43+
44+
# Iterate over each text run within a paragraph
45+
def each_text_run
46+
text_runs.each { |tr| yield(tr) }
47+
end
48+
49+
alias_method :text, :to_s
50+
end
51+
end
52+
end
53+
end

lib/docx/containers/text_run.rb

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,70 @@
1-
require 'docx/containers/container'
2-
3-
module Docx
4-
module Elements
5-
module Containers
6-
class TextRun
7-
include Container
8-
include Elements::Element
9-
10-
DEFAULT_FORMATTING = {
11-
italic: false,
12-
bold: false,
13-
underline: false
14-
}
15-
16-
TAG = 'r'
17-
18-
attr_reader :text
19-
attr_reader :formatting
20-
21-
def initialize(node)
22-
@node = node
23-
@text_nodes = @node.xpath('w:t').map {|t_node| Elements::Text.new(t_node) }
24-
@properties_tag = 'rPr'
25-
@text = parse_text || ''
26-
@formatting = parse_formatting || DEFAULT_FORMATTING
27-
end
28-
29-
def text=(content)
30-
if @text_nodes.size == 1
31-
@text_nodes.first.content = content
32-
elsif @text_nodes.empty?
33-
new_t = Elements::Text.create_within(self)
34-
new_t.content = content
35-
end
36-
end
37-
38-
def parse_text
39-
@text_nodes.map(&:content).join('')
40-
end
41-
42-
def parse_formatting
43-
{
44-
italic: !@node.xpath('.//w:i').empty?,
45-
bold: !@node.xpath('.//w:b').empty?,
46-
underline: !@node.xpath('.//w:u').empty?
47-
}
48-
end
49-
50-
def to_s
51-
@text
52-
end
53-
54-
def italicized?
55-
@formatting[:italic]
56-
end
57-
58-
def bolded?
59-
@formatting[:bold]
60-
end
61-
62-
def underlined?
63-
@formatting[:underline]
64-
end
65-
end
66-
end
67-
end
68-
end
1+
require 'docx/containers/container'
2+
3+
module Docx
4+
module Elements
5+
module Containers
6+
class TextRun
7+
include Container
8+
include Elements::Element
9+
10+
DEFAULT_FORMATTING = {
11+
italic: false,
12+
bold: false,
13+
underline: false
14+
}
15+
16+
TAG = 'r'
17+
18+
attr_reader :text
19+
attr_reader :formatting
20+
21+
def initialize(node)
22+
@node = node
23+
@text_nodes = @node.xpath('w:t').map {|t_node| Elements::Text.new(t_node) }
24+
@properties_tag = 'rPr'
25+
@text = parse_text || ''
26+
@formatting = parse_formatting || DEFAULT_FORMATTING
27+
end
28+
29+
# Set text of text run
30+
def text=(content)
31+
if @text_nodes.size == 1
32+
@text_nodes.first.content = content
33+
elsif @text_nodes.empty?
34+
new_t = Elements::Text.create_within(self)
35+
new_t.content = content
36+
end
37+
end
38+
39+
# Returns text contained within text run
40+
def parse_text
41+
@text_nodes.map(&:content).join('')
42+
end
43+
44+
def parse_formatting
45+
{
46+
italic: !@node.xpath('.//w:i').empty?,
47+
bold: !@node.xpath('.//w:b').empty?,
48+
underline: !@node.xpath('.//w:u').empty?
49+
}
50+
end
51+
52+
def to_s
53+
@text
54+
end
55+
56+
def italicized?
57+
@formatting[:italic]
58+
end
59+
60+
def bolded?
61+
@formatting[:bold]
62+
end
63+
64+
def underlined?
65+
@formatting[:underline]
66+
end
67+
end
68+
end
69+
end
70+
end

lib/docx/document.rb

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
require 'zip/zip'
33

44
module Docx
5+
# The Document class wraps around a docx file and provides methods to
6+
# interface with it.
7+
#
8+
# # get a Docx::Document for a docx file in the local directory
9+
# doc = Docx::Document.open("test.docx")
10+
#
11+
# # get the text from the document
12+
# puts doc.text
13+
#
14+
# # do the same thing in a block
15+
# Docx::Document.open("test.docx") do |d|
16+
# puts d.text
17+
# end
518
class Document
619
delegate :paragraphs, :bookmarks, :to => :@parser
720
delegate :doc, :xml, :zip, :to => :@parser
@@ -14,25 +27,28 @@ def initialize(path, &block)
1427
end
1528
end
1629

30+
# @param [String] path
31+
# @return [Docx::Document]
1732
def self.open(path, &block)
1833
self.new(path, &block)
1934
end
2035

36+
##
37+
# +Deprecated+
38+
#
39+
# Iterates over paragraphs within document
2140
def each_paragraph
2241
paragraphs.each { |p| yield(p) }
2342
end
2443

44+
# @return [String]
2545
def to_s
2646
paragraphs.map(&:to_s).join("\n")
2747
end
2848

29-
# TODO: Flesh this out to be compatible with other files
30-
# TODO: Method to set flag on files that have been edited, probably by inserting something at the
31-
# end of methods that make edits?
32-
def update
33-
@replace["word/document.xml"] = doc.serialize :save_with => 0
34-
end
35-
49+
# Save document to provided path
50+
# call-seq:
51+
# save(arg1) => void
3652
def save(path)
3753
update
3854
Zip::ZipOutputStream.open(path) do |out|
@@ -50,5 +66,17 @@ def save(path)
5066
end
5167

5268
alias_method :text, :to_s
69+
70+
private
71+
72+
#--
73+
# TODO: Flesh this out to be compatible with other files
74+
# TODO: Method to set flag on files that have been edited, probably by inserting something at the
75+
# end of methods that make edits?
76+
#++
77+
def update
78+
@replace["word/document.xml"] = doc.serialize :save_with => 0
79+
end
80+
5381
end
5482
end

0 commit comments

Comments
 (0)