Class: EJX::Template::Subtemplate

Inherits:
Node
  • Object
show all
Defined in:
lib/ejx/template/subtemplate.rb

Direct Known Subclasses

Multitemplate

Instance Attribute Summary collapse

Attributes inherited from Node

#children

Instance Method Summary collapse

Constructor Details

#initialize(opening, modifiers, append: true) ⇒ Subtemplate

Returns a new instance of Subtemplate.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ejx/template/subtemplate.rb', line 18

def initialize(opening, modifiers, append: true)
  @children = [opening]
  @modifiers = modifiers
  @append = append
  @balance_stack = opening ? EJX::Template::BalanceScanner.parse(opening) : []
  
  @assigned_to_variable = @children.first&.match(/\A\s*(var|const|let)\s+(\S+)/)&.send(:[], 2)
  @async = false
  
  if match = @children.first&.match(/(?:async\s+)?function(:?\s+\w+)?\s*\([^\)]*\)\s*\{\s*\Z/m)
    @function_type = :regular
    @async = match[0].start_with?('async')
    @assigned_to_variable = true if !match[1].nil?
  elsif match = @children.first&.match(/(?:async)?(?:\s*\([^\)\(]*\)|(?:(?<=\()|\s+)[^\(\s]+)?\s*=>\s*\{\s*\Z/m)
    @function_type = :arrow
    @async = match[0].start_with?('async')
  end
end

Instance Attribute Details

#modifiersObject (readonly)

Returns the value of attribute modifiers.



3
4
5
# File 'lib/ejx/template/subtemplate.rb', line 3

def modifiers
  @modifiers
end

Instance Method Details

#arrow_function?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ejx/template/subtemplate.rb', line 14

def arrow_function?
  @function_type == :arrow
end

#balanceObject



55
56
57
# File 'lib/ejx/template/subtemplate.rb', line 55

def balance
  @balance_stack
end

#balanced?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/ejx/template/subtemplate.rb', line 51

def balanced?
  @balance_stack.empty?
end

#ending_balanceObject



59
60
61
# File 'lib/ejx/template/subtemplate.rb', line 59

def ending_balance
  @balance_stack.reverse.map { |i| EJX::Template::BalanceScanner::BALANCE[i] }
end

#function?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/ejx/template/subtemplate.rb', line 10

def function?
  !!@function_type
end

#push(*values) ⇒ Object Also known as: <<



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ejx/template/subtemplate.rb', line 37

def push(*values)
  values.each do |value|
    case value
    when EJX::Template::JS
      EJX::Template::BalanceScanner.parse(value.value, @balance_stack)
    when String
      EJX::Template::BalanceScanner.parse(value, @balance_stack)
    end
  end
  
  super
end

#to_js(indentation: 4, var_generator: nil, append: "__output", promises: '__promises') ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ejx/template/subtemplate.rb', line 63

def to_js(indentation: 4, var_generator: nil, append: "__output", promises: '__promises')
  output = ''

  global_output_var = var_generator.next if !assigned_to_variable?
  sub_global_output_var = var_generator.next
  output_var = var_generator.next

  if assigned_to_variable?
    output << "#{' '*indentation}#{@children.first}\n"
    output << "#{' '*(indentation+4)}var #{sub_global_output_var}_promises = [];\n"
  elsif !(@modifiers & [:escape, :unescape]).empty?
    output << "#{' '*indentation}var #{global_output_var}_result = #{@children.first}\n"
    output << "#{' '*(indentation+4)}var #{sub_global_output_var}_promises = [];\n"
  else
    output << <<~JS.gsub(/\n+\Z/, '')
      #{' '*(indentation)}var #{global_output_var}_results = [];
      #{' '*(indentation)}var #{global_output_var}_promises = [];
      #{' '*(indentation)}var #{global_output_var}_result = 
    JS
    
    indentation += 4
    output << if arrow_function?
      @children.first.sub(/((?:async)?(?:\s*\([^\)\(]*\)|(?:(?<=\()|\s+)[^\(\s]+)?\s*=>\s*\{\s*)\Z/m, <<~JS)
        (...__args) => {
        #{' '*(indentation)}var #{sub_global_output_var}_results = [];
        #{' '*(indentation)}var #{sub_global_output_var}_promises = [];
        #{' '*(indentation)}var #{sub_global_output_var}_result = (\\1
      JS
    else
      @children.first.sub(/((?:async\s+)?\s*function\s*\([^\)\(]*\)\s*\{\s*)\Z/m, <<~JS)
        (...__args) => {
        #{' '*(indentation)}var #{sub_global_output_var}_results = [];
        #{' '*(indentation)}var #{sub_global_output_var}_promises = [];
        #{' '*(indentation)}var #{sub_global_output_var}_result = (\\1
      JS
    end
  end

  output << "#{' '*(indentation+4)}var #{output_var} = [];\n"
  
  @children[1..-2].each do |child|
    promise_var = "#{sub_global_output_var}_promises"
    output << case child
    when EJX::Template::String
      "#{' '*(indentation+4)}__ejx_append(#{child.to_js}, #{output_var}, 'unescape', #{promise_var});\n"
    else
      child.to_js(indentation: indentation + 4, var_generator: var_generator, append: output_var, promises: promise_var)
    end
  end

  if !assigned_to_variable? && (@modifiers & [:escape, :unescape]).empty?
    output << ' '*(indentation+4) << "#{sub_global_output_var}_results.push(#{output_var});\n"
  end

  if async?
    output << ' '*(indentation+4) << "await Promise.all(#{sub_global_output_var}_promises);\n"
    output << ' '*(indentation+4) << "return #{output_var};\n";
  elsif assigned_to_variable?
    output << ' '*(indentation+4)
    output << "return #{sub_global_output_var}_promises.length === 0 ? #{output_var} : Promise.all(#{sub_global_output_var}_promises).then(() => #{output_var});\n"
  else
    output << ' '*(indentation+4) << "return Promise.all(#{sub_global_output_var}_promises).then(() => #{output_var});\n"
  end

  output << ' '*((@modifiers & [:escape, :unescape]).empty? ? indentation : indentation-4)

  split = @children.last.strip.delete_suffix(';').split(/\}/, 2)
  output << split[0]
    
  if !assigned_to_variable? && (@modifiers & [:escape, :unescape]).empty?
    output << "})(...__args);\n"
    output << "#{' '*indentation}__ejx_append(#{sub_global_output_var}_results, #{global_output_var}_results, 'escape', #{global_output_var}_promises, #{sub_global_output_var}_result);\n"
    output << ' '*(indentation) << "return #{sub_global_output_var}_result;\n"
    output << ' '*(indentation-4) << "}" << split[1]
  else
    output << ' '*(indentation-4) << "}" << split[1]
  end
  indentation = indentation - 4
  
  if assigned_to_variable?
    output << ";\n"
    if !(@modifiers & [:escape, :unescape]).empty?
      output << "#{' '*indentation}__ejx_append(#{@assigned_to_variable}, #{append}, 'escape', #{promises});\n"
    end
  else
    output << ";\n"
    if !(@modifiers & [:escape, :unescape]).empty?
      output << "#{' '*(indentation+4)}__ejx_append(#{global_output_var}_result, #{append}, 'escape', #{promises});\n"
    else
      output << "#{' '*indentation}__ejx_append(#{global_output_var}_results.flat(1), #{append}, 'escape', #{promises}, "
      output << "(#{global_output_var}_result instanceof Promise) ? #{global_output_var}_result.then(() => Promise.all(#{global_output_var}_promises).then(r => r.flat(1))) : Promise.all(#{global_output_var}_promises).then(r => r.flat(1)));\n"
    end
  end

  output
end

#to_sub_js(indentation: 4, var_generator: nil, promises: '__promises') ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ejx/template/subtemplate.rb', line 160

def to_sub_js(indentation: 4, var_generator: nil, promises: '__promises')
  output_var = var_generator.next
  
  output = "#{' '*(indentation)}var #{output_var} = [];\n"

  @children[1..-1].each do |child|
    output << case child
    when EJX::Template::String
      "#{' '*(indentation)}__ejx_append(#{child.to_js}, #{output_var}, 'unescape', #{promises});\n"
    else
      child.to_js(indentation: indentation, var_generator: var_generator, append: output_var)
    end
  end
  output << ' '*(indentation) << "return #{output_var};\n";

  output
end