tina4ruby 3.11.15 → 3.11.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +80 -80
- data/LICENSE.txt +21 -21
- data/README.md +137 -137
- data/exe/tina4ruby +5 -5
- data/lib/tina4/ai.rb +696 -696
- data/lib/tina4/api.rb +189 -189
- data/lib/tina4/auth.rb +305 -305
- data/lib/tina4/auto_crud.rb +244 -244
- data/lib/tina4/cache.rb +154 -154
- data/lib/tina4/cli.rb +1449 -1449
- data/lib/tina4/constants.rb +46 -46
- data/lib/tina4/container.rb +74 -74
- data/lib/tina4/cors.rb +74 -74
- data/lib/tina4/crud.rb +692 -692
- data/lib/tina4/database/sqlite3_adapter.rb +165 -165
- data/lib/tina4/database.rb +625 -625
- data/lib/tina4/database_result.rb +208 -208
- data/lib/tina4/debug.rb +8 -8
- data/lib/tina4/dev.rb +14 -14
- data/lib/tina4/dev_admin.rb +1291 -935
- data/lib/tina4/dev_mailbox.rb +191 -191
- data/lib/tina4/drivers/firebird_driver.rb +124 -124
- data/lib/tina4/drivers/mongodb_driver.rb +561 -561
- data/lib/tina4/drivers/mssql_driver.rb +112 -112
- data/lib/tina4/drivers/mysql_driver.rb +90 -90
- data/lib/tina4/drivers/odbc_driver.rb +191 -191
- data/lib/tina4/drivers/postgres_driver.rb +116 -116
- data/lib/tina4/drivers/sqlite_driver.rb +122 -122
- data/lib/tina4/env.rb +95 -95
- data/lib/tina4/error_overlay.rb +252 -252
- data/lib/tina4/events.rb +109 -109
- data/lib/tina4/field_types.rb +154 -154
- data/lib/tina4/frond.rb +2087 -2025
- data/lib/tina4/gallery/auth/meta.json +1 -1
- data/lib/tina4/gallery/auth/src/routes/api/gallery_auth.rb +114 -114
- data/lib/tina4/gallery/database/meta.json +1 -1
- data/lib/tina4/gallery/database/src/routes/api/gallery_db.rb +43 -43
- data/lib/tina4/gallery/error-overlay/meta.json +1 -1
- data/lib/tina4/gallery/error-overlay/src/routes/api/gallery_crash.rb +17 -17
- data/lib/tina4/gallery/orm/meta.json +1 -1
- data/lib/tina4/gallery/orm/src/routes/api/gallery_products.rb +16 -16
- data/lib/tina4/gallery/queue/meta.json +1 -1
- data/lib/tina4/gallery/queue/src/routes/api/gallery_queue.rb +325 -325
- data/lib/tina4/gallery/rest-api/meta.json +1 -1
- data/lib/tina4/gallery/rest-api/src/routes/api/gallery_hello.rb +14 -14
- data/lib/tina4/gallery/templates/meta.json +1 -1
- data/lib/tina4/gallery/templates/src/routes/gallery_page.rb +12 -12
- data/lib/tina4/gallery/templates/src/templates/gallery_page.twig +257 -257
- data/lib/tina4/graphql.rb +966 -966
- data/lib/tina4/health.rb +39 -39
- data/lib/tina4/html_element.rb +170 -170
- data/lib/tina4/job.rb +80 -80
- data/lib/tina4/localization.rb +168 -168
- data/lib/tina4/log.rb +203 -203
- data/lib/tina4/mcp.rb +871 -696
- data/lib/tina4/messenger.rb +587 -587
- data/lib/tina4/metrics.rb +793 -793
- data/lib/tina4/middleware.rb +445 -445
- data/lib/tina4/migration.rb +451 -451
- data/lib/tina4/orm.rb +790 -790
- data/lib/tina4/plan.rb +471 -0
- data/lib/tina4/project_index.rb +366 -0
- data/lib/tina4/public/css/tina4.css +2463 -2463
- data/lib/tina4/public/css/tina4.min.css +1 -1
- data/lib/tina4/public/images/logo.svg +5 -5
- data/lib/tina4/public/js/frond.min.js +2 -2
- data/lib/tina4/public/js/tina4-dev-admin.js +1264 -565
- data/lib/tina4/public/js/tina4-dev-admin.min.js +1264 -480
- data/lib/tina4/public/js/tina4.min.js +92 -92
- data/lib/tina4/public/js/tina4js.min.js +48 -48
- data/lib/tina4/public/swagger/index.html +90 -90
- data/lib/tina4/public/swagger/oauth2-redirect.html +63 -63
- data/lib/tina4/query_builder.rb +380 -380
- data/lib/tina4/queue.rb +366 -366
- data/lib/tina4/queue_backends/kafka_backend.rb +80 -80
- data/lib/tina4/queue_backends/lite_backend.rb +298 -298
- data/lib/tina4/queue_backends/mongo_backend.rb +126 -126
- data/lib/tina4/queue_backends/rabbitmq_backend.rb +73 -73
- data/lib/tina4/rack_app.rb +817 -817
- data/lib/tina4/rate_limiter.rb +130 -130
- data/lib/tina4/request.rb +268 -268
- data/lib/tina4/response.rb +346 -346
- data/lib/tina4/response_cache.rb +551 -551
- data/lib/tina4/router.rb +406 -406
- data/lib/tina4/scss/tina4css/_alerts.scss +34 -34
- data/lib/tina4/scss/tina4css/_badges.scss +22 -22
- data/lib/tina4/scss/tina4css/_buttons.scss +69 -69
- data/lib/tina4/scss/tina4css/_cards.scss +49 -49
- data/lib/tina4/scss/tina4css/_forms.scss +156 -156
- data/lib/tina4/scss/tina4css/_grid.scss +81 -81
- data/lib/tina4/scss/tina4css/_modals.scss +84 -84
- data/lib/tina4/scss/tina4css/_nav.scss +149 -149
- data/lib/tina4/scss/tina4css/_reset.scss +94 -94
- data/lib/tina4/scss/tina4css/_tables.scss +54 -54
- data/lib/tina4/scss/tina4css/_typography.scss +55 -55
- data/lib/tina4/scss/tina4css/_utilities.scss +197 -197
- data/lib/tina4/scss/tina4css/_variables.scss +117 -117
- data/lib/tina4/scss/tina4css/base.scss +1 -1
- data/lib/tina4/scss/tina4css/colors.scss +48 -48
- data/lib/tina4/scss/tina4css/tina4.scss +17 -17
- data/lib/tina4/scss_compiler.rb +178 -178
- data/lib/tina4/seeder.rb +567 -567
- data/lib/tina4/service_runner.rb +303 -303
- data/lib/tina4/session.rb +297 -297
- data/lib/tina4/session_handlers/database_handler.rb +72 -72
- data/lib/tina4/session_handlers/file_handler.rb +67 -67
- data/lib/tina4/session_handlers/mongo_handler.rb +49 -49
- data/lib/tina4/session_handlers/redis_handler.rb +43 -43
- data/lib/tina4/session_handlers/valkey_handler.rb +43 -43
- data/lib/tina4/shutdown.rb +84 -84
- data/lib/tina4/sql_translation.rb +158 -158
- data/lib/tina4/swagger.rb +124 -124
- data/lib/tina4/template.rb +894 -894
- data/lib/tina4/templates/base.twig +26 -26
- data/lib/tina4/templates/errors/302.twig +14 -14
- data/lib/tina4/templates/errors/401.twig +9 -9
- data/lib/tina4/templates/errors/403.twig +29 -29
- data/lib/tina4/templates/errors/404.twig +29 -29
- data/lib/tina4/templates/errors/500.twig +38 -38
- data/lib/tina4/templates/errors/502.twig +9 -9
- data/lib/tina4/templates/errors/503.twig +12 -12
- data/lib/tina4/templates/errors/base.twig +37 -37
- data/lib/tina4/test_client.rb +159 -159
- data/lib/tina4/testing.rb +340 -340
- data/lib/tina4/validator.rb +174 -174
- data/lib/tina4/version.rb +1 -1
- data/lib/tina4/webserver.rb +312 -312
- data/lib/tina4/websocket.rb +343 -343
- data/lib/tina4/websocket_backplane.rb +190 -190
- data/lib/tina4/wsdl.rb +564 -564
- data/lib/tina4.rb +460 -458
- data/lib/tina4ruby.rb +4 -4
- metadata +5 -3
data/lib/tina4/frond.rb
CHANGED
|
@@ -1,2025 +1,2087 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# Tina4 Frond Engine -- Lexer, parser, and runtime.
|
|
4
|
-
# Zero-dependency twig-like template engine.
|
|
5
|
-
# Supports: variables, filters, if/elseif/else/endif, for/else/endfor,
|
|
6
|
-
# extends/block, include, macro, set, comments, whitespace control, tests,
|
|
7
|
-
# fragment caching, sandboxing, auto-escaping, custom filters/tests/globals.
|
|
8
|
-
|
|
9
|
-
require "json"
|
|
10
|
-
require "digest"
|
|
11
|
-
require "base64"
|
|
12
|
-
require "cgi"
|
|
13
|
-
require "uri"
|
|
14
|
-
require "date"
|
|
15
|
-
require "time"
|
|
16
|
-
require "securerandom"
|
|
17
|
-
|
|
18
|
-
module Tina4
|
|
19
|
-
# Marker class for strings that should not be auto-escaped in Frond.
|
|
20
|
-
class SafeString < String
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
class Frond
|
|
24
|
-
# -- Token types ----------------------------------------------------------
|
|
25
|
-
TEXT = :text
|
|
26
|
-
VAR = :var # {{ ... }}
|
|
27
|
-
BLOCK = :block # {% ... %}
|
|
28
|
-
COMMENT = :comment # {# ... #}
|
|
29
|
-
|
|
30
|
-
# Regex to split template source into tokens
|
|
31
|
-
TOKEN_RE = /(\{%-?\s*.*?\s*-?%\})|(\{\{-?\s*.*?\s*-?\}\})|(\{#.*?#\})/m
|
|
32
|
-
|
|
33
|
-
# HTML escape table
|
|
34
|
-
HTML_ESCAPE_MAP = { "&" => "&", "<" => "<", ">" => ">",
|
|
35
|
-
'"' => """, "'" => "'" }.freeze
|
|
36
|
-
HTML_ESCAPE_RE = /[&<>"']/
|
|
37
|
-
|
|
38
|
-
# -- Compiled regex constants (optimization: avoid re-compiling in methods) --
|
|
39
|
-
EXTENDS_RE = /\{%-?\s*extends\s+["'](.+?)["']\s*-?%\}/
|
|
40
|
-
BLOCK_RE = /\{%-?\s*block\s+(\w+)\s*-?%\}(.*?)\{%-?\s*endblock\s*-?%\}/m
|
|
41
|
-
STRING_LIT_RE = /\A["'](.*)["']\z/
|
|
42
|
-
INTEGER_RE = /\A-?\d+\z/
|
|
43
|
-
FLOAT_RE = /\A-?\d+\.\d+\z/
|
|
44
|
-
ARRAY_LIT_RE = /\A\[(.+)\]\z/m
|
|
45
|
-
HASH_LIT_RE = /\A\{(.+)\}\z/m
|
|
46
|
-
HASH_PAIR_RE = /\A\s*(?:["']([^"']+)["']|(\w+))\s*:\s*(.+)\z/
|
|
47
|
-
RANGE_LIT_RE = /\A(\d+)\.\.(\d+)\z/
|
|
48
|
-
ARITHMETIC_OPS = [" + ", " - ", " * ", " // ", " / ", " % ", " ** "].freeze
|
|
49
|
-
FUNC_CALL_RE = /\A(\w+)\s*\((.*)\)\z/m
|
|
50
|
-
FILTER_WITH_ARGS_RE = /\A(\w+)\s*\((.*)\)\z/m
|
|
51
|
-
FILTER_CMP_RE = /\A(\w+)\s*(!=|==|>=|<=|>|<)\s*(.+)\z/
|
|
52
|
-
OR_SPLIT_RE = /\s+or\s+/
|
|
53
|
-
AND_SPLIT_RE = /\s+and\s+/
|
|
54
|
-
IS_NOT_RE = /\A(.+?)\s+is\s+not\s+(\w+)(.*)\z/
|
|
55
|
-
IS_RE = /\A(.+?)\s+is\s+(\w+)(.*)\z/
|
|
56
|
-
NOT_IN_RE = /\A(.+?)\s+not\s+in\s+(.+)\z/
|
|
57
|
-
IN_RE = /\A(.+?)\s+in\s+(.+)\z/
|
|
58
|
-
DIVISIBLE_BY_RE = /\s*by\s*\(\s*(\d+)\s*\)/
|
|
59
|
-
RESOLVE_SPLIT_RE = /\.|\[([^\]]+)\]/
|
|
60
|
-
RESOLVE_STRIP_RE = /\A["']|["']\z/
|
|
61
|
-
DIGIT_RE = /\A\d+\z/
|
|
62
|
-
FOR_RE = /\Afor\s+(\w+)(?:\s*,\s*(\w+))?\s+in\s+(.+)\z/
|
|
63
|
-
SET_RE = /\Aset\s+(\w+)\s*=\s*(.+)\z/m
|
|
64
|
-
INCLUDE_RE = /\Ainclude\s+["'](.+?)["'](?:\s+with\s+(.+))?\z/
|
|
65
|
-
MACRO_RE = /\Amacro\s+(\w+)\s*\(([^)]*)\)/
|
|
66
|
-
FROM_IMPORT_RE = /\Afrom\s+["'](.+?)["']\s+import\s+(.+)/
|
|
67
|
-
CACHE_RE = /\Acache\s+["'](.+?)["']\s*(\d+)?/
|
|
68
|
-
SPACELESS_RE = />\s+</
|
|
69
|
-
AUTOESCAPE_RE = /\Aautoescape\s+(false|true)/
|
|
70
|
-
STRIPTAGS_RE = /<[^>]+>/
|
|
71
|
-
THOUSANDS_RE = /(\d)(?=(\d{3})+(?!\d))/
|
|
72
|
-
SLUG_CLEAN_RE = /[^a-z0-9]+/
|
|
73
|
-
SLUG_TRIM_RE = /\A-|-\z/
|
|
74
|
-
|
|
75
|
-
# Set of common no-arg filter names that can be inlined for speed
|
|
76
|
-
INLINE_FILTERS = %w[upper lower length trim capitalize title string int escape e].each_with_object({}) { |f, h| h[f] = true }.freeze
|
|
77
|
-
|
|
78
|
-
# -- Lazy context overlay for for-loops (avoids full Hash#dup) --
|
|
79
|
-
class LoopContext
|
|
80
|
-
def initialize(parent)
|
|
81
|
-
@parent = parent
|
|
82
|
-
@local = {}
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def [](key)
|
|
86
|
-
@local.key?(key) ? @local[key] : @parent[key]
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def []=(key, value)
|
|
90
|
-
@local[key] = value
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def key?(key)
|
|
94
|
-
@local.key?(key) || @parent.key?(key)
|
|
95
|
-
end
|
|
96
|
-
alias include? key?
|
|
97
|
-
alias has_key? key?
|
|
98
|
-
|
|
99
|
-
def fetch(key, *args, &block)
|
|
100
|
-
if @local.key?(key)
|
|
101
|
-
@local[key]
|
|
102
|
-
elsif @parent.key?(key)
|
|
103
|
-
@parent[key]
|
|
104
|
-
elsif block
|
|
105
|
-
yield key
|
|
106
|
-
elsif !args.empty?
|
|
107
|
-
args[0]
|
|
108
|
-
else
|
|
109
|
-
raise KeyError, "key not found: #{key.inspect}"
|
|
110
|
-
end
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def merge(other)
|
|
114
|
-
dup_hash = to_h
|
|
115
|
-
dup_hash.merge!(other)
|
|
116
|
-
dup_hash
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def merge!(other)
|
|
120
|
-
other.each { |k, v| @local[k] = v }
|
|
121
|
-
self
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def dup
|
|
125
|
-
copy = LoopContext.new(@parent)
|
|
126
|
-
@local.each { |k, v| copy[k] = v }
|
|
127
|
-
copy
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def to_h
|
|
131
|
-
h = @parent.is_a?(LoopContext) ? @parent.to_h : @parent.dup
|
|
132
|
-
@local.each { |k, v| h[k] = v }
|
|
133
|
-
h
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
def each(&block)
|
|
137
|
-
to_h.each(&block)
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
def respond_to_missing?(name, include_private = false)
|
|
141
|
-
@parent.respond_to?(name, include_private) || super
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
def is_a?(klass)
|
|
145
|
-
klass == Hash || super
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
def keys
|
|
149
|
-
(@parent.is_a?(LoopContext) ? @parent.keys : @parent.keys) | @local.keys
|
|
150
|
-
end
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
# -----------------------------------------------------------------------
|
|
154
|
-
# Public API
|
|
155
|
-
# -----------------------------------------------------------------------
|
|
156
|
-
|
|
157
|
-
attr_reader :template_dir
|
|
158
|
-
|
|
159
|
-
def initialize(template_dir: "src/templates")
|
|
160
|
-
@template_dir = template_dir
|
|
161
|
-
@filters = default_filters
|
|
162
|
-
@globals = {}
|
|
163
|
-
@tests = default_tests
|
|
164
|
-
@auto_escape = true
|
|
165
|
-
|
|
166
|
-
# Sandboxing
|
|
167
|
-
@sandbox = false
|
|
168
|
-
@allowed_filters = nil
|
|
169
|
-
@allowed_tags = nil
|
|
170
|
-
@allowed_vars = nil
|
|
171
|
-
|
|
172
|
-
# Fragment cache: key => [html, expires_at]
|
|
173
|
-
@fragment_cache = {}
|
|
174
|
-
|
|
175
|
-
# Token pre-compilation cache
|
|
176
|
-
@compiled = {} # {template_name => [tokens, mtime]}
|
|
177
|
-
@compiled_strings = {} # {md5_hash => tokens}
|
|
178
|
-
|
|
179
|
-
# Parsed filter chain cache: expr_string => [variable, filters]
|
|
180
|
-
@filter_chain_cache = {}
|
|
181
|
-
|
|
182
|
-
# Resolved dotted-path split cache: expr_string => parts_array
|
|
183
|
-
@resolve_cache = {}
|
|
184
|
-
|
|
185
|
-
# Sandbox root-var split cache: var_name => root_var_string
|
|
186
|
-
@dotted_split_cache = {}
|
|
187
|
-
|
|
188
|
-
# Built-in global functions
|
|
189
|
-
register_builtin_globals
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
# Render a template file with data. Uses token caching for performance.
|
|
193
|
-
def render(template, data = {})
|
|
194
|
-
context = @globals.merge(stringify_keys(data))
|
|
195
|
-
|
|
196
|
-
path = File.join(@template_dir, template)
|
|
197
|
-
raise "Template not found: #{path}" unless File.exist?(path)
|
|
198
|
-
|
|
199
|
-
debug_mode = ENV.fetch("TINA4_DEBUG", "").downcase == "true"
|
|
200
|
-
|
|
201
|
-
unless debug_mode
|
|
202
|
-
# Production: use permanent cache (no filesystem checks)
|
|
203
|
-
cached = @compiled[template]
|
|
204
|
-
return execute_cached(cached[0], context) if cached
|
|
205
|
-
end
|
|
206
|
-
# Dev mode: skip cache entirely — always re-read and re-tokenize
|
|
207
|
-
# so edits to partials and extended base templates are detected
|
|
208
|
-
|
|
209
|
-
# Cache miss — load, tokenize, cache
|
|
210
|
-
source = File.read(path, encoding: "utf-8")
|
|
211
|
-
mtime = File.mtime(path)
|
|
212
|
-
tokens = tokenize(source)
|
|
213
|
-
@compiled[template] = [tokens, mtime]
|
|
214
|
-
execute_with_tokens(source, tokens, context)
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
# Render a template string directly. Uses token caching for performance.
|
|
218
|
-
def render_string(source, data = {})
|
|
219
|
-
context = @globals.merge(stringify_keys(data))
|
|
220
|
-
|
|
221
|
-
key = Digest::MD5.hexdigest(source)
|
|
222
|
-
cached_tokens = @compiled_strings[key]
|
|
223
|
-
|
|
224
|
-
if cached_tokens
|
|
225
|
-
return execute_cached(cached_tokens, context)
|
|
226
|
-
end
|
|
227
|
-
|
|
228
|
-
tokens = tokenize(source)
|
|
229
|
-
@compiled_strings[key] = tokens
|
|
230
|
-
execute_cached(tokens, context)
|
|
231
|
-
end
|
|
232
|
-
|
|
233
|
-
# Clear all compiled template caches.
|
|
234
|
-
def clear_cache
|
|
235
|
-
@compiled.clear
|
|
236
|
-
@compiled_strings.clear
|
|
237
|
-
@filter_chain_cache.clear
|
|
238
|
-
@resolve_cache.clear
|
|
239
|
-
@dotted_split_cache.clear
|
|
240
|
-
end
|
|
241
|
-
|
|
242
|
-
# Register a custom filter.
|
|
243
|
-
def add_filter(name, &blk)
|
|
244
|
-
@filters[name.to_s] = blk
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
# Register a custom test.
|
|
248
|
-
def add_test(name, &blk)
|
|
249
|
-
@tests[name.to_s] = blk
|
|
250
|
-
end
|
|
251
|
-
|
|
252
|
-
# Register a global variable available in all templates.
|
|
253
|
-
def add_global(name, value)
|
|
254
|
-
@globals[name.to_s] = value
|
|
255
|
-
end
|
|
256
|
-
|
|
257
|
-
# Enable sandbox mode.
|
|
258
|
-
def sandbox(filters: nil, tags: nil, vars: nil)
|
|
259
|
-
@sandbox = true
|
|
260
|
-
@allowed_filters = filters ? filters.map(&:to_s) : nil
|
|
261
|
-
@allowed_tags = tags ? tags.map(&:to_s) : nil
|
|
262
|
-
@allowed_vars = vars ? vars.map(&:to_s) : nil
|
|
263
|
-
self
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
# Disable sandbox mode.
|
|
267
|
-
def unsandbox
|
|
268
|
-
@sandbox = false
|
|
269
|
-
@allowed_filters = nil
|
|
270
|
-
@allowed_tags = nil
|
|
271
|
-
@allowed_vars = nil
|
|
272
|
-
self
|
|
273
|
-
end
|
|
274
|
-
|
|
275
|
-
# Utility: HTML escape
|
|
276
|
-
def self.escape_html(str)
|
|
277
|
-
str.to_s.gsub(HTML_ESCAPE_RE, HTML_ESCAPE_MAP)
|
|
278
|
-
end
|
|
279
|
-
|
|
280
|
-
private
|
|
281
|
-
|
|
282
|
-
# -----------------------------------------------------------------------
|
|
283
|
-
# Tokenizer
|
|
284
|
-
# -----------------------------------------------------------------------
|
|
285
|
-
|
|
286
|
-
# Regex to extract {% raw %}...{% endraw %} blocks before tokenizing
|
|
287
|
-
RAW_BLOCK_RE = /\{%-?\s*raw\s*-?%\}(.*?)\{%-?\s*endraw\s*-?%\}/m
|
|
288
|
-
|
|
289
|
-
def tokenize(source)
|
|
290
|
-
# 1. Extract raw blocks and replace with placeholders
|
|
291
|
-
raw_blocks = []
|
|
292
|
-
source = source.gsub(RAW_BLOCK_RE) do
|
|
293
|
-
idx = raw_blocks.length
|
|
294
|
-
raw_blocks << Regexp.last_match(1)
|
|
295
|
-
"\x00RAW_#{idx}\x00"
|
|
296
|
-
end
|
|
297
|
-
|
|
298
|
-
# 2. Normal tokenization
|
|
299
|
-
tokens = []
|
|
300
|
-
pos = 0
|
|
301
|
-
source.scan(TOKEN_RE) do
|
|
302
|
-
m = Regexp.last_match
|
|
303
|
-
start = m.begin(0)
|
|
304
|
-
tokens << [TEXT, source[pos...start]] if start > pos
|
|
305
|
-
|
|
306
|
-
raw = m[0]
|
|
307
|
-
if raw.start_with?("{#")
|
|
308
|
-
tokens << [COMMENT, raw]
|
|
309
|
-
elsif raw.start_with?("{{")
|
|
310
|
-
tokens << [VAR, raw]
|
|
311
|
-
elsif raw.start_with?("{%")
|
|
312
|
-
tokens << [BLOCK, raw]
|
|
313
|
-
end
|
|
314
|
-
pos = m.end(0)
|
|
315
|
-
end
|
|
316
|
-
tokens << [TEXT, source[pos..]] if pos < source.length
|
|
317
|
-
|
|
318
|
-
# 3. Restore raw block placeholders as literal TEXT
|
|
319
|
-
unless raw_blocks.empty?
|
|
320
|
-
tokens = tokens.map do |ttype, value|
|
|
321
|
-
if ttype == TEXT && value.include?("\x00RAW_")
|
|
322
|
-
raw_blocks.each_with_index do |content, idx|
|
|
323
|
-
value = value.gsub("\x00RAW_#{idx}\x00", content)
|
|
324
|
-
end
|
|
325
|
-
end
|
|
326
|
-
[ttype, value]
|
|
327
|
-
end
|
|
328
|
-
end
|
|
329
|
-
|
|
330
|
-
tokens
|
|
331
|
-
end
|
|
332
|
-
|
|
333
|
-
# Strip delimiters from a tag and detect whitespace control markers.
|
|
334
|
-
# Returns [content, strip_before, strip_after].
|
|
335
|
-
def strip_tag(raw)
|
|
336
|
-
inner = raw[2..-3] # remove {{ }} or {% %} or {# #}
|
|
337
|
-
strip_before = false
|
|
338
|
-
strip_after = false
|
|
339
|
-
|
|
340
|
-
if inner.start_with?("-")
|
|
341
|
-
strip_before = true
|
|
342
|
-
inner = inner[1..]
|
|
343
|
-
end
|
|
344
|
-
if inner.end_with?("-")
|
|
345
|
-
strip_after = true
|
|
346
|
-
inner = inner[0..-2]
|
|
347
|
-
end
|
|
348
|
-
|
|
349
|
-
[inner.strip, strip_before, strip_after]
|
|
350
|
-
end
|
|
351
|
-
|
|
352
|
-
# -----------------------------------------------------------------------
|
|
353
|
-
# Template loading
|
|
354
|
-
# -----------------------------------------------------------------------
|
|
355
|
-
|
|
356
|
-
def load_template(name)
|
|
357
|
-
path = File.join(@template_dir, name)
|
|
358
|
-
raise "Template not found: #{path}" unless File.exist?(path)
|
|
359
|
-
|
|
360
|
-
File.read(path, encoding: "utf-8")
|
|
361
|
-
end
|
|
362
|
-
|
|
363
|
-
# -----------------------------------------------------------------------
|
|
364
|
-
# Execution
|
|
365
|
-
# -----------------------------------------------------------------------
|
|
366
|
-
|
|
367
|
-
def execute_cached(tokens, context)
|
|
368
|
-
# Check if first non-text token is an extends block
|
|
369
|
-
tokens.each do |ttype, raw|
|
|
370
|
-
next if ttype == TEXT && raw.strip.empty?
|
|
371
|
-
if ttype == BLOCK
|
|
372
|
-
content, _, _ = strip_tag(raw)
|
|
373
|
-
if content.start_with?("extends ")
|
|
374
|
-
# Extends requires source-based execution for block extraction
|
|
375
|
-
source = tokens.map { |_, v| v }.join
|
|
376
|
-
return execute(source, context)
|
|
377
|
-
end
|
|
378
|
-
end
|
|
379
|
-
break
|
|
380
|
-
end
|
|
381
|
-
render_tokens(tokens, context)
|
|
382
|
-
end
|
|
383
|
-
|
|
384
|
-
def execute_with_tokens(source, tokens, context)
|
|
385
|
-
# Handle extends first
|
|
386
|
-
if source =~ EXTENDS_RE
|
|
387
|
-
parent_name = Regexp.last_match(1)
|
|
388
|
-
parent_source = load_template(parent_name)
|
|
389
|
-
child_blocks = extract_blocks(source)
|
|
390
|
-
return render_with_blocks(parent_source, context, child_blocks)
|
|
391
|
-
end
|
|
392
|
-
|
|
393
|
-
render_tokens(tokens, context)
|
|
394
|
-
end
|
|
395
|
-
|
|
396
|
-
def execute(source, context)
|
|
397
|
-
# Handle extends first
|
|
398
|
-
if source =~ EXTENDS_RE
|
|
399
|
-
parent_name = Regexp.last_match(1)
|
|
400
|
-
parent_source = load_template(parent_name)
|
|
401
|
-
child_blocks = extract_blocks(source)
|
|
402
|
-
return render_with_blocks(parent_source, context, child_blocks)
|
|
403
|
-
end
|
|
404
|
-
|
|
405
|
-
render_tokens(tokenize(source), context)
|
|
406
|
-
end
|
|
407
|
-
|
|
408
|
-
def extract_blocks(source)
|
|
409
|
-
blocks = {}
|
|
410
|
-
source.scan(BLOCK_RE) do
|
|
411
|
-
blocks[Regexp.last_match(1)] = Regexp.last_match(2)
|
|
412
|
-
end
|
|
413
|
-
blocks
|
|
414
|
-
end
|
|
415
|
-
|
|
416
|
-
def render_with_blocks(parent_source, context, child_blocks)
|
|
417
|
-
engine = self
|
|
418
|
-
result = parent_source.gsub(BLOCK_RE) do
|
|
419
|
-
name = Regexp.last_match(1)
|
|
420
|
-
parent_content = Regexp.last_match(2)
|
|
421
|
-
block_source = child_blocks.fetch(name, parent_content)
|
|
422
|
-
|
|
423
|
-
# Make parent() and super() available inside child blocks
|
|
424
|
-
rendered_parent = nil
|
|
425
|
-
get_parent = lambda do
|
|
426
|
-
rendered_parent ||= Tina4::SafeString.new(
|
|
427
|
-
engine.send(:render_tokens, tokenize(parent_content), context)
|
|
428
|
-
)
|
|
429
|
-
rendered_parent
|
|
430
|
-
end
|
|
431
|
-
|
|
432
|
-
block_ctx = context.merge("parent" => get_parent, "super" => get_parent)
|
|
433
|
-
render_tokens(tokenize(block_source), block_ctx)
|
|
434
|
-
end
|
|
435
|
-
render_tokens(tokenize(result), context)
|
|
436
|
-
end
|
|
437
|
-
|
|
438
|
-
# -----------------------------------------------------------------------
|
|
439
|
-
# Token renderer
|
|
440
|
-
# -----------------------------------------------------------------------
|
|
441
|
-
|
|
442
|
-
def render_tokens(tokens, context)
|
|
443
|
-
output = []
|
|
444
|
-
i = 0
|
|
445
|
-
|
|
446
|
-
while i < tokens.length
|
|
447
|
-
ttype, raw = tokens[i]
|
|
448
|
-
|
|
449
|
-
case ttype
|
|
450
|
-
when TEXT
|
|
451
|
-
output << raw
|
|
452
|
-
i += 1
|
|
453
|
-
|
|
454
|
-
when COMMENT
|
|
455
|
-
i += 1
|
|
456
|
-
|
|
457
|
-
when VAR
|
|
458
|
-
content, strip_b, strip_a = strip_tag(raw)
|
|
459
|
-
output[-1] = output[-1].rstrip if strip_b && !output.empty?
|
|
460
|
-
|
|
461
|
-
result = eval_var(content, context)
|
|
462
|
-
output << (result.nil? ? "" : result.to_s)
|
|
463
|
-
|
|
464
|
-
if strip_a && i + 1 < tokens.length && tokens[i + 1][0] == TEXT
|
|
465
|
-
tokens[i + 1] = [TEXT, tokens[i + 1][1].lstrip]
|
|
466
|
-
end
|
|
467
|
-
i += 1
|
|
468
|
-
|
|
469
|
-
when BLOCK
|
|
470
|
-
content, strip_b, strip_a = strip_tag(raw)
|
|
471
|
-
output[-1] = output[-1].rstrip if strip_b && !output.empty?
|
|
472
|
-
|
|
473
|
-
tag = content.split[0] || ""
|
|
474
|
-
|
|
475
|
-
case tag
|
|
476
|
-
when "if"
|
|
477
|
-
result, i = handle_if(tokens, i, context)
|
|
478
|
-
output << result
|
|
479
|
-
when "for"
|
|
480
|
-
result, i = handle_for(tokens, i, context)
|
|
481
|
-
output << result
|
|
482
|
-
when "set"
|
|
483
|
-
handle_set(content, context)
|
|
484
|
-
i += 1
|
|
485
|
-
when "include"
|
|
486
|
-
if @sandbox && @allowed_tags && !@allowed_tags.include?("include")
|
|
487
|
-
i += 1
|
|
488
|
-
else
|
|
489
|
-
output << handle_include(content, context)
|
|
490
|
-
i += 1
|
|
491
|
-
end
|
|
492
|
-
when "macro"
|
|
493
|
-
i = handle_macro(tokens, i, context)
|
|
494
|
-
when "from"
|
|
495
|
-
handle_from_import(content, context)
|
|
496
|
-
i += 1
|
|
497
|
-
when "cache"
|
|
498
|
-
result, i = handle_cache(tokens, i, context)
|
|
499
|
-
output << result
|
|
500
|
-
when "spaceless"
|
|
501
|
-
result, i = handle_spaceless(tokens, i, context)
|
|
502
|
-
output << result
|
|
503
|
-
when "autoescape"
|
|
504
|
-
result, i = handle_autoescape(tokens, i, context)
|
|
505
|
-
output << result
|
|
506
|
-
when "block", "endblock", "extends"
|
|
507
|
-
i += 1
|
|
508
|
-
else
|
|
509
|
-
i += 1
|
|
510
|
-
end
|
|
511
|
-
|
|
512
|
-
if strip_a && i < tokens.length && tokens[i][0] == TEXT
|
|
513
|
-
tokens[i] = [TEXT, tokens[i][1].lstrip]
|
|
514
|
-
end
|
|
515
|
-
else
|
|
516
|
-
i += 1
|
|
517
|
-
end
|
|
518
|
-
end
|
|
519
|
-
|
|
520
|
-
output.join
|
|
521
|
-
end
|
|
522
|
-
|
|
523
|
-
# -----------------------------------------------------------------------
|
|
524
|
-
# Variable evaluation
|
|
525
|
-
# -----------------------------------------------------------------------
|
|
526
|
-
|
|
527
|
-
def eval_var(expr, context)
|
|
528
|
-
# Check for top-level ternary BEFORE splitting filters so that
|
|
529
|
-
# expressions like ``products|length != 1 ? "s" : ""`` work correctly.
|
|
530
|
-
ternary_pos = find_ternary(expr)
|
|
531
|
-
if ternary_pos != -1
|
|
532
|
-
cond_part = expr[0...ternary_pos].strip
|
|
533
|
-
rest = expr[(ternary_pos + 1)..]
|
|
534
|
-
colon_pos = find_colon(rest)
|
|
535
|
-
if colon_pos != -1
|
|
536
|
-
true_part = rest[0...colon_pos].strip
|
|
537
|
-
false_part = rest[(colon_pos + 1)..].strip
|
|
538
|
-
cond = eval_var_raw(cond_part, context)
|
|
539
|
-
return truthy?(cond) ? eval_var(true_part, context) : eval_var(false_part, context)
|
|
540
|
-
end
|
|
541
|
-
end
|
|
542
|
-
|
|
543
|
-
eval_var_inner(expr, context)
|
|
544
|
-
end
|
|
545
|
-
|
|
546
|
-
def eval_var_raw(expr, context)
|
|
547
|
-
var_name, filters = parse_filter_chain(expr)
|
|
548
|
-
value = eval_expr(var_name, context)
|
|
549
|
-
filters.each do |fname, args|
|
|
550
|
-
next if fname == "raw" || fname == "safe"
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
end
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
if
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
next
|
|
739
|
-
end
|
|
740
|
-
if ch ==
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
#
|
|
758
|
-
|
|
759
|
-
def
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
#
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
return
|
|
963
|
-
|
|
964
|
-
return
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
return :
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
parts
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
#
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
#
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
return
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
elsif tag == "
|
|
1348
|
-
|
|
1349
|
-
if strip_b_tag && !
|
|
1350
|
-
|
|
1351
|
-
end
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
if
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
i
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
def
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
def
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
elsif
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
"
|
|
1789
|
-
"
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
"
|
|
1793
|
-
"
|
|
1794
|
-
"
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
"
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
"
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
},
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
#
|
|
1885
|
-
|
|
1886
|
-
"
|
|
1887
|
-
"
|
|
1888
|
-
"
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
},
|
|
1893
|
-
"
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
end
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
#
|
|
1985
|
-
#
|
|
1986
|
-
#
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
#
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Tina4 Frond Engine -- Lexer, parser, and runtime.
|
|
4
|
+
# Zero-dependency twig-like template engine.
|
|
5
|
+
# Supports: variables, filters, if/elseif/else/endif, for/else/endfor,
|
|
6
|
+
# extends/block, include, macro, set, comments, whitespace control, tests,
|
|
7
|
+
# fragment caching, sandboxing, auto-escaping, custom filters/tests/globals.
|
|
8
|
+
|
|
9
|
+
require "json"
|
|
10
|
+
require "digest"
|
|
11
|
+
require "base64"
|
|
12
|
+
require "cgi"
|
|
13
|
+
require "uri"
|
|
14
|
+
require "date"
|
|
15
|
+
require "time"
|
|
16
|
+
require "securerandom"
|
|
17
|
+
|
|
18
|
+
module Tina4
|
|
19
|
+
# Marker class for strings that should not be auto-escaped in Frond.
|
|
20
|
+
class SafeString < String
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class Frond
|
|
24
|
+
# -- Token types ----------------------------------------------------------
|
|
25
|
+
TEXT = :text
|
|
26
|
+
VAR = :var # {{ ... }}
|
|
27
|
+
BLOCK = :block # {% ... %}
|
|
28
|
+
COMMENT = :comment # {# ... #}
|
|
29
|
+
|
|
30
|
+
# Regex to split template source into tokens
|
|
31
|
+
TOKEN_RE = /(\{%-?\s*.*?\s*-?%\})|(\{\{-?\s*.*?\s*-?\}\})|(\{#.*?#\})/m
|
|
32
|
+
|
|
33
|
+
# HTML escape table
|
|
34
|
+
HTML_ESCAPE_MAP = { "&" => "&", "<" => "<", ">" => ">",
|
|
35
|
+
'"' => """, "'" => "'" }.freeze
|
|
36
|
+
HTML_ESCAPE_RE = /[&<>"']/
|
|
37
|
+
|
|
38
|
+
# -- Compiled regex constants (optimization: avoid re-compiling in methods) --
|
|
39
|
+
EXTENDS_RE = /\{%-?\s*extends\s+["'](.+?)["']\s*-?%\}/
|
|
40
|
+
BLOCK_RE = /\{%-?\s*block\s+(\w+)\s*-?%\}(.*?)\{%-?\s*endblock\s*-?%\}/m
|
|
41
|
+
STRING_LIT_RE = /\A["'](.*)["']\z/
|
|
42
|
+
INTEGER_RE = /\A-?\d+\z/
|
|
43
|
+
FLOAT_RE = /\A-?\d+\.\d+\z/
|
|
44
|
+
ARRAY_LIT_RE = /\A\[(.+)\]\z/m
|
|
45
|
+
HASH_LIT_RE = /\A\{(.+)\}\z/m
|
|
46
|
+
HASH_PAIR_RE = /\A\s*(?:["']([^"']+)["']|(\w+))\s*:\s*(.+)\z/
|
|
47
|
+
RANGE_LIT_RE = /\A(\d+)\.\.(\d+)\z/
|
|
48
|
+
ARITHMETIC_OPS = [" + ", " - ", " * ", " // ", " / ", " % ", " ** "].freeze
|
|
49
|
+
FUNC_CALL_RE = /\A(\w+)\s*\((.*)\)\z/m
|
|
50
|
+
FILTER_WITH_ARGS_RE = /\A(\w+)\s*\((.*)\)\z/m
|
|
51
|
+
FILTER_CMP_RE = /\A(\w+)\s*(!=|==|>=|<=|>|<)\s*(.+)\z/
|
|
52
|
+
OR_SPLIT_RE = /\s+or\s+/
|
|
53
|
+
AND_SPLIT_RE = /\s+and\s+/
|
|
54
|
+
IS_NOT_RE = /\A(.+?)\s+is\s+not\s+(\w+)(.*)\z/
|
|
55
|
+
IS_RE = /\A(.+?)\s+is\s+(\w+)(.*)\z/
|
|
56
|
+
NOT_IN_RE = /\A(.+?)\s+not\s+in\s+(.+)\z/
|
|
57
|
+
IN_RE = /\A(.+?)\s+in\s+(.+)\z/
|
|
58
|
+
DIVISIBLE_BY_RE = /\s*by\s*\(\s*(\d+)\s*\)/
|
|
59
|
+
RESOLVE_SPLIT_RE = /\.|\[([^\]]+)\]/
|
|
60
|
+
RESOLVE_STRIP_RE = /\A["']|["']\z/
|
|
61
|
+
DIGIT_RE = /\A\d+\z/
|
|
62
|
+
FOR_RE = /\Afor\s+(\w+)(?:\s*,\s*(\w+))?\s+in\s+(.+)\z/
|
|
63
|
+
SET_RE = /\Aset\s+(\w+)\s*=\s*(.+)\z/m
|
|
64
|
+
INCLUDE_RE = /\Ainclude\s+["'](.+?)["'](?:\s+with\s+(.+))?\z/
|
|
65
|
+
MACRO_RE = /\Amacro\s+(\w+)\s*\(([^)]*)\)/
|
|
66
|
+
FROM_IMPORT_RE = /\Afrom\s+["'](.+?)["']\s+import\s+(.+)/
|
|
67
|
+
CACHE_RE = /\Acache\s+["'](.+?)["']\s*(\d+)?/
|
|
68
|
+
SPACELESS_RE = />\s+</
|
|
69
|
+
AUTOESCAPE_RE = /\Aautoescape\s+(false|true)/
|
|
70
|
+
STRIPTAGS_RE = /<[^>]+>/
|
|
71
|
+
THOUSANDS_RE = /(\d)(?=(\d{3})+(?!\d))/
|
|
72
|
+
SLUG_CLEAN_RE = /[^a-z0-9]+/
|
|
73
|
+
SLUG_TRIM_RE = /\A-|-\z/
|
|
74
|
+
|
|
75
|
+
# Set of common no-arg filter names that can be inlined for speed
|
|
76
|
+
INLINE_FILTERS = %w[upper lower length trim capitalize title string int escape e].each_with_object({}) { |f, h| h[f] = true }.freeze
|
|
77
|
+
|
|
78
|
+
# -- Lazy context overlay for for-loops (avoids full Hash#dup) --
|
|
79
|
+
class LoopContext
|
|
80
|
+
def initialize(parent)
|
|
81
|
+
@parent = parent
|
|
82
|
+
@local = {}
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def [](key)
|
|
86
|
+
@local.key?(key) ? @local[key] : @parent[key]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def []=(key, value)
|
|
90
|
+
@local[key] = value
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def key?(key)
|
|
94
|
+
@local.key?(key) || @parent.key?(key)
|
|
95
|
+
end
|
|
96
|
+
alias include? key?
|
|
97
|
+
alias has_key? key?
|
|
98
|
+
|
|
99
|
+
def fetch(key, *args, &block)
|
|
100
|
+
if @local.key?(key)
|
|
101
|
+
@local[key]
|
|
102
|
+
elsif @parent.key?(key)
|
|
103
|
+
@parent[key]
|
|
104
|
+
elsif block
|
|
105
|
+
yield key
|
|
106
|
+
elsif !args.empty?
|
|
107
|
+
args[0]
|
|
108
|
+
else
|
|
109
|
+
raise KeyError, "key not found: #{key.inspect}"
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def merge(other)
|
|
114
|
+
dup_hash = to_h
|
|
115
|
+
dup_hash.merge!(other)
|
|
116
|
+
dup_hash
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def merge!(other)
|
|
120
|
+
other.each { |k, v| @local[k] = v }
|
|
121
|
+
self
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def dup
|
|
125
|
+
copy = LoopContext.new(@parent)
|
|
126
|
+
@local.each { |k, v| copy[k] = v }
|
|
127
|
+
copy
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def to_h
|
|
131
|
+
h = @parent.is_a?(LoopContext) ? @parent.to_h : @parent.dup
|
|
132
|
+
@local.each { |k, v| h[k] = v }
|
|
133
|
+
h
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def each(&block)
|
|
137
|
+
to_h.each(&block)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def respond_to_missing?(name, include_private = false)
|
|
141
|
+
@parent.respond_to?(name, include_private) || super
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def is_a?(klass)
|
|
145
|
+
klass == Hash || super
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def keys
|
|
149
|
+
(@parent.is_a?(LoopContext) ? @parent.keys : @parent.keys) | @local.keys
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# -----------------------------------------------------------------------
|
|
154
|
+
# Public API
|
|
155
|
+
# -----------------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
attr_reader :template_dir
|
|
158
|
+
|
|
159
|
+
def initialize(template_dir: "src/templates")
|
|
160
|
+
@template_dir = template_dir
|
|
161
|
+
@filters = default_filters
|
|
162
|
+
@globals = {}
|
|
163
|
+
@tests = default_tests
|
|
164
|
+
@auto_escape = true
|
|
165
|
+
|
|
166
|
+
# Sandboxing
|
|
167
|
+
@sandbox = false
|
|
168
|
+
@allowed_filters = nil
|
|
169
|
+
@allowed_tags = nil
|
|
170
|
+
@allowed_vars = nil
|
|
171
|
+
|
|
172
|
+
# Fragment cache: key => [html, expires_at]
|
|
173
|
+
@fragment_cache = {}
|
|
174
|
+
|
|
175
|
+
# Token pre-compilation cache
|
|
176
|
+
@compiled = {} # {template_name => [tokens, mtime]}
|
|
177
|
+
@compiled_strings = {} # {md5_hash => tokens}
|
|
178
|
+
|
|
179
|
+
# Parsed filter chain cache: expr_string => [variable, filters]
|
|
180
|
+
@filter_chain_cache = {}
|
|
181
|
+
|
|
182
|
+
# Resolved dotted-path split cache: expr_string => parts_array
|
|
183
|
+
@resolve_cache = {}
|
|
184
|
+
|
|
185
|
+
# Sandbox root-var split cache: var_name => root_var_string
|
|
186
|
+
@dotted_split_cache = {}
|
|
187
|
+
|
|
188
|
+
# Built-in global functions
|
|
189
|
+
register_builtin_globals
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Render a template file with data. Uses token caching for performance.
|
|
193
|
+
def render(template, data = {})
|
|
194
|
+
context = @globals.merge(stringify_keys(data))
|
|
195
|
+
|
|
196
|
+
path = File.join(@template_dir, template)
|
|
197
|
+
raise "Template not found: #{path}" unless File.exist?(path)
|
|
198
|
+
|
|
199
|
+
debug_mode = ENV.fetch("TINA4_DEBUG", "").downcase == "true"
|
|
200
|
+
|
|
201
|
+
unless debug_mode
|
|
202
|
+
# Production: use permanent cache (no filesystem checks)
|
|
203
|
+
cached = @compiled[template]
|
|
204
|
+
return execute_cached(cached[0], context) if cached
|
|
205
|
+
end
|
|
206
|
+
# Dev mode: skip cache entirely — always re-read and re-tokenize
|
|
207
|
+
# so edits to partials and extended base templates are detected
|
|
208
|
+
|
|
209
|
+
# Cache miss — load, tokenize, cache
|
|
210
|
+
source = File.read(path, encoding: "utf-8")
|
|
211
|
+
mtime = File.mtime(path)
|
|
212
|
+
tokens = tokenize(source)
|
|
213
|
+
@compiled[template] = [tokens, mtime]
|
|
214
|
+
execute_with_tokens(source, tokens, context)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Render a template string directly. Uses token caching for performance.
|
|
218
|
+
def render_string(source, data = {})
|
|
219
|
+
context = @globals.merge(stringify_keys(data))
|
|
220
|
+
|
|
221
|
+
key = Digest::MD5.hexdigest(source)
|
|
222
|
+
cached_tokens = @compiled_strings[key]
|
|
223
|
+
|
|
224
|
+
if cached_tokens
|
|
225
|
+
return execute_cached(cached_tokens, context)
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
tokens = tokenize(source)
|
|
229
|
+
@compiled_strings[key] = tokens
|
|
230
|
+
execute_cached(tokens, context)
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Clear all compiled template caches.
|
|
234
|
+
def clear_cache
|
|
235
|
+
@compiled.clear
|
|
236
|
+
@compiled_strings.clear
|
|
237
|
+
@filter_chain_cache.clear
|
|
238
|
+
@resolve_cache.clear
|
|
239
|
+
@dotted_split_cache.clear
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# Register a custom filter.
|
|
243
|
+
def add_filter(name, &blk)
|
|
244
|
+
@filters[name.to_s] = blk
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# Register a custom test.
|
|
248
|
+
def add_test(name, &blk)
|
|
249
|
+
@tests[name.to_s] = blk
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# Register a global variable available in all templates.
|
|
253
|
+
def add_global(name, value)
|
|
254
|
+
@globals[name.to_s] = value
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Enable sandbox mode.
|
|
258
|
+
def sandbox(filters: nil, tags: nil, vars: nil)
|
|
259
|
+
@sandbox = true
|
|
260
|
+
@allowed_filters = filters ? filters.map(&:to_s) : nil
|
|
261
|
+
@allowed_tags = tags ? tags.map(&:to_s) : nil
|
|
262
|
+
@allowed_vars = vars ? vars.map(&:to_s) : nil
|
|
263
|
+
self
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Disable sandbox mode.
|
|
267
|
+
def unsandbox
|
|
268
|
+
@sandbox = false
|
|
269
|
+
@allowed_filters = nil
|
|
270
|
+
@allowed_tags = nil
|
|
271
|
+
@allowed_vars = nil
|
|
272
|
+
self
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Utility: HTML escape
|
|
276
|
+
def self.escape_html(str)
|
|
277
|
+
str.to_s.gsub(HTML_ESCAPE_RE, HTML_ESCAPE_MAP)
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
private
|
|
281
|
+
|
|
282
|
+
# -----------------------------------------------------------------------
|
|
283
|
+
# Tokenizer
|
|
284
|
+
# -----------------------------------------------------------------------
|
|
285
|
+
|
|
286
|
+
# Regex to extract {% raw %}...{% endraw %} blocks before tokenizing
|
|
287
|
+
RAW_BLOCK_RE = /\{%-?\s*raw\s*-?%\}(.*?)\{%-?\s*endraw\s*-?%\}/m
|
|
288
|
+
|
|
289
|
+
def tokenize(source)
|
|
290
|
+
# 1. Extract raw blocks and replace with placeholders
|
|
291
|
+
raw_blocks = []
|
|
292
|
+
source = source.gsub(RAW_BLOCK_RE) do
|
|
293
|
+
idx = raw_blocks.length
|
|
294
|
+
raw_blocks << Regexp.last_match(1)
|
|
295
|
+
"\x00RAW_#{idx}\x00"
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# 2. Normal tokenization
|
|
299
|
+
tokens = []
|
|
300
|
+
pos = 0
|
|
301
|
+
source.scan(TOKEN_RE) do
|
|
302
|
+
m = Regexp.last_match
|
|
303
|
+
start = m.begin(0)
|
|
304
|
+
tokens << [TEXT, source[pos...start]] if start > pos
|
|
305
|
+
|
|
306
|
+
raw = m[0]
|
|
307
|
+
if raw.start_with?("{#")
|
|
308
|
+
tokens << [COMMENT, raw]
|
|
309
|
+
elsif raw.start_with?("{{")
|
|
310
|
+
tokens << [VAR, raw]
|
|
311
|
+
elsif raw.start_with?("{%")
|
|
312
|
+
tokens << [BLOCK, raw]
|
|
313
|
+
end
|
|
314
|
+
pos = m.end(0)
|
|
315
|
+
end
|
|
316
|
+
tokens << [TEXT, source[pos..]] if pos < source.length
|
|
317
|
+
|
|
318
|
+
# 3. Restore raw block placeholders as literal TEXT
|
|
319
|
+
unless raw_blocks.empty?
|
|
320
|
+
tokens = tokens.map do |ttype, value|
|
|
321
|
+
if ttype == TEXT && value.include?("\x00RAW_")
|
|
322
|
+
raw_blocks.each_with_index do |content, idx|
|
|
323
|
+
value = value.gsub("\x00RAW_#{idx}\x00", content)
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
[ttype, value]
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
tokens
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# Strip delimiters from a tag and detect whitespace control markers.
|
|
334
|
+
# Returns [content, strip_before, strip_after].
|
|
335
|
+
def strip_tag(raw)
|
|
336
|
+
inner = raw[2..-3] # remove {{ }} or {% %} or {# #}
|
|
337
|
+
strip_before = false
|
|
338
|
+
strip_after = false
|
|
339
|
+
|
|
340
|
+
if inner.start_with?("-")
|
|
341
|
+
strip_before = true
|
|
342
|
+
inner = inner[1..]
|
|
343
|
+
end
|
|
344
|
+
if inner.end_with?("-")
|
|
345
|
+
strip_after = true
|
|
346
|
+
inner = inner[0..-2]
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
[inner.strip, strip_before, strip_after]
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# -----------------------------------------------------------------------
|
|
353
|
+
# Template loading
|
|
354
|
+
# -----------------------------------------------------------------------
|
|
355
|
+
|
|
356
|
+
def load_template(name)
|
|
357
|
+
path = File.join(@template_dir, name)
|
|
358
|
+
raise "Template not found: #{path}" unless File.exist?(path)
|
|
359
|
+
|
|
360
|
+
File.read(path, encoding: "utf-8")
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# -----------------------------------------------------------------------
|
|
364
|
+
# Execution
|
|
365
|
+
# -----------------------------------------------------------------------
|
|
366
|
+
|
|
367
|
+
def execute_cached(tokens, context)
|
|
368
|
+
# Check if first non-text token is an extends block
|
|
369
|
+
tokens.each do |ttype, raw|
|
|
370
|
+
next if ttype == TEXT && raw.strip.empty?
|
|
371
|
+
if ttype == BLOCK
|
|
372
|
+
content, _, _ = strip_tag(raw)
|
|
373
|
+
if content.start_with?("extends ")
|
|
374
|
+
# Extends requires source-based execution for block extraction
|
|
375
|
+
source = tokens.map { |_, v| v }.join
|
|
376
|
+
return execute(source, context)
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
break
|
|
380
|
+
end
|
|
381
|
+
render_tokens(tokens, context)
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def execute_with_tokens(source, tokens, context)
|
|
385
|
+
# Handle extends first
|
|
386
|
+
if source =~ EXTENDS_RE
|
|
387
|
+
parent_name = Regexp.last_match(1)
|
|
388
|
+
parent_source = load_template(parent_name)
|
|
389
|
+
child_blocks = extract_blocks(source)
|
|
390
|
+
return render_with_blocks(parent_source, context, child_blocks)
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
render_tokens(tokens, context)
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
def execute(source, context)
|
|
397
|
+
# Handle extends first
|
|
398
|
+
if source =~ EXTENDS_RE
|
|
399
|
+
parent_name = Regexp.last_match(1)
|
|
400
|
+
parent_source = load_template(parent_name)
|
|
401
|
+
child_blocks = extract_blocks(source)
|
|
402
|
+
return render_with_blocks(parent_source, context, child_blocks)
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
render_tokens(tokenize(source), context)
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def extract_blocks(source)
|
|
409
|
+
blocks = {}
|
|
410
|
+
source.scan(BLOCK_RE) do
|
|
411
|
+
blocks[Regexp.last_match(1)] = Regexp.last_match(2)
|
|
412
|
+
end
|
|
413
|
+
blocks
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
def render_with_blocks(parent_source, context, child_blocks)
|
|
417
|
+
engine = self
|
|
418
|
+
result = parent_source.gsub(BLOCK_RE) do
|
|
419
|
+
name = Regexp.last_match(1)
|
|
420
|
+
parent_content = Regexp.last_match(2)
|
|
421
|
+
block_source = child_blocks.fetch(name, parent_content)
|
|
422
|
+
|
|
423
|
+
# Make parent() and super() available inside child blocks
|
|
424
|
+
rendered_parent = nil
|
|
425
|
+
get_parent = lambda do
|
|
426
|
+
rendered_parent ||= Tina4::SafeString.new(
|
|
427
|
+
engine.send(:render_tokens, tokenize(parent_content), context)
|
|
428
|
+
)
|
|
429
|
+
rendered_parent
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
block_ctx = context.merge("parent" => get_parent, "super" => get_parent)
|
|
433
|
+
render_tokens(tokenize(block_source), block_ctx)
|
|
434
|
+
end
|
|
435
|
+
render_tokens(tokenize(result), context)
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
# -----------------------------------------------------------------------
|
|
439
|
+
# Token renderer
|
|
440
|
+
# -----------------------------------------------------------------------
|
|
441
|
+
|
|
442
|
+
def render_tokens(tokens, context)
|
|
443
|
+
output = []
|
|
444
|
+
i = 0
|
|
445
|
+
|
|
446
|
+
while i < tokens.length
|
|
447
|
+
ttype, raw = tokens[i]
|
|
448
|
+
|
|
449
|
+
case ttype
|
|
450
|
+
when TEXT
|
|
451
|
+
output << raw
|
|
452
|
+
i += 1
|
|
453
|
+
|
|
454
|
+
when COMMENT
|
|
455
|
+
i += 1
|
|
456
|
+
|
|
457
|
+
when VAR
|
|
458
|
+
content, strip_b, strip_a = strip_tag(raw)
|
|
459
|
+
output[-1] = output[-1].rstrip if strip_b && !output.empty?
|
|
460
|
+
|
|
461
|
+
result = eval_var(content, context)
|
|
462
|
+
output << (result.nil? ? "" : result.to_s)
|
|
463
|
+
|
|
464
|
+
if strip_a && i + 1 < tokens.length && tokens[i + 1][0] == TEXT
|
|
465
|
+
tokens[i + 1] = [TEXT, tokens[i + 1][1].lstrip]
|
|
466
|
+
end
|
|
467
|
+
i += 1
|
|
468
|
+
|
|
469
|
+
when BLOCK
|
|
470
|
+
content, strip_b, strip_a = strip_tag(raw)
|
|
471
|
+
output[-1] = output[-1].rstrip if strip_b && !output.empty?
|
|
472
|
+
|
|
473
|
+
tag = content.split[0] || ""
|
|
474
|
+
|
|
475
|
+
case tag
|
|
476
|
+
when "if"
|
|
477
|
+
result, i = handle_if(tokens, i, context)
|
|
478
|
+
output << result
|
|
479
|
+
when "for"
|
|
480
|
+
result, i = handle_for(tokens, i, context)
|
|
481
|
+
output << result
|
|
482
|
+
when "set"
|
|
483
|
+
handle_set(content, context)
|
|
484
|
+
i += 1
|
|
485
|
+
when "include"
|
|
486
|
+
if @sandbox && @allowed_tags && !@allowed_tags.include?("include")
|
|
487
|
+
i += 1
|
|
488
|
+
else
|
|
489
|
+
output << handle_include(content, context)
|
|
490
|
+
i += 1
|
|
491
|
+
end
|
|
492
|
+
when "macro"
|
|
493
|
+
i = handle_macro(tokens, i, context)
|
|
494
|
+
when "from"
|
|
495
|
+
handle_from_import(content, context)
|
|
496
|
+
i += 1
|
|
497
|
+
when "cache"
|
|
498
|
+
result, i = handle_cache(tokens, i, context)
|
|
499
|
+
output << result
|
|
500
|
+
when "spaceless"
|
|
501
|
+
result, i = handle_spaceless(tokens, i, context)
|
|
502
|
+
output << result
|
|
503
|
+
when "autoescape"
|
|
504
|
+
result, i = handle_autoescape(tokens, i, context)
|
|
505
|
+
output << result
|
|
506
|
+
when "block", "endblock", "extends"
|
|
507
|
+
i += 1
|
|
508
|
+
else
|
|
509
|
+
i += 1
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
if strip_a && i < tokens.length && tokens[i][0] == TEXT
|
|
513
|
+
tokens[i] = [TEXT, tokens[i][1].lstrip]
|
|
514
|
+
end
|
|
515
|
+
else
|
|
516
|
+
i += 1
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
output.join
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
# -----------------------------------------------------------------------
|
|
524
|
+
# Variable evaluation
|
|
525
|
+
# -----------------------------------------------------------------------
|
|
526
|
+
|
|
527
|
+
def eval_var(expr, context)
|
|
528
|
+
# Check for top-level ternary BEFORE splitting filters so that
|
|
529
|
+
# expressions like ``products|length != 1 ? "s" : ""`` work correctly.
|
|
530
|
+
ternary_pos = find_ternary(expr)
|
|
531
|
+
if ternary_pos != -1
|
|
532
|
+
cond_part = expr[0...ternary_pos].strip
|
|
533
|
+
rest = expr[(ternary_pos + 1)..]
|
|
534
|
+
colon_pos = find_colon(rest)
|
|
535
|
+
if colon_pos != -1
|
|
536
|
+
true_part = rest[0...colon_pos].strip
|
|
537
|
+
false_part = rest[(colon_pos + 1)..].strip
|
|
538
|
+
cond = eval_var_raw(cond_part, context)
|
|
539
|
+
return truthy?(cond) ? eval_var(true_part, context) : eval_var(false_part, context)
|
|
540
|
+
end
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
eval_var_inner(expr, context)
|
|
544
|
+
end
|
|
545
|
+
|
|
546
|
+
def eval_var_raw(expr, context)
|
|
547
|
+
var_name, filters = parse_filter_chain(expr)
|
|
548
|
+
value = eval_expr(var_name, context)
|
|
549
|
+
filters.each do |fname, args|
|
|
550
|
+
next if fname == "raw" || fname == "safe"
|
|
551
|
+
|
|
552
|
+
# Filter + property-access chain: `first.groupSummary` — apply
|
|
553
|
+
# the filter, then traverse the path on the result using a
|
|
554
|
+
# synthetic context so eval_expr's dotted resolution does the
|
|
555
|
+
# work. Parity with tina4-python + tina4-php.
|
|
556
|
+
real_fname, tail_path = split_filter_name_and_path(fname)
|
|
557
|
+
if !tail_path.empty? && @filters[real_fname]
|
|
558
|
+
evaluated_args = args.map { |a| eval_filter_arg(a, context) }
|
|
559
|
+
value = @filters[real_fname].call(value, *evaluated_args)
|
|
560
|
+
value = eval_expr("__frond_filter_tmp.#{tail_path}",
|
|
561
|
+
{ "__frond_filter_tmp" => value })
|
|
562
|
+
next
|
|
563
|
+
end
|
|
564
|
+
|
|
565
|
+
fn = @filters[fname]
|
|
566
|
+
if fn
|
|
567
|
+
evaluated_args = args.map { |a| eval_filter_arg(a, context) }
|
|
568
|
+
value = fn.call(value, *evaluated_args)
|
|
569
|
+
else
|
|
570
|
+
# The filter name may include a trailing comparison operator,
|
|
571
|
+
# e.g. "length != 1". Extract the real filter name and the
|
|
572
|
+
# comparison suffix, apply the filter, then evaluate the comparison.
|
|
573
|
+
m = fname.match(FILTER_CMP_RE)
|
|
574
|
+
if m
|
|
575
|
+
real_filter = m[1]
|
|
576
|
+
op = m[2]
|
|
577
|
+
right_expr = m[3].strip
|
|
578
|
+
fn2 = @filters[real_filter]
|
|
579
|
+
if fn2
|
|
580
|
+
evaluated_args = args.map { |a| eval_filter_arg(a, context) }
|
|
581
|
+
value = fn2.call(value, *evaluated_args)
|
|
582
|
+
end
|
|
583
|
+
right = eval_expr(right_expr, context)
|
|
584
|
+
value = case op
|
|
585
|
+
when "!=" then value != right
|
|
586
|
+
when "==" then value == right
|
|
587
|
+
when ">=" then value >= right
|
|
588
|
+
when "<=" then value <= right
|
|
589
|
+
when ">" then value > right
|
|
590
|
+
when "<" then value < right
|
|
591
|
+
else false
|
|
592
|
+
end rescue false
|
|
593
|
+
else
|
|
594
|
+
value = eval_expr(fname, context)
|
|
595
|
+
end
|
|
596
|
+
end
|
|
597
|
+
end
|
|
598
|
+
value
|
|
599
|
+
end
|
|
600
|
+
|
|
601
|
+
def eval_var_inner(expr, context)
|
|
602
|
+
var_name, filters = parse_filter_chain(expr)
|
|
603
|
+
|
|
604
|
+
# Sandbox: check variable access
|
|
605
|
+
if @sandbox && @allowed_vars
|
|
606
|
+
root_var = @dotted_split_cache[var_name]
|
|
607
|
+
unless root_var
|
|
608
|
+
root_var = var_name.split(".")[0].split("[")[0].strip
|
|
609
|
+
@dotted_split_cache[var_name] = root_var
|
|
610
|
+
end
|
|
611
|
+
return "" if !root_var.empty? && !@allowed_vars.include?(root_var) && root_var != "loop"
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
value = eval_expr(var_name, context)
|
|
615
|
+
|
|
616
|
+
is_safe = false
|
|
617
|
+
filters.each do |fname, args|
|
|
618
|
+
if fname == "raw" || fname == "safe"
|
|
619
|
+
is_safe = true
|
|
620
|
+
next
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
# Sandbox: check filter access
|
|
624
|
+
if @sandbox && @allowed_filters && !@allowed_filters.include?(fname)
|
|
625
|
+
next
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
# Filter + property-access chain: `first.groupSummary` — apply
|
|
629
|
+
# the filter, then traverse the path on the result. Done BEFORE
|
|
630
|
+
# the inline fast-path so cases like `items|first.name` work
|
|
631
|
+
# regardless of whether `first` is an inline filter too.
|
|
632
|
+
real_fname, tail_path = split_filter_name_and_path(fname)
|
|
633
|
+
if !tail_path.empty? && @filters[real_fname]
|
|
634
|
+
evaluated_args = args.map { |a| eval_filter_arg(a, context) }
|
|
635
|
+
value = @filters[real_fname].call(value, *evaluated_args)
|
|
636
|
+
value = eval_expr("__frond_filter_tmp.#{tail_path}",
|
|
637
|
+
{ "__frond_filter_tmp" => value })
|
|
638
|
+
next
|
|
639
|
+
end
|
|
640
|
+
|
|
641
|
+
# Inline common no-arg filters for speed (skip generic dispatch)
|
|
642
|
+
if args.empty? && INLINE_FILTERS.include?(fname)
|
|
643
|
+
value = case fname
|
|
644
|
+
when "upper" then value.to_s.upcase
|
|
645
|
+
when "lower" then value.to_s.downcase
|
|
646
|
+
when "length" then value.respond_to?(:length) ? value.length : value.to_s.length
|
|
647
|
+
when "trim" then value.to_s.strip
|
|
648
|
+
when "capitalize" then value.to_s.capitalize
|
|
649
|
+
when "title" then value.to_s.split.map(&:capitalize).join(" ")
|
|
650
|
+
when "string" then value.to_s
|
|
651
|
+
when "int" then value.to_i
|
|
652
|
+
when "escape", "e" then Frond.escape_html(value.to_s)
|
|
653
|
+
else value
|
|
654
|
+
end
|
|
655
|
+
next
|
|
656
|
+
end
|
|
657
|
+
|
|
658
|
+
fn = @filters[fname]
|
|
659
|
+
if fn
|
|
660
|
+
evaluated_args = args.map { |a| eval_filter_arg(a, context) }
|
|
661
|
+
value = fn.call(value, *evaluated_args)
|
|
662
|
+
end
|
|
663
|
+
end
|
|
664
|
+
|
|
665
|
+
# Auto-escape HTML unless marked safe or SafeString
|
|
666
|
+
if @auto_escape && !is_safe && value.is_a?(String) && !value.is_a?(SafeString)
|
|
667
|
+
value = Frond.escape_html(value)
|
|
668
|
+
end
|
|
669
|
+
|
|
670
|
+
value
|
|
671
|
+
end
|
|
672
|
+
|
|
673
|
+
def eval_filter_arg(arg, context)
|
|
674
|
+
return Regexp.last_match(1) if arg =~ STRING_LIT_RE
|
|
675
|
+
return arg.to_i if arg =~ INTEGER_RE
|
|
676
|
+
return arg.to_f if arg =~ FLOAT_RE
|
|
677
|
+
eval_expr(arg, context)
|
|
678
|
+
end
|
|
679
|
+
|
|
680
|
+
# Find the first occurrence of +needle+ that is not inside quotes or
|
|
681
|
+
# parentheses. Returns the index, or -1 if not found.
|
|
682
|
+
def find_outside_quotes(expr, needle)
|
|
683
|
+
in_q = nil
|
|
684
|
+
depth = 0
|
|
685
|
+
bracket_depth = 0
|
|
686
|
+
i = 0
|
|
687
|
+
nlen = needle.length
|
|
688
|
+
while i <= expr.length - nlen
|
|
689
|
+
ch = expr[i]
|
|
690
|
+
if (ch == '"' || ch == "'") && depth == 0
|
|
691
|
+
if in_q.nil?
|
|
692
|
+
in_q = ch
|
|
693
|
+
elsif ch == in_q
|
|
694
|
+
in_q = nil
|
|
695
|
+
end
|
|
696
|
+
i += 1
|
|
697
|
+
next
|
|
698
|
+
end
|
|
699
|
+
if in_q
|
|
700
|
+
i += 1
|
|
701
|
+
next
|
|
702
|
+
end
|
|
703
|
+
if ch == "("
|
|
704
|
+
depth += 1
|
|
705
|
+
elsif ch == ")"
|
|
706
|
+
depth -= 1
|
|
707
|
+
elsif ch == "["
|
|
708
|
+
bracket_depth += 1
|
|
709
|
+
elsif ch == "]"
|
|
710
|
+
bracket_depth -= 1
|
|
711
|
+
end
|
|
712
|
+
if depth == 0 && bracket_depth == 0 && expr[i, nlen] == needle
|
|
713
|
+
return i
|
|
714
|
+
end
|
|
715
|
+
i += 1
|
|
716
|
+
end
|
|
717
|
+
-1
|
|
718
|
+
end
|
|
719
|
+
|
|
720
|
+
# Find the index of a top-level ``?`` that is part of a ternary operator.
|
|
721
|
+
# Respects quoted strings, parentheses, and skips ``??`` (null coalesce).
|
|
722
|
+
# Returns -1 if not found.
|
|
723
|
+
def find_ternary(expr)
|
|
724
|
+
depth = 0
|
|
725
|
+
in_quote = nil
|
|
726
|
+
i = 0
|
|
727
|
+
len = expr.length
|
|
728
|
+
while i < len
|
|
729
|
+
ch = expr[i]
|
|
730
|
+
if in_quote
|
|
731
|
+
in_quote = nil if ch == in_quote
|
|
732
|
+
i += 1
|
|
733
|
+
next
|
|
734
|
+
end
|
|
735
|
+
if ch == '"' || ch == "'"
|
|
736
|
+
in_quote = ch
|
|
737
|
+
i += 1
|
|
738
|
+
next
|
|
739
|
+
end
|
|
740
|
+
if ch == "("
|
|
741
|
+
depth += 1
|
|
742
|
+
elsif ch == ")"
|
|
743
|
+
depth -= 1
|
|
744
|
+
elsif ch == "?" && depth == 0
|
|
745
|
+
# Skip ``??`` (null coalesce)
|
|
746
|
+
if i + 1 < len && expr[i + 1] == "?"
|
|
747
|
+
i += 2
|
|
748
|
+
next
|
|
749
|
+
end
|
|
750
|
+
return i
|
|
751
|
+
end
|
|
752
|
+
i += 1
|
|
753
|
+
end
|
|
754
|
+
-1
|
|
755
|
+
end
|
|
756
|
+
|
|
757
|
+
# Find the index of the top-level ``:`` that separates the true/false
|
|
758
|
+
# branches of a ternary. Respects quotes and parentheses.
|
|
759
|
+
def find_colon(expr)
|
|
760
|
+
depth = 0
|
|
761
|
+
in_quote = nil
|
|
762
|
+
expr.each_char.with_index do |ch, i|
|
|
763
|
+
if in_quote
|
|
764
|
+
in_quote = nil if ch == in_quote
|
|
765
|
+
next
|
|
766
|
+
end
|
|
767
|
+
if ch == '"' || ch == "'"
|
|
768
|
+
in_quote = ch
|
|
769
|
+
next
|
|
770
|
+
end
|
|
771
|
+
if ch == "("
|
|
772
|
+
depth += 1
|
|
773
|
+
elsif ch == ")"
|
|
774
|
+
depth -= 1
|
|
775
|
+
elsif ch == ":" && depth == 0
|
|
776
|
+
return i
|
|
777
|
+
end
|
|
778
|
+
end
|
|
779
|
+
-1
|
|
780
|
+
end
|
|
781
|
+
|
|
782
|
+
# -----------------------------------------------------------------------
|
|
783
|
+
# Filter chain parser
|
|
784
|
+
# -----------------------------------------------------------------------
|
|
785
|
+
|
|
786
|
+
# Split "first.groupSummary" into ["first", "groupSummary"] so a
|
|
787
|
+
# filter segment followed by property access — `{{ x | first.name }}`
|
|
788
|
+
# — applies the filter then traverses the path on the result.
|
|
789
|
+
# Returns [fname, ""] when no structural dot is present.
|
|
790
|
+
#
|
|
791
|
+
# The split point must sit outside parens/brackets/braces and quotes
|
|
792
|
+
# so filter args like `round(1.5)` or `date("Y.m.d")` don't false-
|
|
793
|
+
# trigger. Parity with tina4-python and tina4-php.
|
|
794
|
+
def split_filter_name_and_path(fname)
|
|
795
|
+
depth = 0
|
|
796
|
+
in_q = nil
|
|
797
|
+
i = 0
|
|
798
|
+
n = fname.length
|
|
799
|
+
while i < n
|
|
800
|
+
ch = fname[i]
|
|
801
|
+
if in_q
|
|
802
|
+
in_q = nil if ch == in_q && (i.zero? || fname[i - 1] != "\\")
|
|
803
|
+
i += 1
|
|
804
|
+
next
|
|
805
|
+
end
|
|
806
|
+
case ch
|
|
807
|
+
when '"', "'"
|
|
808
|
+
in_q = ch
|
|
809
|
+
when "(", "[", "{"
|
|
810
|
+
depth += 1
|
|
811
|
+
when ")", "]", "}"
|
|
812
|
+
depth -= 1
|
|
813
|
+
when "."
|
|
814
|
+
return [fname[0...i], fname[(i + 1)..]] if depth.zero?
|
|
815
|
+
end
|
|
816
|
+
i += 1
|
|
817
|
+
end
|
|
818
|
+
[fname, ""]
|
|
819
|
+
end
|
|
820
|
+
|
|
821
|
+
def parse_filter_chain(expr)
|
|
822
|
+
cached = @filter_chain_cache[expr]
|
|
823
|
+
return cached if cached
|
|
824
|
+
|
|
825
|
+
parts = split_on_pipe(expr)
|
|
826
|
+
variable = parts[0].strip
|
|
827
|
+
filters = []
|
|
828
|
+
|
|
829
|
+
parts[1..].each do |f|
|
|
830
|
+
f = f.strip
|
|
831
|
+
if f =~ FILTER_WITH_ARGS_RE
|
|
832
|
+
name = Regexp.last_match(1)
|
|
833
|
+
raw_args = Regexp.last_match(2).strip
|
|
834
|
+
args = raw_args.empty? ? [] : parse_args(raw_args)
|
|
835
|
+
filters << [name, args]
|
|
836
|
+
else
|
|
837
|
+
filters << [f.strip, []]
|
|
838
|
+
end
|
|
839
|
+
end
|
|
840
|
+
|
|
841
|
+
result = [variable, filters].freeze
|
|
842
|
+
@filter_chain_cache[expr] = result
|
|
843
|
+
result
|
|
844
|
+
end
|
|
845
|
+
|
|
846
|
+
# Split expression on | but not inside quotes or parens.
|
|
847
|
+
def split_on_pipe(expr)
|
|
848
|
+
parts = []
|
|
849
|
+
current = +""
|
|
850
|
+
in_quote = nil
|
|
851
|
+
depth = 0
|
|
852
|
+
|
|
853
|
+
expr.each_char do |ch|
|
|
854
|
+
if in_quote
|
|
855
|
+
current << ch
|
|
856
|
+
in_quote = nil if ch == in_quote
|
|
857
|
+
elsif ch == '"' || ch == "'"
|
|
858
|
+
in_quote = ch
|
|
859
|
+
current << ch
|
|
860
|
+
elsif ch == "("
|
|
861
|
+
depth += 1
|
|
862
|
+
current << ch
|
|
863
|
+
elsif ch == ")"
|
|
864
|
+
depth -= 1
|
|
865
|
+
current << ch
|
|
866
|
+
elsif ch == "|" && depth == 0
|
|
867
|
+
parts << current
|
|
868
|
+
current = +""
|
|
869
|
+
else
|
|
870
|
+
current << ch
|
|
871
|
+
end
|
|
872
|
+
end
|
|
873
|
+
parts << current unless current.empty?
|
|
874
|
+
parts
|
|
875
|
+
end
|
|
876
|
+
|
|
877
|
+
def parse_args(raw)
|
|
878
|
+
args = []
|
|
879
|
+
current = +""
|
|
880
|
+
in_quote = nil
|
|
881
|
+
depth = 0
|
|
882
|
+
|
|
883
|
+
raw.each_char do |ch|
|
|
884
|
+
if in_quote
|
|
885
|
+
if ch == in_quote
|
|
886
|
+
in_quote = nil
|
|
887
|
+
end
|
|
888
|
+
current << ch
|
|
889
|
+
elsif ch == '"' || ch == "'"
|
|
890
|
+
in_quote = ch
|
|
891
|
+
current << ch
|
|
892
|
+
elsif ch == "(" || ch == "{" || ch == "["
|
|
893
|
+
depth += 1
|
|
894
|
+
current << ch
|
|
895
|
+
elsif ch == ")" || ch == "}" || ch == "]"
|
|
896
|
+
depth -= 1
|
|
897
|
+
current << ch
|
|
898
|
+
elsif ch == "," && depth == 0
|
|
899
|
+
args << current.strip
|
|
900
|
+
current = +""
|
|
901
|
+
else
|
|
902
|
+
current << ch
|
|
903
|
+
end
|
|
904
|
+
end
|
|
905
|
+
args << current.strip unless current.strip.empty?
|
|
906
|
+
args
|
|
907
|
+
end
|
|
908
|
+
|
|
909
|
+
# -----------------------------------------------------------------------
|
|
910
|
+
# Expression evaluator
|
|
911
|
+
# -----------------------------------------------------------------------
|
|
912
|
+
|
|
913
|
+
# ── Expression evaluator (dispatcher) ──────────────────────────────
|
|
914
|
+
# Each expression type is handled by a focused helper method.
|
|
915
|
+
# Helpers return :not_matched when the expression doesn't match their
|
|
916
|
+
# type, so the dispatcher falls through to the next handler.
|
|
917
|
+
|
|
918
|
+
def eval_expr(expr, context)
|
|
919
|
+
expr = expr.strip
|
|
920
|
+
return nil if expr.empty?
|
|
921
|
+
|
|
922
|
+
result = eval_literal(expr)
|
|
923
|
+
return result unless result == :not_literal
|
|
924
|
+
|
|
925
|
+
result = eval_collection_literal(expr, context)
|
|
926
|
+
return result unless result == :not_collection
|
|
927
|
+
|
|
928
|
+
return eval_expr(expr[1..-2], context) if matched_parens?(expr)
|
|
929
|
+
|
|
930
|
+
result = eval_ternary(expr, context)
|
|
931
|
+
return result unless result == :not_ternary
|
|
932
|
+
|
|
933
|
+
result = eval_inline_if(expr, context)
|
|
934
|
+
return result unless result == :not_inline_if
|
|
935
|
+
|
|
936
|
+
result = eval_null_coalesce(expr, context)
|
|
937
|
+
return result unless result == :not_coalesce
|
|
938
|
+
|
|
939
|
+
result = eval_concat(expr, context)
|
|
940
|
+
return result unless result == :not_concat
|
|
941
|
+
|
|
942
|
+
return eval_comparison(expr, context) if has_comparison?(expr)
|
|
943
|
+
|
|
944
|
+
result = eval_arithmetic(expr, context)
|
|
945
|
+
return result unless result == :not_arithmetic
|
|
946
|
+
|
|
947
|
+
result = eval_function_call(expr, context)
|
|
948
|
+
return result unless result == :not_function
|
|
949
|
+
|
|
950
|
+
resolve(expr, context)
|
|
951
|
+
end
|
|
952
|
+
|
|
953
|
+
# ── Literal values: strings, numbers, booleans, null ──
|
|
954
|
+
|
|
955
|
+
def eval_literal(expr)
|
|
956
|
+
if (expr.start_with?('"') && expr.end_with?('"')) ||
|
|
957
|
+
(expr.start_with?("'") && expr.end_with?("'"))
|
|
958
|
+
return expr[1..-2]
|
|
959
|
+
end
|
|
960
|
+
return expr.to_i if expr =~ INTEGER_RE
|
|
961
|
+
return expr.to_f if expr =~ FLOAT_RE
|
|
962
|
+
return true if expr == "true"
|
|
963
|
+
return false if expr == "false"
|
|
964
|
+
return nil if expr == "null" || expr == "none" || expr == "nil"
|
|
965
|
+
:not_literal
|
|
966
|
+
end
|
|
967
|
+
|
|
968
|
+
# ── Collection literals: arrays, hashes, ranges ──
|
|
969
|
+
|
|
970
|
+
def eval_collection_literal(expr, context)
|
|
971
|
+
if expr =~ ARRAY_LIT_RE
|
|
972
|
+
inner = Regexp.last_match(1)
|
|
973
|
+
return split_args_toplevel(inner).map { |item| eval_expr(item.strip, context) }
|
|
974
|
+
end
|
|
975
|
+
if expr =~ HASH_LIT_RE
|
|
976
|
+
inner = Regexp.last_match(1)
|
|
977
|
+
hash = {}
|
|
978
|
+
split_args_toplevel(inner).each do |pair|
|
|
979
|
+
if pair =~ HASH_PAIR_RE
|
|
980
|
+
key = Regexp.last_match(1) || Regexp.last_match(2)
|
|
981
|
+
hash[key] = eval_expr(Regexp.last_match(3).strip, context)
|
|
982
|
+
end
|
|
983
|
+
end
|
|
984
|
+
return hash
|
|
985
|
+
end
|
|
986
|
+
if expr =~ RANGE_LIT_RE
|
|
987
|
+
return (Regexp.last_match(1).to_i..Regexp.last_match(2).to_i).to_a
|
|
988
|
+
end
|
|
989
|
+
:not_collection
|
|
990
|
+
end
|
|
991
|
+
|
|
992
|
+
# ── Parenthesized sub-expression check ──
|
|
993
|
+
|
|
994
|
+
def matched_parens?(expr)
|
|
995
|
+
return false unless expr.start_with?("(") && expr.end_with?(")")
|
|
996
|
+
depth = 0
|
|
997
|
+
expr.each_char.with_index do |ch, pi|
|
|
998
|
+
depth += 1 if ch == "("
|
|
999
|
+
depth -= 1 if ch == ")"
|
|
1000
|
+
return false if depth == 0 && pi < expr.length - 1
|
|
1001
|
+
end
|
|
1002
|
+
true
|
|
1003
|
+
end
|
|
1004
|
+
|
|
1005
|
+
# ── Ternary: condition ? "yes" : "no" ──
|
|
1006
|
+
|
|
1007
|
+
def eval_ternary(expr, context)
|
|
1008
|
+
q_pos = find_outside_quotes(expr, "?")
|
|
1009
|
+
return :not_ternary unless q_pos && q_pos > 0
|
|
1010
|
+
cond_part = expr[0...q_pos].strip
|
|
1011
|
+
rest = expr[(q_pos + 1)..]
|
|
1012
|
+
c_pos = find_outside_quotes(rest, ":")
|
|
1013
|
+
return :not_ternary unless c_pos && c_pos >= 0
|
|
1014
|
+
true_part = rest[0...c_pos].strip
|
|
1015
|
+
false_part = rest[(c_pos + 1)..].strip
|
|
1016
|
+
cond = eval_expr(cond_part, context)
|
|
1017
|
+
truthy?(cond) ? eval_expr(true_part, context) : eval_expr(false_part, context)
|
|
1018
|
+
end
|
|
1019
|
+
|
|
1020
|
+
# ── Inline if: value if condition else other_value ──
|
|
1021
|
+
|
|
1022
|
+
def eval_inline_if(expr, context)
|
|
1023
|
+
if_pos = find_outside_quotes(expr, " if ")
|
|
1024
|
+
return :not_inline_if unless if_pos && if_pos >= 0
|
|
1025
|
+
else_pos = find_outside_quotes(expr, " else ")
|
|
1026
|
+
return :not_inline_if unless else_pos && else_pos > if_pos
|
|
1027
|
+
value_part = expr[0...if_pos].strip
|
|
1028
|
+
cond_part = expr[(if_pos + 4)...else_pos].strip
|
|
1029
|
+
else_part = expr[(else_pos + 6)..].strip
|
|
1030
|
+
cond = eval_expr(cond_part, context)
|
|
1031
|
+
truthy?(cond) ? eval_expr(value_part, context) : eval_expr(else_part, context)
|
|
1032
|
+
end
|
|
1033
|
+
|
|
1034
|
+
# ── Null coalescing: value ?? "default" ──
|
|
1035
|
+
|
|
1036
|
+
def eval_null_coalesce(expr, context)
|
|
1037
|
+
return :not_coalesce unless expr.include?("??")
|
|
1038
|
+
left, _, right = expr.partition("??")
|
|
1039
|
+
val = eval_expr(left.strip, context)
|
|
1040
|
+
val.nil? ? eval_expr(right.strip, context) : val
|
|
1041
|
+
end
|
|
1042
|
+
|
|
1043
|
+
# ── String concatenation: a ~ b ──
|
|
1044
|
+
|
|
1045
|
+
def eval_concat(expr, context)
|
|
1046
|
+
return :not_concat unless expr.include?("~")
|
|
1047
|
+
parts = expr.split("~")
|
|
1048
|
+
parts.map { |p| (eval_expr(p.strip, context) || "").to_s }.join
|
|
1049
|
+
end
|
|
1050
|
+
|
|
1051
|
+
# ── Arithmetic: +, -, *, //, /, %, ** ──
|
|
1052
|
+
|
|
1053
|
+
def eval_arithmetic(expr, context)
|
|
1054
|
+
ARITHMETIC_OPS.each do |op|
|
|
1055
|
+
pos = find_outside_quotes(expr, op)
|
|
1056
|
+
next unless pos && pos >= 0
|
|
1057
|
+
l_val = eval_expr(expr[0...pos].strip, context)
|
|
1058
|
+
r_val = eval_expr(expr[(pos + op.length)..].strip, context)
|
|
1059
|
+
return apply_math(l_val, op.strip, r_val)
|
|
1060
|
+
end
|
|
1061
|
+
:not_arithmetic
|
|
1062
|
+
end
|
|
1063
|
+
|
|
1064
|
+
# ── Function call: name(arg1, arg2) ──
|
|
1065
|
+
|
|
1066
|
+
def eval_function_call(expr, context)
|
|
1067
|
+
return :not_function unless expr =~ FUNC_CALL_RE
|
|
1068
|
+
fn_name = Regexp.last_match(1)
|
|
1069
|
+
raw_args = Regexp.last_match(2).strip
|
|
1070
|
+
fn = context[fn_name]
|
|
1071
|
+
return :not_function unless fn.respond_to?(:call)
|
|
1072
|
+
args = raw_args.empty? ? [] : split_args_toplevel(raw_args).map { |a| eval_expr(a.strip, context) }
|
|
1073
|
+
fn.call(*args)
|
|
1074
|
+
end
|
|
1075
|
+
|
|
1076
|
+
def has_comparison?(expr)
|
|
1077
|
+
[" not in ", " in ", " is not ", " is ", "!=", "==", ">=", "<=", ">", "<",
|
|
1078
|
+
" and ", " or ", " not "].any? { |op| expr.include?(op) }
|
|
1079
|
+
end
|
|
1080
|
+
|
|
1081
|
+
# Split comma-separated args at top level (not inside quotes/parens/brackets).
|
|
1082
|
+
def split_args_toplevel(str)
|
|
1083
|
+
parts = []
|
|
1084
|
+
current = +""
|
|
1085
|
+
in_quote = nil
|
|
1086
|
+
depth = 0
|
|
1087
|
+
|
|
1088
|
+
str.each_char do |ch|
|
|
1089
|
+
if in_quote
|
|
1090
|
+
current << ch
|
|
1091
|
+
in_quote = nil if ch == in_quote
|
|
1092
|
+
elsif ch == '"' || ch == "'"
|
|
1093
|
+
in_quote = ch
|
|
1094
|
+
current << ch
|
|
1095
|
+
elsif ch == "(" || ch == "[" || ch == "{"
|
|
1096
|
+
depth += 1
|
|
1097
|
+
current << ch
|
|
1098
|
+
elsif ch == ")" || ch == "]" || ch == "}"
|
|
1099
|
+
depth -= 1
|
|
1100
|
+
current << ch
|
|
1101
|
+
elsif ch == "," && depth == 0
|
|
1102
|
+
parts << current.strip
|
|
1103
|
+
current = +""
|
|
1104
|
+
else
|
|
1105
|
+
current << ch
|
|
1106
|
+
end
|
|
1107
|
+
end
|
|
1108
|
+
parts << current.strip unless current.strip.empty?
|
|
1109
|
+
parts
|
|
1110
|
+
end
|
|
1111
|
+
|
|
1112
|
+
# -----------------------------------------------------------------------
|
|
1113
|
+
# Comparison / logical evaluator
|
|
1114
|
+
# -----------------------------------------------------------------------
|
|
1115
|
+
|
|
1116
|
+
def eval_comparison(expr, context, eval_fn = nil)
|
|
1117
|
+
eval_fn ||= method(:eval_expr)
|
|
1118
|
+
expr = expr.strip
|
|
1119
|
+
|
|
1120
|
+
# Handle 'not' prefix
|
|
1121
|
+
if expr.start_with?("not ")
|
|
1122
|
+
return !eval_comparison(expr[4..], context, eval_fn)
|
|
1123
|
+
end
|
|
1124
|
+
|
|
1125
|
+
# 'or' (lowest precedence)
|
|
1126
|
+
or_parts = expr.split(OR_SPLIT_RE)
|
|
1127
|
+
if or_parts.length > 1
|
|
1128
|
+
return or_parts.any? { |p| eval_comparison(p, context, eval_fn) }
|
|
1129
|
+
end
|
|
1130
|
+
|
|
1131
|
+
# 'and'
|
|
1132
|
+
and_parts = expr.split(AND_SPLIT_RE)
|
|
1133
|
+
if and_parts.length > 1
|
|
1134
|
+
return and_parts.all? { |p| eval_comparison(p, context, eval_fn) }
|
|
1135
|
+
end
|
|
1136
|
+
|
|
1137
|
+
# 'is not' test
|
|
1138
|
+
if expr =~ IS_NOT_RE
|
|
1139
|
+
return !eval_test(Regexp.last_match(1).strip, Regexp.last_match(2),
|
|
1140
|
+
Regexp.last_match(3).strip, context, eval_fn)
|
|
1141
|
+
end
|
|
1142
|
+
|
|
1143
|
+
# 'is' test
|
|
1144
|
+
if expr =~ IS_RE
|
|
1145
|
+
return eval_test(Regexp.last_match(1).strip, Regexp.last_match(2),
|
|
1146
|
+
Regexp.last_match(3).strip, context, eval_fn)
|
|
1147
|
+
end
|
|
1148
|
+
|
|
1149
|
+
# 'not in'
|
|
1150
|
+
if expr =~ NOT_IN_RE
|
|
1151
|
+
val = eval_fn.call(Regexp.last_match(1).strip, context)
|
|
1152
|
+
collection = eval_fn.call(Regexp.last_match(2).strip, context)
|
|
1153
|
+
return !(collection.respond_to?(:include?) && collection.include?(val))
|
|
1154
|
+
end
|
|
1155
|
+
|
|
1156
|
+
# 'in'
|
|
1157
|
+
if expr =~ IN_RE
|
|
1158
|
+
val = eval_fn.call(Regexp.last_match(1).strip, context)
|
|
1159
|
+
collection = eval_fn.call(Regexp.last_match(2).strip, context)
|
|
1160
|
+
return collection.respond_to?(:include?) ? collection.include?(val) : false
|
|
1161
|
+
end
|
|
1162
|
+
|
|
1163
|
+
# Binary comparison operators
|
|
1164
|
+
[["!=", ->(a, b) { a != b }],
|
|
1165
|
+
["==", ->(a, b) { a == b }],
|
|
1166
|
+
[">=", ->(a, b) { a.to_f >= b.to_f }],
|
|
1167
|
+
["<=", ->(a, b) { a.to_f <= b.to_f }],
|
|
1168
|
+
[">", ->(a, b) { a.to_f > b.to_f }],
|
|
1169
|
+
["<", ->(a, b) { a.to_f < b.to_f }]].each do |op, fn|
|
|
1170
|
+
if expr.include?(op)
|
|
1171
|
+
left, _, right = expr.partition(op)
|
|
1172
|
+
l = eval_fn.call(left.strip, context)
|
|
1173
|
+
r = eval_fn.call(right.strip, context)
|
|
1174
|
+
begin
|
|
1175
|
+
return fn.call(l, r)
|
|
1176
|
+
rescue
|
|
1177
|
+
return false
|
|
1178
|
+
end
|
|
1179
|
+
end
|
|
1180
|
+
end
|
|
1181
|
+
|
|
1182
|
+
# Fall through to simple eval
|
|
1183
|
+
val = eval_fn.call(expr, context)
|
|
1184
|
+
truthy?(val)
|
|
1185
|
+
end
|
|
1186
|
+
|
|
1187
|
+
# -----------------------------------------------------------------------
|
|
1188
|
+
# Tests ('is' expressions)
|
|
1189
|
+
# -----------------------------------------------------------------------
|
|
1190
|
+
|
|
1191
|
+
def eval_test(value_expr, test_name, args_str, context, eval_fn = nil)
|
|
1192
|
+
eval_fn ||= method(:eval_expr)
|
|
1193
|
+
val = eval_fn.call(value_expr, context)
|
|
1194
|
+
|
|
1195
|
+
# 'divisible by(n)'
|
|
1196
|
+
if test_name == "divisible"
|
|
1197
|
+
if args_str =~ DIVISIBLE_BY_RE
|
|
1198
|
+
n = Regexp.last_match(1).to_i
|
|
1199
|
+
return val.is_a?(Integer) && (val % n).zero?
|
|
1200
|
+
end
|
|
1201
|
+
return false
|
|
1202
|
+
end
|
|
1203
|
+
|
|
1204
|
+
# Check custom tests first
|
|
1205
|
+
custom = @tests[test_name]
|
|
1206
|
+
return custom.call(val) if custom
|
|
1207
|
+
|
|
1208
|
+
false
|
|
1209
|
+
end
|
|
1210
|
+
|
|
1211
|
+
def default_tests
|
|
1212
|
+
{
|
|
1213
|
+
"defined" => ->(v) { !v.nil? },
|
|
1214
|
+
"empty" => ->(v) { v.nil? || (v.respond_to?(:empty?) && v.empty?) || v == 0 || v == false },
|
|
1215
|
+
"null" => ->(v) { v.nil? },
|
|
1216
|
+
"none" => ->(v) { v.nil? },
|
|
1217
|
+
"even" => ->(v) { v.is_a?(Integer) && v.even? },
|
|
1218
|
+
"odd" => ->(v) { v.is_a?(Integer) && v.odd? },
|
|
1219
|
+
"iterable" => ->(v) { v.respond_to?(:each) && !v.is_a?(String) },
|
|
1220
|
+
"string" => ->(v) { v.is_a?(String) },
|
|
1221
|
+
"number" => ->(v) { v.is_a?(Numeric) },
|
|
1222
|
+
"boolean" => ->(v) { v.is_a?(TrueClass) || v.is_a?(FalseClass) },
|
|
1223
|
+
}
|
|
1224
|
+
end
|
|
1225
|
+
|
|
1226
|
+
# -----------------------------------------------------------------------
|
|
1227
|
+
# Variable resolver
|
|
1228
|
+
# -----------------------------------------------------------------------
|
|
1229
|
+
|
|
1230
|
+
def resolve(expr, context)
|
|
1231
|
+
parts = @resolve_cache[expr]
|
|
1232
|
+
unless parts
|
|
1233
|
+
parts = expr.split(RESOLVE_SPLIT_RE).reject(&:empty?)
|
|
1234
|
+
@resolve_cache[expr] = parts
|
|
1235
|
+
end
|
|
1236
|
+
|
|
1237
|
+
value = context
|
|
1238
|
+
|
|
1239
|
+
parts.each do |part|
|
|
1240
|
+
part = part.strip.gsub(RESOLVE_STRIP_RE, "") # strip quotes from bracket access
|
|
1241
|
+
if value.is_a?(Hash) || value.is_a?(LoopContext)
|
|
1242
|
+
value = value[part] || value[part.to_sym]
|
|
1243
|
+
elsif value.is_a?(Array)
|
|
1244
|
+
# Slice syntax: value[1:5], value[:10], value[start:end]
|
|
1245
|
+
if part.include?(":") && !(part.start_with?('"') || part.start_with?("'"))
|
|
1246
|
+
slice_parts = part.split(":", 2)
|
|
1247
|
+
s_start = slice_parts[0].strip.empty? ? nil : eval_expr(slice_parts[0].strip, context).to_i
|
|
1248
|
+
s_end = slice_parts[1].strip.empty? ? nil : eval_expr(slice_parts[1].strip, context).to_i
|
|
1249
|
+
if s_start && s_end
|
|
1250
|
+
value = value[s_start...s_end]
|
|
1251
|
+
elsif s_start
|
|
1252
|
+
value = value[s_start..]
|
|
1253
|
+
elsif s_end
|
|
1254
|
+
value = value[0...s_end]
|
|
1255
|
+
else
|
|
1256
|
+
value = value.dup
|
|
1257
|
+
end
|
|
1258
|
+
next
|
|
1259
|
+
end
|
|
1260
|
+
idx = if part =~ DIGIT_RE
|
|
1261
|
+
part.to_i
|
|
1262
|
+
else
|
|
1263
|
+
eval_expr(part, context)
|
|
1264
|
+
end
|
|
1265
|
+
idx = idx.to_i if idx.is_a?(Numeric)
|
|
1266
|
+
value = idx.is_a?(Integer) ? value[idx] : nil
|
|
1267
|
+
elsif value.respond_to?(part.to_sym)
|
|
1268
|
+
value = value.send(part.to_sym)
|
|
1269
|
+
else
|
|
1270
|
+
return nil
|
|
1271
|
+
end
|
|
1272
|
+
return nil if value.nil?
|
|
1273
|
+
end
|
|
1274
|
+
|
|
1275
|
+
value
|
|
1276
|
+
end
|
|
1277
|
+
|
|
1278
|
+
# -----------------------------------------------------------------------
|
|
1279
|
+
# Math
|
|
1280
|
+
# -----------------------------------------------------------------------
|
|
1281
|
+
|
|
1282
|
+
def apply_math(left, op, right)
|
|
1283
|
+
l = (left || 0).to_f
|
|
1284
|
+
r = (right || 0).to_f
|
|
1285
|
+
# Preserve int type when both operands are int-like (except for / which returns float)
|
|
1286
|
+
both_int = l == l.to_i && r == r.to_i && op != "/"
|
|
1287
|
+
result = case op
|
|
1288
|
+
when "+" then l + r
|
|
1289
|
+
when "-" then l - r
|
|
1290
|
+
when "*" then l * r
|
|
1291
|
+
when "/" then r != 0 ? l / r : 0
|
|
1292
|
+
when "//" then r != 0 ? (l / r).floor : 0
|
|
1293
|
+
when "%" then r != 0 ? l % r : 0
|
|
1294
|
+
when "**" then l ** r
|
|
1295
|
+
else 0
|
|
1296
|
+
end
|
|
1297
|
+
both_int && result == result.to_i ? result.to_i : result.to_f == result.to_i ? result.to_i : result
|
|
1298
|
+
end
|
|
1299
|
+
|
|
1300
|
+
# -----------------------------------------------------------------------
|
|
1301
|
+
# Block handlers
|
|
1302
|
+
# -----------------------------------------------------------------------
|
|
1303
|
+
|
|
1304
|
+
# {% if %}...{% elseif %}...{% else %}...{% endif %}
|
|
1305
|
+
def handle_if(tokens, start, context)
|
|
1306
|
+
content, _, strip_a_open = strip_tag(tokens[start][1])
|
|
1307
|
+
condition_expr = content.sub(/\Aif\s+/, "").strip
|
|
1308
|
+
|
|
1309
|
+
branches = []
|
|
1310
|
+
current_tokens = []
|
|
1311
|
+
current_cond = condition_expr
|
|
1312
|
+
depth = 0
|
|
1313
|
+
i = start + 1
|
|
1314
|
+
|
|
1315
|
+
# If the opening {%- if -%} has strip_after, lstrip the first body text
|
|
1316
|
+
pending_lstrip = strip_a_open
|
|
1317
|
+
|
|
1318
|
+
while i < tokens.length
|
|
1319
|
+
ttype, raw = tokens[i]
|
|
1320
|
+
if ttype == BLOCK
|
|
1321
|
+
tag_content, strip_b_tag, strip_a_tag = strip_tag(raw)
|
|
1322
|
+
tag = tag_content.split[0] || ""
|
|
1323
|
+
|
|
1324
|
+
if tag == "if"
|
|
1325
|
+
depth += 1
|
|
1326
|
+
current_tokens << tokens[i]
|
|
1327
|
+
elsif tag == "endif" && depth > 0
|
|
1328
|
+
depth -= 1
|
|
1329
|
+
current_tokens << tokens[i]
|
|
1330
|
+
elsif tag == "endif" && depth == 0
|
|
1331
|
+
# Apply strip_before from endif to last body token
|
|
1332
|
+
if strip_b_tag && !current_tokens.empty? && current_tokens[-1][0] == TEXT
|
|
1333
|
+
current_tokens[-1] = [TEXT, current_tokens[-1][1].rstrip]
|
|
1334
|
+
end
|
|
1335
|
+
branches << [current_cond, current_tokens]
|
|
1336
|
+
i += 1
|
|
1337
|
+
break
|
|
1338
|
+
elsif (tag == "elseif" || tag == "elif") && depth == 0
|
|
1339
|
+
# Apply strip_before from elseif to last body token
|
|
1340
|
+
if strip_b_tag && !current_tokens.empty? && current_tokens[-1][0] == TEXT
|
|
1341
|
+
current_tokens[-1] = [TEXT, current_tokens[-1][1].rstrip]
|
|
1342
|
+
end
|
|
1343
|
+
branches << [current_cond, current_tokens]
|
|
1344
|
+
current_cond = tag_content.sub(/\A(?:elseif|elif)\s+/, "").strip
|
|
1345
|
+
current_tokens = []
|
|
1346
|
+
pending_lstrip = strip_a_tag
|
|
1347
|
+
elsif tag == "else" && depth == 0
|
|
1348
|
+
# Apply strip_before from else to last body token
|
|
1349
|
+
if strip_b_tag && !current_tokens.empty? && current_tokens[-1][0] == TEXT
|
|
1350
|
+
current_tokens[-1] = [TEXT, current_tokens[-1][1].rstrip]
|
|
1351
|
+
end
|
|
1352
|
+
branches << [current_cond, current_tokens]
|
|
1353
|
+
current_cond = nil
|
|
1354
|
+
current_tokens = []
|
|
1355
|
+
pending_lstrip = strip_a_tag
|
|
1356
|
+
else
|
|
1357
|
+
current_tokens << tokens[i]
|
|
1358
|
+
end
|
|
1359
|
+
else
|
|
1360
|
+
tok = tokens[i]
|
|
1361
|
+
if pending_lstrip && ttype == TEXT
|
|
1362
|
+
tok = [TEXT, tok[1].lstrip]
|
|
1363
|
+
pending_lstrip = false
|
|
1364
|
+
end
|
|
1365
|
+
current_tokens << tok
|
|
1366
|
+
end
|
|
1367
|
+
i += 1
|
|
1368
|
+
end
|
|
1369
|
+
|
|
1370
|
+
branches.each do |cond, branch_tokens|
|
|
1371
|
+
if cond.nil? || eval_comparison(cond, context, method(:eval_var_raw))
|
|
1372
|
+
return [render_tokens(branch_tokens.dup, context), i]
|
|
1373
|
+
end
|
|
1374
|
+
end
|
|
1375
|
+
|
|
1376
|
+
["", i]
|
|
1377
|
+
end
|
|
1378
|
+
|
|
1379
|
+
# {% for item in items %}...{% else %}...{% endfor %}
|
|
1380
|
+
def handle_for(tokens, start, context)
|
|
1381
|
+
content, _, strip_a_open = strip_tag(tokens[start][1])
|
|
1382
|
+
m = content.match(FOR_RE)
|
|
1383
|
+
return ["", start + 1] unless m
|
|
1384
|
+
|
|
1385
|
+
var1 = m[1]
|
|
1386
|
+
var2 = m[2]
|
|
1387
|
+
iterable_expr = m[3].strip
|
|
1388
|
+
|
|
1389
|
+
body_tokens = []
|
|
1390
|
+
else_tokens = []
|
|
1391
|
+
in_else = false
|
|
1392
|
+
for_depth = 0
|
|
1393
|
+
if_depth = 0
|
|
1394
|
+
i = start + 1
|
|
1395
|
+
pending_lstrip = strip_a_open
|
|
1396
|
+
|
|
1397
|
+
while i < tokens.length
|
|
1398
|
+
ttype, raw = tokens[i]
|
|
1399
|
+
if ttype == BLOCK
|
|
1400
|
+
tag_content, strip_b_tag, strip_a_tag = strip_tag(raw)
|
|
1401
|
+
tag = tag_content.split[0] || ""
|
|
1402
|
+
|
|
1403
|
+
if tag == "for"
|
|
1404
|
+
for_depth += 1
|
|
1405
|
+
(in_else ? else_tokens : body_tokens) << tokens[i]
|
|
1406
|
+
elsif tag == "endfor" && for_depth > 0
|
|
1407
|
+
for_depth -= 1
|
|
1408
|
+
(in_else ? else_tokens : body_tokens) << tokens[i]
|
|
1409
|
+
elsif tag == "endfor" && for_depth == 0
|
|
1410
|
+
target = in_else ? else_tokens : body_tokens
|
|
1411
|
+
if strip_b_tag && !target.empty? && target[-1][0] == TEXT
|
|
1412
|
+
target[-1] = [TEXT, target[-1][1].rstrip]
|
|
1413
|
+
end
|
|
1414
|
+
i += 1
|
|
1415
|
+
break
|
|
1416
|
+
elsif tag == "if"
|
|
1417
|
+
if_depth += 1
|
|
1418
|
+
(in_else ? else_tokens : body_tokens) << tokens[i]
|
|
1419
|
+
elsif tag == "endif"
|
|
1420
|
+
if_depth -= 1
|
|
1421
|
+
(in_else ? else_tokens : body_tokens) << tokens[i]
|
|
1422
|
+
elsif tag == "else" && for_depth == 0 && if_depth == 0
|
|
1423
|
+
if strip_b_tag && !body_tokens.empty? && body_tokens[-1][0] == TEXT
|
|
1424
|
+
body_tokens[-1] = [TEXT, body_tokens[-1][1].rstrip]
|
|
1425
|
+
end
|
|
1426
|
+
in_else = true
|
|
1427
|
+
pending_lstrip = strip_a_tag
|
|
1428
|
+
else
|
|
1429
|
+
(in_else ? else_tokens : body_tokens) << tokens[i]
|
|
1430
|
+
end
|
|
1431
|
+
else
|
|
1432
|
+
tok = tokens[i]
|
|
1433
|
+
if pending_lstrip && ttype == TEXT
|
|
1434
|
+
tok = [TEXT, tok[1].lstrip]
|
|
1435
|
+
pending_lstrip = false
|
|
1436
|
+
end
|
|
1437
|
+
(in_else ? else_tokens : body_tokens) << tok
|
|
1438
|
+
end
|
|
1439
|
+
i += 1
|
|
1440
|
+
end
|
|
1441
|
+
|
|
1442
|
+
iterable = eval_expr(iterable_expr, context)
|
|
1443
|
+
|
|
1444
|
+
if iterable.nil? || (iterable.respond_to?(:empty?) && iterable.empty?)
|
|
1445
|
+
if else_tokens.any?
|
|
1446
|
+
return [render_tokens(else_tokens.dup, context), i]
|
|
1447
|
+
end
|
|
1448
|
+
return ["", i]
|
|
1449
|
+
end
|
|
1450
|
+
|
|
1451
|
+
output = []
|
|
1452
|
+
items = iterable.is_a?(Hash) ? iterable.to_a : Array(iterable)
|
|
1453
|
+
total = items.length
|
|
1454
|
+
|
|
1455
|
+
items.each_with_index do |item, idx|
|
|
1456
|
+
loop_ctx = LoopContext.new(context)
|
|
1457
|
+
loop_ctx["loop"] = {
|
|
1458
|
+
"index" => idx + 1,
|
|
1459
|
+
"index0" => idx,
|
|
1460
|
+
"first" => idx == 0,
|
|
1461
|
+
"last" => idx == total - 1,
|
|
1462
|
+
"length" => total,
|
|
1463
|
+
"revindex" => total - idx,
|
|
1464
|
+
"revindex0" => total - idx - 1,
|
|
1465
|
+
"even" => ((idx + 1) % 2).zero?,
|
|
1466
|
+
"odd" => ((idx + 1) % 2) != 0,
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
if iterable.is_a?(Hash)
|
|
1470
|
+
key, value = item
|
|
1471
|
+
if var2
|
|
1472
|
+
loop_ctx[var1] = key
|
|
1473
|
+
loop_ctx[var2] = value
|
|
1474
|
+
else
|
|
1475
|
+
loop_ctx[var1] = key
|
|
1476
|
+
end
|
|
1477
|
+
else
|
|
1478
|
+
if var2
|
|
1479
|
+
loop_ctx[var1] = idx
|
|
1480
|
+
loop_ctx[var2] = item
|
|
1481
|
+
else
|
|
1482
|
+
loop_ctx[var1] = item
|
|
1483
|
+
end
|
|
1484
|
+
end
|
|
1485
|
+
|
|
1486
|
+
output << render_tokens(body_tokens.dup, loop_ctx)
|
|
1487
|
+
end
|
|
1488
|
+
|
|
1489
|
+
[output.join, i]
|
|
1490
|
+
end
|
|
1491
|
+
|
|
1492
|
+
# {% set name = expr %}
|
|
1493
|
+
def handle_set(content, context)
|
|
1494
|
+
if content =~ SET_RE
|
|
1495
|
+
name = Regexp.last_match(1)
|
|
1496
|
+
expr = Regexp.last_match(2).strip
|
|
1497
|
+
context[name] = eval_var_raw(expr, context)
|
|
1498
|
+
end
|
|
1499
|
+
end
|
|
1500
|
+
|
|
1501
|
+
# {% include "file.html" %}
|
|
1502
|
+
def handle_include(content, context)
|
|
1503
|
+
ignore_missing = content.include?("ignore missing")
|
|
1504
|
+
content = content.gsub("ignore missing", "").strip
|
|
1505
|
+
|
|
1506
|
+
m = content.match(INCLUDE_RE)
|
|
1507
|
+
return "" unless m
|
|
1508
|
+
|
|
1509
|
+
filename = m[1]
|
|
1510
|
+
with_expr = m[2]
|
|
1511
|
+
|
|
1512
|
+
begin
|
|
1513
|
+
source = load_template(filename)
|
|
1514
|
+
rescue
|
|
1515
|
+
return "" if ignore_missing
|
|
1516
|
+
raise
|
|
1517
|
+
end
|
|
1518
|
+
|
|
1519
|
+
inc_context = context.dup
|
|
1520
|
+
if with_expr
|
|
1521
|
+
extra = eval_expr(with_expr, context)
|
|
1522
|
+
inc_context.merge!(stringify_keys(extra)) if extra.is_a?(Hash)
|
|
1523
|
+
end
|
|
1524
|
+
|
|
1525
|
+
execute(source, inc_context)
|
|
1526
|
+
end
|
|
1527
|
+
|
|
1528
|
+
# {% macro name(args) %}...{% endmacro %}
|
|
1529
|
+
def handle_macro(tokens, start, context)
|
|
1530
|
+
content, _, _ = strip_tag(tokens[start][1])
|
|
1531
|
+
m = content.match(MACRO_RE)
|
|
1532
|
+
unless m
|
|
1533
|
+
i = start + 1
|
|
1534
|
+
while i < tokens.length
|
|
1535
|
+
if tokens[i][0] == BLOCK && tokens[i][1].include?("endmacro")
|
|
1536
|
+
return i + 1
|
|
1537
|
+
end
|
|
1538
|
+
i += 1
|
|
1539
|
+
end
|
|
1540
|
+
return i
|
|
1541
|
+
end
|
|
1542
|
+
|
|
1543
|
+
macro_name = m[1]
|
|
1544
|
+
param_names = m[2].split(",").map(&:strip).reject(&:empty?)
|
|
1545
|
+
|
|
1546
|
+
body_tokens = []
|
|
1547
|
+
i = start + 1
|
|
1548
|
+
while i < tokens.length
|
|
1549
|
+
if tokens[i][0] == BLOCK && tokens[i][1].include?("endmacro")
|
|
1550
|
+
i += 1
|
|
1551
|
+
break
|
|
1552
|
+
end
|
|
1553
|
+
body_tokens << tokens[i]
|
|
1554
|
+
i += 1
|
|
1555
|
+
end
|
|
1556
|
+
|
|
1557
|
+
engine = self
|
|
1558
|
+
captured_body = body_tokens.dup
|
|
1559
|
+
captured_context = context
|
|
1560
|
+
|
|
1561
|
+
context[macro_name] = lambda { |*args|
|
|
1562
|
+
macro_ctx = captured_context.dup
|
|
1563
|
+
param_names.each_with_index do |pname, pi|
|
|
1564
|
+
macro_ctx[pname] = pi < args.length ? args[pi] : nil
|
|
1565
|
+
end
|
|
1566
|
+
Tina4::SafeString.new(engine.send(:render_tokens, captured_body.dup, macro_ctx))
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
i
|
|
1570
|
+
end
|
|
1571
|
+
|
|
1572
|
+
# {% from "file" import macro1, macro2 %}
|
|
1573
|
+
def handle_from_import(content, context)
|
|
1574
|
+
m = content.match(FROM_IMPORT_RE)
|
|
1575
|
+
return unless m
|
|
1576
|
+
|
|
1577
|
+
filename = m[1]
|
|
1578
|
+
names = m[2].split(",").map(&:strip).reject(&:empty?)
|
|
1579
|
+
|
|
1580
|
+
source = load_template(filename)
|
|
1581
|
+
tokens = tokenize(source)
|
|
1582
|
+
|
|
1583
|
+
i = 0
|
|
1584
|
+
while i < tokens.length
|
|
1585
|
+
ttype, raw = tokens[i]
|
|
1586
|
+
if ttype == BLOCK
|
|
1587
|
+
tag_content, _, _ = strip_tag(raw)
|
|
1588
|
+
tag = (tag_content.split[0] || "")
|
|
1589
|
+
if tag == "macro"
|
|
1590
|
+
macro_m = tag_content.match(MACRO_RE)
|
|
1591
|
+
if macro_m && names.include?(macro_m[1])
|
|
1592
|
+
macro_name = macro_m[1]
|
|
1593
|
+
param_names = macro_m[2].split(",").map(&:strip).reject(&:empty?)
|
|
1594
|
+
|
|
1595
|
+
body_tokens = []
|
|
1596
|
+
i += 1
|
|
1597
|
+
while i < tokens.length
|
|
1598
|
+
if tokens[i][0] == BLOCK && tokens[i][1].include?("endmacro")
|
|
1599
|
+
i += 1
|
|
1600
|
+
break
|
|
1601
|
+
end
|
|
1602
|
+
body_tokens << tokens[i]
|
|
1603
|
+
i += 1
|
|
1604
|
+
end
|
|
1605
|
+
|
|
1606
|
+
context[macro_name] = _make_macro_fn(body_tokens.dup, param_names.dup, context.dup)
|
|
1607
|
+
next
|
|
1608
|
+
end
|
|
1609
|
+
end
|
|
1610
|
+
end
|
|
1611
|
+
i += 1
|
|
1612
|
+
end
|
|
1613
|
+
end
|
|
1614
|
+
|
|
1615
|
+
# Build an isolated lambda for a macro — avoids closure-in-loop variable sharing.
|
|
1616
|
+
def _make_macro_fn(body_tokens, param_names, ctx)
|
|
1617
|
+
engine = self
|
|
1618
|
+
lambda { |*args|
|
|
1619
|
+
macro_ctx = ctx.dup
|
|
1620
|
+
param_names.each_with_index do |pname, pi|
|
|
1621
|
+
macro_ctx[pname] = pi < args.length ? args[pi] : nil
|
|
1622
|
+
end
|
|
1623
|
+
Tina4::SafeString.new(engine.send(:render_tokens, body_tokens.dup, macro_ctx))
|
|
1624
|
+
}
|
|
1625
|
+
end
|
|
1626
|
+
|
|
1627
|
+
# {% cache "key" ttl %}...{% endcache %}
|
|
1628
|
+
def handle_cache(tokens, start, context)
|
|
1629
|
+
content, _, _ = strip_tag(tokens[start][1])
|
|
1630
|
+
m = content.match(CACHE_RE)
|
|
1631
|
+
cache_key = m ? m[1] : "default"
|
|
1632
|
+
ttl = m && m[2] ? m[2].to_i : 60
|
|
1633
|
+
|
|
1634
|
+
# Check cache
|
|
1635
|
+
cached = @fragment_cache[cache_key]
|
|
1636
|
+
if cached
|
|
1637
|
+
html_content, expires_at = cached
|
|
1638
|
+
if Time.now.to_f < expires_at
|
|
1639
|
+
# Skip to endcache
|
|
1640
|
+
i = start + 1
|
|
1641
|
+
depth = 0
|
|
1642
|
+
while i < tokens.length
|
|
1643
|
+
if tokens[i][0] == BLOCK
|
|
1644
|
+
tc, _, _ = strip_tag(tokens[i][1])
|
|
1645
|
+
tag = tc.split[0] || ""
|
|
1646
|
+
if tag == "cache"
|
|
1647
|
+
depth += 1
|
|
1648
|
+
elsif tag == "endcache"
|
|
1649
|
+
return [html_content, i + 1] if depth == 0
|
|
1650
|
+
depth -= 1
|
|
1651
|
+
end
|
|
1652
|
+
end
|
|
1653
|
+
i += 1
|
|
1654
|
+
end
|
|
1655
|
+
return [html_content, i]
|
|
1656
|
+
end
|
|
1657
|
+
end
|
|
1658
|
+
|
|
1659
|
+
body_tokens = []
|
|
1660
|
+
i = start + 1
|
|
1661
|
+
depth = 0
|
|
1662
|
+
while i < tokens.length
|
|
1663
|
+
if tokens[i][0] == BLOCK
|
|
1664
|
+
tc, _, _ = strip_tag(tokens[i][1])
|
|
1665
|
+
tag = tc.split[0] || ""
|
|
1666
|
+
if tag == "cache"
|
|
1667
|
+
depth += 1
|
|
1668
|
+
body_tokens << tokens[i]
|
|
1669
|
+
elsif tag == "endcache"
|
|
1670
|
+
if depth == 0
|
|
1671
|
+
i += 1
|
|
1672
|
+
break
|
|
1673
|
+
end
|
|
1674
|
+
depth -= 1
|
|
1675
|
+
body_tokens << tokens[i]
|
|
1676
|
+
else
|
|
1677
|
+
body_tokens << tokens[i]
|
|
1678
|
+
end
|
|
1679
|
+
else
|
|
1680
|
+
body_tokens << tokens[i]
|
|
1681
|
+
end
|
|
1682
|
+
i += 1
|
|
1683
|
+
end
|
|
1684
|
+
|
|
1685
|
+
rendered = render_tokens(body_tokens.dup, context)
|
|
1686
|
+
@fragment_cache[cache_key] = [rendered, Time.now.to_f + ttl]
|
|
1687
|
+
[rendered, i]
|
|
1688
|
+
end
|
|
1689
|
+
|
|
1690
|
+
def handle_spaceless(tokens, start, context)
|
|
1691
|
+
body_tokens = []
|
|
1692
|
+
i = start + 1
|
|
1693
|
+
depth = 0
|
|
1694
|
+
while i < tokens.length
|
|
1695
|
+
if tokens[i][0] == BLOCK
|
|
1696
|
+
tc, _, _ = strip_tag(tokens[i][1])
|
|
1697
|
+
tag = tc.split[0] || ""
|
|
1698
|
+
if tag == "spaceless"
|
|
1699
|
+
depth += 1
|
|
1700
|
+
body_tokens << tokens[i]
|
|
1701
|
+
elsif tag == "endspaceless"
|
|
1702
|
+
if depth == 0
|
|
1703
|
+
i += 1
|
|
1704
|
+
break
|
|
1705
|
+
end
|
|
1706
|
+
depth -= 1
|
|
1707
|
+
body_tokens << tokens[i]
|
|
1708
|
+
else
|
|
1709
|
+
body_tokens << tokens[i]
|
|
1710
|
+
end
|
|
1711
|
+
else
|
|
1712
|
+
body_tokens << tokens[i]
|
|
1713
|
+
end
|
|
1714
|
+
i += 1
|
|
1715
|
+
end
|
|
1716
|
+
|
|
1717
|
+
rendered = render_tokens(body_tokens.dup, context)
|
|
1718
|
+
rendered = rendered.gsub(SPACELESS_RE, "><")
|
|
1719
|
+
[rendered, i]
|
|
1720
|
+
end
|
|
1721
|
+
|
|
1722
|
+
def handle_autoescape(tokens, start, context)
|
|
1723
|
+
content, _, _ = strip_tag(tokens[start][1])
|
|
1724
|
+
mode_match = content.match(AUTOESCAPE_RE)
|
|
1725
|
+
auto_escape_on = !(mode_match && mode_match[1] == "false")
|
|
1726
|
+
|
|
1727
|
+
body_tokens = []
|
|
1728
|
+
i = start + 1
|
|
1729
|
+
depth = 0
|
|
1730
|
+
while i < tokens.length
|
|
1731
|
+
if tokens[i][0] == BLOCK
|
|
1732
|
+
tc, _, _ = strip_tag(tokens[i][1])
|
|
1733
|
+
tag = tc.split[0] || ""
|
|
1734
|
+
if tag == "autoescape"
|
|
1735
|
+
depth += 1
|
|
1736
|
+
body_tokens << tokens[i]
|
|
1737
|
+
elsif tag == "endautoescape"
|
|
1738
|
+
if depth == 0
|
|
1739
|
+
i += 1
|
|
1740
|
+
break
|
|
1741
|
+
end
|
|
1742
|
+
depth -= 1
|
|
1743
|
+
body_tokens << tokens[i]
|
|
1744
|
+
else
|
|
1745
|
+
body_tokens << tokens[i]
|
|
1746
|
+
end
|
|
1747
|
+
else
|
|
1748
|
+
body_tokens << tokens[i]
|
|
1749
|
+
end
|
|
1750
|
+
i += 1
|
|
1751
|
+
end
|
|
1752
|
+
|
|
1753
|
+
if !auto_escape_on
|
|
1754
|
+
old_auto_escape = @auto_escape
|
|
1755
|
+
@auto_escape = false
|
|
1756
|
+
rendered = render_tokens(body_tokens.dup, context)
|
|
1757
|
+
@auto_escape = old_auto_escape
|
|
1758
|
+
else
|
|
1759
|
+
rendered = render_tokens(body_tokens.dup, context)
|
|
1760
|
+
end
|
|
1761
|
+
|
|
1762
|
+
[rendered, i]
|
|
1763
|
+
end
|
|
1764
|
+
|
|
1765
|
+
# -----------------------------------------------------------------------
|
|
1766
|
+
# Helpers
|
|
1767
|
+
# -----------------------------------------------------------------------
|
|
1768
|
+
|
|
1769
|
+
def truthy?(val)
|
|
1770
|
+
return false if val.nil? || val == false || val == 0 || val == ""
|
|
1771
|
+
return false if val.respond_to?(:empty?) && val.empty?
|
|
1772
|
+
true
|
|
1773
|
+
end
|
|
1774
|
+
|
|
1775
|
+
def stringify_keys(hash)
|
|
1776
|
+
return {} unless hash.is_a?(Hash)
|
|
1777
|
+
hash.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
|
|
1778
|
+
end
|
|
1779
|
+
|
|
1780
|
+
# -----------------------------------------------------------------------
|
|
1781
|
+
# Built-in filters (53 total)
|
|
1782
|
+
# -----------------------------------------------------------------------
|
|
1783
|
+
|
|
1784
|
+
def default_filters
|
|
1785
|
+
{
|
|
1786
|
+
# -- Text --
|
|
1787
|
+
"upper" => ->(v, *_a) { v.to_s.upcase },
|
|
1788
|
+
"lower" => ->(v, *_a) { v.to_s.downcase },
|
|
1789
|
+
"capitalize" => ->(v, *_a) { v.to_s.capitalize },
|
|
1790
|
+
"title" => ->(v, *_a) { v.to_s.split.map(&:capitalize).join(" ") },
|
|
1791
|
+
"trim" => ->(v, *_a) { v.to_s.strip },
|
|
1792
|
+
"ltrim" => ->(v, *_a) { v.to_s.lstrip },
|
|
1793
|
+
"rtrim" => ->(v, *_a) { v.to_s.rstrip },
|
|
1794
|
+
"replace" => ->(v, *a) {
|
|
1795
|
+
if a.length == 1 && a[0].is_a?(Hash)
|
|
1796
|
+
result = v.to_s
|
|
1797
|
+
a[0].each { |old, new_val| result = result.gsub(old.to_s, new_val.to_s) }
|
|
1798
|
+
result
|
|
1799
|
+
elsif a.length >= 2
|
|
1800
|
+
v.to_s.gsub(a[0].to_s, a[1].to_s)
|
|
1801
|
+
else
|
|
1802
|
+
v.to_s
|
|
1803
|
+
end
|
|
1804
|
+
},
|
|
1805
|
+
"striptags" => ->(v, *_a) { v.to_s.gsub(STRIPTAGS_RE, "") },
|
|
1806
|
+
|
|
1807
|
+
# -- Encoding --
|
|
1808
|
+
"escape" => ->(v, *_a) { Frond.escape_html(v.to_s) },
|
|
1809
|
+
"e" => ->(v, *_a) { Frond.escape_html(v.to_s) },
|
|
1810
|
+
"raw" => ->(v, *_a) { v },
|
|
1811
|
+
"safe" => ->(v, *_a) { v },
|
|
1812
|
+
"json_encode" => ->(v, *_a) { JSON.generate(v) rescue v.to_s },
|
|
1813
|
+
"json_decode" => ->(v, *_a) { v.is_a?(String) ? (JSON.parse(v) rescue v) : v },
|
|
1814
|
+
"base64_encode" => ->(v, *_a) { Base64.strict_encode64(v.is_a?(String) ? v : v.to_s) },
|
|
1815
|
+
"base64encode" => ->(v, *_a) { Base64.strict_encode64(v.is_a?(String) ? v : v.to_s) },
|
|
1816
|
+
"base64_decode" => ->(v, *_a) { Base64.decode64(v.to_s) },
|
|
1817
|
+
"base64decode" => ->(v, *_a) { Base64.decode64(v.to_s) },
|
|
1818
|
+
"data_uri" => ->(v, *_a) {
|
|
1819
|
+
if v.is_a?(Hash)
|
|
1820
|
+
ct = v[:type] || v["type"] || "application/octet-stream"
|
|
1821
|
+
raw = v[:content] || v["content"] || ""
|
|
1822
|
+
raw = raw.respond_to?(:read) ? raw.read : raw
|
|
1823
|
+
"data:#{ct};base64,#{Base64.strict_encode64(raw.to_s)}"
|
|
1824
|
+
else
|
|
1825
|
+
v.to_s
|
|
1826
|
+
end
|
|
1827
|
+
},
|
|
1828
|
+
"url_encode" => ->(v, *_a) { CGI.escape(v.to_s) },
|
|
1829
|
+
|
|
1830
|
+
# -- JSON / JS --
|
|
1831
|
+
"to_json" => ->(v, *a) {
|
|
1832
|
+
indent = a[0] ? a[0].to_i : nil
|
|
1833
|
+
json = indent ? JSON.pretty_generate(v) : JSON.generate(v)
|
|
1834
|
+
# Escape <, >, & for safe HTML embedding
|
|
1835
|
+
Tina4::SafeString.new(json.gsub("<", '\u003c').gsub(">", '\u003e').gsub("&", '\u0026'))
|
|
1836
|
+
},
|
|
1837
|
+
"tojson" => ->(v, *a) {
|
|
1838
|
+
indent = a[0] ? a[0].to_i : nil
|
|
1839
|
+
json = indent ? JSON.pretty_generate(v) : JSON.generate(v)
|
|
1840
|
+
Tina4::SafeString.new(json.gsub("<", '\u003c').gsub(">", '\u003e').gsub("&", '\u0026'))
|
|
1841
|
+
},
|
|
1842
|
+
"js_escape" => ->(v, *_a) {
|
|
1843
|
+
Tina4::SafeString.new(
|
|
1844
|
+
v.to_s.gsub("\\", "\\\\").gsub("'", "\\'").gsub('"', '\\"')
|
|
1845
|
+
.gsub("\n", "\\n").gsub("\r", "\\r").gsub("\t", "\\t")
|
|
1846
|
+
)
|
|
1847
|
+
},
|
|
1848
|
+
|
|
1849
|
+
# -- Hashing --
|
|
1850
|
+
"md5" => ->(v, *_a) { Digest::MD5.hexdigest(v.to_s) },
|
|
1851
|
+
"sha256" => ->(v, *_a) { Digest::SHA256.hexdigest(v.to_s) },
|
|
1852
|
+
|
|
1853
|
+
# -- Numbers --
|
|
1854
|
+
"abs" => ->(v, *_a) { v.is_a?(Numeric) ? v.abs : v.to_f.abs },
|
|
1855
|
+
"round" => ->(v, *a) { v.to_f.round(a[0] ? a[0].to_i : 0) },
|
|
1856
|
+
"int" => ->(v, *_a) { v.to_i },
|
|
1857
|
+
"float" => ->(v, *_a) { v.to_f },
|
|
1858
|
+
"number_format" => ->(v, *a) {
|
|
1859
|
+
decimals = a[0] ? a[0].to_i : 0
|
|
1860
|
+
formatted = format("%.#{decimals}f", v.to_f)
|
|
1861
|
+
# Add comma thousands separator
|
|
1862
|
+
parts = formatted.split(".")
|
|
1863
|
+
parts[0] = parts[0].gsub(THOUSANDS_RE, '\\1,')
|
|
1864
|
+
parts.join(".")
|
|
1865
|
+
},
|
|
1866
|
+
|
|
1867
|
+
# -- Date --
|
|
1868
|
+
"date" => ->(v, *a) {
|
|
1869
|
+
fmt = a[0] || "%Y-%m-%d"
|
|
1870
|
+
begin
|
|
1871
|
+
if v.is_a?(String)
|
|
1872
|
+
dt = DateTime.parse(v)
|
|
1873
|
+
dt.strftime(fmt)
|
|
1874
|
+
elsif v.respond_to?(:strftime)
|
|
1875
|
+
v.strftime(fmt)
|
|
1876
|
+
else
|
|
1877
|
+
v.to_s
|
|
1878
|
+
end
|
|
1879
|
+
rescue
|
|
1880
|
+
v.to_s
|
|
1881
|
+
end
|
|
1882
|
+
},
|
|
1883
|
+
|
|
1884
|
+
# -- Arrays --
|
|
1885
|
+
"length" => ->(v, *_a) { v.respond_to?(:length) ? v.length : v.to_s.length },
|
|
1886
|
+
"first" => ->(v, *_a) { v.respond_to?(:first) ? v.first : (v.to_s[0] rescue nil) },
|
|
1887
|
+
"last" => ->(v, *_a) { v.respond_to?(:last) ? v.last : (v.to_s[-1] rescue nil) },
|
|
1888
|
+
"reverse" => ->(v, *_a) { v.respond_to?(:reverse) ? v.reverse : v.to_s.reverse },
|
|
1889
|
+
"sort" => ->(v, *_a) { v.respond_to?(:sort) ? v.sort : v },
|
|
1890
|
+
"shuffle" => ->(v, *_a) { v.respond_to?(:shuffle) ? v.shuffle : v },
|
|
1891
|
+
"unique" => ->(v, *_a) { v.is_a?(Array) ? v.uniq : v },
|
|
1892
|
+
"join" => ->(v, *a) { v.respond_to?(:join) ? v.join(a[0] || ", ") : v.to_s },
|
|
1893
|
+
"split" => ->(v, *a) { v.to_s.split(a[0] || " ") },
|
|
1894
|
+
"slice" => ->(v, *a) {
|
|
1895
|
+
if a.length >= 2
|
|
1896
|
+
s = a[0].to_i
|
|
1897
|
+
e = a[1].to_i
|
|
1898
|
+
if v.is_a?(Array)
|
|
1899
|
+
v[s...e]
|
|
1900
|
+
else
|
|
1901
|
+
v.to_s[s...e]
|
|
1902
|
+
end
|
|
1903
|
+
else
|
|
1904
|
+
v
|
|
1905
|
+
end
|
|
1906
|
+
},
|
|
1907
|
+
"batch" => ->(v, *a) {
|
|
1908
|
+
if a[0] && v.respond_to?(:each_slice)
|
|
1909
|
+
v.each_slice(a[0].to_i).to_a
|
|
1910
|
+
else
|
|
1911
|
+
[v]
|
|
1912
|
+
end
|
|
1913
|
+
},
|
|
1914
|
+
"map" => ->(v, *a) {
|
|
1915
|
+
if a[0] && v.is_a?(Array)
|
|
1916
|
+
v.map { |item| item.is_a?(Hash) ? (item[a[0]] || item[a[0].to_sym]) : nil }
|
|
1917
|
+
else
|
|
1918
|
+
v
|
|
1919
|
+
end
|
|
1920
|
+
},
|
|
1921
|
+
"filter" => ->(v, *_a) { v.is_a?(Array) ? v.select { |item| item } : v },
|
|
1922
|
+
"column" => ->(v, *a) {
|
|
1923
|
+
if a[0] && v.is_a?(Array)
|
|
1924
|
+
v.map { |row| row.is_a?(Hash) ? (row[a[0]] || row[a[0].to_sym]) : nil }
|
|
1925
|
+
else
|
|
1926
|
+
v
|
|
1927
|
+
end
|
|
1928
|
+
},
|
|
1929
|
+
|
|
1930
|
+
# -- Dict --
|
|
1931
|
+
"keys" => ->(v, *_a) { v.respond_to?(:keys) ? v.keys : [] },
|
|
1932
|
+
"values" => ->(v, *_a) { v.respond_to?(:values) ? v.values : [v] },
|
|
1933
|
+
"merge" => ->(v, *a) {
|
|
1934
|
+
if v.respond_to?(:merge) && a[0].is_a?(Hash)
|
|
1935
|
+
v.merge(a[0])
|
|
1936
|
+
elsif v.is_a?(Array) && a[0].is_a?(Array)
|
|
1937
|
+
v + a[0]
|
|
1938
|
+
else
|
|
1939
|
+
v
|
|
1940
|
+
end
|
|
1941
|
+
},
|
|
1942
|
+
|
|
1943
|
+
# -- Utility --
|
|
1944
|
+
"default" => ->(v, *a) { (v.nil? || v.to_s.empty?) ? (a[0] || "") : v },
|
|
1945
|
+
# dump filter — gated on TINA4_DEBUG=true via Frond.render_dump.
|
|
1946
|
+
# Both the |dump filter and the dump() global delegate to the same
|
|
1947
|
+
# helper so they produce identical output and obey the same gating.
|
|
1948
|
+
"dump" => ->(v, *_a) { Frond.render_dump(v) },
|
|
1949
|
+
"string" => ->(v, *_a) { v.to_s },
|
|
1950
|
+
"truncate" => ->(v, *a) {
|
|
1951
|
+
len = a[0] ? a[0].to_i : 50
|
|
1952
|
+
str = v.to_s
|
|
1953
|
+
str.length > len ? str[0...len] + "..." : str
|
|
1954
|
+
},
|
|
1955
|
+
"wordwrap" => ->(v, *a) {
|
|
1956
|
+
width = a[0] ? a[0].to_i : 75
|
|
1957
|
+
words = v.to_s.split
|
|
1958
|
+
lines = []
|
|
1959
|
+
current = +""
|
|
1960
|
+
words.each do |word|
|
|
1961
|
+
if !current.empty? && current.length + 1 + word.length > width
|
|
1962
|
+
lines << current
|
|
1963
|
+
current = word
|
|
1964
|
+
else
|
|
1965
|
+
current = current.empty? ? word : "#{current} #{word}"
|
|
1966
|
+
end
|
|
1967
|
+
end
|
|
1968
|
+
lines << current unless current.empty?
|
|
1969
|
+
lines.join("\n")
|
|
1970
|
+
},
|
|
1971
|
+
"slug" => ->(v, *_a) { v.to_s.downcase.gsub(SLUG_CLEAN_RE, "-").gsub(SLUG_TRIM_RE, "") },
|
|
1972
|
+
"nl2br" => ->(v, *_a) { v.to_s.gsub("\n", "<br>\n") },
|
|
1973
|
+
"format" => ->(v, *a) {
|
|
1974
|
+
if a.any?
|
|
1975
|
+
v.to_s % a
|
|
1976
|
+
else
|
|
1977
|
+
v.to_s
|
|
1978
|
+
end
|
|
1979
|
+
},
|
|
1980
|
+
"form_token" => ->(_v, *_a) { Frond.generate_form_token(_v.to_s) },
|
|
1981
|
+
}
|
|
1982
|
+
end
|
|
1983
|
+
|
|
1984
|
+
# -----------------------------------------------------------------------
|
|
1985
|
+
# Built-in globals
|
|
1986
|
+
# -----------------------------------------------------------------------
|
|
1987
|
+
|
|
1988
|
+
def register_builtin_globals
|
|
1989
|
+
@globals["form_token"] = ->(descriptor = "") { Frond.generate_form_token(descriptor.to_s) }
|
|
1990
|
+
@globals["formTokenValue"] = ->(descriptor = "") { Frond.generate_form_token_value(descriptor.to_s) }
|
|
1991
|
+
@globals["form_token_value"] = ->(descriptor = "") { Frond.generate_form_token_value(descriptor.to_s) }
|
|
1992
|
+
|
|
1993
|
+
# Debug helper: {{ dump(x) }} — gated on TINA4_DEBUG=true.
|
|
1994
|
+
# Both this global and the |dump filter call Frond.render_dump which
|
|
1995
|
+
# returns an empty SafeString in production so dump never leaks state.
|
|
1996
|
+
@globals["dump"] = ->(value = nil) { Frond.render_dump(value) }
|
|
1997
|
+
end
|
|
1998
|
+
|
|
1999
|
+
# Render a value as a pre-formatted inspect() wrapped in <pre> tags.
|
|
2000
|
+
#
|
|
2001
|
+
# Gated on TINA4_DEBUG=true. In production (TINA4_DEBUG unset or false)
|
|
2002
|
+
# this returns an empty SafeString to avoid leaking internal state,
|
|
2003
|
+
# object shapes, or sensitive values into rendered HTML.
|
|
2004
|
+
#
|
|
2005
|
+
# Shared by the {{ value|dump }} filter and the {{ dump(value) }}
|
|
2006
|
+
# global function so both produce identical output and obey the same
|
|
2007
|
+
# gating.
|
|
2008
|
+
def self.render_dump(value)
|
|
2009
|
+
return SafeString.new("") unless ENV.fetch("TINA4_DEBUG", "").downcase == "true"
|
|
2010
|
+
|
|
2011
|
+
dumped = value.inspect
|
|
2012
|
+
escaped = dumped
|
|
2013
|
+
.gsub("&", "&")
|
|
2014
|
+
.gsub("<", "<")
|
|
2015
|
+
.gsub(">", ">")
|
|
2016
|
+
.gsub('"', """)
|
|
2017
|
+
SafeString.new("<pre>#{escaped}</pre>")
|
|
2018
|
+
end
|
|
2019
|
+
|
|
2020
|
+
# Generate a JWT form token and return a hidden input element.
|
|
2021
|
+
#
|
|
2022
|
+
# @param descriptor [String] Optional string to enrich the token payload.
|
|
2023
|
+
# - Empty: payload is {"type" => "form"}
|
|
2024
|
+
# - "admin_panel": payload is {"type" => "form", "context" => "admin_panel"}
|
|
2025
|
+
# - "checkout|order_123": payload is {"type" => "form", "context" => "checkout", "ref" => "order_123"}
|
|
2026
|
+
#
|
|
2027
|
+
# @return [String] <input type="hidden" name="formToken" value="TOKEN">
|
|
2028
|
+
# Session ID used by generate_form_token for CSRF session binding.
|
|
2029
|
+
# Set this before rendering templates to bind tokens to the current session.
|
|
2030
|
+
@form_token_session_id = ""
|
|
2031
|
+
|
|
2032
|
+
class << self
|
|
2033
|
+
attr_accessor :form_token_session_id
|
|
2034
|
+
|
|
2035
|
+
# Set the session ID used for CSRF form token binding.
|
|
2036
|
+
# Parity with Python/PHP/Node: Frond.set_form_token_session_id(id)
|
|
2037
|
+
#
|
|
2038
|
+
# @param session_id [String] The session ID to bind form tokens to
|
|
2039
|
+
def set_form_token_session_id(session_id)
|
|
2040
|
+
self.form_token_session_id = session_id
|
|
2041
|
+
end
|
|
2042
|
+
end
|
|
2043
|
+
|
|
2044
|
+
# Generate a raw JWT form token string.
|
|
2045
|
+
#
|
|
2046
|
+
# @param descriptor [String] Optional string to enrich the token payload.
|
|
2047
|
+
# - Empty: payload is {"type" => "form"}
|
|
2048
|
+
# - "admin_panel": payload is {"type" => "form", "context" => "admin_panel"}
|
|
2049
|
+
# - "checkout|order_123": payload is {"type" => "form", "context" => "checkout", "ref" => "order_123"}
|
|
2050
|
+
#
|
|
2051
|
+
# @return [String] The raw JWT token string.
|
|
2052
|
+
def self.generate_form_jwt(descriptor = "")
|
|
2053
|
+
require_relative "log"
|
|
2054
|
+
require_relative "auth"
|
|
2055
|
+
|
|
2056
|
+
payload = { "type" => "form", "nonce" => SecureRandom.hex(8) }
|
|
2057
|
+
if descriptor && !descriptor.empty?
|
|
2058
|
+
if descriptor.include?("|")
|
|
2059
|
+
parts = descriptor.split("|", 2)
|
|
2060
|
+
payload["context"] = parts[0]
|
|
2061
|
+
payload["ref"] = parts[1]
|
|
2062
|
+
else
|
|
2063
|
+
payload["context"] = descriptor
|
|
2064
|
+
end
|
|
2065
|
+
end
|
|
2066
|
+
|
|
2067
|
+
# Include session_id for CSRF session binding
|
|
2068
|
+
sid = form_token_session_id.to_s
|
|
2069
|
+
payload["session_id"] = sid unless sid.empty?
|
|
2070
|
+
|
|
2071
|
+
ttl_minutes = (ENV["TINA4_TOKEN_LIMIT"] || "60").to_i
|
|
2072
|
+
expires_in = ttl_minutes * 60
|
|
2073
|
+
Tina4::Auth.create_token(payload, expires_in: expires_in)
|
|
2074
|
+
end
|
|
2075
|
+
|
|
2076
|
+
def self.generate_form_token(descriptor = "")
|
|
2077
|
+
token = generate_form_jwt(descriptor)
|
|
2078
|
+
Tina4::SafeString.new(%(<input type="hidden" name="formToken" value="#{CGI.escapeHTML(token)}">))
|
|
2079
|
+
end
|
|
2080
|
+
|
|
2081
|
+
# Return just the raw JWT form token string (no <input> wrapper).
|
|
2082
|
+
# Registered as both formTokenValue and form_token_value template globals.
|
|
2083
|
+
def self.generate_form_token_value(descriptor = "")
|
|
2084
|
+
Tina4::SafeString.new(generate_form_jwt(descriptor))
|
|
2085
|
+
end
|
|
2086
|
+
end
|
|
2087
|
+
end
|