Class: Rbpptx::Paragraph

Inherits:
Object
  • Object
show all
Defined in:
lib/rbpptx/paragraph.rb

Overview

A paragraph (+<a:p>+) inside a shape’s text body.

Paragraphs hold a sequence of Runs separated optionally by soft line breaks (+<a:br>+). The #text accessor mirrors Shape#text restricted to this paragraph: runs joined as-is, breaks rendered as “n”. Since paragraphs map 1:1 to <a:p> elements, paragraph boundaries are not rendered as “n” here — that is Shape#text‘s job.

Constant Summary collapse

A_NS =
"http://schemas.openxmlformats.org/drawingml/2006/main".freeze

Instance Method Summary collapse

Constructor Details

#initialize(node:, slide: nil) ⇒ Paragraph

Returns a new instance of Paragraph.

Parameters:

  • node (Nokogiri::XML::Element)

    the <a:p> element

  • slide (Slide, nil) (defaults to: nil)

    back-reference propagated to runs so their setters can mark the slide dirty



15
16
17
18
# File 'lib/rbpptx/paragraph.rb', line 15

def initialize(node:, slide: nil)
  @node = node
  @slide = slide
end

Instance Method Details

#runsArray<Run>

Returns <a:r> children in document order. <a:fld> (field) and <a:br> (line break) elements are not included.

Returns:

  • (Array<Run>)

    <a:r> children in document order. <a:fld> (field) and <a:br> (line break) elements are not included.



22
23
24
# File 'lib/rbpptx/paragraph.rb', line 22

def runs
  @node.xpath("./a:r", "a" => A_NS).map { |r| Run.new(node: r, slide: @slide) }
end

#textString

Returns concatenated text. Run text appears verbatim; <a:br> between runs is rendered as “n”.

Returns:

  • (String)

    concatenated text. Run text appears verbatim; <a:br> between runs is rendered as “n”.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbpptx/paragraph.rb', line 28

def text
  buf = +""
  @node.children.each do |child|
    next unless child.namespace&.href == A_NS

    case child.name
    when "r"
      t = child.at_xpath("./a:t", "a" => A_NS)
      buf << t.text if t
    when "br"
      buf << "\n"
    end
  end
  buf
end