docx 0.2.02 → 0.2.03

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDdiZTAyMWU2MzFmZDBhYTNlMGEyY2MwNGExODI5NWE2ZmYyZTFlZg==
4
+ YzIyMTI3NGE3YWNhMjYyMjUzMTZjYWZjMmJkYjU0OGI4ZDg2OTM3MQ==
5
5
  data.tar.gz: !binary |-
6
- MTUyYTVmMWFiMmI5MDJiNjUzMTBjMjZhYzhlODM0OGQ4ZWM5ZTI1Yg==
6
+ ZmJjNWZjMzc2OTM4NDFmMTkwNzI2Y2JhM2IwZDE5Mzc0MjY3M2I1Mw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MjI5NjRjNDA4M2E1OGM4NTg1NDA4ZGI1ZTA2NWIyZTc5NmJjM2ZiNjcwZjA5
10
- N2Q4NzJmNmFiOWRkMTkzNDcwYTRiODAyMTlhZGYwZmQ2ZmIxNDMzYTgyOWJi
11
- ZmJkM2VlYzUyNTZlZTA5Mjg2ZTRkZmIzNjZjODQxZWY5ZDVmOTY=
9
+ MmIyZWM4MDJjMjY3NGYzZDUzNjBiYmE2YjllZmNiZTA4NjkyYzE3ODg3ZmFi
10
+ OTA4YTQ5ZTY2MzA4Mjc5YWI0YjIyYmIyNzFjZjk3ZWZmODQxNWUwMTNlZjNl
11
+ N2ZiOTUxYzg5ZmYzNzc5MmI3ZjlhZTNiMDY0ZjNmYTE5NzVjOWE=
12
12
  data.tar.gz: !binary |-
13
- OTcyMmI3MzkyYjM5YjFkZDEzYWY5MDljZTAwY2I4MjJjMDkzMjhhNjZhYTJh
14
- NDI1MDcxMmJjYmEzNTQ5MDEyZTM3MTJhZWU2ZmEzYzQ1NGViZTMxMzc0YjQ4
15
- MTMwNTI0NjJkOGQ2MzExMDlmYTk5YTdhNmQ1MzkxOTU4M2RmODM=
13
+ MTRhMjAxMmJlODRhOTJhMzM4OGExZGI5NGFiZTdmZDczZmI0N2QyYjQ0ZDhk
14
+ OGQ0MjlkYWEyNWJlMzhmMGY2ZTFlODVjYmY5MThjM2Q1NzdiOTRkYzlhYjdk
15
+ OWIxNGJkOWM5MzkzNTFlNzNkN2VkOWEzNmRjOWJhOTMxZWY1OTA=
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -15,14 +15,16 @@ requires ruby (only tested with 1.9.3 so far)
15
15
  ``` ruby
16
16
  require 'docx'
17
17
 
18
- d = Docx::Document.open('example.docx')
19
- # Array of paragraphs
20
- d.paragraphs.each do |p|
21
- puts d
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
22
24
  end
23
25
 
24
- # Hash of Bookmarks. Bookmark names as keys correspond to bookmark objects.
25
- d.bookmarks.each_pair do |bookmark_name, bookmark_object|
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|
26
28
  puts bookmark_name
27
29
  end
28
30
  ```
@@ -32,11 +34,17 @@ end
32
34
  ``` ruby
33
35
  require 'docx'
34
36
 
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'])
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')
40
48
  ```
41
49
 
42
50
  ### advanced
@@ -46,12 +54,12 @@ require 'docx'
46
54
 
47
55
  d = Docx::Document.open('example.docx')
48
56
 
49
- # The Nokogiri node on which an element is based can be accessed using #node
57
+ # The Nokogiri::XML::Node on which an element is based can be accessed using #node
50
58
  d.paragraphs.each do |p|
51
59
  puts p.node.inspect
52
60
  end
53
61
 
54
- # The #xpath and #at_xpath are delegated to the node from the element, saving a step
62
+ # The #xpath and #at_xpath methods are delegated to the node from the element, saving a step
55
63
  p_element = d.paragraphs.first
56
64
  p_children = p_element.xpath("//child::*") # selects all children
57
65
  p_child = p_element.at_xpath("//child::*") # selects first child
@@ -1,6 +1,6 @@
1
1
  require 'docx/version'
2
2
 
3
- module Docx
3
+ module Docx #:nodoc:
4
4
  autoload :Document, 'docx/document'
5
5
  end
6
6
 
@@ -10,7 +10,7 @@ module Docx
10
10
  @node.at_xpath("./#{@properties_tag}")
11
11
  end
12
12
 
13
- # TODO: Maybe merge and then clear so there is only one text node left.
13
+ # Erase text within an element
14
14
  def blank!
15
15
  @node.xpath(".//w:t").each {|t| t.content = '' }
16
16
  end
@@ -17,7 +17,7 @@ module Docx
17
17
  @properties_tag = 'pPr'
18
18
  end
19
19
 
20
- # Handle direct text insertion into paragraph on some conditions
20
+ # Set text of paragraph
21
21
  def text=(content)
22
22
  if text_runs.size == 1
23
23
  text_runs.first.text = content
@@ -31,14 +31,17 @@ module Docx
31
31
  end
32
32
  end
33
33
 
34
+ # Return text of paragraph
34
35
  def to_s
35
36
  text_runs.map(&:text).join('')
36
37
  end
37
38
 
39
+ # Array of text runs contained within paragraph
38
40
  def text_runs
39
41
  @node.xpath('w:r').map {|r_node| Containers::TextRun.new(r_node) }
40
42
  end
41
43
 
44
+ # Iterate over each text run within a paragraph
42
45
  def each_text_run
43
46
  text_runs.each { |tr| yield(tr) }
44
47
  end
@@ -26,6 +26,7 @@ module Docx
26
26
  @formatting = parse_formatting || DEFAULT_FORMATTING
27
27
  end
28
28
 
29
+ # Set text of text run
29
30
  def text=(content)
30
31
  if @text_nodes.size == 1
31
32
  @text_nodes.first.content = content
@@ -35,6 +36,7 @@ module Docx
35
36
  end
36
37
  end
37
38
 
39
+ # Returns text contained within text run
38
40
  def parse_text
39
41
  @text_nodes.map(&:content).join('')
40
42
  end
@@ -2,6 +2,19 @@ require 'docx/parser'
2
2
  require 'zip/zip'
3
3
 
4
4
  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
5
18
  class Document
6
19
  delegate :paragraphs, :bookmarks, :to => :@parser
7
20
  delegate :doc, :xml, :zip, :to => :@parser
@@ -14,25 +27,33 @@ module Docx
14
27
  end
15
28
  end
16
29
 
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
17
34
  def self.open(path, &block)
18
35
  self.new(path, &block)
19
36
  end
20
37
 
38
+ ##
39
+ # *Deprecated*
40
+ #
41
+ # Iterates over paragraphs within document
42
+ # call-seq:
43
+ # each_paragraph => Enumerator
21
44
  def each_paragraph
22
45
  paragraphs.each { |p| yield(p) }
23
46
  end
24
47
 
48
+ # call-seq:
49
+ # to_s -> string
25
50
  def to_s
26
51
  paragraphs.map(&:to_s).join("\n")
27
52
  end
28
53
 
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
-
54
+ # Save document to provided path
55
+ # call-seq:
56
+ # save(filepath) => void
36
57
  def save(path)
37
58
  update
38
59
  Zip::ZipOutputStream.open(path) do |out|
@@ -50,5 +71,17 @@ module Docx
50
71
  end
51
72
 
52
73
  alias_method :text, :to_s
74
+
75
+ private
76
+
77
+ #--
78
+ # TODO: Flesh this out to be compatible with other files
79
+ # TODO: Method to set flag on files that have been edited, probably by inserting something at the
80
+ # end of methods that make edits?
81
+ #++
82
+ def update
83
+ @replace["word/document.xml"] = doc.serialize :save_with => 0
84
+ end
85
+
53
86
  end
54
87
  end
@@ -13,16 +13,19 @@ module Docx
13
13
  @name = @node['w:name']
14
14
  end
15
15
 
16
+ # Insert text before bookmarkStart node
16
17
  def insert_text_before(text)
17
18
  text_run = get_run_after
18
19
  text_run.text = "#{text}#{text_run.text}"
19
20
  end
20
21
 
22
+ # Insert text after bookmarkStart node
21
23
  def insert_text_after(text)
22
24
  text_run = get_run_before
23
25
  text_run.text = "#{text_run.text}#{text}"
24
26
  end
25
27
 
28
+ # insert multiple lines starting with paragraph containing bookmark node.
26
29
  def insert_multiple_lines(text_array)
27
30
  # Hold paragraphs to be inserted into, corresponding to the index of the strings in the text array
28
31
  paragraphs = []
@@ -44,6 +47,7 @@ module Docx
44
47
  end
45
48
  end
46
49
 
50
+ # Get text run immediately prior to bookmark node
47
51
  def get_run_before
48
52
  # at_xpath returns the first match found and preceding-sibling returns siblings in the
49
53
  # order they appear in the document not the order as they appear when moving out from
@@ -58,6 +62,7 @@ module Docx
58
62
  end
59
63
  end
60
64
 
65
+ # Get text run immediately after bookmark node
61
66
  def get_run_after
62
67
  if (r_node = @node.at_xpath("./following-sibling::w:r"))
63
68
  Containers::TextRun.new(r_node)
@@ -7,6 +7,7 @@ module Docx
7
7
  module Element
8
8
  DEFAULT_TAG = ''
9
9
 
10
+ # Ensure that a 'tag' corresponding to the XML element that defines the element is defined
10
11
  def self.included(base)
11
12
  base.extend(ClassMethods)
12
13
  base.const_set(:TAG, Element::DEFAULT_TAG) unless base.const_defined?(:TAG)
@@ -20,7 +21,7 @@ module Docx
20
21
  @node.at_xpath("./parent::#{type}")
21
22
  end
22
23
 
23
- # TODO: Should create a docx paragraph from this
24
+ # Get parent paragraph of element
24
25
  def parent_paragraph
25
26
  Elements::Containers::Paragraph.new(parent('w:p'))
26
27
  end
@@ -6,6 +6,7 @@ require 'zip/zip'
6
6
  module Docx
7
7
  class Parser
8
8
  attr_reader :xml, :doc, :zip
9
+
9
10
  def initialize(path)
10
11
  @zip = Zip::ZipFile.open(path)
11
12
  @xml = @zip.read('word/document.xml')
@@ -20,6 +21,7 @@ module Docx
20
21
  @doc.xpath('//w:document//w:body//w:p').map { |p_node| parse_paragraph_from p_node }
21
22
  end
22
23
 
24
+ # Returns hash of bookmarks
23
25
  def bookmarks
24
26
  bkmrks_hsh = Hash.new
25
27
  bkmrks_ary = @doc.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node }
@@ -31,10 +33,12 @@ module Docx
31
33
 
32
34
  private
33
35
 
36
+ # generate Elements::Containers::Paragraph from paragraph XML node
34
37
  def parse_paragraph_from(p_node)
35
38
  Elements::Containers::Paragraph.new(p_node)
36
39
  end
37
40
 
41
+ # generate Elements::Bookmark from bookmark XML node
38
42
  def parse_bookmark_from(b_node)
39
43
  Elements::Bookmark.new(b_node)
40
44
  end
@@ -1,3 +1,3 @@
1
- module Docx
2
- VERSION = '0.2.02'
1
+ module Docx #:nodoc:
2
+ VERSION = '0.2.03'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.02
4
+ version: 0.2.03
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Hunt
@@ -38,7 +38,7 @@ cert_chain:
38
38
  NEV0UFhmRQpLdFE1SGpuNFVDNTg0R0pyR01haFVnbXhMbmZleXVZWXVZTlFF
39
39
  M1VzVlVQYXl6eGtQVW9DOFZzT0orV1ZmMnMrCkcxL1VkNGRVYkVtOGdRL2J4
40
40
  bDM1TzBBdkhCcz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
41
- date: 2013-06-01 00:00:00.000000000 Z
41
+ date: 2013-06-08 00:00:00.000000000 Z
42
42
  dependencies:
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: nokogiri
@@ -88,7 +88,6 @@ files:
88
88
  - lib/docx/elements/text.rb
89
89
  - lib/docx/elements.rb
90
90
  - lib/docx/parser.rb
91
- - lib/docx/test.rb
92
91
  - lib/docx/version.rb
93
92
  - lib/docx.rb
94
93
  homepage: https://github.com/chrahunt/docx
metadata.gz.sig CHANGED
@@ -1,2 +1,3 @@
1
- ��g��:��PUg v8����U���+�����L��Đ�b��V3D��-�4���V?i4�N3}�C�UfL>T����UZRވY��*.HC�8Iӹ�,/�?�(����s�]��i֟�@{��@p�����,�
2
- M�Y@���Q6v�@ٸ1�ɱՆכ0�aD�쫦ݕ�ќ
1
+ ����G��`ۼ/�W$���ѣ=�J'��H1gS�� ���ޭ�h!PK�A)d����x󼂷{%��YMo8���%��iRtc��fJs���^U8E\����f�`��Hf⇒8:Z�c�� \|68L��&��
2
+ w�{Pږ�n�9��௨�4�!�\.�{k' �UD�5i�ߛ �إ hL�ĵ� �M�����{4��8�Ğ�(ͣ��%c��X��v�|���[
3
+ `fj)Xux��H�;XE:Q]�"@���
@@ -1,4 +0,0 @@
1
- require_relative 'document'
2
-
3
- document = Docx::Document.new('../test/fixtures/basic.docx')
4
- puts document.bookmarks