Class: Vapid::Template

Inherits:
Object show all
Defined in:
lib/vapid/template.rb,
lib/vapid/template/node.rb,
lib/vapid/template/parser.rb

Overview

Template handler

Defined Under Namespace

Classes: Node, Parser

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ Template

Returns a new instance of Template.



9
10
11
12
# File 'lib/vapid/template.rb', line 9

def initialize(html)
  @html = html
  @development = Server.environment == :development
end

Instance Method Details

#renderObject

rubocop:disable Metrics/AbcSize



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vapid/template.rb', line 30

def render
  @parser = Parser.new(@html, default_group_content)

  @parser.walk set_context: method(:fetch_content) do |node, content|
    node.clone(content.size - 1) if node.cloneable?
    record = node.clone? ? content[node.clone_index] : content.first
    apply_directives(node, record)
  end

  add_development_styles if @development

  @parser.to_html
end

#treeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vapid/template.rb', line 14

def tree
  branches = {}

  @parser = Parser.new(@html, default_group)
  @parser.walk do |node, group_expr|
    branches[group_expr] ||= {}
    node.directives.each do |type, field|
      branches[group_expr][field] ||= []
      branches[group_expr][field] << type
    end
  end

  branches
end