Module: QDA::Filters::TemplatedOutput

Includes:
Templates
Defined in:
lib/weft/filters/templates.rb

Overview

Included in output classes to enable them to be output via template (using PageTemplate under the hood).

Constant Summary collapse

TEMPLATES_DIR =
File.join( WEFT_SHAREDIR, 'templates' )

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object

Whenever this module is included in another class, it automatically checks whether a valid filter has been defined. If one has (by defining what QDA class it can output, what template file it should use, and what file extension it creates), then the filter is registered for use with the app (so it can be queried through Filters.export_filters()



70
71
72
73
74
75
76
77
78
# File 'lib/weft/filters/templates.rb', line 70

def TemplatedOutput.included(other)
  if other.is_a?(Class) and
    defined?(other::EXPORT_CLASS) and
    defined?(other::TEMPLATE_FILE) and
    defined?(other::EXTENSION) and
    defined?(PageTemplate)
    QDA::Filters.register_filter(other)
  end
end

Instance Method Details

#variables(obj) ⇒ Object

Returns a hash containing the named variables that should be templated. By default simply returns a hash containing the named variable ‘obj’ which contains the object being templated. If a variables() method is defined in the superclass (eg CategoryOutput) then the results from that are merged with the default. For example, CategoryOutput includes the named variable “text” which contains all the text marked by the category.



58
59
60
61
62
# File 'lib/weft/filters/templates.rb', line 58

def variables(obj)
  super.merge( 'obj' => obj )
rescue NoMethodError
  { 'obj' => obj  }
end

#write(obj, file = STDOUT) ⇒ Object

write templated output of the QDA object obj (eg a Category, a Document or a CodeReview) to the file or io file.



42
43
44
45
46
47
48
49
# File 'lib/weft/filters/templates.rb', line 42

def write(obj, file = STDOUT)
  tpl = PageTemplate::Parser.new('source' => CommentStrippingFileSource,
                                 'include_paths' => TEMPLATES_DIR,
                                 'preprocessor' => ExtendedPreprocessor )
  tpl.load(self.class::TEMPLATE_FILE)
  variables(obj).each { | k, v | tpl[k] = v }
  file.puts( tpl.output() )
end