Sha256: f7a1944ec6aa66f4fdd2d8a5c77c66172d1ece478417255eddc6d361703598a1

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

# ~*~ encoding: utf-8 ~*~
require 'spirit/constants'
require 'spirit/errors'
require 'spirit/render'

module Spirit

  # Document written in Genie Markup Language.
  # @todo TODO clean?
  class Document

    attr_accessor :data, :engine

    # Creates a new document from the given source. It should contain valid
    # markdown + embedded YAML.
    # @param [#read, #to_str] source
    # @param [Hash] opts         options for {::Redcarpet}
    def initialize(source, opts={})
      opts = MARKDOWN_EXTENSIONS.merge opts
      rndr = Render::HTML.new(name: opts[:name])
      @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

    # @return [Boolean] true iff if was a clean parse with no errors.
    def clean?
      # TODO
    end

    # Rendered output is returned as a string if +anIO+ is not provided. The
    # output is sanitized with {Spirit::Render::Sanitize}, and should be
    # considered safe for embedding into a HTML page without further escaping or
    # sanitization.
    #
    # @param [IO] anIO  if given, the HTML partial will be written to it.
    # @return [String]  if +anIO+ was not provided, returns the HTML string.
    # @return [Fixnum]  if +anIO+ was provided, returns the number of bytes
    #                   written.
    # @raise [Spirit::Error] if a fatal error is encountered.
    def render(anIO=nil)
      output = engine.render(data)
      return anIO.write(output) unless anIO.nil?
      output
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spirit-0.2 lib/spirit/document.rb
spirit-0.1.0.pre.2 lib/spirit/document.rb
spirit-0.1.0.pre.1 lib/spirit/document.rb