Class: Spirit::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/spirit/document.rb

Overview

Document written in Genie Markup Language.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, opts = {}) ⇒ Document

Creates a new document from the given source. It should contain valid markdown + embedded YAML.

Parameters:

  • source (#read, #to_str)
  • opts (Hash) (defaults to: {})

    options for Redcarpet



18
19
20
21
22
23
24
25
26
# File 'lib/spirit/document.rb', line 18

def initialize(source, opts={})
  opts = MARKDOWN_EXTENSIONS.merge opts
  @rndr   = Render::HTML.new
  @engine = ::Redcarpet::Markdown.new(rndr, opts)
  @data   = case
            when source.respond_to?(:to_str) then source.to_str
            when source.respond_to?(:read) then source.read
            else nil end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/spirit/document.rb', line 11

def data
  @data
end

#engineObject (readonly)

Returns the value of attribute engine.



11
12
13
# File 'lib/spirit/document.rb', line 11

def engine
  @engine
end

#rndrObject (readonly)

Returns the value of attribute rndr.



11
12
13
# File 'lib/spirit/document.rb', line 11

def rndr
  @rndr
end

Instance Method Details

#render(anIO = nil) ⇒ String, Fixnum

Rendered output is returned as a string if anIO is not provided. The output is sanitized with Render::Sanitize, and should be considered safe for embedding into a HTML page without further escaping or sanitization.

Parameters:

  • anIO (IO) (defaults to: nil)

    if given, the HTML partial will be written to it.

Returns:

  • (String)

    if anIO was not provided, returns the HTML string.

  • (Fixnum)

    if anIO was provided, returns the number of bytes written.

Raises:



38
39
40
41
42
# File 'lib/spirit/document.rb', line 38

def render(anIO=nil)
  output = engine.render(data)
  return anIO.write(output) unless anIO.nil?
  output
end