Class: Apogee::PageRenderer

Inherits:
Object
  • Object
show all
Includes:
BuilderHelpers
Defined in:
lib/apogee/page_renderer.rb

Overview

Processes site pages for distribution

Constant Summary collapse

TOKEN_COMMENT_PATTERN =
/\A<!--[^>]*-->/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BuilderHelpers

#css?, #css_paths, #image_paths, #images?, #js?, #js_paths, #layout

Instance Attribute Details

#page_contentObject (readonly)

Returns the value of attribute page_content.



13
14
15
# File 'lib/apogee/page_renderer.rb', line 13

def page_content
  @page_content
end

Instance Method Details

#automatic_tokensObject



31
32
33
34
35
36
37
38
# File 'lib/apogee/page_renderer.rb', line 31

def automatic_tokens
  {
    'layout' => 'default',
    'css_bundle' => css_bundle,
    'js_bundle' => js_bundle,
    'page_content' => cleaned_page_content
  }
end

#cleaned_page_contentObject



66
67
68
# File 'lib/apogee/page_renderer.rb', line 66

def cleaned_page_content
  page_content.gsub(TOKEN_COMMENT_PATTERN, '').strip
end

#css_bundleObject



54
55
56
57
58
# File 'lib/apogee/page_renderer.rb', line 54

def css_bundle
  return '' unless css?

  '<link rel="stylesheet" href="styles.css">'
end

#js_bundleObject



60
61
62
63
64
# File 'lib/apogee/page_renderer.rb', line 60

def js_bundle
  return '' unless js?

  '<script src="script.js"></script>'
end

#layout_contentObject



23
24
25
# File 'lib/apogee/page_renderer.rb', line 23

def layout_content
  layout(tokens['layout'])
end

#parsed_tokensObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/apogee/page_renderer.rb', line 40

def parsed_tokens
  doc = page_content
          .match(TOKEN_COMMENT_PATTERN)[0]
          .delete_prefix('<!--')
          .delete_suffix('-->')
          .each_line
          .map(&:strip)
          .join("\n")

  YAML.safe_load(doc)
rescue StandardError
  {}
end

#render(page_content) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/apogee/page_renderer.rb', line 15

def render(page_content)
  @page_content = page_content

  tokens.inject(layout_content) do |rendered, (name, value)|
    rendered.gsub("{{#{name}}}", value)
  end
end

#tokensObject



27
28
29
# File 'lib/apogee/page_renderer.rb', line 27

def tokens
  automatic_tokens.merge(parsed_tokens)
end