Class: Parchment::Paragraph

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

Overview

A Paragraph holds several TextRun objects and default formatting for them.

Direct Known Subclasses

DOCX::Paragraph, ODT::Paragraph, TXT::Paragraph

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#html_tag

Constructor Details

#initializeParagraph

This does not accept any arguments because the primary work for this is done in the formatter’s subclass.



24
25
26
27
28
# File 'lib/parchment/paragraph.rb', line 24

def initialize
  raise MissingFormatterMethodError unless @node
  @default_font_size = @document.default_paragraph_style.font_size
  set_text_runs
end

Instance Attribute Details

#default_font_sizeObject (readonly)

(Integer) It’s what it sounds like.



19
20
21
# File 'lib/parchment/paragraph.rb', line 19

def default_font_size
  @default_font_size
end

#styleObject (readonly)

(Style) The primary Style for the Paragraph.



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

def style
  @style
end

#text_runsObject (readonly)

(Array) All the TextRun children that the Paragraph has.



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

def text_runs
  @text_runs
end

Instance Method Details

#font_sizeObject

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



33
34
35
# File 'lib/parchment/paragraph.rb', line 33

def font_size
  @style.font_size || @default_font_size
end

#to_htmlObject

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



54
55
56
57
58
59
60
61
# File 'lib/parchment/paragraph.rb', line 54

def to_html
  html = ''
  text_runs.each { |text_run| html << text_run.to_html }
  styles = {}
  styles['font-size'] = "#{font_size}pt" unless font_size.nil?
  styles['text-align'] = @style.text_align unless @style.aligned_left?
  html_tag(:p, content: html, styles: styles)
end

#to_sObject Also known as: text

Output the unformatted Paragraph’s content as a String.



46
47
48
# File 'lib/parchment/paragraph.rb', line 46

def to_s
  @text_runs.map(&:to_s).join('')
end