Skip to content

Commit 29fac45

Browse files
committed
More documentation, better examples in Readme
1 parent 3e67413 commit 29fac45

5 files changed

Lines changed: 97 additions & 82 deletions

File tree

README.md

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,77 @@
1-
# docx
2-
3-
a ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, and saving the document.
4-
5-
## usage
6-
7-
### install
8-
9-
requires ruby (only tested with 1.9.3 so far)
10-
11-
gem install docx
12-
13-
### reading
14-
15-
``` ruby
16-
require 'docx'
17-
18-
d = Docx::Document.open('example.docx')
19-
# Array of paragraphs
20-
d.paragraphs.each do |p|
21-
puts d
22-
end
23-
24-
# Hash of Bookmarks. Bookmark names as keys correspond to bookmark objects.
25-
d.bookmarks.each_pair do |bookmark_name, bookmark_object|
26-
puts bookmark_name
27-
end
28-
```
29-
30-
### writing
31-
32-
``` ruby
33-
require 'docx'
34-
35-
d = Docx::Document.open('example.docx')
36-
# Insert a single line after a bookmark
37-
d.bookmarks['example_bookmark'].insert_after("Hello world.")
38-
# Each value in array is put on a separate line
39-
d.bookmarks['example_bookmark'].insert_multiple_lines_after(['Hello', 'World', 'foo'])
40-
```
41-
42-
### advanced
43-
44-
``` ruby
45-
require 'docx'
46-
47-
d = Docx::Document.open('example.docx')
48-
49-
# The Nokogiri node on which an element is based can be accessed using #node
50-
d.paragraphs.each do |p|
51-
puts p.node.inspect
52-
end
53-
54-
# The #xpath and #at_xpath are delegated to the node from the element, saving a step
55-
p_element = d.paragraphs.first
56-
p_children = p_element.xpath("//child::*") # selects all children
57-
p_child = p_element.at_xpath("//child::*") # selects first child
58-
```
59-
60-
## Development
61-
62-
### todo
63-
64-
* Add better formatting identification for specific nodes and other formatting indicators (text size, paragraph spacing)
65-
* Calculate element formatting based on values present in element properties as well as properties inherited from parents
66-
* Default formatting of inserted elements to inherited values
67-
* Implement formattable elements.
68-
* Implement styles.
1+
# docx
2+
3+
a ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, and saving the document.
4+
5+
## usage
6+
7+
### install
8+
9+
requires ruby (only tested with 1.9.3 so far)
10+
11+
gem install docx
12+
13+
### reading
14+
15+
``` ruby
16+
require 'docx'
17+
18+
# Create a Docx::Document object for our existing docx file
19+
doc = Docx::Document.open('example.docx')
20+
21+
# Retrieve and display paragraphs
22+
doc.paragraphs.each do |p|
23+
puts p
24+
end
25+
26+
# Retrieve and display bookmarks, returned as hash with bookmark names as keys and objects as values
27+
doc.bookmarks.each_pair do |bookmark_name, bookmark_object|
28+
puts bookmark_name
29+
end
30+
```
31+
32+
### writing
33+
34+
``` ruby
35+
require 'docx'
36+
37+
# Create a Docx::Document object for our existing docx file
38+
doc = Docx::Document.open('example.docx')
39+
40+
# Insert a single line of text after one of our bookmarks
41+
doc.bookmarks['example_bookmark'].insert_after("Hello world.")
42+
43+
# Insert multiple lines of text at our bookmark
44+
doc.bookmarks['example_bookmark_2'].insert_multiple_lines_after(['Hello', 'World', 'foo'])
45+
46+
# Save document to specified path
47+
doc.save('example-edited.docx')
48+
```
49+
50+
### advanced
51+
52+
``` ruby
53+
require 'docx'
54+
55+
d = Docx::Document.open('example.docx')
56+
57+
# The Nokogiri::XML::Node on which an element is based can be accessed using #node
58+
d.paragraphs.each do |p|
59+
puts p.node.inspect
60+
end
61+
62+
# The #xpath and #at_xpath methods are delegated to the node from the element, saving a step
63+
p_element = d.paragraphs.first
64+
p_children = p_element.xpath("//child::*") # selects all children
65+
p_child = p_element.at_xpath("//child::*") # selects first child
66+
```
67+
68+
## Development
69+
70+
### todo
71+
72+
* Add better formatting identification for specific nodes and other formatting indicators (text size, paragraph spacing)
73+
* Calculate element formatting based on values present in element properties as well as properties inherited from parents
74+
* Default formatting of inserted elements to inherited values
75+
* Implement formattable elements.
76+
* Implement styles.
6977
* Easier multi-line text insertion at a single bookmark (inserting paragraph nodes after the one containing the bookmark)

lib/docx.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
require 'docx/version'
2-
3-
module Docx
4-
autoload :Document, 'docx/document'
5-
end
6-
1+
require 'docx/version'
2+
3+
module Docx #:nodoc:
4+
autoload :Document, 'docx/document'
5+
end
6+
77
require 'docx/core_ext/module'

lib/docx/document.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,33 @@ def initialize(path, &block)
2727
end
2828
end
2929

30-
# @param [String] path
31-
# @return [Docx::Document]
30+
# With no associated block, Docx::Document.open is a synonym for Docx::Document.new. If the optional code block is given, it will be passed the opened +docx+ file as an argument and the Docx::Document oject will automatically be closed when the block terminates. The values of the block will be returned from Docx::Document.open.
31+
# call-seq:
32+
# open(filepath) => file
33+
# open(filepath) {|file| block } => obj
3234
def self.open(path, &block)
3335
self.new(path, &block)
3436
end
3537

3638
##
37-
# +Deprecated+
39+
# *Deprecated*
3840
#
3941
# Iterates over paragraphs within document
42+
# call-seq:
43+
# each_paragraph => Enumerator
4044
def each_paragraph
4145
paragraphs.each { |p| yield(p) }
4246
end
4347

44-
# @return [String]
48+
# call-seq:
49+
# to_s -> string
4550
def to_s
4651
paragraphs.map(&:to_s).join("\n")
4752
end
4853

4954
# Save document to provided path
5055
# call-seq:
51-
# save(arg1) => void
56+
# save(filepath) => void
5257
def save(path)
5358
update
5459
Zip::ZipOutputStream.open(path) do |out|

lib/docx/elements/bookmark.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ def initialize(node)
1313
@name = @node['w:name']
1414
end
1515

16-
# Insert text before bookmark node
16+
# Insert text before bookmarkStart node
1717
def insert_text_before(text)
1818
text_run = get_run_after
1919
text_run.text = "#{text}#{text_run.text}"
2020
end
2121

22-
# Insert text after bookmark node
22+
# Insert text after bookmarkStart node
2323
def insert_text_after(text)
2424
text_run = get_run_before
2525
text_run.text = "#{text_run.text}#{text}"
2626
end
2727

28-
# Insert multiple lines adjacent to bookmark node
28+
# insert multiple lines starting with paragraph containing bookmark node.
2929
def insert_multiple_lines(text_array)
3030
# Hold paragraphs to be inserted into, corresponding to the index of the strings in the text array
3131
paragraphs = []

lib/docx/parser.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ def bookmarks
3333

3434
private
3535

36+
# generate Elements::Containers::Paragraph from paragraph XML node
3637
def parse_paragraph_from(p_node)
3738
Elements::Containers::Paragraph.new(p_node)
3839
end
3940

41+
# generate Elements::Bookmark from bookmark XML node
4042
def parse_bookmark_from(b_node)
4143
Elements::Bookmark.new(b_node)
4244
end

0 commit comments

Comments
 (0)