class Parchment::Document

Primary Document class.

A Document is the primary “container” for everything necessary to format and display its contents. Holds Paragraph and Style objects.

Attributes

default_paragraph_style[R]

(Style) A Style referenced when style attributes on the child are not available.

paragraphs[R]

(Array) Paragraph objects that belong to the Document.

styles[R]

(Array) All the Styles that belong to the Document that other objects (i.e. Paragraphs, TextRuns) can reference and apply.

Public Class Methods

new(content_file, styles_file) click to toggle source
content_file

(File) The primary content file of the document.

styles_file

(File) The styles file from the document.

# File lib/parchment/document.rb, line 28
def initialize(content_file, styles_file)
  @content_xml = Nokogiri::XML(content_file)
  @styles_xml = Nokogiri::XML(styles_file)
  set_styles
  set_paragraphs
end

Public Instance Methods

get_style_by_id(id) click to toggle source

Returns the Style based on the id given.

The XML document formats in particular store their styles in elements with unique identifiers. OpenOffice uses these extensively, relying on them to specify formatting for the text. DOCX, not so much, but it does have user-defined styles.

id

(String) The unique identifier of the Style.

# File lib/parchment/document.rb, line 48
def get_style_by_id(id)
  styles.select { |style| id == style.id }.first
end
to_html() click to toggle source

Output entire document as a HTML fragment String.

# File lib/parchment/document.rb, line 54
def to_html
  paragraphs.map(&:to_html).join("\n")
end

Private Instance Methods

set_default_paragraph_style() click to toggle source
# File lib/parchment/document.rb, line 73
def set_default_paragraph_style
  raise MissingFormatterMethodError
end
set_paragraphs() click to toggle source

These methods add the Document’s children and default settings.

These methods should be defined in the appropriate class in the formatter module. i.e. Parchment::ODT::Document will have these.

# File lib/parchment/document.rb, line 65
def set_paragraphs
  raise MissingFormatterMethodError
end
set_styles() click to toggle source
# File lib/parchment/document.rb, line 69
def set_styles
  raise MissingFormatterMethodError
end