Class: Cadenza::BaseRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/cadenza/base_renderer.rb

Overview

BaseRenderer is a class you can use to more easily and cleanly implement your own rendering class. To use this then subclass BaseRenderer and implement the appropriate render_xyz methods (see #render for details).

Direct Known Subclasses

TextRenderer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_io) ⇒ BaseRenderer

creates a new renderer and assigns the given output io object to it

Parameters:

  • output_io (IO)

    the IO object which will be written to



15
16
17
# File 'lib/cadenza/base_renderer.rb', line 15

def initialize(output_io)
   @output = output_io
end

Instance Attribute Details

#documentDocumentNode (readonly)

Deprecated.

temporary hack, will be removed later

Returns the node which is at the root of the AST.

Returns:

  • (DocumentNode)

    the node which is at the root of the AST



11
12
13
# File 'lib/cadenza/base_renderer.rb', line 11

def document
  @document
end

#outputIO (readonly)

Returns the io object that is being written to.

Returns:

  • (IO)

    the io object that is being written to



7
8
9
# File 'lib/cadenza/base_renderer.rb', line 7

def output
  @output
end

Instance Method Details

#render(node, context, blocks = {}) ⇒ Object

renders the given document node to the output stream given in the constructor. this method will call the render_xyz method for the node given, where xyz is the demodulized underscored version of the node’s class name. for example: given a Cadenza::DocumentNode this method will call render_document_node

Parameters:

  • node (Node)

    the node to render

  • context (Context)

    the context to render with

  • blocks (Hash) (defaults to: {})

    a mapping of the block names to the matching Cadenza::BlockNode. The blocks given should be rendered instead of blocks of the same name in the given document.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cadenza/base_renderer.rb', line 30

def render(node, context, blocks={})
   #TODO: memoizing this is a terrible smell, add a "parent" hierarchy so
   # we can always find the root node from any node in the AST
   @document ||= node

   node_type = node.class.name.split("::").last

   node_name = underscore(node_type).gsub!(/_node$/, '')

   send("render_#{node_name}", node, context, blocks)
end