Class: Brandish::Processors::Common::Output Abstract

Inherits:
Brandish::Processor::Base show all
Defined in:
lib/brandish/processors/common/output.rb

Overview

This class is abstract.

Please implement #find_path and #template_data, and register the processor with Brandish::Processor::Base.register.

Outputs the result of processing the document. Without this processor, the document is not output, and most other processors have no effect.

Options:

  • :template - Optional. The name of the template to use. This defaults to the format used.

Direct Known Subclasses

HTML::Output

Instance Attribute Summary

Attributes inherited from Brandish::Processor::Base

#context

Instance Method Summary collapse

Methods inherited from Brandish::Processor::Base

#accept, #call, #initialize, #process_block, #process_command, #process_root, #process_text, register

Constructor Details

This class inherits a constructor from Brandish::Processor::Base

Instance Method Details

#find_path::Pathname

This method is abstract.

Finds the output path.

Returns:

  • (::Pathname)


56
57
58
59
# File 'lib/brandish/processors/common/output.rb', line 56

def find_path
  fail ProcessorNotImplementedError,
    "Please implement #{self.class}#find_path"
end

#postprocess(root) ⇒ void

This method returns an undefined value.

Postprocess the result of processing. The given root should only have text children, and should respond successfully to #flatten. This will render the template, and write it out to the value given in @path.



46
47
48
49
50
# File 'lib/brandish/processors/common/output.rb', line 46

def postprocess(root)
  @root = root
  value = @template.render!(template_data, strict_variables: true)
  @path.open("wb") { |f| f.write(value) }
end

#setupvoid

This method returns an undefined value.

Sets up the path and template for the output processor. It first attempts to find the output path, creating the directory to that path if needed. Then, it builds up the template that will later then be used to render the data.



28
29
30
31
32
33
34
35
36
# File 'lib/brandish/processors/common/output.rb', line 28

def setup
  super

  @path = find_path.tap { |p| p.dirname.mkpath }
  template_option = @options.fetch(:template, @context.form.format.to_s)
  template_path = ::Pathname.new(template_option).sub_ext(".liquid")
  template_full = @context.configure.templates.find(template_path)
  @template = ::Liquid::Template.parse(template_full.read)
end

#template_data{::String => ::Object}

This method is abstract.

A hash of data to pass to the template for rendering. The keys should always be strings.

Returns:

  • ({::String => ::Object})


66
67
68
69
# File 'lib/brandish/processors/common/output.rb', line 66

def template_data
  fail ProcessorNotImplementedError,
    "Please implement #{self.class}#template_data"
end