Class: Rbpptx::Run

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

Overview

A single run (+<a:r>+) inside a paragraph.

Runs are the smallest unit of text that shares a single set of run properties (+<a:rPr>+: font, size, color, language, hyperlink, …). The text setter replaces only the content of the run’s <a:t> element, so every other property of the run survives the edit untouched — the surgical counterpart to the bulldozer-style Shape#text= setter.

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Run.

Parameters:

  • node (Nokogiri::XML::Element)

    the <a:r> element

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

    back-reference used to mark the slide dirty when this run is mutated



17
18
19
20
# File 'lib/rbpptx/run.rb', line 17

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

Instance Method Details

#textString

Returns the run’s text content (the inner text of <a:t>), or “” if the run has no <a:t> element.

Returns:

  • (String)

    the run’s text content (the inner text of <a:t>), or “” if the run has no <a:t> element



24
25
26
27
# File 'lib/rbpptx/run.rb', line 24

def text
  t = @node.at_xpath("./a:t", "a" => A_NS)
  t ? t.text : ""
end

#text=(new_text) ⇒ String

Replaces the run’s text content. Only the <a:t> element’s character data is rewritten; <a:rPr> and any other children of the run are left in place. If the run has no <a:t> element, one is appended.

Parameters:

  • new_text (String)

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rbpptx/run.rb', line 35

def text=(new_text)
  value = new_text.to_s
  t = @node.at_xpath("./a:t", "a" => A_NS)
  if t
    t.content = value
  else
    @node.add_child("<a:t>#{CGI.escapeHTML(value)}</a:t>")
  end
  @slide&.mark_dirty!
  value
end