Class: Parchment::TextRun

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/parchment/text_run.rb

Overview

A “run” of text within a Paragraph. Each run may have its own style attributes different from that of the Paragraph. These are iterated through to generate a line of formatted output.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#html_tag

Constructor Details

#initialize(paragraph, document) ⇒ TextRun

Returns a new instance of TextRun.



15
16
17
18
19
# File 'lib/parchment/text_run.rb', line 15

def initialize(paragraph, document)
  raise MissingFormatterMethodError unless @node
  @content = @node.content
  @default_font_size = paragraph.font_size
end

Instance Attribute Details

#styleObject (readonly)

(Style) The primary Style for the TextRun.



13
14
15
# File 'lib/parchment/text_run.rb', line 13

def style
  @style
end

Instance Method Details

#font_sizeObject

The font size of the TextRun. Will return the Paragraph’s default font size if not defined.



24
25
26
# File 'lib/parchment/text_run.rb', line 24

def font_size
  @style.font_size || @default_font_size
end

#to_htmlObject

Return a HTML element String with formatting based on the TextRun’s properties.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/parchment/text_run.rb', line 45

def to_html
  html = @content
  html = html_tag(:em, content: html) if italic?
  html = html_tag(:strong, content: html) if bold?
  styles = {}
  styles['text-decoration'] = 'underline' if underline?
  # No need to be granular with font size down to the span level if it doesn't vary.
  styles['font-size'] = "#{font_size}pt" if font_size != @default_font_size
  html = html_tag(:span, content: html, styles: styles) unless styles.empty?
  return html
end

#to_sObject Also known as: text

Output the unformatted TextRun’s content as a String.



37
38
39
# File 'lib/parchment/text_run.rb', line 37

def to_s
  @content
end