Class: Msgtrail::PageRenderer

Inherits:
Object
  • Object
show all
Includes:
RenderHelper
Defined in:
lib/msgtrail/renderers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RenderHelper

#as_rfc3339, #now_as_rfc3339, #titlecase, #xml_safe

Constructor Details

#initialize(layout_filepath, template_filepath, config) ⇒ PageRenderer

Returns a new instance of PageRenderer.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/msgtrail/renderers.rb', line 33

def initialize(layout_filepath, template_filepath, config)
  self.article = {}
  self.config = config
  self.markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, fenced_code_blocks: true, strikethrough: true)
  self.plaintext = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
  self.theme_directory = File.join(config.working_directory, config.settings.file_matter.theme_directory)
  begin
    self.layout = File.read(layout_filepath)
  rescue
    puts("Can't find '#{layout_filepath}' (#{$!})")
    exit(2)
  end
  begin
    self.template = File.read(template_filepath)
  rescue
    puts("Can't find '#{template_filepath}' (#{$!})")
    exit(2)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(missing_method_name, *args, &block) ⇒ Object

Offer shortcut ‘cfg` to `settings.config` for use inside ERBs



79
80
81
82
83
84
85
# File 'lib/msgtrail/renderers.rb', line 79

def method_missing(missing_method_name, *args, &block)
  if 'cfg' == missing_method_name.to_s
    self.config.settings
  else
    super
  end
end

Instance Attribute Details

#articleObject

Returns the value of attribute article.



31
32
33
# File 'lib/msgtrail/renderers.rb', line 31

def article
  @article
end

#articlesObject

Returns the value of attribute articles.



31
32
33
# File 'lib/msgtrail/renderers.rb', line 31

def articles
  @articles
end

#configObject

Returns the value of attribute config.



31
32
33
# File 'lib/msgtrail/renderers.rb', line 31

def config
  @config
end

#layoutObject

Returns the value of attribute layout.



31
32
33
# File 'lib/msgtrail/renderers.rb', line 31

def layout
  @layout
end

#markdownObject

Returns the value of attribute markdown.



31
32
33
# File 'lib/msgtrail/renderers.rb', line 31

def markdown
  @markdown
end

#plaintextObject

Returns the value of attribute plaintext.



31
32
33
# File 'lib/msgtrail/renderers.rb', line 31

def plaintext
  @plaintext
end

#templateObject

Returns the value of attribute template.



31
32
33
# File 'lib/msgtrail/renderers.rb', line 31

def template
  @template
end

#theme_directoryObject

Returns the value of attribute theme_directory.



31
32
33
# File 'lib/msgtrail/renderers.rb', line 31

def theme_directory
  @theme_directory
end

Instance Method Details

#_render(template) ⇒ Object



65
66
67
# File 'lib/msgtrail/renderers.rb', line 65

def _render(template)
  ERB.new(template).result(binding)
end

#renderObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/msgtrail/renderers.rb', line 53

def render
  erbs = [self.template, self.layout] # Note order!
  # Inject accepts value and block. Nil in inject(nil) sets initial value.
  # First iteration: prev = nil and erb = self.template.
  # Since self.template has no yield only a string is rendered.
  # Second iteration: prev = rendered string and erb = self.layout.
  # Since self.layout has yield, yield will be replaced by string in prev.
  erbs.inject(nil) do |prev, erb|
    _render(erb) { prev }
  end
end

#render_partial(partial_filename, variables) ⇒ Object



69
70
71
72
# File 'lib/msgtrail/renderers.rb', line 69

def render_partial(partial_filename, variables)
  partial_filepath = File.join(self.theme_directory, "_#{partial_filename}.html.erb")
  PartialRenderer.new(partial_filepath, variables, self.config).render
end

#track(event, sub_event = nil) ⇒ Object



74
75
76
# File 'lib/msgtrail/renderers.rb', line 74

def track(event, sub_event = nil)
  "<img src=\"#{self.config.settings.domain_matter.tracking_pixel_url}?e=#{event}&se=#{sub_event}\" width=\"1\" height=\"1\" alt=\".\"/>" if self.config.settings.domain_matter.tracking_pixel_url
end