Class: LEWT::LiquidRenderer

Inherits:
Extension show all
Defined in:
lib/extensions/liquid-renderer.rb

Overview

The Liquid Renderer LEWT Extension handles rendering processed data to TEXT, HTML, and PDF formats using the liquid templating engine at its core. This allows for easy marking up of templates to be used with arbitrary LEWT extensions and processing them into multiple human readable formats on the fly.

Instance Attribute Summary collapse

Attributes inherited from Extension

#command_name, #customers, #enterprise, #lewt_settings, #lewt_stash, #options

Instance Method Summary collapse

Methods inherited from Extension

#get_matched_customers, #lewt_extensions

Constructor Details

#initializeLiquidRenderer

Sets up this extension and registers its run-time options.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/extensions/liquid-renderer.rb', line 20

def initialize ()
  options = {
    :method => {
      :definition => "Specify html, text, pdf, or any combination of the three to define output method",
      :default => "text",
      :short_flag => "-m",
      :type => String
    },
    :save_path => {
      :definition => "Specify where to save the output file (required for PDFs)",
      :type => String
    },
    :liquid_template => {
      :definition => "Override the template that liquid render should use. Defaults to the template which matches the processor name but you will want to override this if you are using multiple processors.",
      :type => String
    }
  }
  super({:cmd => "liquid_render", :options => options })
end

Instance Attribute Details

#htmlTemplateObject (readonly)

Returns the value of attribute htmlTemplate.



17
18
19
# File 'lib/extensions/liquid-renderer.rb', line 17

def htmlTemplate
  @htmlTemplate
end

#markupObject (readonly)

Returns the value of attribute markup.



17
18
19
# File 'lib/extensions/liquid-renderer.rb', line 17

def markup
  @markup
end

#pdfTemplateObject (readonly)

Returns the value of attribute pdfTemplate.



17
18
19
# File 'lib/extensions/liquid-renderer.rb', line 17

def pdfTemplate
  @pdfTemplate
end

#stylesheetObject (readonly)

Returns the value of attribute stylesheet.



17
18
19
# File 'lib/extensions/liquid-renderer.rb', line 17

def stylesheet
  @stylesheet
end

#textTemplateObject (readonly)

Returns the value of attribute textTemplate.



17
18
19
# File 'lib/extensions/liquid-renderer.rb', line 17

def textTemplate
  @textTemplate
end

Instance Method Details

#load_templates(template) ⇒ Object

Loads the plaint-text, html, & (optionally) pdf template files of the given template name and parses it with the Liquid class

template [String]

The name of the template to load.



42
43
44
45
46
# File 'lib/extensions/liquid-renderer.rb', line 42

def load_templates ( template )
  @textTemplate = Liquid::Template::parse( File.open( File.expand_path( lewt_stash  + "/templates/#{template}.text.liquid", __FILE__) ).read )
  @htmlTemplate = Liquid::Template::parse( File.open( File.expand_path( lewt_stash + "/templates/#{template}.html.liquid", __FILE__) ).read )
  @stylesheet = File.expand_path( lewt_stash + '/templates/style.css', __FILE__)
end

#render(options, data) ⇒ Object

Called on LEWT render cycle, this method outputs the data as per a pre-formated liquid template.

options [Hash]

The options hash passed to this function by the Lewt program.

data [Array]

An array of hash data to format.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/extensions/liquid-renderer.rb', line 51

def render ( options, data )
  output = Array.new
  # template name is always the same as processor name
  template = options[:liquid_template] != nil ? options[:liquid_template] : options[:process]
  load_templates( template )

  data.each_with_index do |d, i|

    if options[:method].match "text"
      r = textTemplate.render(d)
      if options[:save_path]
        save_name = format_save_name( options, i )
        File.open( save_name, 'w') {|f| f.write r }
        output << save_name
      else
        output << r
      end
    end
    
    if options[:method].match "html"
      r = htmlTemplate.render(d)
      if options[:save_path]
        save_name = format_save_name( options, i )
        File.open( save_name, 'w') {|f| f.write r }
        output << save_name
      else
        output << r
      end
    end
    
    if options[:method].match "pdf"
      raise ArgumentError,"--save-file flag must be specified for PDF output in #{self.class.name}" if !options[:save_path]
      save_name = format_save_name( options, i )
      html = htmlTemplate.render(d)
      kit = PDFKit.new(html, :page_size => 'A4')
      kit.stylesheets << @stylesheet
      file = kit.to_file( save_name )
      output << save_name
    end
  end
  # if options[:dump_output] != false
  #   output.each do |r|
  #     puts r
  #   end
  # end

  return output
end