Class: Parchment::TextRun
- Inherits:
-
Object
- Object
- Parchment::TextRun
- 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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#style ⇒ Object
readonly
(Style) The primary Style for the TextRun.
Instance Method Summary collapse
-
#font_size ⇒ Object
The font size of the TextRun.
-
#initialize(paragraph, document) ⇒ TextRun
constructor
A new instance of TextRun.
-
#to_html ⇒ Object
Return a HTML element String with formatting based on the TextRun’s properties.
-
#to_s ⇒ Object
(also: #text)
Output the unformatted TextRun’s content as a String.
Methods included from Helpers
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
#style ⇒ Object (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_size ⇒ Object
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_html ⇒ Object
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_s ⇒ Object 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 |