Class: Diml::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/diml/format/formatter.rb

Overview

Main Formatting class responsible for traversing the document content tree and rendering according to the strategy

Instance Method Summary collapse

Constructor Details

#initialize(diml_document, strategy = :markdown) ⇒ Formatter

Decorator class for basic content Receives an keyword in the DiML language and renders it within the Markdown format.



15
16
17
18
# File 'lib/diml/format/formatter.rb', line 15

def initialize(diml_document, strategy = :markdown)
  @doc = diml_document
  @decorator = select_decorator(strategy)
end

Instance Method Details

#formatObject

Creates a plain string of markdown For use by client to save as a file if chosen.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/diml/format/formatter.rb', line 23

def format
  content_tree = @doc.content

  # If the root has no children just render an empty string
  raise ArgumentError, "Document is invalid: no root found for content tree" unless content_tree.root?

  raise ArgumentError, "Document is invalid: no content found" unless content_tree.has_children?

  io = StringIO.new
  recursive_render(content_tree, io)
end