|
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 |
0 commit comments