Class: Luoma::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/luoma/template.rb,
sig/luoma/template.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, source, nodes, globals: nil, name: nil, overlay: nil, up_to_date: nil) ⇒ Template

(Environment, String, t_block, ?globals: t_namespace?, ?name: String?, ?overlay: t_namespace?, ?up_to_date: Proc::_Callable?) -> void

Parameters:

  • env (Environment)
  • source (String)
  • nodes (t_block)
  • globals: (t_namespace, nil) (defaults to: nil)
  • name: (String, nil) (defaults to: nil)
  • overlay: (t_namespace, nil) (defaults to: nil)
  • up_to_date: (Object, nil) (defaults to: nil)


14
15
16
17
18
19
20
21
22
23
# File 'lib/luoma/template.rb', line 14

def initialize(env, source, nodes, globals: nil, name: nil, overlay: nil, up_to_date: nil)
  @env = env
  @source = source
  @nodes = nodes
  @globals = globals
  @name = name || ""
  @overlay = overlay
  @up_to_date = up_to_date
  @lines = nil
end

Instance Attribute Details

#envEnvironment (readonly)

Returns the value of attribute env.

Returns:



5
6
7
# File 'lib/luoma/template.rb', line 5

def env
  @env
end

#globalst_namespace? (readonly)

Returns the value of attribute globals.

Returns:

  • (t_namespace, nil)


5
6
7
# File 'lib/luoma/template.rb', line 5

def globals
  @globals
end

#nameString (readonly)

Returns the value of attribute name.

Returns:

  • (String)


5
6
7
# File 'lib/luoma/template.rb', line 5

def name
  @name
end

#nodest_block (readonly)

Returns the value of attribute nodes.

Returns:

  • (t_block)


5
6
7
# File 'lib/luoma/template.rb', line 5

def nodes
  @nodes
end

#overlayt_namespace? (readonly)

Returns the value of attribute overlay.

Returns:

  • (t_namespace, nil)


5
6
7
# File 'lib/luoma/template.rb', line 5

def overlay
  @overlay
end

#sourceString (readonly)

Returns the value of attribute source.

Returns:

  • (String)


5
6
7
# File 'lib/luoma/template.rb', line 5

def source
  @source
end

#up_to_dateObject (readonly)

Returns the value of attribute up_to_date.

Returns:

  • (Object)


5
6
7
# File 'lib/luoma/template.rb', line 5

def up_to_date
  @up_to_date
end

Instance Method Details

#analyze(include_partials: false) ⇒ Luoma::StaticAnalysis::Result

Statically analyze this template and report variable, tag and filter usage.

(?include_partials: bool) -> Luoma::StaticAnalysis::Result

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:



75
76
77
# File 'lib/luoma/template.rb', line 75

def analyze(include_partials: false)
  Luoma::StaticAnalysis.analyze(self, include_partials: include_partials)
end

#commentsArray[Comment]

Return an array of comment nodes found in this template.

Comment nodes have token and text attributes. Use template.comments.map(&:text) to get an array of comment strings. Each comment string includes leading and trailing whitespace.

Note that this method does not try to load included or render templates when looking. for comment nodes.

() -> Array

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/luoma/template.rb', line 145

def comments
  context = RenderContext.new(self)
  nodes = [] # : Array[Comment]

  # @type var visit: ^(Markup) -> void
  visit = lambda do |node|
    nodes << node if node.is_a?(Comment)

    node.children(context).each do |child|
      visit.call(child)
    end
  end

  @nodes.each { |node| visit.call(node) unless node.is_a?(String) }

  nodes
end

#filter_names(include_partials: false) ⇒ Array[String]

Return the names of all filters used in this template.

(?include_partials: bool) -> Array

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:

  • (Array[String])


124
125
126
# File 'lib/luoma/template.rb', line 124

def filter_names(include_partials: false)
  analyze(include_partials: include_partials).filters.keys
end

#global_variable_paths(include_partials: false) ⇒ Array[String]

Return an array of global variables used in this template, including path segments.

(?include_partials: bool) -> Array

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:

  • (Array[String])


110
111
112
# File 'lib/luoma/template.rb', line 110

def global_variable_paths(include_partials: false)
  analyze(include_partials: include_partials).globals.values.flatten.map { |v| v[:path] }.uniq
end

#global_variable_segments(include_partials: false) ⇒ Array[untyped]

Return an array of global variables used in this template, each as an array of segments.

(?include_partials: bool) -> Array

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:

  • (Array[untyped])


117
118
119
# File 'lib/luoma/template.rb', line 117

def global_variable_segments(include_partials: false)
  analyze(include_partials: include_partials).globals.values.flatten.map { |v| v[:segments] }.uniq
end

#global_variables(include_partials: false) ⇒ Array[String]

Return an array of global variables used in this template, without path segments.

(?include_partials: bool) -> Array

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:

  • (Array[String])


103
104
105
# File 'lib/luoma/template.rb', line 103

def global_variables(include_partials: false)
  analyze(include_partials: include_partials).globals.keys
end

#linesArray[String]

Return this template's source code split into lines.

Signature:

  • () -> Array[String]

Returns:

  • (Array[String])


60
61
62
# File 'lib/luoma/template.rb', line 60

def lines
  @lines ||= @source.lines(chomp: false)
end

#make_globals(namespace) ⇒ t_namespace?

(t_namespace?) -> t_namespace?

Parameters:

  • namespace (t_namespace, nil)

Returns:

  • (t_namespace, nil)


168
169
170
171
172
173
174
175
176
# File 'lib/luoma/template.rb', line 168

def make_globals(namespace)
  return @globals if namespace.nil? && @overlay.nil?

  namespace_ = {} #: t_namespace
  namespace_.merge!(@globals) if @globals # steep:ignore
  namespace_.merge!(@overlay) if @overlay # steep:ignore
  namespace_.merge!(namespace) if namespace
  namespace_
end

#render(data = nil) ⇒ String

(t_namespace?) -> String

Parameters:

  • data (t_namespace, nil) (defaults to: nil)

Returns:

  • (String)


27
28
29
30
31
# File 'lib/luoma/template.rb', line 27

def render(data = nil)
  buffer = +""
  context = RenderContext.new(self, globals: make_globals(data))
  render_with_context(context, buffer)
end

#render_with_context(context, buffer, isolated: true) ⇒ String

(RenderContext, String, ?isolated: bool) -> String

Parameters:

  • context (RenderContext)
  • buffer (String)
  • isolated: (Boolean) (defaults to: true)

Returns:

  • (String)


40
41
42
43
# File 'lib/luoma/template.rb', line 40

def render_with_context(context, buffer, isolated: true)
  Luoma.render_block(@nodes, context, buffer, root: isolated)
  buffer
end

#tag_names(include_partials: false) ⇒ Array[String]

Return the names of all tags used in this template.

(?include_partials: bool) -> Array

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:

  • (Array[String])


131
132
133
# File 'lib/luoma/template.rb', line 131

def tag_names(include_partials: false)
  analyze(include_partials: include_partials).tags.keys
end

#up_to_date?Boolean

Return false if this template is stale and needs to be loaded again. nil is returned if an up_to_date proc is not available.

Returns:

  • (Boolean)


66
67
68
# File 'lib/luoma/template.rb', line 66

def up_to_date?
  @up_to_date&.call
end

#variable_paths(include_partials: false) ⇒ Array[String]

Return an array of variables used in this template, including path segments.

(?include_partials: bool) -> Array

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:

  • (Array[String])


89
90
91
# File 'lib/luoma/template.rb', line 89

def variable_paths(include_partials: false)
  analyze(include_partials: include_partials).variables.values.flatten.map { |v| v[:path] }.uniq
end

#variable_segments(include_partials: false) ⇒ Array[untyped]

Return an array of variables used in this template, each as an array of segments.

(?include_partials: bool) -> Array

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:

  • (Array[untyped])


96
97
98
# File 'lib/luoma/template.rb', line 96

def variable_segments(include_partials: false)
  analyze(include_partials: include_partials).variables.values.flatten.map { |v| v[:segments] }.uniq
end

#variables(include_partials: false) ⇒ Array[String]

Return an array of variable names used in this template, without path segments.

(?include_partials: bool) -> Array

Parameters:

  • include_partials: (Boolean) (defaults to: false)

Returns:

  • (Array[String])


82
83
84
# File 'lib/luoma/template.rb', line 82

def variables(include_partials: false)
  analyze(include_partials: include_partials).variables.keys
end

#with_globals(globals) ⇒ Template

(t_namespace) -> Template

Parameters:

  • namespace (t_namespace)

Returns:



47
48
49
50
51
52
53
54
55
# File 'lib/luoma/template.rb', line 47

def with_globals(globals)
  Template.new(
    @env, @source, @nodes,
    globals: globals,
    name: @name,
    overlay: @overlay,
    up_to_date: @up_to_date
  )
end