Class: Cadmus::Renderers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cadmus/renderers.rb

Overview

The simplest Cadmus renderer. It will render Liquid templates to HTML and plain text (removing the HTML tags from the plain text output).

Direct Known Subclasses

Markdown

Constant Summary collapse

DEFAULT_HTML_SANITIZER =
Rails::Html::FullSanitizer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



22
23
24
25
26
27
28
# File 'lib/cadmus/renderers.rb', line 22

def initialize
  self.default_registers = {}
  self.default_filters = []
  self.default_assigns = {}

  self.html_sanitizer = Rails.application.config.action_view.full_sanitizer || DEFAULT_HTML_SANITIZER.new
end

Instance Attribute Details

#default_assignsObject

Returns the value of attribute default_assigns.



18
19
20
# File 'lib/cadmus/renderers.rb', line 18

def default_assigns
  @default_assigns
end

#default_filtersObject

Returns the value of attribute default_filters.



18
19
20
# File 'lib/cadmus/renderers.rb', line 18

def default_filters
  @default_filters
end

#default_registersObject

Returns the value of attribute default_registers.



18
19
20
# File 'lib/cadmus/renderers.rb', line 18

def default_registers
  @default_registers
end

#html_sanitizerObject

Returns the value of attribute html_sanitizer.



18
19
20
# File 'lib/cadmus/renderers.rb', line 18

def html_sanitizer
  @html_sanitizer
end

Instance Method Details

#preprocess(template, format, options = {}) ⇒ String

The preprocess method performs the initial rendering of the Liquid template using the a combination of the default_filters, default_assigns, default_registers, and any :assigns, :filters, and :registers options passed in as options.

Parameters:

  • template (Liquid::Template)

    the Liquid template to render.

  • format (Symbol)

    the format being used for rendering. (This is ignored in the Base implementation of +preprocess+ and is only used in Base's +render+ method, but is passed here in case subclasses wish to make use of the format information when overriding preprocess.)

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

    additional options that can be passed to override default rendering behavior.

Options Hash (options):

  • :assigns (Hash)

    additional assign variables that will be made available to the template.

  • :registers (Hash)

    additional register variables that will be made available to the template.

  • :filters (Hash)

    additional filters to be made available to the template.

Returns:

  • (String)

    the raw results of rendering the Liquid template.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cadmus/renderers.rb', line 43

def preprocess(template, format, options={})
  render_args = [
    default_assigns.merge(options[:assigns] || {}),
    {
      :filters   => default_filters + (options[:filters] || []),
      :registers => default_registers.merge(options[:registers] || {})
    }
  ]

  template.render(*render_args)
end

#render(template, format, options = {}) ⇒ Object

Render a given Liquid template to the specified format. This renderer implementation supports +:html+ and +:text+ formats, but other implementations may support other formats.

Parameters:

  • template (Liquid::Template)

    the Liquid template to render.

  • format (Symbol)

    the format being used for rendering. +:html+ will result in a string that's marked as safe for HTML rendering (and thus won't be escaped by Rails). +:text+ will strip all HTML tags out of the template result.

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

    additional rendering options. See the +preprocess+ method for a description of available options.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cadmus/renderers.rb', line 64

def render(template, format, options={})
  content = preprocess(template, format, options)

  case format.to_sym
  when :html
    content.html_safe
  when :text
    html_sanitizer.sanitize content
  else
    raise "Format #{format.inspect} unsupported by #{self.class.name}"
  end
end