mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
feat: Added mustache templating.
This commit is contained in:
@@ -1,8 +1,4 @@
|
|||||||
- [ ] add content-hash fingerprinting for tailwind cache busting.
|
- [ ] add content-hash fingerprinting for tailwind cache busting.
|
||||||
- [ ] create template language
|
|
||||||
- Look at the source for 3 template languages that support mustache syntax,
|
|
||||||
and see how they implement the tempaltes, then pick the best one. One of them
|
|
||||||
should be Hugo / go.
|
|
||||||
- [ ] Evaluate Tufte CSS — borrow sidenote CSS or replace TailwindCSS entirely
|
- [ ] Evaluate Tufte CSS — borrow sidenote CSS or replace TailwindCSS entirely
|
||||||
- Option B: Steal Tufte's sidenote/margin-note CSS (adapt for dark theme), keep TailwindCSS
|
- Option B: Steal Tufte's sidenote/margin-note CSS (adapt for dark theme), keep TailwindCSS
|
||||||
- Option C: Full Tufte CSS — drop TailwindCSS, no build step, semantic HTML, customize for dark theme + Roboto
|
- Option C: Full Tufte CSS — drop TailwindCSS, no build step, semantic HTML, customize for dark theme + Roboto
|
||||||
@@ -16,3 +12,4 @@
|
|||||||
- [ ] Content-hash fingerprinting for CSS and JS cache busting
|
- [ ] Content-hash fingerprinting for CSS and JS cache busting
|
||||||
- [ ] OpenGraph meta tags
|
- [ ] OpenGraph meta tags
|
||||||
- [ ] Search up for `thor.json` files.
|
- [ ] Search up for `thor.json` files.
|
||||||
|
- [ ] Partials inside sections still produce duplicate items — a fundamental issue with the mustache library's token handling.
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ Site_Config :: struct {
|
|||||||
base_url: string `args:"name=base-url"`,
|
base_url: string `args:"name=base-url"`,
|
||||||
content_dir: string `args:"name=content"`,
|
content_dir: string `args:"name=content"`,
|
||||||
output_dir: string `args:"name=output"`,
|
output_dir: string `args:"name=output"`,
|
||||||
|
layouts_dir: string,
|
||||||
author: string,
|
author: string,
|
||||||
social: []Social_Link,
|
social: []Social_Link,
|
||||||
drafts: bool `args:"name=drafts"`,
|
drafts: bool `args:"name=drafts"`,
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ main :: proc() {
|
|||||||
if config.output_dir == "" {
|
if config.output_dir == "" {
|
||||||
config.output_dir = fmt.tprintf("%s/public", config_dir)
|
config.output_dir = fmt.tprintf("%s/public", config_dir)
|
||||||
}
|
}
|
||||||
|
if config.layouts_dir == "" {
|
||||||
|
config.layouts_dir = fmt.tprintf("%s/layouts", config_dir)
|
||||||
|
}
|
||||||
if config.base_url == "" {
|
if config.base_url == "" {
|
||||||
config.base_url = "http://localhost:8080"
|
config.base_url = "http://localhost:8080"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
*/**/.DS_Store
|
||||||
|
.DS_Store
|
||||||
|
tmp/
|
||||||
|
bin/
|
||||||
|
*/**/odin-mustache.dSYM/
|
||||||
|
*/**/odin-mustache
|
||||||
|
*/**/odin-mustache-test.dSYM/
|
||||||
|
*/**/odin-mustache-test
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
[submodule "spec"]
|
||||||
|
path = spec
|
||||||
|
url = git@github.com:mustache/spec.git
|
||||||
|
branch = v1.4.1
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
flags := "-vet -show-timings -strict-style -vet-cast -vet-tabs -vet-using-param -disallow-do -vet-semicolon"
|
||||||
|
name := "odin-mustache"
|
||||||
|
|
||||||
|
build:
|
||||||
|
@mkdir -p bin
|
||||||
|
odin build . -out:bin/{{name}} -debug {{flags}}
|
||||||
|
|
||||||
|
test: build
|
||||||
|
odin test . -out:bin/{{name}}
|
||||||
|
|
||||||
|
run: build
|
||||||
|
bin/{{name}} test/template.txt test/data.json test/layout.txt
|
||||||
|
|
||||||
|
check:
|
||||||
|
odin check . {{flags}}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2025 Benjamin Block
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
# odin-mustache
|
||||||
|
Native implementation of {{mustache}} templates in [Odin](https://odin-lang.org).
|
||||||
|
|
||||||
|
https://github.com/benjamindblock/odin-mustache/assets/1155805/7d794861-e308-4132-aaa0-f800392208a4
|
||||||
|
|
||||||
|
All features are implemented, except for the ability to change delimiters.
|
||||||
|
|
||||||
|
All in tests in the [official mustache spec](https://github.com/mustache/spec) pass successfully (except for the delimiters spec suite).
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
For more information about mustache, see the [mustache project page](https://mustache.github.io) or the mustache [man](https://mustache.github.io/mustache.5.html) [pages](https://mustache.github.io/mustache.1.html).
|
||||||
|
|
||||||
|
View some [example mustache files](https://github.com/mustache/mustache/tree/master/examples) to get an overview.
|
||||||
|
|
||||||
|
## Spec
|
||||||
|
|
||||||
|
The [mustache-spec](https://github.com/mustache/spec) repo is added as a git submodule for testing purposes. To run tests, ensure that you run `git clone --recursive` and/or `git submodule update` as needed.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### CLI Usage
|
||||||
|
```
|
||||||
|
Usage:
|
||||||
|
odin-mustache [path to template] [path to JSON] [OPTIONAL - layout]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
$ odin-mustache template.txt data.json
|
||||||
|
```
|
||||||
|
|
||||||
|
### Odin Usage
|
||||||
|
#### 1. `render(template: string, data: any, partials: any, allocator := context.allocator)`
|
||||||
|
|
||||||
|
Renders a template, provided as a `string`. `data` and `partials` should be *either* a `map[string]...` or a `struct`.
|
||||||
|
|
||||||
|
All `map` arguments passed **must** be keyed with `string` type data. When parsing a Mustache template, the text inside a tag (eg. `name` inside `{{name}}`) will be parsed as a `string`.
|
||||||
|
|
||||||
|
#### 2. `render_from_filename(filename: string, data: any, partials: any, allocator := context.allocator)`
|
||||||
|
|
||||||
|
Renders a template stored in a text file using `data` and `partials` provided.
|
||||||
|
|
||||||
|
#### 3. `render_with_json(template: string, json_filename: string, allocator := context.allocator)`
|
||||||
|
|
||||||
|
Renders a template `string` using data and partials stored inside a JSON file. `odin-mustache` will handle loading the JSON into a usable format for Mustache to work with.
|
||||||
|
|
||||||
|
**NOTE**: The JSON file leverages the following top-level keys:
|
||||||
|
```
|
||||||
|
"data": [required]
|
||||||
|
"partials": [optional]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4. `render_from_filename_with_json(filename: string, json_filename: string, allocator := context.allocator)`
|
||||||
|
|
||||||
|
Renders a template stored in a text file using data and partials stored inside a JSON file. `odin-mustache` will handle loading the JSON into a usable format for Mustache to work with.
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
```odin
|
||||||
|
input := "Hello, {{name}}!"
|
||||||
|
data: map[string]string = {
|
||||||
|
"name" = "St. Charles",
|
||||||
|
}
|
||||||
|
output, err := render(input, data, context.temp_allocator)
|
||||||
|
// => "Hello, St. Charles!"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Escaping
|
||||||
|
`odin-mustache` follows the official mustache HTML escaping rules. That is, if you enclose a variable with two curly brackets, `{{var}}`, the contents are HTML-escaped. For instance, strings like `5 > 2` are converted to `5 > 2`. To use raw characters, use three curly brackets `{{{var}}}`.
|
||||||
|
|
||||||
|
## Layouts
|
||||||
|
`odin-mustache` supports rendering templates with layouts.
|
||||||
|
|
||||||
|
A layout is a regular template with a special function. It accepts **a `{{content}}` tag**. This is where the output of a child template will be inserted. Layouts have access to the same data provided to the regular template.
|
||||||
|
|
||||||
|
Layouts can render content in both `{{normal}}` and `{{{literal}}}` tags.
|
||||||
|
|
||||||
|
This is helpful for rendering scenarios like websites where the same elements (`<head>`, `<footer>`, etc.) are shared across all pages, each of which has their own page-specific template.
|
||||||
|
|
||||||
|
```
|
||||||
|
<html>
|
||||||
|
{{{head}}
|
||||||
|
<body>
|
||||||
|
{{{content}}}
|
||||||
|
</body>
|
||||||
|
{{{footer}}
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
### CLI Usage
|
||||||
|
```
|
||||||
|
Usage:
|
||||||
|
odin-mustache [path to template] [path to JSON] [OPTIONAL - layout]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
$ odin-mustache template.html data.json layout.html
|
||||||
|
```
|
||||||
|
|
||||||
|
### Odin Usage
|
||||||
|
To render a layout in Odin, all four of the above render procedures have `in_layout` and `in_layout_file` variations. These methods will insert the rendered content of a template within the given layout. This is convenient for rendering HTML views inside a larger layout, amongst other use-cases.
|
||||||
|
|
||||||
|
The full list of corresponding methods is:
|
||||||
|
- `render_in_layout(template: string, data: any, layout: string, partials: any, allocator := context.allocator)`
|
||||||
|
- `render_in_layout_file(template: string, data: any, layout_filename: string, partials: any, allocator := context.allocator)`
|
||||||
|
- `render_from_filename_in_layout(filename: string, data: any, layout: string, partials: any, allocator := context.allocator)`
|
||||||
|
- `render_from_filename_in_layout_file(filename: string, data: any, layout_filename: string, partials: any, allocator := context.allocator)`
|
||||||
|
- `render_with_json_in_layout(template: string, json_filename: string, layout: string, allocator := context.allocator)`
|
||||||
|
- `render_with_json_in_layout_file(template: string, json_filename: string, layout_filename: string, allocator := context.allocator)`
|
||||||
|
- `render_from_filename_with_json_in_layout(filename: string, json_filename: string, layout: string, allocator := context.allocator)`
|
||||||
|
- `render_from_filename_with_json_in_layout_file(filename: string, json_filename: string, layout_filename: string, allocator := context.allocator)`
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
```odin
|
||||||
|
template := "Hello, {{name}}."
|
||||||
|
data := map[string]string{"name" = "Kilgarvan"}
|
||||||
|
layout := `Above >>
|
||||||
|
{{content}}
|
||||||
|
<< Below`
|
||||||
|
|
||||||
|
output, _ := render_in_layout(template, data, layout, allocator := context.temp_allocator)
|
||||||
|
fmt.println(output)
|
||||||
|
|
||||||
|
// Above >>
|
||||||
|
// Hello, Kilgarvan.
|
||||||
|
// << Below
|
||||||
|
```
|
||||||
|
|
||||||
|
## Precompiled Templates
|
||||||
|
`odin-mustache` works in two steps:
|
||||||
|
1. Lexing and parsing
|
||||||
|
2. Rendering with data
|
||||||
|
|
||||||
|
If you are rendering the same template multiple times (ex: sending out personalized emails to subscribers), the lexing+parsing step can be performed once to create a compiled template. This compiled template can then be used multiple times with different data.
|
||||||
|
|
||||||
|
### Example
|
||||||
|
```odin
|
||||||
|
src := "Hello, {{name}}!"
|
||||||
|
template: Template
|
||||||
|
output: string
|
||||||
|
data: map[string]string
|
||||||
|
partials: map[string]string
|
||||||
|
|
||||||
|
lexer := Lexer{src=src, delim=CORE_DEF}
|
||||||
|
lexer_parse(&lexer)
|
||||||
|
|
||||||
|
data = {"name" = "St. Charles"}
|
||||||
|
template = Template{lexer=lexer, data=data, partials=partials}
|
||||||
|
output, _ = template_render(&template)
|
||||||
|
// => Hello, St. Charles!
|
||||||
|
|
||||||
|
data = {"name" = "Edouard"}
|
||||||
|
template = Template{lexer=lexer, data=data, partials=partials}
|
||||||
|
output, _ = template_render(&template)
|
||||||
|
// => Hello, Edouard!
|
||||||
|
```
|
||||||
|
|
||||||
|
## Future Work
|
||||||
|
- Validate JSON keys
|
||||||
|
- Better CLI argument parsing
|
||||||
|
- Improve error handling and reporting
|
||||||
|
- Add optional logging for debugging and performance work
|
||||||
|
- Configurable precision for floating point types
|
||||||
|
- Add support for changing delimiters
|
||||||
|
- Special loop conditionals (eg., checking for the first iteration or last iteration)
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
Hello, {{name}}!
|
||||||
|
|
||||||
|
Languages you know:
|
||||||
|
{{#languages}}
|
||||||
|
- {{.}}
|
||||||
|
{{/languages}}
|
||||||
|
|
||||||
|
{{#show_footer}}
|
||||||
|
---
|
||||||
|
Rendered with odin-mustache.
|
||||||
|
{{/show_footer}}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:os"
|
||||||
|
|
||||||
|
import mustache "../.."
|
||||||
|
|
||||||
|
Person :: struct {
|
||||||
|
name: string,
|
||||||
|
languages: []string,
|
||||||
|
show_footer: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
main :: proc() {
|
||||||
|
data := Person{
|
||||||
|
name = "World",
|
||||||
|
languages = {"Odin", "C", "Rust"},
|
||||||
|
show_footer = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := mustache.render_from_filename("hello.mustache", data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.eprintfln("render error: %v", err)
|
||||||
|
os.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.print(result)
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,960 @@
|
|||||||
|
#+feature dynamic-literals
|
||||||
|
package mustache
|
||||||
|
|
||||||
|
import "core:encoding/json"
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:os"
|
||||||
|
import "core:reflect"
|
||||||
|
import "core:slice"
|
||||||
|
import "core:testing"
|
||||||
|
|
||||||
|
COMMENTS_SPEC :: "mustache/spec/specs/comments.json"
|
||||||
|
DELIMITERS_SPEC :: "mustache/spec/specs/delimiters.json"
|
||||||
|
INTERPOLATION_SPEC :: "mustache/spec/specs/interpolation.json"
|
||||||
|
INVERTED_SPEC :: "mustache/spec/specs/inverted.json"
|
||||||
|
PARTIALS_SPEC :: "mustache/spec/specs/partials.json"
|
||||||
|
SECTIONS_SPEC :: "mustache/spec/specs/sections.json"
|
||||||
|
|
||||||
|
Test_Struct :: struct {
|
||||||
|
name: string,
|
||||||
|
email: string,
|
||||||
|
}
|
||||||
|
Test_Map :: map[string]string
|
||||||
|
Test_List :: [dynamic]string
|
||||||
|
Test_Data :: union {
|
||||||
|
Test_Struct,
|
||||||
|
Test_Map,
|
||||||
|
Test_List,
|
||||||
|
string,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. A map from string => JSON_Data
|
||||||
|
// 2. A list of JSON_Data
|
||||||
|
// 3. A value of some kind (string, int, etc.)
|
||||||
|
JSON_Map :: distinct map[string]JSON_Data
|
||||||
|
JSON_List :: distinct [dynamic]JSON_Data
|
||||||
|
JSON_Data :: union {
|
||||||
|
JSON_Map,
|
||||||
|
JSON_List,
|
||||||
|
string,
|
||||||
|
}
|
||||||
|
|
||||||
|
load_json :: proc(val: json.Value) -> (loaded: JSON_Data) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
switch _val in val {
|
||||||
|
case bool, string:
|
||||||
|
v := fmt.tprintf("%v", _val)
|
||||||
|
loaded = v
|
||||||
|
case i64, f64:
|
||||||
|
str := fmt.tprintf("%.2f", val)
|
||||||
|
decimal_str := trim_decimal_string(str)
|
||||||
|
loaded = decimal_str
|
||||||
|
case json.Object:
|
||||||
|
data := make(JSON_Map)
|
||||||
|
for k, v in _val {
|
||||||
|
new_k := fmt.tprintf("%v", k)
|
||||||
|
data[new_k] = load_json(v)
|
||||||
|
}
|
||||||
|
loaded = data
|
||||||
|
case json.Array:
|
||||||
|
data := make(JSON_List)
|
||||||
|
for v in _val {
|
||||||
|
append(&data, load_json(v))
|
||||||
|
}
|
||||||
|
loaded = data
|
||||||
|
case json.Null:
|
||||||
|
}
|
||||||
|
|
||||||
|
return loaded
|
||||||
|
}
|
||||||
|
|
||||||
|
load_spec :: proc(filename: string) -> (json.Value) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
data, read_err := os.read_entire_file_from_path(filename, context.allocator)
|
||||||
|
if read_err != nil {
|
||||||
|
fmt.println("Failed to load the file!")
|
||||||
|
os.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
json_data, err := json.parse(data)
|
||||||
|
if err != .None {
|
||||||
|
fmt.println("Failed to parse the .json file")
|
||||||
|
fmt.println("Error:", err)
|
||||||
|
os.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_data
|
||||||
|
}
|
||||||
|
|
||||||
|
assert :: proc(
|
||||||
|
t: ^testing.T,
|
||||||
|
actual: bool,
|
||||||
|
msg: string,
|
||||||
|
exp := #caller_expression(actual),
|
||||||
|
loc := #caller_location,
|
||||||
|
) {
|
||||||
|
testing.expect(t, actual, msg, exp, loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_not :: proc(
|
||||||
|
t: ^testing.T,
|
||||||
|
actual: bool,
|
||||||
|
msg: string,
|
||||||
|
exp := #caller_expression(actual),
|
||||||
|
loc := #caller_location,
|
||||||
|
) {
|
||||||
|
testing.expect(t, !actual, msg, exp, loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_mustache :: proc(
|
||||||
|
t: ^testing.T,
|
||||||
|
input: string,
|
||||||
|
data: any,
|
||||||
|
exp_output: string,
|
||||||
|
partials: any = map[string]string{},
|
||||||
|
loc := #caller_location,
|
||||||
|
) {
|
||||||
|
output, _ := render(input, data, partials)
|
||||||
|
testing.expect_value(t, output, exp_output, loc)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{x}}, nice to meet you. My name is {{y}}."
|
||||||
|
|
||||||
|
data := make(Test_Map, 2)
|
||||||
|
data["x"] = "Vincent"
|
||||||
|
data["y"] = "R2D2"
|
||||||
|
|
||||||
|
exp_output := "Hello, Vincent, nice to meet you. My name is R2D2."
|
||||||
|
output, _ := render(template, data)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_in_layout :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{x}}, nice to meet you. My name is {{y}}."
|
||||||
|
|
||||||
|
data := make(Test_Map, 2)
|
||||||
|
data["x"] = "Vincent"
|
||||||
|
data["y"] = "R2D2"
|
||||||
|
|
||||||
|
layout := "\nAbove.\n{{content}}\nBelow."
|
||||||
|
|
||||||
|
exp_output := "\nAbove.\nHello, Vincent, nice to meet you. My name is R2D2.\nBelow."
|
||||||
|
output, _ := render_in_layout(template, data, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_in_layout_with_data_for_layout :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello"
|
||||||
|
|
||||||
|
data := make(Test_Map, 1, context.temp_allocator)
|
||||||
|
data["x"] = "42"
|
||||||
|
|
||||||
|
layout := "\n{{x}}\n{{content}}\n"
|
||||||
|
|
||||||
|
exp_output := "\n42\nHello\n"
|
||||||
|
output, _ := render_in_layout(template, data, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_in_layout_file :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{name}}."
|
||||||
|
data := make(Test_Map, 1, context.temp_allocator)
|
||||||
|
data["name"] = "Vincent"
|
||||||
|
layout := "mustache/test/layout.txt"
|
||||||
|
|
||||||
|
exp_output := "Begin layout >>\nHello, Vincent.\n<< End layout\n"
|
||||||
|
output, _ := render_in_layout_file(template, data, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_from_filename :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "mustache/test/template.txt"
|
||||||
|
data := make(Test_Map, 1, context.temp_allocator)
|
||||||
|
data["name"] = "Vincent"
|
||||||
|
|
||||||
|
exp_output := "Hello, this is Vincent.\n"
|
||||||
|
output, _ := render_from_filename(template, data)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_from_filename_in_layout :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "mustache/test/template.txt"
|
||||||
|
data := make(Test_Map, 1, context.temp_allocator)
|
||||||
|
data["name"] = "Vincent"
|
||||||
|
layout := "\nAbove.\n{{content}}\nBelow."
|
||||||
|
|
||||||
|
exp_output := "\nAbove.\nHello, this is Vincent.\nBelow."
|
||||||
|
output, _ := render_from_filename_in_layout(template, data, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_from_filename_in_layout_file :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "mustache/test/template.txt"
|
||||||
|
data := make(Test_Map, 1, context.temp_allocator)
|
||||||
|
data["name"] = "Vincent"
|
||||||
|
layout := "mustache/test/layout.txt"
|
||||||
|
|
||||||
|
exp_output := "Begin layout >>\nHello, this is Vincent.\n<< End layout\n"
|
||||||
|
output, _ := render_from_filename_in_layout_file(template, data, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_with_json :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{name}}."
|
||||||
|
json := "mustache/test/data.json"
|
||||||
|
|
||||||
|
exp_output := "Hello, Kilgarvan."
|
||||||
|
output, _ := render_with_json(template, json)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
defer(delete(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_with_json_in_layout :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{name}}."
|
||||||
|
json := "mustache/test/data.json"
|
||||||
|
layout := "\nAbove.\n{{content}}\nBelow."
|
||||||
|
|
||||||
|
exp_output := "\nAbove.\nHello, Kilgarvan.\nBelow."
|
||||||
|
output, _ := render_with_json_in_layout(template, json, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_with_json_in_layout_file :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{name}}."
|
||||||
|
json := "mustache/test/data.json"
|
||||||
|
layout := "mustache/test/layout.txt"
|
||||||
|
|
||||||
|
exp_output := "Begin layout >>\nHello, Kilgarvan.\n<< End layout\n"
|
||||||
|
output, _ := render_with_json_in_layout_file(template, json, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_from_filename_with_json_in_layout :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "mustache/test/template.txt"
|
||||||
|
json := "mustache/test/data.json"
|
||||||
|
layout := "\nAbove.\n{{content}}\nBelow."
|
||||||
|
|
||||||
|
exp_output := "\nAbove.\nHello, this is Kilgarvan.\nBelow."
|
||||||
|
output, _ := render_from_filename_with_json_in_layout(template, json, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_render_from_filename_with_json_in_layout_file :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "mustache/test/template.txt"
|
||||||
|
json := "mustache/test/data.json"
|
||||||
|
layout := "mustache/test/layout.txt"
|
||||||
|
|
||||||
|
exp_output := "Begin layout >>\nHello, this is Kilgarvan.\n<< End layout\n"
|
||||||
|
output, _ := render_from_filename_with_json_in_layout_file(template, json, layout)
|
||||||
|
testing.expect_value(t, output, exp_output)
|
||||||
|
|
||||||
|
delete(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_struct :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{name}}. Send an email to {{email}}."
|
||||||
|
data := Test_Struct {"Vincent", "foo@example.com"}
|
||||||
|
exp_output := "Hello, Vincent. Send an email to foo@example.com."
|
||||||
|
assert_mustache(t, template, data, exp_output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_struct_union :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{name}}. Send an email to {{email}}."
|
||||||
|
data: Test_Data
|
||||||
|
data = Test_Struct {"Vincent", "foo@example.com"}
|
||||||
|
exp_output := "Hello, Vincent. Send an email to foo@example.com."
|
||||||
|
assert_mustache(t, template, data, exp_output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_struct_inside_map :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{name}}. Send an email to {{#email}}{{address}}{{/email}}."
|
||||||
|
|
||||||
|
data := make(map[string]Test_Data, 2, context.temp_allocator)
|
||||||
|
data["name"] = "Vincent"
|
||||||
|
data["email"] = make(Test_Map, 1, context.temp_allocator)
|
||||||
|
email := data["email"].(Test_Map)
|
||||||
|
email["address"] = "foo@example.com"
|
||||||
|
data["email"] = email
|
||||||
|
|
||||||
|
exp_output := "Hello, Vincent. Send an email to foo@example.com."
|
||||||
|
assert_mustache(t, template, data, exp_output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_list :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "{{#names}}{{.}}{{/names}}"
|
||||||
|
|
||||||
|
data := make(map[string][dynamic]string, 1, context.temp_allocator)
|
||||||
|
names := make([dynamic]string, 2, context.temp_allocator)
|
||||||
|
append(&names, "Helena", " Bloomington")
|
||||||
|
data["names"] = names
|
||||||
|
|
||||||
|
exp_output := "Helena Bloomington"
|
||||||
|
assert_mustache(t, template, data, exp_output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_no_interpolation :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {Mustache}!"
|
||||||
|
data := ""
|
||||||
|
exp_output := "Hello, {Mustache}!"
|
||||||
|
assert_mustache(t, template, data, exp_output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_literal_tag :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
template := "Hello, {{{verb1}}}."
|
||||||
|
data := Test_Map {
|
||||||
|
"verb1" = "I like < >",
|
||||||
|
}
|
||||||
|
defer(delete(data))
|
||||||
|
|
||||||
|
exp_output := "Hello, I like < >."
|
||||||
|
assert_mustache(t, template, data, exp_output)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_interpolation_spec :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
spec := load_spec(INTERPOLATION_SPEC)
|
||||||
|
defer json.destroy_value(spec)
|
||||||
|
|
||||||
|
root := spec.(json.Object)
|
||||||
|
tests := root["tests"].(json.Array)
|
||||||
|
|
||||||
|
for test in tests {
|
||||||
|
test_obj := test.(json.Object)
|
||||||
|
template := test_obj["template"].(string)
|
||||||
|
exp_output := test_obj["expected"].(string)
|
||||||
|
data := test_obj["data"]
|
||||||
|
input := load_json(data)
|
||||||
|
|
||||||
|
assert_mustache(t, template, input, exp_output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_comments_spec :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
spec := load_spec(COMMENTS_SPEC)
|
||||||
|
defer json.destroy_value(spec)
|
||||||
|
|
||||||
|
root := spec.(json.Object)
|
||||||
|
tests := root["tests"].(json.Array)
|
||||||
|
|
||||||
|
for test in tests {
|
||||||
|
test_obj := test.(json.Object)
|
||||||
|
template := test_obj["template"].(string)
|
||||||
|
exp_output := test_obj["expected"].(string)
|
||||||
|
data := test_obj["data"]
|
||||||
|
input := load_json(data)
|
||||||
|
|
||||||
|
assert_mustache(t, template, input, exp_output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_sections_spec :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
spec := load_spec(SECTIONS_SPEC)
|
||||||
|
defer json.destroy_value(spec)
|
||||||
|
|
||||||
|
root := spec.(json.Object)
|
||||||
|
tests := root["tests"].(json.Array)
|
||||||
|
|
||||||
|
for test in tests {
|
||||||
|
test_obj := test.(json.Object)
|
||||||
|
template := test_obj["template"].(string)
|
||||||
|
exp_output := test_obj["expected"].(string)
|
||||||
|
data := test_obj["data"]
|
||||||
|
input := load_json(data)
|
||||||
|
|
||||||
|
assert_mustache(t, template, input, exp_output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_inverted_spec :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
spec := load_spec(INVERTED_SPEC)
|
||||||
|
defer json.destroy_value(spec)
|
||||||
|
|
||||||
|
root := spec.(json.Object)
|
||||||
|
tests := root["tests"].(json.Array)
|
||||||
|
|
||||||
|
for test in tests {
|
||||||
|
test_obj := test.(json.Object)
|
||||||
|
template := test_obj["template"].(string)
|
||||||
|
exp_output := test_obj["expected"].(string)
|
||||||
|
data := test_obj["data"]
|
||||||
|
input := load_json(data)
|
||||||
|
|
||||||
|
assert_mustache(t, template, input, exp_output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_partials_spec :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
spec := load_spec(PARTIALS_SPEC)
|
||||||
|
defer json.destroy_value(spec)
|
||||||
|
|
||||||
|
root := spec.(json.Object)
|
||||||
|
tests := root["tests"].(json.Array)
|
||||||
|
|
||||||
|
for test in tests {
|
||||||
|
test_obj := test.(json.Object)
|
||||||
|
template := test_obj["template"].(string)
|
||||||
|
exp_output := test_obj["expected"].(string)
|
||||||
|
data := test_obj["data"]
|
||||||
|
input := load_json(data)
|
||||||
|
partials := test_obj["partials"]
|
||||||
|
partials_input := load_json(partials).(JSON_Map)
|
||||||
|
|
||||||
|
assert_mustache(t, template, input, exp_output, partials_input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Someday.
|
||||||
|
// @(test)
|
||||||
|
// test_delimiters_spec :: proc(t: ^testing.T) {
|
||||||
|
// spec := load_spec(DELIMITERS_SPEC)
|
||||||
|
// defer json.destroy_value(spec)
|
||||||
|
|
||||||
|
// root := spec.(json.Object)
|
||||||
|
// tests := root["tests"].(json.Array)
|
||||||
|
|
||||||
|
// for test, i in tests {
|
||||||
|
// if i > 0 do break
|
||||||
|
|
||||||
|
// test_obj := test.(json.Object)
|
||||||
|
// test_name := test_obj["name"].(string)
|
||||||
|
// test_desc := test_obj["desc"].(string)
|
||||||
|
// template := test_obj["template"].(string)
|
||||||
|
// exp_output := test_obj["expected"].(string)
|
||||||
|
// data := test_obj["data"]
|
||||||
|
// input := load_json(data)
|
||||||
|
|
||||||
|
// // Not all the test cases have partials.
|
||||||
|
// partials := test_obj["partials"]
|
||||||
|
// partials_input, ok := load_json(partials).(Map)
|
||||||
|
// if !ok {
|
||||||
|
// partials_input = Map{}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// assert_mustache(t, template, input, exp_output, partials_input)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_map_get :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
// Get the value in a map with one key.
|
||||||
|
output: any
|
||||||
|
mss := make(map[string]string)
|
||||||
|
mcs := make(map[cstring]string)
|
||||||
|
msi := make(map[string]int)
|
||||||
|
mis := make(map[int]string)
|
||||||
|
|
||||||
|
tm := make(Test_Map)
|
||||||
|
mtm := make(map[string]Test_Map)
|
||||||
|
|
||||||
|
u: Test_Data
|
||||||
|
u = make(Test_Map)
|
||||||
|
|
||||||
|
// Simple map
|
||||||
|
delete(mss)
|
||||||
|
mss["name"] = "George"
|
||||||
|
output, _ = map_get(mss, "name")
|
||||||
|
testing.expect_value(t, output.(string), "George")
|
||||||
|
|
||||||
|
// Get the value in a map with one key when key is cstring.
|
||||||
|
delete(mcs)
|
||||||
|
mcs["name"] = "George"
|
||||||
|
output, _ = map_get(mcs, "name")
|
||||||
|
testing.expect_value(t, output.(string), "George")
|
||||||
|
|
||||||
|
// Get the first value in a map with multiple keys.
|
||||||
|
delete(mss)
|
||||||
|
mss["name"] = "George"
|
||||||
|
mss["hometown"] = "Helena"
|
||||||
|
output, _ = map_get(mss, "name")
|
||||||
|
testing.expect_value(t, output.(string), "George")
|
||||||
|
|
||||||
|
// Get the second value in a map with multiple keys.
|
||||||
|
delete(mss)
|
||||||
|
mss["name"] = "George"
|
||||||
|
mss["hometown"] = "Helena"
|
||||||
|
output, _ = map_get(mss, "hometown")
|
||||||
|
testing.expect_value(t, output.(string), "Helena")
|
||||||
|
|
||||||
|
// Get an int
|
||||||
|
delete(msi)
|
||||||
|
msi["phone_number"] = 5555555555
|
||||||
|
output, _ = map_get(msi, "phone_number")
|
||||||
|
testing.expect_value(t, output.(int), 5555555555)
|
||||||
|
|
||||||
|
// Nested, named map.
|
||||||
|
delete(tm)
|
||||||
|
delete(mtm)
|
||||||
|
tm["name"] = "Lee"
|
||||||
|
mtm["person"] = tm
|
||||||
|
output, _ = map_get(mtm, "person")
|
||||||
|
testing.expect_value(t, output.(Test_Map)["name"], tm["name"])
|
||||||
|
|
||||||
|
// Extract from a map type inside a union.
|
||||||
|
delete(u.(Test_Map))
|
||||||
|
(&u.(Test_Map))["name"] = "St. Charles"
|
||||||
|
output, _ = map_get(u, "name")
|
||||||
|
testing.expect_value(t, output.(string), u.(Test_Map)["name"])
|
||||||
|
|
||||||
|
// Return nil when the map is NOT keyed by string, cstring
|
||||||
|
delete(mis)
|
||||||
|
mis[1] = "Customer 1"
|
||||||
|
output, _ = map_get(mis, "1")
|
||||||
|
testing.expect(t, reflect.is_nil(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_struct_get :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
output: any
|
||||||
|
|
||||||
|
data := Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
output = struct_get(data, "name")
|
||||||
|
testing.expect_value(t, output.(string), "Vincent")
|
||||||
|
|
||||||
|
output = struct_get(data, "email")
|
||||||
|
testing.expect_value(t, output.(string), "foo@example.com")
|
||||||
|
|
||||||
|
output = struct_get("foo", "email")
|
||||||
|
testing.expect(
|
||||||
|
t,
|
||||||
|
reflect.is_nil(output),
|
||||||
|
"String argument that is NOT a Struct returns nil",
|
||||||
|
)
|
||||||
|
|
||||||
|
test_map := make(Test_Map)
|
||||||
|
test_map["name"] = "Lee"
|
||||||
|
output = struct_get(test_map, "name")
|
||||||
|
testing.expect(
|
||||||
|
t,
|
||||||
|
reflect.is_nil(output),
|
||||||
|
"Map argument that is NOT a Struct returns nil",
|
||||||
|
)
|
||||||
|
|
||||||
|
// Extract from a map type inside a union.
|
||||||
|
u_struct: Test_Data
|
||||||
|
u_struct = Test_Struct{"St. Charles", "foo@example.com"}
|
||||||
|
output = struct_get(u_struct, "name")
|
||||||
|
testing.expect_value(t, output.(string), u_struct.(Test_Struct).name)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_is_map :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
m := map[string]string{"Vincent" = "Edgar"}
|
||||||
|
assert(t, is_map(m), "Regular map should return true")
|
||||||
|
delete(m)
|
||||||
|
|
||||||
|
m = Test_Map{"name" = "Lee"}
|
||||||
|
assert(t, is_map(m), "Named map should return true")
|
||||||
|
delete(m)
|
||||||
|
|
||||||
|
data: Test_Data
|
||||||
|
data = Test_Map{ "name" = "Vincent" }
|
||||||
|
assert(
|
||||||
|
t,
|
||||||
|
is_map(data),
|
||||||
|
"Union with Map variant should be considered a Map",
|
||||||
|
)
|
||||||
|
delete(data.(Test_Map))
|
||||||
|
|
||||||
|
data = Test_List{ "foo", "bar", "baz" }
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_map(data),
|
||||||
|
"Union with list variant should not be considered a Map",
|
||||||
|
)
|
||||||
|
delete(data.(Test_List))
|
||||||
|
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_map(Test_Struct{"Vincent", "foo@example.com"}),
|
||||||
|
"Struct should not be a map",
|
||||||
|
)
|
||||||
|
|
||||||
|
u: Test_Data
|
||||||
|
u = Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_map(u),
|
||||||
|
"Struct variant of a Union should not be considered a Map",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_is_list :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
arr := [1]string{"element1"}
|
||||||
|
assert(t, is_list(arr), "Array should be considered a list")
|
||||||
|
assert(t, is_list(arr[:]), "Slice should be considered a list")
|
||||||
|
|
||||||
|
dyn_arr := make([dynamic]string, 1, 1)
|
||||||
|
append(&dyn_arr, "element1")
|
||||||
|
assert(t, is_list(dyn_arr), "Dynamic array should be considered a list")
|
||||||
|
|
||||||
|
u_arr: Test_Data
|
||||||
|
u_arr = make(Test_List, 1, 1)
|
||||||
|
append(&u_arr.(Test_List), "element1")
|
||||||
|
assert(t, is_list(u_arr), "Dynamic array in a union should be considered a list")
|
||||||
|
|
||||||
|
assert_not(t, is_list("foo"), "string should not be considered a list")
|
||||||
|
assert_not(t, is_list(1), "int should not be considered a list")
|
||||||
|
|
||||||
|
u_map: Test_Data
|
||||||
|
u_map = make(Test_Map, 1)
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_list(u_map),
|
||||||
|
"Map in union that has list type should not be considered a list",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_is_struct :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
assert(
|
||||||
|
t,
|
||||||
|
is_struct(Test_Struct{"Vincent", "foo@example.com"}),
|
||||||
|
"Struct should be considered a Struct",
|
||||||
|
)
|
||||||
|
|
||||||
|
u: Test_Data
|
||||||
|
u = Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
assert(
|
||||||
|
t,
|
||||||
|
is_struct(u),
|
||||||
|
"Struct variant of a union should be considered a Struct",
|
||||||
|
)
|
||||||
|
|
||||||
|
data: Test_Data
|
||||||
|
data = make(Test_Map)
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_struct(data),
|
||||||
|
"Union with map variant should not be considered a Struct",
|
||||||
|
)
|
||||||
|
|
||||||
|
data = make(Test_List)
|
||||||
|
append(&data.(Test_List), "foo", "bar", "baz")
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_struct(data),
|
||||||
|
"Union with list variant should not be considered a Struct",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_is_union :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
u: Test_Data
|
||||||
|
u = make(Test_Map)
|
||||||
|
assert(
|
||||||
|
t,
|
||||||
|
is_union(u),
|
||||||
|
"Union is union",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
tm := make(Test_Map)
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_union(tm),
|
||||||
|
"Union member with a type is not a union",
|
||||||
|
)
|
||||||
|
|
||||||
|
tl := make(Test_List)
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_union(tl),
|
||||||
|
"Union member with a type is not a union",
|
||||||
|
)
|
||||||
|
|
||||||
|
m := make(map[string]string)
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_union(m),
|
||||||
|
"Map should not be a union",
|
||||||
|
)
|
||||||
|
|
||||||
|
ts := Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
assert_not(
|
||||||
|
t,
|
||||||
|
is_union(ts),
|
||||||
|
"Struct should not be a union",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_data_len :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
data: any
|
||||||
|
|
||||||
|
data = make([dynamic]string)
|
||||||
|
testing.expect_value(t, data_len(data), 0)
|
||||||
|
|
||||||
|
data = make([dynamic]string)
|
||||||
|
append(&data.([dynamic]string), "Vincent")
|
||||||
|
testing.expect_value(t, data_len(data), 1)
|
||||||
|
|
||||||
|
data = "FooBar"
|
||||||
|
testing.expect_value(t, data_len(data), 6)
|
||||||
|
|
||||||
|
data = Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
testing.expect_value(t, data_len(data), 2)
|
||||||
|
|
||||||
|
u: Test_Data
|
||||||
|
u = Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
testing.expect_value(t, data_len(u), 2)
|
||||||
|
|
||||||
|
u = make(Test_Map)
|
||||||
|
(&u.(Test_Map))["name"] = "St. Charles"
|
||||||
|
testing.expect_value(t, data_len(u), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_has_key :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
data: any
|
||||||
|
|
||||||
|
data = Test_Struct{"St. Charles", "foo@example.com"}
|
||||||
|
assert(t, has_key(data, "name"), "Should return true if stuct has field")
|
||||||
|
|
||||||
|
data = make(map[string]int)
|
||||||
|
(&data.(map[string]int))["A1"] = 1
|
||||||
|
assert(t, has_key(data, "A1"), "Should return true if map has key")
|
||||||
|
assert_not(t, has_key(data, "B2"), "Should return false if map does not have key")
|
||||||
|
|
||||||
|
u: Test_Data
|
||||||
|
u = make(Test_Map)
|
||||||
|
(&u.(Test_Map))["name"] = "St. Charles"
|
||||||
|
assert(t, has_key(u, "name"), "Should return true if union-map has key")
|
||||||
|
assert_not(t, has_key(u, "email"), "Should return false if union-map does not have key")
|
||||||
|
|
||||||
|
u = Test_Struct{"St. Charles", "foo@example.com"}
|
||||||
|
assert(t, has_key(u, "name"), "Should return true if union-struct has key")
|
||||||
|
assert_not(t, has_key(u, "XXX"), "Should return false if union-struct does not have key")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_list_at :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
arr := [2]string{"foo", "bar"}
|
||||||
|
testing.expect_value(t, list_at(arr, 0).(string), "foo")
|
||||||
|
testing.expect_value(t, list_at(arr, 1).(string), "bar")
|
||||||
|
|
||||||
|
testing.expect_value(t, list_at(arr[:], 0).(string), "foo")
|
||||||
|
testing.expect_value(t, list_at(arr[:], 1).(string), "bar")
|
||||||
|
|
||||||
|
dyn := slice.clone_to_dynamic(arr[:])
|
||||||
|
testing.expect_value(t, list_at(dyn, 0).(string), "foo")
|
||||||
|
testing.expect_value(t, list_at(dyn, 1).(string), "bar")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_dig :: proc(t: ^testing.T) {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
|
||||||
|
output: any
|
||||||
|
keys := make([dynamic]string)
|
||||||
|
|
||||||
|
// Pull out a struct value
|
||||||
|
d1: any
|
||||||
|
d1 = Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
append(&keys, "name")
|
||||||
|
output = dig(d1, keys[:])
|
||||||
|
testing.expect_value(t, output.(string), "Vincent")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a map value
|
||||||
|
d2: any
|
||||||
|
d2 = make(map[string]string)
|
||||||
|
(&d2.(map[string]string))["name"] = "Edgar"
|
||||||
|
keys = {"name"}
|
||||||
|
output = dig(d2, keys[:])
|
||||||
|
testing.expect_value(t, output.(string), "Edgar")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a nested map value
|
||||||
|
d3: any
|
||||||
|
d3 = make(map[string]map[string]string)
|
||||||
|
c1 := make(map[string]string)
|
||||||
|
c1["name"] = "Kurt"
|
||||||
|
c1["email"] = "test@example.com"
|
||||||
|
(&d3.(map[string]map[string]string))["customer1"] = c1
|
||||||
|
keys = {"customer1", "email"}
|
||||||
|
output = dig(d3, keys[:])
|
||||||
|
testing.expect_value(t, output.(string), "test@example.com")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a nested map
|
||||||
|
d4 := make(map[string]map[string]string)
|
||||||
|
c2 := make(map[string]string)
|
||||||
|
c2["name"] = "Kurt"
|
||||||
|
c2["email"] = "test@example.com"
|
||||||
|
d4["customer1"] = c2
|
||||||
|
keys = {"customer1"}
|
||||||
|
output = dig(d4, keys[:])
|
||||||
|
testing.expect_value(t, output.(map[string]string)["name"], "Kurt")
|
||||||
|
testing.expect_value(t, output.(map[string]string)["email"], "test@example.com")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a list
|
||||||
|
d5: any
|
||||||
|
d5 = Test_List{"El1", "El2"}
|
||||||
|
keys = {"key1"}
|
||||||
|
output = dig(d5, keys[:])
|
||||||
|
testing.expect_value(t, output.(Test_List)[0], d5.(Test_List)[0])
|
||||||
|
testing.expect_value(t, output.(Test_List)[1], d5.(Test_List)[1])
|
||||||
|
delete(d5.(Test_List))
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a struct inside a map
|
||||||
|
d6: any
|
||||||
|
d6 = make(map[string]Test_Struct)
|
||||||
|
c3 := Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
(&d6.(map[string]Test_Struct))["customer1"] = c3
|
||||||
|
keys = {"customer1", "email"}
|
||||||
|
output = dig(d6, keys[:])
|
||||||
|
testing.expect_value(t, output.(string), "foo@example.com")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a string with dot notation
|
||||||
|
d7 := "Hello, world!"
|
||||||
|
keys = {"."}
|
||||||
|
output = dig(d7, keys[:])
|
||||||
|
testing.expect_value(t, output.(string), "Hello, world!")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Return nil when string and not dot notation.
|
||||||
|
d8 := "Hello, world!"
|
||||||
|
keys = {"XXX"}
|
||||||
|
output = dig(d8, keys[:])
|
||||||
|
assert(t, reflect.is_nil(output), "Nil string when a key that is not '.' is provided.")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a nil struct value
|
||||||
|
d9 := Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
keys = {"XXX"}
|
||||||
|
output = dig(d9, keys[:])
|
||||||
|
assert(t, reflect.is_nil(output), "Struct without a matching field should be nil")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a nil struct value with multiple keys
|
||||||
|
d10 := Test_Struct{"Vincent", "foo@example.com"}
|
||||||
|
keys = {"XXX", "YYY"}
|
||||||
|
output = dig(d10, keys[:])
|
||||||
|
assert(t, reflect.is_nil(output), "Struct without a matching field should be nil")
|
||||||
|
delete(keys)
|
||||||
|
|
||||||
|
// Pull out a nil struct value
|
||||||
|
d11 := make(map[string]string)
|
||||||
|
d11["name"] = "Vincent"
|
||||||
|
keys = {"XXX"}
|
||||||
|
output = dig(d11, keys[:])
|
||||||
|
assert(t, reflect.is_nil(output), "Map without a matching field should be nil")
|
||||||
|
delete(keys)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
2011-03-20: v1.1.2
|
||||||
|
Added tests for standalone tags at string boundaries.
|
||||||
|
Added tests for rendering lambda returns after delimiter changes.
|
||||||
|
|
||||||
|
2011-03-20: v1.0.3
|
||||||
|
Added tests for standalone tags at string boundaries.
|
||||||
|
Added tests for rendering lambda returns after delimiter changes.
|
||||||
|
|
||||||
|
2011-03-05: v1.1.1
|
||||||
|
Added tests for indented inline sections.
|
||||||
|
Added tests for Windows-style newlines.
|
||||||
|
|
||||||
|
2011-03-05: v1.0.2
|
||||||
|
Added tests for indented inline sections.
|
||||||
|
Added tests for Windows-style newlines.
|
||||||
|
|
||||||
|
2011-03-04: v1.1.0
|
||||||
|
Implicit iterators.
|
||||||
|
A single period (`.`) may now be used as a name in Interpolation tags,
|
||||||
|
which represents the top of stack (cast as a String).
|
||||||
|
Dotted names.
|
||||||
|
Names containing one or more periods should be resolved as chained
|
||||||
|
properties; naïvely, this is like nesting section tags, but with some
|
||||||
|
built-in scoping protections.
|
||||||
|
|
||||||
|
2011-03-02: v1.0.1
|
||||||
|
Clarifying a point in the README about version compliance.
|
||||||
|
Adding high-level documentation to each spec file.
|
||||||
|
|
||||||
|
2011-02-28: v1.0.0
|
||||||
|
Initial Release
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
Copyright (c) 2010-2022 Mustache Contributors
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
The repository at https://github.com/mustache/spec is the formal standard for
|
||||||
|
Mustache. It defines both normal usage and edge-case behavior for libraries
|
||||||
|
parsing the Mustache templating language (or a superset thereof).
|
||||||
|
|
||||||
|
The specification is developed as a series of YAML files, under the `specs`
|
||||||
|
directory.
|
||||||
|
|
||||||
|
Versioning
|
||||||
|
----------
|
||||||
|
This specification is being [semantically versioned](http://semver.org).
|
||||||
|
Roughly described, major version changes will always represent backwards
|
||||||
|
incompatible changes, minor version changes will always represent new language
|
||||||
|
features and will be backwards compatible, and patch ('tiny') version changes
|
||||||
|
will always be bug fixes. For the purposes of semantic versioning, the public
|
||||||
|
API is the contents of the `specs` directory and the algorithm for testing
|
||||||
|
against it.
|
||||||
|
|
||||||
|
Mustache implementations SHOULD report the most recent version of the spec
|
||||||
|
(major and minor version numbers). If an implementation has support for any
|
||||||
|
optional modules, they SHOULD indicate so with a remark attached to the
|
||||||
|
version number (e.g. "vX.Y, including lambdas" or "v.X.Y+λ"). It is
|
||||||
|
RECOMMENDED that implementations not supporting at least v1.0.0 of this spec
|
||||||
|
refer to themselves as "Mustache-like", or "Mustache-inspired".
|
||||||
|
|
||||||
|
Alternate Formats
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Since YAML is a reasonably complex format that not every language has good
|
||||||
|
tools for working with, we also provide JSON versions of the specs on a
|
||||||
|
best-effort basis.
|
||||||
|
|
||||||
|
These should be identical to the YAML specifications, but if you find the need
|
||||||
|
to regenerate them, they can be trivially rebuilt by invoking `rake build`.
|
||||||
|
|
||||||
|
It is also worth noting that some specifications (notably, the lambda module)
|
||||||
|
rely on YAML "tags" to denote special types of data (e.g. source code). Since
|
||||||
|
JSON offers no way to denote this, a special key ("`__tag__`") is injected
|
||||||
|
with the name of the tag as its value. See `TESTING.md` for more information
|
||||||
|
about handling tagged data.
|
||||||
|
|
||||||
|
Optional Modules
|
||||||
|
----------------
|
||||||
|
|
||||||
|
Specification files beginning with a tilde (`~`) describe optional modules.
|
||||||
|
As a guideline, a module may be a candidate for optionality when:
|
||||||
|
|
||||||
|
* It does not affect the core syntax of the language.
|
||||||
|
* It does not significantly affect the output of rendered templates.
|
||||||
|
* It concerns implementation language features or data types that are not
|
||||||
|
common to or core in every targeted language.
|
||||||
|
* The lack of support by an implementation does not diminish the usage of
|
||||||
|
Mustache in the target language.
|
||||||
|
|
||||||
|
As an example, the lambda module is primarily concerned with the handling of a
|
||||||
|
particular data type (code). This is a type of data that may be difficult to
|
||||||
|
support in some languages, and users of those languages will not see the lack
|
||||||
|
as an 'inconsistency' between implementations.
|
||||||
|
|
||||||
|
Support for specific pragmas or syntax extensions, however, are best managed
|
||||||
|
outside this core specification, as adjunct specifications.
|
||||||
|
|
||||||
|
Implementors are strongly encouraged to support any and all modules they are
|
||||||
|
reasonably capable of supporting.
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
require 'json'
|
||||||
|
require 'yaml'
|
||||||
|
|
||||||
|
# Our custom YAML tags must retain their magic.
|
||||||
|
class TaggedMap < Hash
|
||||||
|
yaml_tag '!code'
|
||||||
|
def init_with(psych_coder)
|
||||||
|
self.replace({:__tag__ => 'code'}.merge(psych_coder.map))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
YAML::add_tag('code', TaggedMap)
|
||||||
|
|
||||||
|
desc 'Build all alternate versions of the specs.'
|
||||||
|
multitask :build => [ 'build:json' ]
|
||||||
|
|
||||||
|
namespace :build do
|
||||||
|
note = 'Do not edit this file; changes belong in the appropriate YAML file.'
|
||||||
|
|
||||||
|
desc 'Build JSON versions of the specs.'
|
||||||
|
task :json do
|
||||||
|
rm(Dir['specs/*.json'], :verbose => false)
|
||||||
|
Dir.glob('specs/*.yml').each do |filename|
|
||||||
|
json_file = filename.gsub('.yml', '.json')
|
||||||
|
|
||||||
|
File.open(json_file, 'w') do |file|
|
||||||
|
warning = {:__ATTN__ => note}
|
||||||
|
doc = YAML.load_file(
|
||||||
|
filename,
|
||||||
|
permitted_classes: [TaggedMap]
|
||||||
|
)
|
||||||
|
file << JSON.pretty_generate(warning.merge(doc)) << "\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
Testing your Mustache implementation against this specification should be
|
||||||
|
relatively simple. If you have a readily available testing framework on your
|
||||||
|
platform, your task may be even simpler.
|
||||||
|
|
||||||
|
In general, the process for each `.yml` file is as follows:
|
||||||
|
|
||||||
|
1. Use a YAML parser to load the file.
|
||||||
|
|
||||||
|
2. For each test in the `tests` array:
|
||||||
|
|
||||||
|
1. Ensure that each element of the `partials` hash (if it exists) is
|
||||||
|
stored in a place where the interpreter will look for it.
|
||||||
|
|
||||||
|
2. If your implementation will not support lambdas, feel free to skip
|
||||||
|
over the optional `~lambdas.yml` file.
|
||||||
|
|
||||||
|
Otherwise, ensure that each member of `data` tagged with `!code` is
|
||||||
|
properly processed into a language-specific lambda reference.
|
||||||
|
|
||||||
|
* e.g. Given this YAML data hash:
|
||||||
|
|
||||||
|
`{ x: !code { ruby: 'proc { "x" }', perl: 'sub { "x" }' } }`
|
||||||
|
|
||||||
|
a Ruby-based Mustache implementation would process it such that it
|
||||||
|
was equivalent to this Ruby hash:
|
||||||
|
|
||||||
|
`{ 'x' => proc { "x" } }`
|
||||||
|
|
||||||
|
* If your implementation language does not currently have lambda
|
||||||
|
examples in the spec, feel free to implement them and send a pull
|
||||||
|
request.
|
||||||
|
|
||||||
|
* The JSON version of the spec represents these tagged values as a
|
||||||
|
hash with a `__tag__` key of `code`.
|
||||||
|
|
||||||
|
3. Render the template (stored in the `template` key) with the given
|
||||||
|
`data` hash.
|
||||||
|
|
||||||
|
4. Compare the results of your rendering against the `expected` value;
|
||||||
|
any differences should be reported, along with any useful debugging
|
||||||
|
information.
|
||||||
|
|
||||||
|
* Of note, the `desc` key contains a rough one-line description of
|
||||||
|
the behavior being tested – this is most useful in conjunction with
|
||||||
|
the file name and test `name`.
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
{
|
||||||
|
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
|
||||||
|
"overview": "Comment tags represent content that should never appear in the resulting\noutput.\n\nThe tag's content may contain any substring (including newlines) EXCEPT the\nclosing delimiter.\n\nComment tags SHOULD be treated as standalone when appropriate.\n",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "Inline",
|
||||||
|
"desc": "Comment blocks should be removed from the template.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "12345{{! Comment Block! }}67890",
|
||||||
|
"expected": "1234567890"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Multiline",
|
||||||
|
"desc": "Multiline comments should be permitted.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "12345{{!\n This is a\n multi-line comment...\n}}67890\n",
|
||||||
|
"expected": "1234567890\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone",
|
||||||
|
"desc": "All standalone comment lines should be removed.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "Begin.\n{{! Comment Block! }}\nEnd.\n",
|
||||||
|
"expected": "Begin.\nEnd.\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Indented Standalone",
|
||||||
|
"desc": "All standalone comment lines should be removed.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "Begin.\n {{! Indented Comment Block! }}\nEnd.\n",
|
||||||
|
"expected": "Begin.\nEnd.\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Line Endings",
|
||||||
|
"desc": "\"\\r\\n\" should be considered a newline for standalone tags.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "|\r\n{{! Standalone Comment }}\r\n|",
|
||||||
|
"expected": "|\r\n|"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Previous Line",
|
||||||
|
"desc": "Standalone tags should not require a newline to precede them.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": " {{! I'm Still Standalone }}\n!",
|
||||||
|
"expected": "!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Newline",
|
||||||
|
"desc": "Standalone tags should not require a newline to follow them.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "!\n {{! I'm Still Standalone }}",
|
||||||
|
"expected": "!\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Multiline Standalone",
|
||||||
|
"desc": "All standalone comment lines should be removed.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "Begin.\n{{!\nSomething's going on here...\n}}\nEnd.\n",
|
||||||
|
"expected": "Begin.\nEnd.\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Indented Multiline Standalone",
|
||||||
|
"desc": "All standalone comment lines should be removed.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "Begin.\n {{!\n Something's going on here...\n }}\nEnd.\n",
|
||||||
|
"expected": "Begin.\nEnd.\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Indented Inline",
|
||||||
|
"desc": "Inline comments should not strip whitespace",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": " 12 {{! 34 }}\n",
|
||||||
|
"expected": " 12 \n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Surrounding Whitespace",
|
||||||
|
"desc": "Comment removal should preserve surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "12345 {{! Comment Block! }} 67890",
|
||||||
|
"expected": "12345 67890"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Variable Name Collision",
|
||||||
|
"desc": "Comments must never render, even if variable with same name exists.",
|
||||||
|
"data": {
|
||||||
|
"! comment": 1,
|
||||||
|
"! comment ": 2,
|
||||||
|
"!comment": 3,
|
||||||
|
"comment": 4
|
||||||
|
},
|
||||||
|
"template": "comments never show: >{{! comment }}<",
|
||||||
|
"expected": "comments never show: ><"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
overview: |
|
||||||
|
Comment tags represent content that should never appear in the resulting
|
||||||
|
output.
|
||||||
|
|
||||||
|
The tag's content may contain any substring (including newlines) EXCEPT the
|
||||||
|
closing delimiter.
|
||||||
|
|
||||||
|
Comment tags SHOULD be treated as standalone when appropriate.
|
||||||
|
tests:
|
||||||
|
- name: Inline
|
||||||
|
desc: Comment blocks should be removed from the template.
|
||||||
|
data: { }
|
||||||
|
template: '12345{{! Comment Block! }}67890'
|
||||||
|
expected: '1234567890'
|
||||||
|
|
||||||
|
- name: Multiline
|
||||||
|
desc: Multiline comments should be permitted.
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
12345{{!
|
||||||
|
This is a
|
||||||
|
multi-line comment...
|
||||||
|
}}67890
|
||||||
|
expected: |
|
||||||
|
1234567890
|
||||||
|
|
||||||
|
- name: Standalone
|
||||||
|
desc: All standalone comment lines should be removed.
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
Begin.
|
||||||
|
{{! Comment Block! }}
|
||||||
|
End.
|
||||||
|
expected: |
|
||||||
|
Begin.
|
||||||
|
End.
|
||||||
|
|
||||||
|
- name: Indented Standalone
|
||||||
|
desc: All standalone comment lines should be removed.
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
Begin.
|
||||||
|
{{! Indented Comment Block! }}
|
||||||
|
End.
|
||||||
|
expected: |
|
||||||
|
Begin.
|
||||||
|
End.
|
||||||
|
|
||||||
|
- name: Standalone Line Endings
|
||||||
|
desc: '"\r\n" should be considered a newline for standalone tags.'
|
||||||
|
data: { }
|
||||||
|
template: "|\r\n{{! Standalone Comment }}\r\n|"
|
||||||
|
expected: "|\r\n|"
|
||||||
|
|
||||||
|
- name: Standalone Without Previous Line
|
||||||
|
desc: Standalone tags should not require a newline to precede them.
|
||||||
|
data: { }
|
||||||
|
template: " {{! I'm Still Standalone }}\n!"
|
||||||
|
expected: "!"
|
||||||
|
|
||||||
|
- name: Standalone Without Newline
|
||||||
|
desc: Standalone tags should not require a newline to follow them.
|
||||||
|
data: { }
|
||||||
|
template: "!\n {{! I'm Still Standalone }}"
|
||||||
|
expected: "!\n"
|
||||||
|
|
||||||
|
- name: Multiline Standalone
|
||||||
|
desc: All standalone comment lines should be removed.
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
Begin.
|
||||||
|
{{!
|
||||||
|
Something's going on here...
|
||||||
|
}}
|
||||||
|
End.
|
||||||
|
expected: |
|
||||||
|
Begin.
|
||||||
|
End.
|
||||||
|
|
||||||
|
- name: Indented Multiline Standalone
|
||||||
|
desc: All standalone comment lines should be removed.
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
Begin.
|
||||||
|
{{!
|
||||||
|
Something's going on here...
|
||||||
|
}}
|
||||||
|
End.
|
||||||
|
expected: |
|
||||||
|
Begin.
|
||||||
|
End.
|
||||||
|
|
||||||
|
- name: Indented Inline
|
||||||
|
desc: Inline comments should not strip whitespace
|
||||||
|
data: { }
|
||||||
|
template: " 12 {{! 34 }}\n"
|
||||||
|
expected: " 12 \n"
|
||||||
|
|
||||||
|
- name: Surrounding Whitespace
|
||||||
|
desc: Comment removal should preserve surrounding whitespace.
|
||||||
|
data: { }
|
||||||
|
template: '12345 {{! Comment Block! }} 67890'
|
||||||
|
expected: '12345 67890'
|
||||||
|
|
||||||
|
- name: Variable Name Collision
|
||||||
|
desc: Comments must never render, even if variable with same name exists.
|
||||||
|
data: { '! comment': 1, '! comment ': 2, '!comment': 3, 'comment': 4}
|
||||||
|
template: 'comments never show: >{{! comment }}<'
|
||||||
|
expected: 'comments never show: ><'
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
{
|
||||||
|
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
|
||||||
|
"overview": "Set Delimiter tags are used to change the tag delimiters for all content\nfollowing the tag in the current compilation unit.\n\nThe tag's content MUST be any two non-whitespace sequences (separated by\nwhitespace) EXCEPT an equals sign ('=') followed by the current closing\ndelimiter.\n\nSet Delimiter tags SHOULD be treated as standalone when appropriate.\n",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "Pair Behavior",
|
||||||
|
"desc": "The equals sign (used on both sides) should permit delimiter changes.",
|
||||||
|
"data": {
|
||||||
|
"text": "Hey!"
|
||||||
|
},
|
||||||
|
"template": "{{=<% %>=}}(<%text%>)",
|
||||||
|
"expected": "(Hey!)"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Special Characters",
|
||||||
|
"desc": "Characters with special meaning regexen should be valid delimiters.",
|
||||||
|
"data": {
|
||||||
|
"text": "It worked!"
|
||||||
|
},
|
||||||
|
"template": "({{=[ ]=}}[text])",
|
||||||
|
"expected": "(It worked!)"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Sections",
|
||||||
|
"desc": "Delimiters set outside sections should persist.",
|
||||||
|
"data": {
|
||||||
|
"section": true,
|
||||||
|
"data": "I got interpolated."
|
||||||
|
},
|
||||||
|
"template": "[\n{{#section}}\n {{data}}\n |data|\n{{/section}}\n\n{{= | | =}}\n|#section|\n {{data}}\n |data|\n|/section|\n]\n",
|
||||||
|
"expected": "[\n I got interpolated.\n |data|\n\n {{data}}\n I got interpolated.\n]\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Inverted Sections",
|
||||||
|
"desc": "Delimiters set outside inverted sections should persist.",
|
||||||
|
"data": {
|
||||||
|
"section": false,
|
||||||
|
"data": "I got interpolated."
|
||||||
|
},
|
||||||
|
"template": "[\n{{^section}}\n {{data}}\n |data|\n{{/section}}\n\n{{= | | =}}\n|^section|\n {{data}}\n |data|\n|/section|\n]\n",
|
||||||
|
"expected": "[\n I got interpolated.\n |data|\n\n {{data}}\n I got interpolated.\n]\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Partial Inheritence",
|
||||||
|
"desc": "Delimiters set in a parent template should not affect a partial.",
|
||||||
|
"data": {
|
||||||
|
"value": "yes"
|
||||||
|
},
|
||||||
|
"partials": {
|
||||||
|
"include": ".{{value}}."
|
||||||
|
},
|
||||||
|
"template": "[ {{>include}} ]\n{{= | | =}}\n[ |>include| ]\n",
|
||||||
|
"expected": "[ .yes. ]\n[ .yes. ]\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Post-Partial Behavior",
|
||||||
|
"desc": "Delimiters set in a partial should not affect the parent template.",
|
||||||
|
"data": {
|
||||||
|
"value": "yes"
|
||||||
|
},
|
||||||
|
"partials": {
|
||||||
|
"include": ".{{value}}. {{= | | =}} .|value|."
|
||||||
|
},
|
||||||
|
"template": "[ {{>include}} ]\n[ .{{value}}. .|value|. ]\n",
|
||||||
|
"expected": "[ .yes. .yes. ]\n[ .yes. .|value|. ]\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Surrounding Whitespace",
|
||||||
|
"desc": "Surrounding whitespace should be left untouched.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "| {{=@ @=}} |",
|
||||||
|
"expected": "| |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Outlying Whitespace (Inline)",
|
||||||
|
"desc": "Whitespace should be left untouched.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": " | {{=@ @=}}\n",
|
||||||
|
"expected": " | \n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Tag",
|
||||||
|
"desc": "Standalone lines should be removed from the template.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "Begin.\n{{=@ @=}}\nEnd.\n",
|
||||||
|
"expected": "Begin.\nEnd.\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Indented Standalone Tag",
|
||||||
|
"desc": "Indented standalone lines should be removed from the template.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "Begin.\n {{=@ @=}}\nEnd.\n",
|
||||||
|
"expected": "Begin.\nEnd.\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Line Endings",
|
||||||
|
"desc": "\"\\r\\n\" should be considered a newline for standalone tags.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "|\r\n{{= @ @ =}}\r\n|",
|
||||||
|
"expected": "|\r\n|"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Previous Line",
|
||||||
|
"desc": "Standalone tags should not require a newline to precede them.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": " {{=@ @=}}\n=",
|
||||||
|
"expected": "="
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Newline",
|
||||||
|
"desc": "Standalone tags should not require a newline to follow them.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "=\n {{=@ @=}}",
|
||||||
|
"expected": "=\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Pair with Padding",
|
||||||
|
"desc": "Superfluous in-tag whitespace should be ignored.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "|{{= @ @ =}}|",
|
||||||
|
"expected": "||"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
overview: |
|
||||||
|
Set Delimiter tags are used to change the tag delimiters for all content
|
||||||
|
following the tag in the current compilation unit.
|
||||||
|
|
||||||
|
The tag's content MUST be any two non-whitespace sequences (separated by
|
||||||
|
whitespace) EXCEPT an equals sign ('=') followed by the current closing
|
||||||
|
delimiter.
|
||||||
|
|
||||||
|
Set Delimiter tags SHOULD be treated as standalone when appropriate.
|
||||||
|
tests:
|
||||||
|
- name: Pair Behavior
|
||||||
|
desc: The equals sign (used on both sides) should permit delimiter changes.
|
||||||
|
data: { text: 'Hey!' }
|
||||||
|
template: '{{=<% %>=}}(<%text%>)'
|
||||||
|
expected: '(Hey!)'
|
||||||
|
|
||||||
|
- name: Special Characters
|
||||||
|
desc: Characters with special meaning regexen should be valid delimiters.
|
||||||
|
data: { text: 'It worked!' }
|
||||||
|
template: '({{=[ ]=}}[text])'
|
||||||
|
expected: '(It worked!)'
|
||||||
|
|
||||||
|
- name: Sections
|
||||||
|
desc: Delimiters set outside sections should persist.
|
||||||
|
data: { section: true, data: 'I got interpolated.' }
|
||||||
|
template: |
|
||||||
|
[
|
||||||
|
{{#section}}
|
||||||
|
{{data}}
|
||||||
|
|data|
|
||||||
|
{{/section}}
|
||||||
|
|
||||||
|
{{= | | =}}
|
||||||
|
|#section|
|
||||||
|
{{data}}
|
||||||
|
|data|
|
||||||
|
|/section|
|
||||||
|
]
|
||||||
|
expected: |
|
||||||
|
[
|
||||||
|
I got interpolated.
|
||||||
|
|data|
|
||||||
|
|
||||||
|
{{data}}
|
||||||
|
I got interpolated.
|
||||||
|
]
|
||||||
|
|
||||||
|
- name: Inverted Sections
|
||||||
|
desc: Delimiters set outside inverted sections should persist.
|
||||||
|
data: { section: false, data: 'I got interpolated.' }
|
||||||
|
template: |
|
||||||
|
[
|
||||||
|
{{^section}}
|
||||||
|
{{data}}
|
||||||
|
|data|
|
||||||
|
{{/section}}
|
||||||
|
|
||||||
|
{{= | | =}}
|
||||||
|
|^section|
|
||||||
|
{{data}}
|
||||||
|
|data|
|
||||||
|
|/section|
|
||||||
|
]
|
||||||
|
expected: |
|
||||||
|
[
|
||||||
|
I got interpolated.
|
||||||
|
|data|
|
||||||
|
|
||||||
|
{{data}}
|
||||||
|
I got interpolated.
|
||||||
|
]
|
||||||
|
|
||||||
|
- name: Partial Inheritence
|
||||||
|
desc: Delimiters set in a parent template should not affect a partial.
|
||||||
|
data: { value: 'yes' }
|
||||||
|
partials:
|
||||||
|
include: '.{{value}}.'
|
||||||
|
template: |
|
||||||
|
[ {{>include}} ]
|
||||||
|
{{= | | =}}
|
||||||
|
[ |>include| ]
|
||||||
|
expected: |
|
||||||
|
[ .yes. ]
|
||||||
|
[ .yes. ]
|
||||||
|
|
||||||
|
- name: Post-Partial Behavior
|
||||||
|
desc: Delimiters set in a partial should not affect the parent template.
|
||||||
|
data: { value: 'yes' }
|
||||||
|
partials:
|
||||||
|
include: '.{{value}}. {{= | | =}} .|value|.'
|
||||||
|
template: |
|
||||||
|
[ {{>include}} ]
|
||||||
|
[ .{{value}}. .|value|. ]
|
||||||
|
expected: |
|
||||||
|
[ .yes. .yes. ]
|
||||||
|
[ .yes. .|value|. ]
|
||||||
|
|
||||||
|
# Whitespace Sensitivity
|
||||||
|
|
||||||
|
- name: Surrounding Whitespace
|
||||||
|
desc: Surrounding whitespace should be left untouched.
|
||||||
|
data: { }
|
||||||
|
template: '| {{=@ @=}} |'
|
||||||
|
expected: '| |'
|
||||||
|
|
||||||
|
- name: Outlying Whitespace (Inline)
|
||||||
|
desc: Whitespace should be left untouched.
|
||||||
|
data: { }
|
||||||
|
template: " | {{=@ @=}}\n"
|
||||||
|
expected: " | \n"
|
||||||
|
|
||||||
|
- name: Standalone Tag
|
||||||
|
desc: Standalone lines should be removed from the template.
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
Begin.
|
||||||
|
{{=@ @=}}
|
||||||
|
End.
|
||||||
|
expected: |
|
||||||
|
Begin.
|
||||||
|
End.
|
||||||
|
|
||||||
|
- name: Indented Standalone Tag
|
||||||
|
desc: Indented standalone lines should be removed from the template.
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
Begin.
|
||||||
|
{{=@ @=}}
|
||||||
|
End.
|
||||||
|
expected: |
|
||||||
|
Begin.
|
||||||
|
End.
|
||||||
|
|
||||||
|
- name: Standalone Line Endings
|
||||||
|
desc: '"\r\n" should be considered a newline for standalone tags.'
|
||||||
|
data: { }
|
||||||
|
template: "|\r\n{{= @ @ =}}\r\n|"
|
||||||
|
expected: "|\r\n|"
|
||||||
|
|
||||||
|
- name: Standalone Without Previous Line
|
||||||
|
desc: Standalone tags should not require a newline to precede them.
|
||||||
|
data: { }
|
||||||
|
template: " {{=@ @=}}\n="
|
||||||
|
expected: "="
|
||||||
|
|
||||||
|
- name: Standalone Without Newline
|
||||||
|
desc: Standalone tags should not require a newline to follow them.
|
||||||
|
data: { }
|
||||||
|
template: "=\n {{=@ @=}}"
|
||||||
|
expected: "=\n"
|
||||||
|
|
||||||
|
# Whitespace Insensitivity
|
||||||
|
|
||||||
|
- name: Pair with Padding
|
||||||
|
desc: Superfluous in-tag whitespace should be ignored.
|
||||||
|
data: { }
|
||||||
|
template: '|{{= @ @ =}}|'
|
||||||
|
expected: '||'
|
||||||
@@ -0,0 +1,422 @@
|
|||||||
|
{
|
||||||
|
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
|
||||||
|
"overview": "Interpolation tags are used to integrate dynamic content into the template.\n\nThe tag's content MUST be a non-whitespace character sequence NOT containing\nthe current closing delimiter.\n\nThis tag's content names the data to replace the tag. A single period (`.`)\nindicates that the item currently sitting atop the context stack should be\nused; otherwise, name resolution is as follows:\n 1) Split the name on periods; the first part is the name to resolve, any\n remaining parts should be retained.\n 2) Walk the context stack from top to bottom, finding the first context\n that is a) a hash containing the name as a key OR b) an object responding\n to a method with the given name.\n 3) If the context is a hash, the data is the value associated with the\n name.\n 4) If the context is an object, the data is the value returned by the\n method with the given name.\n 5) If any name parts were retained in step 1, each should be resolved\n against a context stack containing only the result from the former\n resolution. If any part fails resolution, the result should be considered\n falsey, and should interpolate as the empty string.\nData should be coerced into a string (and escaped, if appropriate) before\ninterpolation.\n\nThe Interpolation tags MUST NOT be treated as standalone.\n",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "No Interpolation",
|
||||||
|
"desc": "Mustache-free templates should render as-is.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "Hello from {Mustache}!\n",
|
||||||
|
"expected": "Hello from {Mustache}!\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Basic Interpolation",
|
||||||
|
"desc": "Unadorned tags should interpolate content into the template.",
|
||||||
|
"data": {
|
||||||
|
"subject": "world"
|
||||||
|
},
|
||||||
|
"template": "Hello, {{subject}}!\n",
|
||||||
|
"expected": "Hello, world!\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "No Re-interpolation",
|
||||||
|
"desc": "Interpolated tag output should not be re-interpolated.",
|
||||||
|
"data": {
|
||||||
|
"template": "{{planet}}",
|
||||||
|
"planet": "Earth"
|
||||||
|
},
|
||||||
|
"template": "{{template}}: {{planet}}",
|
||||||
|
"expected": "{{planet}}: Earth"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "HTML Escaping",
|
||||||
|
"desc": "Basic interpolation should be HTML escaped.",
|
||||||
|
"data": {
|
||||||
|
"forbidden": "& \" < >"
|
||||||
|
},
|
||||||
|
"template": "These characters should be HTML escaped: {{forbidden}}\n",
|
||||||
|
"expected": "These characters should be HTML escaped: & " < >\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache",
|
||||||
|
"desc": "Triple mustaches should interpolate without HTML escaping.",
|
||||||
|
"data": {
|
||||||
|
"forbidden": "& \" < >"
|
||||||
|
},
|
||||||
|
"template": "These characters should not be HTML escaped: {{{forbidden}}}\n",
|
||||||
|
"expected": "These characters should not be HTML escaped: & \" < >\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ampersand",
|
||||||
|
"desc": "Ampersand should interpolate without HTML escaping.",
|
||||||
|
"data": {
|
||||||
|
"forbidden": "& \" < >"
|
||||||
|
},
|
||||||
|
"template": "These characters should not be HTML escaped: {{&forbidden}}\n",
|
||||||
|
"expected": "These characters should not be HTML escaped: & \" < >\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Basic Integer Interpolation",
|
||||||
|
"desc": "Integers should interpolate seamlessly.",
|
||||||
|
"data": {
|
||||||
|
"mph": 85
|
||||||
|
},
|
||||||
|
"template": "\"{{mph}} miles an hour!\"",
|
||||||
|
"expected": "\"85 miles an hour!\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache Integer Interpolation",
|
||||||
|
"desc": "Integers should interpolate seamlessly.",
|
||||||
|
"data": {
|
||||||
|
"mph": 85
|
||||||
|
},
|
||||||
|
"template": "\"{{{mph}}} miles an hour!\"",
|
||||||
|
"expected": "\"85 miles an hour!\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ampersand Integer Interpolation",
|
||||||
|
"desc": "Integers should interpolate seamlessly.",
|
||||||
|
"data": {
|
||||||
|
"mph": 85
|
||||||
|
},
|
||||||
|
"template": "\"{{&mph}} miles an hour!\"",
|
||||||
|
"expected": "\"85 miles an hour!\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Basic Decimal Interpolation",
|
||||||
|
"desc": "Decimals should interpolate seamlessly with proper significance.",
|
||||||
|
"data": {
|
||||||
|
"power": 1.21
|
||||||
|
},
|
||||||
|
"template": "\"{{power}} jiggawatts!\"",
|
||||||
|
"expected": "\"1.21 jiggawatts!\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache Decimal Interpolation",
|
||||||
|
"desc": "Decimals should interpolate seamlessly with proper significance.",
|
||||||
|
"data": {
|
||||||
|
"power": 1.21
|
||||||
|
},
|
||||||
|
"template": "\"{{{power}}} jiggawatts!\"",
|
||||||
|
"expected": "\"1.21 jiggawatts!\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ampersand Decimal Interpolation",
|
||||||
|
"desc": "Decimals should interpolate seamlessly with proper significance.",
|
||||||
|
"data": {
|
||||||
|
"power": 1.21
|
||||||
|
},
|
||||||
|
"template": "\"{{&power}} jiggawatts!\"",
|
||||||
|
"expected": "\"1.21 jiggawatts!\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Basic Null Interpolation",
|
||||||
|
"desc": "Nulls should interpolate as the empty string.",
|
||||||
|
"data": {
|
||||||
|
"cannot": null
|
||||||
|
},
|
||||||
|
"template": "I ({{cannot}}) be seen!",
|
||||||
|
"expected": "I () be seen!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache Null Interpolation",
|
||||||
|
"desc": "Nulls should interpolate as the empty string.",
|
||||||
|
"data": {
|
||||||
|
"cannot": null
|
||||||
|
},
|
||||||
|
"template": "I ({{{cannot}}}) be seen!",
|
||||||
|
"expected": "I () be seen!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ampersand Null Interpolation",
|
||||||
|
"desc": "Nulls should interpolate as the empty string.",
|
||||||
|
"data": {
|
||||||
|
"cannot": null
|
||||||
|
},
|
||||||
|
"template": "I ({{&cannot}}) be seen!",
|
||||||
|
"expected": "I () be seen!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Basic Context Miss Interpolation",
|
||||||
|
"desc": "Failed context lookups should default to empty strings.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "I ({{cannot}}) be seen!",
|
||||||
|
"expected": "I () be seen!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache Context Miss Interpolation",
|
||||||
|
"desc": "Failed context lookups should default to empty strings.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "I ({{{cannot}}}) be seen!",
|
||||||
|
"expected": "I () be seen!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ampersand Context Miss Interpolation",
|
||||||
|
"desc": "Failed context lookups should default to empty strings.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "I ({{&cannot}}) be seen!",
|
||||||
|
"expected": "I () be seen!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Basic Interpolation",
|
||||||
|
"desc": "Dotted names should be considered a form of shorthand for sections.",
|
||||||
|
"data": {
|
||||||
|
"person": {
|
||||||
|
"name": "Joe"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{person.name}}\" == \"{{#person}}{{name}}{{/person}}\"",
|
||||||
|
"expected": "\"Joe\" == \"Joe\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Triple Mustache Interpolation",
|
||||||
|
"desc": "Dotted names should be considered a form of shorthand for sections.",
|
||||||
|
"data": {
|
||||||
|
"person": {
|
||||||
|
"name": "Joe"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{{person.name}}}\" == \"{{#person}}{{{name}}}{{/person}}\"",
|
||||||
|
"expected": "\"Joe\" == \"Joe\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Ampersand Interpolation",
|
||||||
|
"desc": "Dotted names should be considered a form of shorthand for sections.",
|
||||||
|
"data": {
|
||||||
|
"person": {
|
||||||
|
"name": "Joe"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{&person.name}}\" == \"{{#person}}{{&name}}{{/person}}\"",
|
||||||
|
"expected": "\"Joe\" == \"Joe\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Arbitrary Depth",
|
||||||
|
"desc": "Dotted names should be functional to any level of nesting.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
"c": {
|
||||||
|
"d": {
|
||||||
|
"e": {
|
||||||
|
"name": "Phil"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{a.b.c.d.e.name}}\" == \"Phil\"",
|
||||||
|
"expected": "\"Phil\" == \"Phil\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Broken Chains",
|
||||||
|
"desc": "Any falsey value prior to the last part of the name should yield ''.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{a.b.c}}\" == \"\"",
|
||||||
|
"expected": "\"\" == \"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Broken Chain Resolution",
|
||||||
|
"desc": "Each part of a dotted name should resolve only against its parent.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"c": {
|
||||||
|
"name": "Jim"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{a.b.c.name}}\" == \"\"",
|
||||||
|
"expected": "\"\" == \"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Initial Resolution",
|
||||||
|
"desc": "The first part of a dotted name should resolve as any other name.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
"c": {
|
||||||
|
"d": {
|
||||||
|
"e": {
|
||||||
|
"name": "Phil"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"b": {
|
||||||
|
"c": {
|
||||||
|
"d": {
|
||||||
|
"e": {
|
||||||
|
"name": "Wrong"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{#a}}{{b.c.d.e.name}}{{/a}}\" == \"Phil\"",
|
||||||
|
"expected": "\"Phil\" == \"Phil\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Context Precedence",
|
||||||
|
"desc": "Dotted names should be resolved against former resolutions.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"b": {
|
||||||
|
"c": "ERROR"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{#a}}{{b.c}}{{/a}}",
|
||||||
|
"expected": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names are never single keys",
|
||||||
|
"desc": "Dotted names shall not be parsed as single, atomic keys",
|
||||||
|
"data": {
|
||||||
|
"a.b": "c"
|
||||||
|
},
|
||||||
|
"template": "{{a.b}}",
|
||||||
|
"expected": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - No Masking",
|
||||||
|
"desc": "Dotted Names in a given context are unvavailable due to dot splitting",
|
||||||
|
"data": {
|
||||||
|
"a.b": "c",
|
||||||
|
"a": {
|
||||||
|
"b": "d"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{a.b}}",
|
||||||
|
"expected": "d"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterators - Basic Interpolation",
|
||||||
|
"desc": "Unadorned tags should interpolate content into the template.",
|
||||||
|
"data": "world",
|
||||||
|
"template": "Hello, {{.}}!\n",
|
||||||
|
"expected": "Hello, world!\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterators - HTML Escaping",
|
||||||
|
"desc": "Basic interpolation should be HTML escaped.",
|
||||||
|
"data": "& \" < >",
|
||||||
|
"template": "These characters should be HTML escaped: {{.}}\n",
|
||||||
|
"expected": "These characters should be HTML escaped: & " < >\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterators - Triple Mustache",
|
||||||
|
"desc": "Triple mustaches should interpolate without HTML escaping.",
|
||||||
|
"data": "& \" < >",
|
||||||
|
"template": "These characters should not be HTML escaped: {{{.}}}\n",
|
||||||
|
"expected": "These characters should not be HTML escaped: & \" < >\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterators - Ampersand",
|
||||||
|
"desc": "Ampersand should interpolate without HTML escaping.",
|
||||||
|
"data": "& \" < >",
|
||||||
|
"template": "These characters should not be HTML escaped: {{&.}}\n",
|
||||||
|
"expected": "These characters should not be HTML escaped: & \" < >\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterators - Basic Integer Interpolation",
|
||||||
|
"desc": "Integers should interpolate seamlessly.",
|
||||||
|
"data": 85,
|
||||||
|
"template": "\"{{.}} miles an hour!\"",
|
||||||
|
"expected": "\"85 miles an hour!\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Interpolation - Surrounding Whitespace",
|
||||||
|
"desc": "Interpolation should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": "| {{string}} |",
|
||||||
|
"expected": "| --- |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache - Surrounding Whitespace",
|
||||||
|
"desc": "Interpolation should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": "| {{{string}}} |",
|
||||||
|
"expected": "| --- |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ampersand - Surrounding Whitespace",
|
||||||
|
"desc": "Interpolation should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": "| {{&string}} |",
|
||||||
|
"expected": "| --- |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Interpolation - Standalone",
|
||||||
|
"desc": "Standalone interpolation should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": " {{string}}\n",
|
||||||
|
"expected": " ---\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache - Standalone",
|
||||||
|
"desc": "Standalone interpolation should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": " {{{string}}}\n",
|
||||||
|
"expected": " ---\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ampersand - Standalone",
|
||||||
|
"desc": "Standalone interpolation should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": " {{&string}}\n",
|
||||||
|
"expected": " ---\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Interpolation With Padding",
|
||||||
|
"desc": "Superfluous in-tag whitespace should be ignored.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": "|{{ string }}|",
|
||||||
|
"expected": "|---|"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache With Padding",
|
||||||
|
"desc": "Superfluous in-tag whitespace should be ignored.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": "|{{{ string }}}|",
|
||||||
|
"expected": "|---|"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ampersand With Padding",
|
||||||
|
"desc": "Superfluous in-tag whitespace should be ignored.",
|
||||||
|
"data": {
|
||||||
|
"string": "---"
|
||||||
|
},
|
||||||
|
"template": "|{{& string }}|",
|
||||||
|
"expected": "|---|"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,317 @@
|
|||||||
|
overview: |
|
||||||
|
Interpolation tags are used to integrate dynamic content into the template.
|
||||||
|
|
||||||
|
The tag's content MUST be a non-whitespace character sequence NOT containing
|
||||||
|
the current closing delimiter.
|
||||||
|
|
||||||
|
This tag's content names the data to replace the tag. A single period (`.`)
|
||||||
|
indicates that the item currently sitting atop the context stack should be
|
||||||
|
used; otherwise, name resolution is as follows:
|
||||||
|
1) Split the name on periods; the first part is the name to resolve, any
|
||||||
|
remaining parts should be retained.
|
||||||
|
2) Walk the context stack from top to bottom, finding the first context
|
||||||
|
that is a) a hash containing the name as a key OR b) an object responding
|
||||||
|
to a method with the given name.
|
||||||
|
3) If the context is a hash, the data is the value associated with the
|
||||||
|
name.
|
||||||
|
4) If the context is an object, the data is the value returned by the
|
||||||
|
method with the given name.
|
||||||
|
5) If any name parts were retained in step 1, each should be resolved
|
||||||
|
against a context stack containing only the result from the former
|
||||||
|
resolution. If any part fails resolution, the result should be considered
|
||||||
|
falsey, and should interpolate as the empty string.
|
||||||
|
Data should be coerced into a string (and escaped, if appropriate) before
|
||||||
|
interpolation.
|
||||||
|
|
||||||
|
The Interpolation tags MUST NOT be treated as standalone.
|
||||||
|
tests:
|
||||||
|
- name: No Interpolation
|
||||||
|
desc: Mustache-free templates should render as-is.
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
Hello from {Mustache}!
|
||||||
|
expected: |
|
||||||
|
Hello from {Mustache}!
|
||||||
|
|
||||||
|
- name: Basic Interpolation
|
||||||
|
desc: Unadorned tags should interpolate content into the template.
|
||||||
|
data: { subject: "world" }
|
||||||
|
template: |
|
||||||
|
Hello, {{subject}}!
|
||||||
|
expected: |
|
||||||
|
Hello, world!
|
||||||
|
|
||||||
|
- name: No Re-interpolation
|
||||||
|
desc: Interpolated tag output should not be re-interpolated.
|
||||||
|
data: { template: '{{planet}}', planet: 'Earth' }
|
||||||
|
template: '{{template}}: {{planet}}'
|
||||||
|
expected: '{{planet}}: Earth'
|
||||||
|
|
||||||
|
- name: HTML Escaping
|
||||||
|
desc: Basic interpolation should be HTML escaped.
|
||||||
|
data: { forbidden: '& " < >' }
|
||||||
|
template: |
|
||||||
|
These characters should be HTML escaped: {{forbidden}}
|
||||||
|
expected: |
|
||||||
|
These characters should be HTML escaped: & " < >
|
||||||
|
|
||||||
|
- name: Triple Mustache
|
||||||
|
desc: Triple mustaches should interpolate without HTML escaping.
|
||||||
|
data: { forbidden: '& " < >' }
|
||||||
|
template: |
|
||||||
|
These characters should not be HTML escaped: {{{forbidden}}}
|
||||||
|
expected: |
|
||||||
|
These characters should not be HTML escaped: & " < >
|
||||||
|
|
||||||
|
- name: Ampersand
|
||||||
|
desc: Ampersand should interpolate without HTML escaping.
|
||||||
|
data: { forbidden: '& " < >' }
|
||||||
|
template: |
|
||||||
|
These characters should not be HTML escaped: {{&forbidden}}
|
||||||
|
expected: |
|
||||||
|
These characters should not be HTML escaped: & " < >
|
||||||
|
|
||||||
|
- name: Basic Integer Interpolation
|
||||||
|
desc: Integers should interpolate seamlessly.
|
||||||
|
data: { mph: 85 }
|
||||||
|
template: '"{{mph}} miles an hour!"'
|
||||||
|
expected: '"85 miles an hour!"'
|
||||||
|
|
||||||
|
- name: Triple Mustache Integer Interpolation
|
||||||
|
desc: Integers should interpolate seamlessly.
|
||||||
|
data: { mph: 85 }
|
||||||
|
template: '"{{{mph}}} miles an hour!"'
|
||||||
|
expected: '"85 miles an hour!"'
|
||||||
|
|
||||||
|
- name: Ampersand Integer Interpolation
|
||||||
|
desc: Integers should interpolate seamlessly.
|
||||||
|
data: { mph: 85 }
|
||||||
|
template: '"{{&mph}} miles an hour!"'
|
||||||
|
expected: '"85 miles an hour!"'
|
||||||
|
|
||||||
|
- name: Basic Decimal Interpolation
|
||||||
|
desc: Decimals should interpolate seamlessly with proper significance.
|
||||||
|
data: { power: 1.210 }
|
||||||
|
template: '"{{power}} jiggawatts!"'
|
||||||
|
expected: '"1.21 jiggawatts!"'
|
||||||
|
|
||||||
|
- name: Triple Mustache Decimal Interpolation
|
||||||
|
desc: Decimals should interpolate seamlessly with proper significance.
|
||||||
|
data: { power: 1.210 }
|
||||||
|
template: '"{{{power}}} jiggawatts!"'
|
||||||
|
expected: '"1.21 jiggawatts!"'
|
||||||
|
|
||||||
|
- name: Ampersand Decimal Interpolation
|
||||||
|
desc: Decimals should interpolate seamlessly with proper significance.
|
||||||
|
data: { power: 1.210 }
|
||||||
|
template: '"{{&power}} jiggawatts!"'
|
||||||
|
expected: '"1.21 jiggawatts!"'
|
||||||
|
|
||||||
|
- name: Basic Null Interpolation
|
||||||
|
desc: Nulls should interpolate as the empty string.
|
||||||
|
data: { cannot: null }
|
||||||
|
template: "I ({{cannot}}) be seen!"
|
||||||
|
expected: "I () be seen!"
|
||||||
|
|
||||||
|
- name: Triple Mustache Null Interpolation
|
||||||
|
desc: Nulls should interpolate as the empty string.
|
||||||
|
data: { cannot: null }
|
||||||
|
template: "I ({{{cannot}}}) be seen!"
|
||||||
|
expected: "I () be seen!"
|
||||||
|
|
||||||
|
- name: Ampersand Null Interpolation
|
||||||
|
desc: Nulls should interpolate as the empty string.
|
||||||
|
data: { cannot: null }
|
||||||
|
template: "I ({{&cannot}}) be seen!"
|
||||||
|
expected: "I () be seen!"
|
||||||
|
|
||||||
|
# Context Misses
|
||||||
|
|
||||||
|
- name: Basic Context Miss Interpolation
|
||||||
|
desc: Failed context lookups should default to empty strings.
|
||||||
|
data: { }
|
||||||
|
template: "I ({{cannot}}) be seen!"
|
||||||
|
expected: "I () be seen!"
|
||||||
|
|
||||||
|
- name: Triple Mustache Context Miss Interpolation
|
||||||
|
desc: Failed context lookups should default to empty strings.
|
||||||
|
data: { }
|
||||||
|
template: "I ({{{cannot}}}) be seen!"
|
||||||
|
expected: "I () be seen!"
|
||||||
|
|
||||||
|
- name: Ampersand Context Miss Interpolation
|
||||||
|
desc: Failed context lookups should default to empty strings.
|
||||||
|
data: { }
|
||||||
|
template: "I ({{&cannot}}) be seen!"
|
||||||
|
expected: "I () be seen!"
|
||||||
|
|
||||||
|
# Dotted Names
|
||||||
|
|
||||||
|
- name: Dotted Names - Basic Interpolation
|
||||||
|
desc: Dotted names should be considered a form of shorthand for sections.
|
||||||
|
data: { person: { name: 'Joe' } }
|
||||||
|
template: '"{{person.name}}" == "{{#person}}{{name}}{{/person}}"'
|
||||||
|
expected: '"Joe" == "Joe"'
|
||||||
|
|
||||||
|
- name: Dotted Names - Triple Mustache Interpolation
|
||||||
|
desc: Dotted names should be considered a form of shorthand for sections.
|
||||||
|
data: { person: { name: 'Joe' } }
|
||||||
|
template: '"{{{person.name}}}" == "{{#person}}{{{name}}}{{/person}}"'
|
||||||
|
expected: '"Joe" == "Joe"'
|
||||||
|
|
||||||
|
- name: Dotted Names - Ampersand Interpolation
|
||||||
|
desc: Dotted names should be considered a form of shorthand for sections.
|
||||||
|
data: { person: { name: 'Joe' } }
|
||||||
|
template: '"{{&person.name}}" == "{{#person}}{{&name}}{{/person}}"'
|
||||||
|
expected: '"Joe" == "Joe"'
|
||||||
|
|
||||||
|
- name: Dotted Names - Arbitrary Depth
|
||||||
|
desc: Dotted names should be functional to any level of nesting.
|
||||||
|
data:
|
||||||
|
a: { b: { c: { d: { e: { name: 'Phil' } } } } }
|
||||||
|
template: '"{{a.b.c.d.e.name}}" == "Phil"'
|
||||||
|
expected: '"Phil" == "Phil"'
|
||||||
|
|
||||||
|
- name: Dotted Names - Broken Chains
|
||||||
|
desc: Any falsey value prior to the last part of the name should yield ''.
|
||||||
|
data:
|
||||||
|
a: { }
|
||||||
|
template: '"{{a.b.c}}" == ""'
|
||||||
|
expected: '"" == ""'
|
||||||
|
|
||||||
|
- name: Dotted Names - Broken Chain Resolution
|
||||||
|
desc: Each part of a dotted name should resolve only against its parent.
|
||||||
|
data:
|
||||||
|
a: { b: { } }
|
||||||
|
c: { name: 'Jim' }
|
||||||
|
template: '"{{a.b.c.name}}" == ""'
|
||||||
|
expected: '"" == ""'
|
||||||
|
|
||||||
|
- name: Dotted Names - Initial Resolution
|
||||||
|
desc: The first part of a dotted name should resolve as any other name.
|
||||||
|
data:
|
||||||
|
a: { b: { c: { d: { e: { name: 'Phil' } } } } }
|
||||||
|
b: { c: { d: { e: { name: 'Wrong' } } } }
|
||||||
|
template: '"{{#a}}{{b.c.d.e.name}}{{/a}}" == "Phil"'
|
||||||
|
expected: '"Phil" == "Phil"'
|
||||||
|
|
||||||
|
- name: Dotted Names - Context Precedence
|
||||||
|
desc: Dotted names should be resolved against former resolutions.
|
||||||
|
data:
|
||||||
|
a: { b: { } }
|
||||||
|
b: { c: 'ERROR' }
|
||||||
|
template: '{{#a}}{{b.c}}{{/a}}'
|
||||||
|
expected: ''
|
||||||
|
|
||||||
|
- name: Dotted Names are never single keys
|
||||||
|
desc: Dotted names shall not be parsed as single, atomic keys
|
||||||
|
data:
|
||||||
|
a.b: c
|
||||||
|
template: '{{a.b}}'
|
||||||
|
expected: ''
|
||||||
|
|
||||||
|
- name: Dotted Names - No Masking
|
||||||
|
desc: Dotted Names in a given context are unvavailable due to dot splitting
|
||||||
|
data:
|
||||||
|
a.b: c
|
||||||
|
a: { b: d }
|
||||||
|
template: '{{a.b}}'
|
||||||
|
expected: 'd'
|
||||||
|
|
||||||
|
# Implicit Iterators
|
||||||
|
|
||||||
|
- name: Implicit Iterators - Basic Interpolation
|
||||||
|
desc: Unadorned tags should interpolate content into the template.
|
||||||
|
data: "world"
|
||||||
|
template: |
|
||||||
|
Hello, {{.}}!
|
||||||
|
expected: |
|
||||||
|
Hello, world!
|
||||||
|
|
||||||
|
- name: Implicit Iterators - HTML Escaping
|
||||||
|
desc: Basic interpolation should be HTML escaped.
|
||||||
|
data: '& " < >'
|
||||||
|
template: |
|
||||||
|
These characters should be HTML escaped: {{.}}
|
||||||
|
expected: |
|
||||||
|
These characters should be HTML escaped: & " < >
|
||||||
|
|
||||||
|
- name: Implicit Iterators - Triple Mustache
|
||||||
|
desc: Triple mustaches should interpolate without HTML escaping.
|
||||||
|
data: '& " < >'
|
||||||
|
template: |
|
||||||
|
These characters should not be HTML escaped: {{{.}}}
|
||||||
|
expected: |
|
||||||
|
These characters should not be HTML escaped: & " < >
|
||||||
|
|
||||||
|
- name: Implicit Iterators - Ampersand
|
||||||
|
desc: Ampersand should interpolate without HTML escaping.
|
||||||
|
data: '& " < >'
|
||||||
|
template: |
|
||||||
|
These characters should not be HTML escaped: {{&.}}
|
||||||
|
expected: |
|
||||||
|
These characters should not be HTML escaped: & " < >
|
||||||
|
|
||||||
|
- name: Implicit Iterators - Basic Integer Interpolation
|
||||||
|
desc: Integers should interpolate seamlessly.
|
||||||
|
data: 85
|
||||||
|
template: '"{{.}} miles an hour!"'
|
||||||
|
expected: '"85 miles an hour!"'
|
||||||
|
|
||||||
|
# Whitespace Sensitivity
|
||||||
|
|
||||||
|
- name: Interpolation - Surrounding Whitespace
|
||||||
|
desc: Interpolation should not alter surrounding whitespace.
|
||||||
|
data: { string: '---' }
|
||||||
|
template: '| {{string}} |'
|
||||||
|
expected: '| --- |'
|
||||||
|
|
||||||
|
- name: Triple Mustache - Surrounding Whitespace
|
||||||
|
desc: Interpolation should not alter surrounding whitespace.
|
||||||
|
data: { string: '---' }
|
||||||
|
template: '| {{{string}}} |'
|
||||||
|
expected: '| --- |'
|
||||||
|
|
||||||
|
- name: Ampersand - Surrounding Whitespace
|
||||||
|
desc: Interpolation should not alter surrounding whitespace.
|
||||||
|
data: { string: '---' }
|
||||||
|
template: '| {{&string}} |'
|
||||||
|
expected: '| --- |'
|
||||||
|
|
||||||
|
- name: Interpolation - Standalone
|
||||||
|
desc: Standalone interpolation should not alter surrounding whitespace.
|
||||||
|
data: { string: '---' }
|
||||||
|
template: " {{string}}\n"
|
||||||
|
expected: " ---\n"
|
||||||
|
|
||||||
|
- name: Triple Mustache - Standalone
|
||||||
|
desc: Standalone interpolation should not alter surrounding whitespace.
|
||||||
|
data: { string: '---' }
|
||||||
|
template: " {{{string}}}\n"
|
||||||
|
expected: " ---\n"
|
||||||
|
|
||||||
|
- name: Ampersand - Standalone
|
||||||
|
desc: Standalone interpolation should not alter surrounding whitespace.
|
||||||
|
data: { string: '---' }
|
||||||
|
template: " {{&string}}\n"
|
||||||
|
expected: " ---\n"
|
||||||
|
|
||||||
|
# Whitespace Insensitivity
|
||||||
|
|
||||||
|
- name: Interpolation With Padding
|
||||||
|
desc: Superfluous in-tag whitespace should be ignored.
|
||||||
|
data: { string: "---" }
|
||||||
|
template: '|{{ string }}|'
|
||||||
|
expected: '|---|'
|
||||||
|
|
||||||
|
- name: Triple Mustache With Padding
|
||||||
|
desc: Superfluous in-tag whitespace should be ignored.
|
||||||
|
data: { string: "---" }
|
||||||
|
template: '|{{{ string }}}|'
|
||||||
|
expected: '|---|'
|
||||||
|
|
||||||
|
- name: Ampersand With Padding
|
||||||
|
desc: Superfluous in-tag whitespace should be ignored.
|
||||||
|
data: { string: "---" }
|
||||||
|
template: '|{{& string }}|'
|
||||||
|
expected: '|---|'
|
||||||
@@ -0,0 +1,227 @@
|
|||||||
|
{
|
||||||
|
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
|
||||||
|
"overview": "Inverted Section tags and End Section tags are used in combination to wrap a\nsection of the template.\n\nThese tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter; each Inverted Section tag MUST be\nfollowed by an End Section tag with the same content within the same\nsection.\n\nThis tag's content names the data to replace the tag. Name resolution is as\nfollows:\n 1) Split the name on periods; the first part is the name to resolve, any\n remaining parts should be retained.\n 2) Walk the context stack from top to bottom, finding the first context\n that is a) a hash containing the name as a key OR b) an object responding\n to a method with the given name.\n 3) If the context is a hash, the data is the value associated with the\n name.\n 4) If the context is an object and the method with the given name has an\n arity of 1, the method SHOULD be called with a String containing the\n unprocessed contents of the sections; the data is the value returned.\n 5) Otherwise, the data is the value returned by calling the method with\n the given name.\n 6) If any name parts were retained in step 1, each should be resolved\n against a context stack containing only the result from the former\n resolution. If any part fails resolution, the result should be considered\n falsey, and should interpolate as the empty string.\nIf the data is not of a list type, it is coerced into a list as follows: if\nthe data is truthy (e.g. `!!data == true`), use a single-element list\ncontaining the data, otherwise use an empty list.\n\nThis section MUST NOT be rendered unless the data list is empty.\n\nInverted Section and End Section tags SHOULD be treated as standalone when\nappropriate.\n",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "Falsey",
|
||||||
|
"desc": "Falsey sections should have their contents rendered.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": "\"{{^boolean}}This should be rendered.{{/boolean}}\"",
|
||||||
|
"expected": "\"This should be rendered.\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Truthy",
|
||||||
|
"desc": "Truthy sections should have their contents omitted.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": "\"{{^boolean}}This should not be rendered.{{/boolean}}\"",
|
||||||
|
"expected": "\"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Null is falsey",
|
||||||
|
"desc": "Null is falsey.",
|
||||||
|
"data": {
|
||||||
|
"null": null
|
||||||
|
},
|
||||||
|
"template": "\"{{^null}}This should be rendered.{{/null}}\"",
|
||||||
|
"expected": "\"This should be rendered.\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Context",
|
||||||
|
"desc": "Objects and hashes should behave like truthy values.",
|
||||||
|
"data": {
|
||||||
|
"context": {
|
||||||
|
"name": "Joe"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{^context}}Hi {{name}}.{{/context}}\"",
|
||||||
|
"expected": "\"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "List",
|
||||||
|
"desc": "Lists should behave like truthy values.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"n": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"n": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"n": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{^list}}{{n}}{{/list}}\"",
|
||||||
|
"expected": "\"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Empty List",
|
||||||
|
"desc": "Empty lists should behave like falsey values.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{^list}}Yay lists!{{/list}}\"",
|
||||||
|
"expected": "\"Yay lists!\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Doubled",
|
||||||
|
"desc": "Multiple inverted sections per template should be permitted.",
|
||||||
|
"data": {
|
||||||
|
"bool": false,
|
||||||
|
"two": "second"
|
||||||
|
},
|
||||||
|
"template": "{{^bool}}\n* first\n{{/bool}}\n* {{two}}\n{{^bool}}\n* third\n{{/bool}}\n",
|
||||||
|
"expected": "* first\n* second\n* third\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Nested (Falsey)",
|
||||||
|
"desc": "Nested falsey sections should have their contents rendered.",
|
||||||
|
"data": {
|
||||||
|
"bool": false
|
||||||
|
},
|
||||||
|
"template": "| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |",
|
||||||
|
"expected": "| A B C D E |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Nested (Truthy)",
|
||||||
|
"desc": "Nested truthy sections should be omitted.",
|
||||||
|
"data": {
|
||||||
|
"bool": true
|
||||||
|
},
|
||||||
|
"template": "| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |",
|
||||||
|
"expected": "| A E |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Context Misses",
|
||||||
|
"desc": "Failed context lookups should be considered falsey.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "[{{^missing}}Cannot find key 'missing'!{{/missing}}]",
|
||||||
|
"expected": "[Cannot find key 'missing'!]"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Truthy",
|
||||||
|
"desc": "Dotted names should be valid for Inverted Section tags.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
"c": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"\"",
|
||||||
|
"expected": "\"\" == \"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Falsey",
|
||||||
|
"desc": "Dotted names should be valid for Inverted Section tags.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
"c": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"Not Here\"",
|
||||||
|
"expected": "\"Not Here\" == \"Not Here\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Broken Chains",
|
||||||
|
"desc": "Dotted names that cannot be resolved should be considered falsey.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"Not Here\"",
|
||||||
|
"expected": "\"Not Here\" == \"Not Here\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Surrounding Whitespace",
|
||||||
|
"desc": "Inverted sections should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": " | {{^boolean}}\t|\t{{/boolean}} | \n",
|
||||||
|
"expected": " | \t|\t | \n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Internal Whitespace",
|
||||||
|
"desc": "Inverted should not alter internal whitespace.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": " | {{^boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n",
|
||||||
|
"expected": " | \n | \n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Indented Inline Sections",
|
||||||
|
"desc": "Single-line sections should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": " {{^boolean}}NO{{/boolean}}\n {{^boolean}}WAY{{/boolean}}\n",
|
||||||
|
"expected": " NO\n WAY\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Lines",
|
||||||
|
"desc": "Standalone lines should be removed from the template.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": "| This Is\n{{^boolean}}\n|\n{{/boolean}}\n| A Line\n",
|
||||||
|
"expected": "| This Is\n|\n| A Line\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Indented Lines",
|
||||||
|
"desc": "Standalone indented lines should be removed from the template.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": "| This Is\n {{^boolean}}\n|\n {{/boolean}}\n| A Line\n",
|
||||||
|
"expected": "| This Is\n|\n| A Line\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Line Endings",
|
||||||
|
"desc": "\"\\r\\n\" should be considered a newline for standalone tags.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": "|\r\n{{^boolean}}\r\n{{/boolean}}\r\n|",
|
||||||
|
"expected": "|\r\n|"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Previous Line",
|
||||||
|
"desc": "Standalone tags should not require a newline to precede them.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": " {{^boolean}}\n^{{/boolean}}\n/",
|
||||||
|
"expected": "^\n/"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Newline",
|
||||||
|
"desc": "Standalone tags should not require a newline to follow them.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": "^{{^boolean}}\n/\n {{/boolean}}",
|
||||||
|
"expected": "^\n/\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Padding",
|
||||||
|
"desc": "Superfluous in-tag whitespace should be ignored.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": "|{{^ boolean }}={{/ boolean }}|",
|
||||||
|
"expected": "|=|"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,199 @@
|
|||||||
|
overview: |
|
||||||
|
Inverted Section tags and End Section tags are used in combination to wrap a
|
||||||
|
section of the template.
|
||||||
|
|
||||||
|
These tags' content MUST be a non-whitespace character sequence NOT
|
||||||
|
containing the current closing delimiter; each Inverted Section tag MUST be
|
||||||
|
followed by an End Section tag with the same content within the same
|
||||||
|
section.
|
||||||
|
|
||||||
|
This tag's content names the data to replace the tag. Name resolution is as
|
||||||
|
follows:
|
||||||
|
1) Split the name on periods; the first part is the name to resolve, any
|
||||||
|
remaining parts should be retained.
|
||||||
|
2) Walk the context stack from top to bottom, finding the first context
|
||||||
|
that is a) a hash containing the name as a key OR b) an object responding
|
||||||
|
to a method with the given name.
|
||||||
|
3) If the context is a hash, the data is the value associated with the
|
||||||
|
name.
|
||||||
|
4) If the context is an object and the method with the given name has an
|
||||||
|
arity of 1, the method SHOULD be called with a String containing the
|
||||||
|
unprocessed contents of the sections; the data is the value returned.
|
||||||
|
5) Otherwise, the data is the value returned by calling the method with
|
||||||
|
the given name.
|
||||||
|
6) If any name parts were retained in step 1, each should be resolved
|
||||||
|
against a context stack containing only the result from the former
|
||||||
|
resolution. If any part fails resolution, the result should be considered
|
||||||
|
falsey, and should interpolate as the empty string.
|
||||||
|
If the data is not of a list type, it is coerced into a list as follows: if
|
||||||
|
the data is truthy (e.g. `!!data == true`), use a single-element list
|
||||||
|
containing the data, otherwise use an empty list.
|
||||||
|
|
||||||
|
This section MUST NOT be rendered unless the data list is empty.
|
||||||
|
|
||||||
|
Inverted Section and End Section tags SHOULD be treated as standalone when
|
||||||
|
appropriate.
|
||||||
|
tests:
|
||||||
|
- name: Falsey
|
||||||
|
desc: Falsey sections should have their contents rendered.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: '"{{^boolean}}This should be rendered.{{/boolean}}"'
|
||||||
|
expected: '"This should be rendered."'
|
||||||
|
|
||||||
|
- name: Truthy
|
||||||
|
desc: Truthy sections should have their contents omitted.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: '"{{^boolean}}This should not be rendered.{{/boolean}}"'
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Null is falsey
|
||||||
|
desc: Null is falsey.
|
||||||
|
data: { "null": null }
|
||||||
|
template: '"{{^null}}This should be rendered.{{/null}}"'
|
||||||
|
expected: '"This should be rendered."'
|
||||||
|
|
||||||
|
- name: Context
|
||||||
|
desc: Objects and hashes should behave like truthy values.
|
||||||
|
data: { context: { name: 'Joe' } }
|
||||||
|
template: '"{{^context}}Hi {{name}}.{{/context}}"'
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: List
|
||||||
|
desc: Lists should behave like truthy values.
|
||||||
|
data: { list: [ { n: 1 }, { n: 2 }, { n: 3 } ] }
|
||||||
|
template: '"{{^list}}{{n}}{{/list}}"'
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Empty List
|
||||||
|
desc: Empty lists should behave like falsey values.
|
||||||
|
data: { list: [ ] }
|
||||||
|
template: '"{{^list}}Yay lists!{{/list}}"'
|
||||||
|
expected: '"Yay lists!"'
|
||||||
|
|
||||||
|
- name: Doubled
|
||||||
|
desc: Multiple inverted sections per template should be permitted.
|
||||||
|
data: { bool: false, two: 'second' }
|
||||||
|
template: |
|
||||||
|
{{^bool}}
|
||||||
|
* first
|
||||||
|
{{/bool}}
|
||||||
|
* {{two}}
|
||||||
|
{{^bool}}
|
||||||
|
* third
|
||||||
|
{{/bool}}
|
||||||
|
expected: |
|
||||||
|
* first
|
||||||
|
* second
|
||||||
|
* third
|
||||||
|
|
||||||
|
- name: Nested (Falsey)
|
||||||
|
desc: Nested falsey sections should have their contents rendered.
|
||||||
|
data: { bool: false }
|
||||||
|
template: "| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |"
|
||||||
|
expected: "| A B C D E |"
|
||||||
|
|
||||||
|
- name: Nested (Truthy)
|
||||||
|
desc: Nested truthy sections should be omitted.
|
||||||
|
data: { bool: true }
|
||||||
|
template: "| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |"
|
||||||
|
expected: "| A E |"
|
||||||
|
|
||||||
|
- name: Context Misses
|
||||||
|
desc: Failed context lookups should be considered falsey.
|
||||||
|
data: { }
|
||||||
|
template: "[{{^missing}}Cannot find key 'missing'!{{/missing}}]"
|
||||||
|
expected: "[Cannot find key 'missing'!]"
|
||||||
|
|
||||||
|
# Dotted Names
|
||||||
|
|
||||||
|
- name: Dotted Names - Truthy
|
||||||
|
desc: Dotted names should be valid for Inverted Section tags.
|
||||||
|
data: { a: { b: { c: true } } }
|
||||||
|
template: '"{{^a.b.c}}Not Here{{/a.b.c}}" == ""'
|
||||||
|
expected: '"" == ""'
|
||||||
|
|
||||||
|
- name: Dotted Names - Falsey
|
||||||
|
desc: Dotted names should be valid for Inverted Section tags.
|
||||||
|
data: { a: { b: { c: false } } }
|
||||||
|
template: '"{{^a.b.c}}Not Here{{/a.b.c}}" == "Not Here"'
|
||||||
|
expected: '"Not Here" == "Not Here"'
|
||||||
|
|
||||||
|
- name: Dotted Names - Broken Chains
|
||||||
|
desc: Dotted names that cannot be resolved should be considered falsey.
|
||||||
|
data: { a: { } }
|
||||||
|
template: '"{{^a.b.c}}Not Here{{/a.b.c}}" == "Not Here"'
|
||||||
|
expected: '"Not Here" == "Not Here"'
|
||||||
|
|
||||||
|
# Whitespace Sensitivity
|
||||||
|
|
||||||
|
- name: Surrounding Whitespace
|
||||||
|
desc: Inverted sections should not alter surrounding whitespace.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: " | {{^boolean}}\t|\t{{/boolean}} | \n"
|
||||||
|
expected: " | \t|\t | \n"
|
||||||
|
|
||||||
|
- name: Internal Whitespace
|
||||||
|
desc: Inverted should not alter internal whitespace.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: " | {{^boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n"
|
||||||
|
expected: " | \n | \n"
|
||||||
|
|
||||||
|
- name: Indented Inline Sections
|
||||||
|
desc: Single-line sections should not alter surrounding whitespace.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: " {{^boolean}}NO{{/boolean}}\n {{^boolean}}WAY{{/boolean}}\n"
|
||||||
|
expected: " NO\n WAY\n"
|
||||||
|
|
||||||
|
- name: Standalone Lines
|
||||||
|
desc: Standalone lines should be removed from the template.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: |
|
||||||
|
| This Is
|
||||||
|
{{^boolean}}
|
||||||
|
|
|
||||||
|
{{/boolean}}
|
||||||
|
| A Line
|
||||||
|
expected: |
|
||||||
|
| This Is
|
||||||
|
|
|
||||||
|
| A Line
|
||||||
|
|
||||||
|
- name: Standalone Indented Lines
|
||||||
|
desc: Standalone indented lines should be removed from the template.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: |
|
||||||
|
| This Is
|
||||||
|
{{^boolean}}
|
||||||
|
|
|
||||||
|
{{/boolean}}
|
||||||
|
| A Line
|
||||||
|
expected: |
|
||||||
|
| This Is
|
||||||
|
|
|
||||||
|
| A Line
|
||||||
|
|
||||||
|
- name: Standalone Line Endings
|
||||||
|
desc: '"\r\n" should be considered a newline for standalone tags.'
|
||||||
|
data: { boolean: false }
|
||||||
|
template: "|\r\n{{^boolean}}\r\n{{/boolean}}\r\n|"
|
||||||
|
expected: "|\r\n|"
|
||||||
|
|
||||||
|
- name: Standalone Without Previous Line
|
||||||
|
desc: Standalone tags should not require a newline to precede them.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: " {{^boolean}}\n^{{/boolean}}\n/"
|
||||||
|
expected: "^\n/"
|
||||||
|
|
||||||
|
- name: Standalone Without Newline
|
||||||
|
desc: Standalone tags should not require a newline to follow them.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: "^{{^boolean}}\n/\n {{/boolean}}"
|
||||||
|
expected: "^\n/\n"
|
||||||
|
|
||||||
|
# Whitespace Insensitivity
|
||||||
|
|
||||||
|
- name: Padding
|
||||||
|
desc: Superfluous in-tag whitespace should be ignored.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: '|{{^ boolean }}={{/ boolean }}|'
|
||||||
|
expected: '|=|'
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
{
|
||||||
|
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
|
||||||
|
"overview": "Partial tags are used to expand an external template into the current\ntemplate.\n\nThe tag's content MUST be a non-whitespace character sequence NOT containing\nthe current closing delimiter.\n\nThis tag's content names the partial to inject. Set Delimiter tags MUST NOT\naffect the parsing of a partial. The partial MUST be rendered against the\ncontext stack local to the tag. If the named partial cannot be found, the\nempty string SHOULD be used instead, as in interpolations.\n\nPartial tags SHOULD be treated as standalone when appropriate. If this tag\nis used standalone, any whitespace preceding the tag should treated as\nindentation, and prepended to each line of the partial before rendering.\n",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "Basic Behavior",
|
||||||
|
"desc": "The greater-than operator should expand to the named partial.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "\"{{>text}}\"",
|
||||||
|
"partials": {
|
||||||
|
"text": "from partial"
|
||||||
|
},
|
||||||
|
"expected": "\"from partial\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Failed Lookup",
|
||||||
|
"desc": "The empty string should be used when the named partial is not found.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "\"{{>text}}\"",
|
||||||
|
"partials": {
|
||||||
|
},
|
||||||
|
"expected": "\"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Context",
|
||||||
|
"desc": "The greater-than operator should operate within the current context.",
|
||||||
|
"data": {
|
||||||
|
"text": "content"
|
||||||
|
},
|
||||||
|
"template": "\"{{>partial}}\"",
|
||||||
|
"partials": {
|
||||||
|
"partial": "*{{text}}*"
|
||||||
|
},
|
||||||
|
"expected": "\"*content*\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Recursion",
|
||||||
|
"desc": "The greater-than operator should properly recurse.",
|
||||||
|
"data": {
|
||||||
|
"content": "X",
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"content": "Y",
|
||||||
|
"nodes": [
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "{{>node}}",
|
||||||
|
"partials": {
|
||||||
|
"node": "{{content}}<{{#nodes}}{{>node}}{{/nodes}}>"
|
||||||
|
},
|
||||||
|
"expected": "X<Y<>>"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Nested",
|
||||||
|
"desc": "The greater-than operator should work from within partials.",
|
||||||
|
"data": {
|
||||||
|
"a": "hello",
|
||||||
|
"b": "world"
|
||||||
|
},
|
||||||
|
"template": "{{>outer}}",
|
||||||
|
"partials": {
|
||||||
|
"outer": "*{{a}} {{>inner}}*",
|
||||||
|
"inner": "{{b}}!"
|
||||||
|
},
|
||||||
|
"expected": "*hello world!*"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Surrounding Whitespace",
|
||||||
|
"desc": "The greater-than operator should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "| {{>partial}} |",
|
||||||
|
"partials": {
|
||||||
|
"partial": "\t|\t"
|
||||||
|
},
|
||||||
|
"expected": "| \t|\t |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Inline Indentation",
|
||||||
|
"desc": "Whitespace should be left untouched.",
|
||||||
|
"data": {
|
||||||
|
"data": "|"
|
||||||
|
},
|
||||||
|
"template": " {{data}} {{> partial}}\n",
|
||||||
|
"partials": {
|
||||||
|
"partial": ">\n>"
|
||||||
|
},
|
||||||
|
"expected": " | >\n>\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Line Endings",
|
||||||
|
"desc": "\"\\r\\n\" should be considered a newline for standalone tags.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "|\r\n{{>partial}}\r\n|",
|
||||||
|
"partials": {
|
||||||
|
"partial": ">"
|
||||||
|
},
|
||||||
|
"expected": "|\r\n>|"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Previous Line",
|
||||||
|
"desc": "Standalone tags should not require a newline to precede them.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": " {{>partial}}\n>",
|
||||||
|
"partials": {
|
||||||
|
"partial": ">\n>"
|
||||||
|
},
|
||||||
|
"expected": " >\n >>"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Newline",
|
||||||
|
"desc": "Standalone tags should not require a newline to follow them.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": ">\n {{>partial}}",
|
||||||
|
"partials": {
|
||||||
|
"partial": ">\n>"
|
||||||
|
},
|
||||||
|
"expected": ">\n >\n >"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Indentation",
|
||||||
|
"desc": "Each line of the partial should be indented before rendering.",
|
||||||
|
"data": {
|
||||||
|
"content": "<\n->"
|
||||||
|
},
|
||||||
|
"template": "\\\n {{>partial}}\n/\n",
|
||||||
|
"partials": {
|
||||||
|
"partial": "|\n{{{content}}}\n|\n"
|
||||||
|
},
|
||||||
|
"expected": "\\\n |\n <\n->\n |\n/\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Padding Whitespace",
|
||||||
|
"desc": "Superfluous in-tag whitespace should be ignored.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": "|{{> partial }}|",
|
||||||
|
"partials": {
|
||||||
|
"partial": "[]"
|
||||||
|
},
|
||||||
|
"expected": "|[]|"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
overview: |
|
||||||
|
Partial tags are used to expand an external template into the current
|
||||||
|
template.
|
||||||
|
|
||||||
|
The tag's content MUST be a non-whitespace character sequence NOT containing
|
||||||
|
the current closing delimiter.
|
||||||
|
|
||||||
|
This tag's content names the partial to inject. Set Delimiter tags MUST NOT
|
||||||
|
affect the parsing of a partial. The partial MUST be rendered against the
|
||||||
|
context stack local to the tag. If the named partial cannot be found, the
|
||||||
|
empty string SHOULD be used instead, as in interpolations.
|
||||||
|
|
||||||
|
Partial tags SHOULD be treated as standalone when appropriate. If this tag
|
||||||
|
is used standalone, any whitespace preceding the tag should treated as
|
||||||
|
indentation, and prepended to each line of the partial before rendering.
|
||||||
|
tests:
|
||||||
|
- name: Basic Behavior
|
||||||
|
desc: The greater-than operator should expand to the named partial.
|
||||||
|
data: { }
|
||||||
|
template: '"{{>text}}"'
|
||||||
|
partials: { text: 'from partial' }
|
||||||
|
expected: '"from partial"'
|
||||||
|
|
||||||
|
- name: Failed Lookup
|
||||||
|
desc: The empty string should be used when the named partial is not found.
|
||||||
|
data: { }
|
||||||
|
template: '"{{>text}}"'
|
||||||
|
partials: { }
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Context
|
||||||
|
desc: The greater-than operator should operate within the current context.
|
||||||
|
data: { text: 'content' }
|
||||||
|
template: '"{{>partial}}"'
|
||||||
|
partials: { partial: '*{{text}}*' }
|
||||||
|
expected: '"*content*"'
|
||||||
|
|
||||||
|
- name: Recursion
|
||||||
|
desc: The greater-than operator should properly recurse.
|
||||||
|
data: { content: "X", nodes: [ { content: "Y", nodes: [] } ] }
|
||||||
|
template: '{{>node}}'
|
||||||
|
partials: { node: '{{content}}<{{#nodes}}{{>node}}{{/nodes}}>' }
|
||||||
|
expected: 'X<Y<>>'
|
||||||
|
|
||||||
|
- name: Nested
|
||||||
|
desc: The greater-than operator should work from within partials.
|
||||||
|
data: { a: "hello", b: "world" }
|
||||||
|
template: '{{>outer}}'
|
||||||
|
partials: { outer: '*{{a}} {{>inner}}*', inner: '{{b}}!' }
|
||||||
|
expected: '*hello world!*'
|
||||||
|
|
||||||
|
# Whitespace Sensitivity
|
||||||
|
|
||||||
|
- name: Surrounding Whitespace
|
||||||
|
desc: The greater-than operator should not alter surrounding whitespace.
|
||||||
|
data: { }
|
||||||
|
template: '| {{>partial}} |'
|
||||||
|
partials: { partial: "\t|\t" }
|
||||||
|
expected: "| \t|\t |"
|
||||||
|
|
||||||
|
- name: Inline Indentation
|
||||||
|
desc: Whitespace should be left untouched.
|
||||||
|
data: { data: '|' }
|
||||||
|
template: " {{data}} {{> partial}}\n"
|
||||||
|
partials: { partial: ">\n>" }
|
||||||
|
expected: " | >\n>\n"
|
||||||
|
|
||||||
|
- name: Standalone Line Endings
|
||||||
|
desc: '"\r\n" should be considered a newline for standalone tags.'
|
||||||
|
data: { }
|
||||||
|
template: "|\r\n{{>partial}}\r\n|"
|
||||||
|
partials: { partial: ">" }
|
||||||
|
expected: "|\r\n>|"
|
||||||
|
|
||||||
|
- name: Standalone Without Previous Line
|
||||||
|
desc: Standalone tags should not require a newline to precede them.
|
||||||
|
data: { }
|
||||||
|
template: " {{>partial}}\n>"
|
||||||
|
partials: { partial: ">\n>"}
|
||||||
|
expected: " >\n >>"
|
||||||
|
|
||||||
|
- name: Standalone Without Newline
|
||||||
|
desc: Standalone tags should not require a newline to follow them.
|
||||||
|
data: { }
|
||||||
|
template: ">\n {{>partial}}"
|
||||||
|
partials: { partial: ">\n>" }
|
||||||
|
expected: ">\n >\n >"
|
||||||
|
|
||||||
|
- name: Standalone Indentation
|
||||||
|
desc: Each line of the partial should be indented before rendering.
|
||||||
|
data: { content: "<\n->" }
|
||||||
|
template: |
|
||||||
|
\
|
||||||
|
{{>partial}}
|
||||||
|
/
|
||||||
|
partials:
|
||||||
|
partial: |
|
||||||
|
|
|
||||||
|
{{{content}}}
|
||||||
|
|
|
||||||
|
expected: |
|
||||||
|
\
|
||||||
|
|
|
||||||
|
<
|
||||||
|
->
|
||||||
|
|
|
||||||
|
/
|
||||||
|
|
||||||
|
# Whitespace Insensitivity
|
||||||
|
|
||||||
|
- name: Padding Whitespace
|
||||||
|
desc: Superfluous in-tag whitespace should be ignored.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: "|{{> partial }}|"
|
||||||
|
partials: { partial: "[]" }
|
||||||
|
expected: '|[]|'
|
||||||
@@ -0,0 +1,423 @@
|
|||||||
|
{
|
||||||
|
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
|
||||||
|
"overview": "Section tags and End Section tags are used in combination to wrap a section\nof the template for iteration.\n\nThese tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter; each Section tag MUST be followed\nby an End Section tag with the same content within the same section.\n\nThis tag's content names the data to replace the tag. Name resolution is as\nfollows:\n 1) If the name is a single period (.), the data is the item currently\n sitting atop the context stack. Skip the rest of these steps.\n 2) Split the name on periods; the first part is the name to resolve, any\n remaining parts should be retained.\n 3) Walk the context stack from top to bottom, finding the first context\n that is a) a hash containing the name as a key OR b) an object responding\n to a method with the given name.\n 4) If the context is a hash, the data is the value associated with the\n name.\n 5) If the context is an object and the method with the given name has an\n arity of 1, the method SHOULD be called with a String containing the\n unprocessed contents of the sections; the data is the value returned.\n 6) Otherwise, the data is the value returned by calling the method with\n the given name.\n 7) If any name parts were retained in step 1, each should be resolved\n against a context stack containing only the result from the former\n resolution. If any part fails resolution, the result should be considered\n falsey, and should interpolate as the empty string.\n\nIf the data is not of a list type, it is coerced into a list as follows: if\nthe data is truthy (e.g. `!!data == true`), use a single-element list\ncontaining the data, otherwise use an empty list.\n\nFor each element in the data list, the element MUST be pushed onto the\ncontext stack, the section MUST be rendered, and the element MUST be popped\noff the context stack.\n\nSection and End Section tags SHOULD be treated as standalone when\nappropriate.\n",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "Truthy",
|
||||||
|
"desc": "Truthy sections should have their contents rendered.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": "\"{{#boolean}}This should be rendered.{{/boolean}}\"",
|
||||||
|
"expected": "\"This should be rendered.\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Falsey",
|
||||||
|
"desc": "Falsey sections should have their contents omitted.",
|
||||||
|
"data": {
|
||||||
|
"boolean": false
|
||||||
|
},
|
||||||
|
"template": "\"{{#boolean}}This should not be rendered.{{/boolean}}\"",
|
||||||
|
"expected": "\"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Null is falsey",
|
||||||
|
"desc": "Null is falsey.",
|
||||||
|
"data": {
|
||||||
|
"null": null
|
||||||
|
},
|
||||||
|
"template": "\"{{#null}}This should not be rendered.{{/null}}\"",
|
||||||
|
"expected": "\"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Context",
|
||||||
|
"desc": "Objects and hashes should be pushed onto the context stack.",
|
||||||
|
"data": {
|
||||||
|
"context": {
|
||||||
|
"name": "Joe"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{#context}}Hi {{name}}.{{/context}}\"",
|
||||||
|
"expected": "\"Hi Joe.\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Parent contexts",
|
||||||
|
"desc": "Names missing in the current context are looked up in the stack.",
|
||||||
|
"data": {
|
||||||
|
"a": "foo",
|
||||||
|
"b": "wrong",
|
||||||
|
"sec": {
|
||||||
|
"b": "bar"
|
||||||
|
},
|
||||||
|
"c": {
|
||||||
|
"d": "baz"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{#sec}}{{a}}, {{b}}, {{c.d}}{{/sec}}\"",
|
||||||
|
"expected": "\"foo, bar, baz\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Variable test",
|
||||||
|
"desc": "Non-false sections have their value at the top of context,\naccessible as {{.}} or through the parent context. This gives\na simple way to display content conditionally if a variable exists.\n",
|
||||||
|
"data": {
|
||||||
|
"foo": "bar"
|
||||||
|
},
|
||||||
|
"template": "\"{{#foo}}{{.}} is {{foo}}{{/foo}}\"",
|
||||||
|
"expected": "\"bar is bar\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "List Contexts",
|
||||||
|
"desc": "All elements on the context stack should be accessible within lists.",
|
||||||
|
"data": {
|
||||||
|
"tops": [
|
||||||
|
{
|
||||||
|
"tname": {
|
||||||
|
"upper": "A",
|
||||||
|
"lower": "a"
|
||||||
|
},
|
||||||
|
"middles": [
|
||||||
|
{
|
||||||
|
"mname": "1",
|
||||||
|
"bottoms": [
|
||||||
|
{
|
||||||
|
"bname": "x"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bname": "y"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "{{#tops}}{{#middles}}{{tname.lower}}{{mname}}.{{#bottoms}}{{tname.upper}}{{mname}}{{bname}}.{{/bottoms}}{{/middles}}{{/tops}}",
|
||||||
|
"expected": "a1.A1x.A1y."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Deeply Nested Contexts",
|
||||||
|
"desc": "All elements on the context stack should be accessible.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"one": 1
|
||||||
|
},
|
||||||
|
"b": {
|
||||||
|
"two": 2
|
||||||
|
},
|
||||||
|
"c": {
|
||||||
|
"three": 3,
|
||||||
|
"d": {
|
||||||
|
"four": 4,
|
||||||
|
"five": 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{#a}}\n{{one}}\n{{#b}}\n{{one}}{{two}}{{one}}\n{{#c}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{#d}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{#five}}\n{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}\n{{one}}{{two}}{{three}}{{four}}{{.}}6{{.}}{{four}}{{three}}{{two}}{{one}}\n{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}\n{{/five}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{/d}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{/c}}\n{{one}}{{two}}{{one}}\n{{/b}}\n{{one}}\n{{/a}}\n",
|
||||||
|
"expected": "1\n121\n12321\n1234321\n123454321\n12345654321\n123454321\n1234321\n12321\n121\n1\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "List",
|
||||||
|
"desc": "Lists should be iterated; list items should visit the context stack.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"item": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"item": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"item": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}{{item}}{{/list}}\"",
|
||||||
|
"expected": "\"123\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Empty List",
|
||||||
|
"desc": "Empty lists should behave like falsey values.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}Yay lists!{{/list}}\"",
|
||||||
|
"expected": "\"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Doubled",
|
||||||
|
"desc": "Multiple sections per template should be permitted.",
|
||||||
|
"data": {
|
||||||
|
"bool": true,
|
||||||
|
"two": "second"
|
||||||
|
},
|
||||||
|
"template": "{{#bool}}\n* first\n{{/bool}}\n* {{two}}\n{{#bool}}\n* third\n{{/bool}}\n",
|
||||||
|
"expected": "* first\n* second\n* third\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Nested (Truthy)",
|
||||||
|
"desc": "Nested truthy sections should have their contents rendered.",
|
||||||
|
"data": {
|
||||||
|
"bool": true
|
||||||
|
},
|
||||||
|
"template": "| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |",
|
||||||
|
"expected": "| A B C D E |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Nested (Falsey)",
|
||||||
|
"desc": "Nested falsey sections should be omitted.",
|
||||||
|
"data": {
|
||||||
|
"bool": false
|
||||||
|
},
|
||||||
|
"template": "| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |",
|
||||||
|
"expected": "| A E |"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Context Misses",
|
||||||
|
"desc": "Failed context lookups should be considered falsey.",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "[{{#missing}}Found key 'missing'!{{/missing}}]",
|
||||||
|
"expected": "[]"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterator - String",
|
||||||
|
"desc": "Implicit iterators should directly interpolate strings.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
"a",
|
||||||
|
"b",
|
||||||
|
"c",
|
||||||
|
"d",
|
||||||
|
"e"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}({{.}}){{/list}}\"",
|
||||||
|
"expected": "\"(a)(b)(c)(d)(e)\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterator - Integer",
|
||||||
|
"desc": "Implicit iterators should cast integers to strings and interpolate.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
5
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}({{.}}){{/list}}\"",
|
||||||
|
"expected": "\"(1)(2)(3)(4)(5)\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterator - Decimal",
|
||||||
|
"desc": "Implicit iterators should cast decimals to strings and interpolate.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
1.1,
|
||||||
|
2.2,
|
||||||
|
3.3,
|
||||||
|
4.4,
|
||||||
|
5.5
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}({{.}}){{/list}}\"",
|
||||||
|
"expected": "\"(1.1)(2.2)(3.3)(4.4)(5.5)\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterator - Array",
|
||||||
|
"desc": "Implicit iterators should allow iterating over nested arrays.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
[
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"a",
|
||||||
|
"b",
|
||||||
|
"c"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}({{#.}}{{.}}{{/.}}){{/list}}\"",
|
||||||
|
"expected": "\"(123)(abc)\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterator - HTML Escaping",
|
||||||
|
"desc": "Implicit iterators with basic interpolation should be HTML escaped.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
"&",
|
||||||
|
"\"",
|
||||||
|
"<",
|
||||||
|
">"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}({{.}}){{/list}}\"",
|
||||||
|
"expected": "\"(&)(")(<)(>)\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterator - Triple mustache",
|
||||||
|
"desc": "Implicit iterators in triple mustache should interpolate without HTML escaping.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
"&",
|
||||||
|
"\"",
|
||||||
|
"<",
|
||||||
|
">"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}({{{.}}}){{/list}}\"",
|
||||||
|
"expected": "\"(&)(\")(<)(>)\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterator - Ampersand",
|
||||||
|
"desc": "Implicit iterators in an Ampersand tag should interpolate without HTML escaping.",
|
||||||
|
"data": {
|
||||||
|
"list": [
|
||||||
|
"&",
|
||||||
|
"\"",
|
||||||
|
"<",
|
||||||
|
">"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"template": "\"{{#list}}({{&.}}){{/list}}\"",
|
||||||
|
"expected": "\"(&)(\")(<)(>)\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Implicit Iterator - Root-level",
|
||||||
|
"desc": "Implicit iterators should work on root-level lists.",
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"value": "a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value": "b"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"template": "\"{{#.}}({{value}}){{/.}}\"",
|
||||||
|
"expected": "\"(a)(b)\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Truthy",
|
||||||
|
"desc": "Dotted names should be valid for Section tags.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
"c": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{#a.b.c}}Here{{/a.b.c}}\" == \"Here\"",
|
||||||
|
"expected": "\"Here\" == \"Here\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Falsey",
|
||||||
|
"desc": "Dotted names should be valid for Section tags.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
"c": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\"",
|
||||||
|
"expected": "\"\" == \"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Dotted Names - Broken Chains",
|
||||||
|
"desc": "Dotted names that cannot be resolved should be considered falsey.",
|
||||||
|
"data": {
|
||||||
|
"a": {
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\"",
|
||||||
|
"expected": "\"\" == \"\""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Surrounding Whitespace",
|
||||||
|
"desc": "Sections should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": " | {{#boolean}}\t|\t{{/boolean}} | \n",
|
||||||
|
"expected": " | \t|\t | \n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Internal Whitespace",
|
||||||
|
"desc": "Sections should not alter internal whitespace.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": " | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n",
|
||||||
|
"expected": " | \n | \n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Indented Inline Sections",
|
||||||
|
"desc": "Single-line sections should not alter surrounding whitespace.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": " {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n",
|
||||||
|
"expected": " YES\n GOOD\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Lines",
|
||||||
|
"desc": "Standalone lines should be removed from the template.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": "| This Is\n{{#boolean}}\n|\n{{/boolean}}\n| A Line\n",
|
||||||
|
"expected": "| This Is\n|\n| A Line\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Indented Standalone Lines",
|
||||||
|
"desc": "Indented standalone lines should be removed from the template.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": "| This Is\n {{#boolean}}\n|\n {{/boolean}}\n| A Line\n",
|
||||||
|
"expected": "| This Is\n|\n| A Line\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Line Endings",
|
||||||
|
"desc": "\"\\r\\n\" should be considered a newline for standalone tags.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": "|\r\n{{#boolean}}\r\n{{/boolean}}\r\n|",
|
||||||
|
"expected": "|\r\n|"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Previous Line",
|
||||||
|
"desc": "Standalone tags should not require a newline to precede them.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": " {{#boolean}}\n#{{/boolean}}\n/",
|
||||||
|
"expected": "#\n/"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone Without Newline",
|
||||||
|
"desc": "Standalone tags should not require a newline to follow them.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": "#{{#boolean}}\n/\n {{/boolean}}",
|
||||||
|
"expected": "#\n/\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Padding",
|
||||||
|
"desc": "Superfluous in-tag whitespace should be ignored.",
|
||||||
|
"data": {
|
||||||
|
"boolean": true
|
||||||
|
},
|
||||||
|
"template": "|{{# boolean }}={{/ boolean }}|",
|
||||||
|
"expected": "|=|"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,331 @@
|
|||||||
|
overview: |
|
||||||
|
Section tags and End Section tags are used in combination to wrap a section
|
||||||
|
of the template for iteration.
|
||||||
|
|
||||||
|
These tags' content MUST be a non-whitespace character sequence NOT
|
||||||
|
containing the current closing delimiter; each Section tag MUST be followed
|
||||||
|
by an End Section tag with the same content within the same section.
|
||||||
|
|
||||||
|
This tag's content names the data to replace the tag. Name resolution is as
|
||||||
|
follows:
|
||||||
|
1) If the name is a single period (.), the data is the item currently
|
||||||
|
sitting atop the context stack. Skip the rest of these steps.
|
||||||
|
2) Split the name on periods; the first part is the name to resolve, any
|
||||||
|
remaining parts should be retained.
|
||||||
|
3) Walk the context stack from top to bottom, finding the first context
|
||||||
|
that is a) a hash containing the name as a key OR b) an object responding
|
||||||
|
to a method with the given name.
|
||||||
|
4) If the context is a hash, the data is the value associated with the
|
||||||
|
name.
|
||||||
|
5) If the context is an object and the method with the given name has an
|
||||||
|
arity of 1, the method SHOULD be called with a String containing the
|
||||||
|
unprocessed contents of the sections; the data is the value returned.
|
||||||
|
6) Otherwise, the data is the value returned by calling the method with
|
||||||
|
the given name.
|
||||||
|
7) If any name parts were retained in step 1, each should be resolved
|
||||||
|
against a context stack containing only the result from the former
|
||||||
|
resolution. If any part fails resolution, the result should be considered
|
||||||
|
falsey, and should interpolate as the empty string.
|
||||||
|
|
||||||
|
If the data is not of a list type, it is coerced into a list as follows: if
|
||||||
|
the data is truthy (e.g. `!!data == true`), use a single-element list
|
||||||
|
containing the data, otherwise use an empty list.
|
||||||
|
|
||||||
|
For each element in the data list, the element MUST be pushed onto the
|
||||||
|
context stack, the section MUST be rendered, and the element MUST be popped
|
||||||
|
off the context stack.
|
||||||
|
|
||||||
|
Section and End Section tags SHOULD be treated as standalone when
|
||||||
|
appropriate.
|
||||||
|
tests:
|
||||||
|
- name: Truthy
|
||||||
|
desc: Truthy sections should have their contents rendered.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: '"{{#boolean}}This should be rendered.{{/boolean}}"'
|
||||||
|
expected: '"This should be rendered."'
|
||||||
|
|
||||||
|
- name: Falsey
|
||||||
|
desc: Falsey sections should have their contents omitted.
|
||||||
|
data: { boolean: false }
|
||||||
|
template: '"{{#boolean}}This should not be rendered.{{/boolean}}"'
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Null is falsey
|
||||||
|
desc: Null is falsey.
|
||||||
|
data: { "null": null }
|
||||||
|
template: '"{{#null}}This should not be rendered.{{/null}}"'
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Context
|
||||||
|
desc: Objects and hashes should be pushed onto the context stack.
|
||||||
|
data: { context: { name: 'Joe' } }
|
||||||
|
template: '"{{#context}}Hi {{name}}.{{/context}}"'
|
||||||
|
expected: '"Hi Joe."'
|
||||||
|
|
||||||
|
- name: Parent contexts
|
||||||
|
desc: Names missing in the current context are looked up in the stack.
|
||||||
|
data: { a: "foo", b: "wrong", sec: { b: "bar" }, c : { d : "baz" } }
|
||||||
|
template: '"{{#sec}}{{a}}, {{b}}, {{c.d}}{{/sec}}"'
|
||||||
|
expected: '"foo, bar, baz"'
|
||||||
|
|
||||||
|
- name: Variable test
|
||||||
|
desc: |
|
||||||
|
Non-false sections have their value at the top of context,
|
||||||
|
accessible as {{.}} or through the parent context. This gives
|
||||||
|
a simple way to display content conditionally if a variable exists.
|
||||||
|
data: { foo: "bar" }
|
||||||
|
template: '"{{#foo}}{{.}} is {{foo}}{{/foo}}"'
|
||||||
|
expected: '"bar is bar"'
|
||||||
|
|
||||||
|
- name: List Contexts
|
||||||
|
desc: All elements on the context stack should be accessible within lists.
|
||||||
|
data:
|
||||||
|
tops:
|
||||||
|
- tname:
|
||||||
|
upper: "A"
|
||||||
|
lower: "a"
|
||||||
|
middles:
|
||||||
|
- mname: "1"
|
||||||
|
bottoms:
|
||||||
|
- bname: "x"
|
||||||
|
- bname: "y"
|
||||||
|
template: '{{#tops}}{{#middles}}{{tname.lower}}{{mname}}.{{#bottoms}}{{tname.upper}}{{mname}}{{bname}}.{{/bottoms}}{{/middles}}{{/tops}}'
|
||||||
|
expected: 'a1.A1x.A1y.'
|
||||||
|
|
||||||
|
- name: Deeply Nested Contexts
|
||||||
|
desc: All elements on the context stack should be accessible.
|
||||||
|
data:
|
||||||
|
a: { one: 1 }
|
||||||
|
b: { two: 2 }
|
||||||
|
c: { three: 3, d : { four : 4, five : 5 } }
|
||||||
|
template: |
|
||||||
|
{{#a}}
|
||||||
|
{{one}}
|
||||||
|
{{#b}}
|
||||||
|
{{one}}{{two}}{{one}}
|
||||||
|
{{#c}}
|
||||||
|
{{one}}{{two}}{{three}}{{two}}{{one}}
|
||||||
|
{{#d}}
|
||||||
|
{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}
|
||||||
|
{{#five}}
|
||||||
|
{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}
|
||||||
|
{{one}}{{two}}{{three}}{{four}}{{.}}6{{.}}{{four}}{{three}}{{two}}{{one}}
|
||||||
|
{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}
|
||||||
|
{{/five}}
|
||||||
|
{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}
|
||||||
|
{{/d}}
|
||||||
|
{{one}}{{two}}{{three}}{{two}}{{one}}
|
||||||
|
{{/c}}
|
||||||
|
{{one}}{{two}}{{one}}
|
||||||
|
{{/b}}
|
||||||
|
{{one}}
|
||||||
|
{{/a}}
|
||||||
|
expected: |
|
||||||
|
1
|
||||||
|
121
|
||||||
|
12321
|
||||||
|
1234321
|
||||||
|
123454321
|
||||||
|
12345654321
|
||||||
|
123454321
|
||||||
|
1234321
|
||||||
|
12321
|
||||||
|
121
|
||||||
|
1
|
||||||
|
|
||||||
|
- name: List
|
||||||
|
desc: Lists should be iterated; list items should visit the context stack.
|
||||||
|
data: { list: [ { item: 1 }, { item: 2 }, { item: 3 } ] }
|
||||||
|
template: '"{{#list}}{{item}}{{/list}}"'
|
||||||
|
expected: '"123"'
|
||||||
|
|
||||||
|
- name: Empty List
|
||||||
|
desc: Empty lists should behave like falsey values.
|
||||||
|
data: { list: [ ] }
|
||||||
|
template: '"{{#list}}Yay lists!{{/list}}"'
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Doubled
|
||||||
|
desc: Multiple sections per template should be permitted.
|
||||||
|
data: { bool: true, two: 'second' }
|
||||||
|
template: |
|
||||||
|
{{#bool}}
|
||||||
|
* first
|
||||||
|
{{/bool}}
|
||||||
|
* {{two}}
|
||||||
|
{{#bool}}
|
||||||
|
* third
|
||||||
|
{{/bool}}
|
||||||
|
expected: |
|
||||||
|
* first
|
||||||
|
* second
|
||||||
|
* third
|
||||||
|
|
||||||
|
- name: Nested (Truthy)
|
||||||
|
desc: Nested truthy sections should have their contents rendered.
|
||||||
|
data: { bool: true }
|
||||||
|
template: "| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |"
|
||||||
|
expected: "| A B C D E |"
|
||||||
|
|
||||||
|
- name: Nested (Falsey)
|
||||||
|
desc: Nested falsey sections should be omitted.
|
||||||
|
data: { bool: false }
|
||||||
|
template: "| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |"
|
||||||
|
expected: "| A E |"
|
||||||
|
|
||||||
|
- name: Context Misses
|
||||||
|
desc: Failed context lookups should be considered falsey.
|
||||||
|
data: { }
|
||||||
|
template: "[{{#missing}}Found key 'missing'!{{/missing}}]"
|
||||||
|
expected: "[]"
|
||||||
|
|
||||||
|
# Implicit Iterators
|
||||||
|
|
||||||
|
- name: Implicit Iterator - String
|
||||||
|
desc: Implicit iterators should directly interpolate strings.
|
||||||
|
data:
|
||||||
|
list: [ 'a', 'b', 'c', 'd', 'e' ]
|
||||||
|
template: '"{{#list}}({{.}}){{/list}}"'
|
||||||
|
expected: '"(a)(b)(c)(d)(e)"'
|
||||||
|
|
||||||
|
- name: Implicit Iterator - Integer
|
||||||
|
desc: Implicit iterators should cast integers to strings and interpolate.
|
||||||
|
data:
|
||||||
|
list: [ 1, 2, 3, 4, 5 ]
|
||||||
|
template: '"{{#list}}({{.}}){{/list}}"'
|
||||||
|
expected: '"(1)(2)(3)(4)(5)"'
|
||||||
|
|
||||||
|
- name: Implicit Iterator - Decimal
|
||||||
|
desc: Implicit iterators should cast decimals to strings and interpolate.
|
||||||
|
data:
|
||||||
|
list: [ 1.10, 2.20, 3.30, 4.40, 5.50 ]
|
||||||
|
template: '"{{#list}}({{.}}){{/list}}"'
|
||||||
|
expected: '"(1.1)(2.2)(3.3)(4.4)(5.5)"'
|
||||||
|
|
||||||
|
- name: Implicit Iterator - Array
|
||||||
|
desc: Implicit iterators should allow iterating over nested arrays.
|
||||||
|
data:
|
||||||
|
list: [ [1, 2, 3], ['a', 'b', 'c'] ]
|
||||||
|
template: '"{{#list}}({{#.}}{{.}}{{/.}}){{/list}}"'
|
||||||
|
expected: '"(123)(abc)"'
|
||||||
|
|
||||||
|
- name: Implicit Iterator - HTML Escaping
|
||||||
|
desc: Implicit iterators with basic interpolation should be HTML escaped.
|
||||||
|
data:
|
||||||
|
list: [ '&', '"', '<', '>' ]
|
||||||
|
template: '"{{#list}}({{.}}){{/list}}"'
|
||||||
|
expected: '"(&)(")(<)(>)"'
|
||||||
|
|
||||||
|
- name: Implicit Iterator - Triple mustache
|
||||||
|
desc: Implicit iterators in triple mustache should interpolate without HTML escaping.
|
||||||
|
data:
|
||||||
|
list: [ '&', '"', '<', '>' ]
|
||||||
|
template: '"{{#list}}({{{.}}}){{/list}}"'
|
||||||
|
expected: '"(&)(")(<)(>)"'
|
||||||
|
|
||||||
|
- name: Implicit Iterator - Ampersand
|
||||||
|
desc: Implicit iterators in an Ampersand tag should interpolate without HTML escaping.
|
||||||
|
data:
|
||||||
|
list: [ '&', '"', '<', '>' ]
|
||||||
|
template: '"{{#list}}({{&.}}){{/list}}"'
|
||||||
|
expected: '"(&)(")(<)(>)"'
|
||||||
|
|
||||||
|
- name: Implicit Iterator - Root-level
|
||||||
|
desc: Implicit iterators should work on root-level lists.
|
||||||
|
data: [ { value: 'a' }, { value: 'b' } ]
|
||||||
|
template: '"{{#.}}({{value}}){{/.}}"'
|
||||||
|
expected: '"(a)(b)"'
|
||||||
|
|
||||||
|
# Dotted Names
|
||||||
|
|
||||||
|
- name: Dotted Names - Truthy
|
||||||
|
desc: Dotted names should be valid for Section tags.
|
||||||
|
data: { a: { b: { c: true } } }
|
||||||
|
template: '"{{#a.b.c}}Here{{/a.b.c}}" == "Here"'
|
||||||
|
expected: '"Here" == "Here"'
|
||||||
|
|
||||||
|
- name: Dotted Names - Falsey
|
||||||
|
desc: Dotted names should be valid for Section tags.
|
||||||
|
data: { a: { b: { c: false } } }
|
||||||
|
template: '"{{#a.b.c}}Here{{/a.b.c}}" == ""'
|
||||||
|
expected: '"" == ""'
|
||||||
|
|
||||||
|
- name: Dotted Names - Broken Chains
|
||||||
|
desc: Dotted names that cannot be resolved should be considered falsey.
|
||||||
|
data: { a: { } }
|
||||||
|
template: '"{{#a.b.c}}Here{{/a.b.c}}" == ""'
|
||||||
|
expected: '"" == ""'
|
||||||
|
|
||||||
|
# Whitespace Sensitivity
|
||||||
|
|
||||||
|
- name: Surrounding Whitespace
|
||||||
|
desc: Sections should not alter surrounding whitespace.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: " | {{#boolean}}\t|\t{{/boolean}} | \n"
|
||||||
|
expected: " | \t|\t | \n"
|
||||||
|
|
||||||
|
- name: Internal Whitespace
|
||||||
|
desc: Sections should not alter internal whitespace.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: " | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n"
|
||||||
|
expected: " | \n | \n"
|
||||||
|
|
||||||
|
- name: Indented Inline Sections
|
||||||
|
desc: Single-line sections should not alter surrounding whitespace.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: " {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n"
|
||||||
|
expected: " YES\n GOOD\n"
|
||||||
|
|
||||||
|
- name: Standalone Lines
|
||||||
|
desc: Standalone lines should be removed from the template.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: |
|
||||||
|
| This Is
|
||||||
|
{{#boolean}}
|
||||||
|
|
|
||||||
|
{{/boolean}}
|
||||||
|
| A Line
|
||||||
|
expected: |
|
||||||
|
| This Is
|
||||||
|
|
|
||||||
|
| A Line
|
||||||
|
|
||||||
|
- name: Indented Standalone Lines
|
||||||
|
desc: Indented standalone lines should be removed from the template.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: |
|
||||||
|
| This Is
|
||||||
|
{{#boolean}}
|
||||||
|
|
|
||||||
|
{{/boolean}}
|
||||||
|
| A Line
|
||||||
|
expected: |
|
||||||
|
| This Is
|
||||||
|
|
|
||||||
|
| A Line
|
||||||
|
|
||||||
|
- name: Standalone Line Endings
|
||||||
|
desc: '"\r\n" should be considered a newline for standalone tags.'
|
||||||
|
data: { boolean: true }
|
||||||
|
template: "|\r\n{{#boolean}}\r\n{{/boolean}}\r\n|"
|
||||||
|
expected: "|\r\n|"
|
||||||
|
|
||||||
|
- name: Standalone Without Previous Line
|
||||||
|
desc: Standalone tags should not require a newline to precede them.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: " {{#boolean}}\n#{{/boolean}}\n/"
|
||||||
|
expected: "#\n/"
|
||||||
|
|
||||||
|
- name: Standalone Without Newline
|
||||||
|
desc: Standalone tags should not require a newline to follow them.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: "#{{#boolean}}\n/\n {{/boolean}}"
|
||||||
|
expected: "#\n/\n"
|
||||||
|
|
||||||
|
# Whitespace Insensitivity
|
||||||
|
|
||||||
|
- name: Padding
|
||||||
|
desc: Superfluous in-tag whitespace should be ignored.
|
||||||
|
data: { boolean: true }
|
||||||
|
template: '|{{# boolean }}={{/ boolean }}|'
|
||||||
|
expected: '|=|'
|
||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,377 @@
|
|||||||
|
overview: |
|
||||||
|
Rationale: this special notation was introduced primarily to allow the dynamic
|
||||||
|
loading of partials. The main advantage that this notation offers is to allow
|
||||||
|
dynamic loading of partials, which is particularly useful in cases where
|
||||||
|
polymorphic data needs to be rendered in different ways. Such cases would
|
||||||
|
otherwise be possible to render only with solutions that are convoluted,
|
||||||
|
inefficient, or both.
|
||||||
|
|
||||||
|
Example.
|
||||||
|
Let's consider the following data:
|
||||||
|
|
||||||
|
items: [
|
||||||
|
{ content: 'Hello, World!' },
|
||||||
|
{ url: 'http://example.com/foo.jpg' },
|
||||||
|
{ content: 'Some text' },
|
||||||
|
{ content: 'Some other text' },
|
||||||
|
{ url: 'http://example.com/bar.jpg' },
|
||||||
|
{ url: 'http://example.com/baz.jpg' },
|
||||||
|
{ content: 'Last text here' }
|
||||||
|
]
|
||||||
|
|
||||||
|
The goal is to render the different types of items in different ways. The
|
||||||
|
items having a key named `content` should be rendered with the template
|
||||||
|
`text.mustache`:
|
||||||
|
|
||||||
|
{{!text.mustache}}
|
||||||
|
{{content}}
|
||||||
|
|
||||||
|
And the items having a key named `url` should be rendered with the template
|
||||||
|
`image.mustache`:
|
||||||
|
|
||||||
|
{{!image.mustache}}
|
||||||
|
<img src="{{url}}"/>
|
||||||
|
|
||||||
|
There are already several ways to achieve this goal, here below are
|
||||||
|
illustrated and discussed the most significant solutions to this problem.
|
||||||
|
|
||||||
|
Using Pre-Processing
|
||||||
|
|
||||||
|
The idea is to use a secondary templating mechanism to dynamically generate
|
||||||
|
the template that will be rendered.
|
||||||
|
The template that our secondary templating mechanism generates might look
|
||||||
|
like this:
|
||||||
|
|
||||||
|
{{!template.mustache}}
|
||||||
|
{{items.1.content}}
|
||||||
|
<img src="{{items.2.url}}"/>
|
||||||
|
{{items.3.content}}
|
||||||
|
{{items.4.content}}
|
||||||
|
<img src="{{items.5.url}}"/>
|
||||||
|
<img src="{{items.6.url}}"/>
|
||||||
|
{{items.7.content}}
|
||||||
|
|
||||||
|
This solutions offers the advantages of having more control over the template
|
||||||
|
and minimizing the template blocks to the essential ones.
|
||||||
|
The drawbacks are the rendering speed and the complexity that the secondary
|
||||||
|
templating mechanism requires.
|
||||||
|
|
||||||
|
Using Lambdas
|
||||||
|
|
||||||
|
The idea is to inject functions into the data that will be later called from
|
||||||
|
the template.
|
||||||
|
This way the data will look like this:
|
||||||
|
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
content: 'Hello, World!',
|
||||||
|
html: function() { return '{{>text}}'; }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: 'http://example.com/foo.jpg',
|
||||||
|
html: function() { return '{{>image}}'; }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
content: 'Some text',
|
||||||
|
html: function() { return '{{>text}}'; }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
content: 'Some other text',
|
||||||
|
html: function() { return '{{>text}}'; }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: 'http://example.com/bar.jpg',
|
||||||
|
html: function() { return '{{>image}}'; }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: 'http://example.com/baz.jpg',
|
||||||
|
html: function() { return '{{>image}}'; }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
content: 'Last text here',
|
||||||
|
html: function() { return '{{>text}}'; }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
And the template will look like this:
|
||||||
|
|
||||||
|
{{!template.mustache}}
|
||||||
|
{{#items}}
|
||||||
|
{{{html}}}
|
||||||
|
{{/items}}
|
||||||
|
|
||||||
|
The advantage this solution offers is to have a light main template.
|
||||||
|
The drawback is that the data needs to embed logic and template tags in
|
||||||
|
it.
|
||||||
|
|
||||||
|
Using If-Else Blocks
|
||||||
|
|
||||||
|
The idea is to put some logic into the main template so it can select the
|
||||||
|
templates at rendering time:
|
||||||
|
|
||||||
|
{{!template.mustache}}
|
||||||
|
{{#items}}
|
||||||
|
{{#url}}
|
||||||
|
{{>image}}
|
||||||
|
{{/url}}
|
||||||
|
{{#content}}
|
||||||
|
{{>text}}
|
||||||
|
{{/content}}
|
||||||
|
{{/items}}
|
||||||
|
|
||||||
|
The main advantage of this solution is that it works without adding any
|
||||||
|
overhead fields to the data. It also documents which external templates are
|
||||||
|
appropriate for expansion in this position.
|
||||||
|
The drawback is that this solution isn't optimal for heterogeneous data sets
|
||||||
|
as the main template grows linearly with the number of polymorphic variants.
|
||||||
|
|
||||||
|
Using Dynamic Names
|
||||||
|
|
||||||
|
This is the solution proposed by this spec.
|
||||||
|
The idea is to load partials dynamically.
|
||||||
|
This way the data items have to be tagged with the corresponding partial name:
|
||||||
|
|
||||||
|
items: [
|
||||||
|
{ content: 'Hello, World!', dynamic: 'text' },
|
||||||
|
{ url: 'http://example.com/foo.jpg', dynamic: 'image' },
|
||||||
|
{ content: 'Some text', dynamic: 'text' },
|
||||||
|
{ content: 'Some other text', dynamic: 'text' },
|
||||||
|
{ url: 'http://example.com/bar.jpg', dynamic: 'image' },
|
||||||
|
{ url: 'http://example.com/baz.jpg', dynamic: 'image' },
|
||||||
|
{ content: 'Last text here', dynamic: 'text' }
|
||||||
|
]
|
||||||
|
|
||||||
|
And the template would simple look like this:
|
||||||
|
|
||||||
|
{{!template.mustache}}
|
||||||
|
{{#items}}
|
||||||
|
{{>*dynamic}}
|
||||||
|
{{/items}}
|
||||||
|
|
||||||
|
Summary:
|
||||||
|
|
||||||
|
+----------------+---------------------+-----------------------------------+
|
||||||
|
| Approach | Pros | Cons |
|
||||||
|
+----------------+---------------------+-----------------------------------+
|
||||||
|
| Pre-Processing | Essential template, | Secondary templating system |
|
||||||
|
| | more control | needed, slower rendering |
|
||||||
|
| Lambdas | Slim template | Data tagging, logic in data |
|
||||||
|
| If Blocks | No data overhead, | Template linear growth |
|
||||||
|
| | self-documenting | |
|
||||||
|
| Dynamic Names | Slim template | Data tagging |
|
||||||
|
+----------------+---------------------+-----------------------------------+
|
||||||
|
|
||||||
|
Dynamic Names are a special notation to dynamically determine a tag's content.
|
||||||
|
|
||||||
|
Dynamic Names MUST be a non-whitespace character sequence NOT containing
|
||||||
|
the current closing delimiter. A Dynamic Name consists of an asterisk,
|
||||||
|
followed by a dotted name. The dotted name follows the same notation as in an
|
||||||
|
Interpolation tag.
|
||||||
|
|
||||||
|
This tag's dotted name, which is the Dynamic Name excluding the
|
||||||
|
leading asterisk, references a key in the context whose value will be used in
|
||||||
|
place of the Dynamic Name itself as content of the tag. The dotted name
|
||||||
|
resolution produces the same value as an Interpolation tag and does not affect
|
||||||
|
the context for further processing.
|
||||||
|
|
||||||
|
Set Delimiter tags MUST NOT affect the resolution of a Dynamic Name. The
|
||||||
|
Dynamic Names MUST be resolved against the context stack local to the tag.
|
||||||
|
Failed resolution of the dynamic name SHOULD result in nothing being rendered.
|
||||||
|
|
||||||
|
Engines that implement Dynamic Names MUST support their use in Partial tags.
|
||||||
|
In engines that also implement the optional inheritance spec, Dynamic Names
|
||||||
|
inside Parent tags SHOULD be supported as well. Dynamic Names cannot be
|
||||||
|
resolved more than once (Dynamic Names cannot be nested).
|
||||||
|
|
||||||
|
tests:
|
||||||
|
- name: Basic Behavior - Partial
|
||||||
|
desc: The asterisk operator is used for dynamic partials.
|
||||||
|
data: { dynamic: 'content' }
|
||||||
|
template: '"{{>*dynamic}}"'
|
||||||
|
partials: { content: 'Hello, world!' }
|
||||||
|
expected: '"Hello, world!"'
|
||||||
|
|
||||||
|
- name: Basic Behavior - Name Resolution
|
||||||
|
desc: |
|
||||||
|
The asterisk is not part of the name that will be resolved in the context.
|
||||||
|
data: { dynamic: 'content', '*dynamic': 'wrong' }
|
||||||
|
template: '"{{>*dynamic}}"'
|
||||||
|
partials: { content: 'Hello, world!', wrong: 'Invisible' }
|
||||||
|
expected: '"Hello, world!"'
|
||||||
|
|
||||||
|
- name: Context Misses - Partial
|
||||||
|
desc: Failed context lookups should be considered falsey.
|
||||||
|
data: { }
|
||||||
|
template: '"{{>*missing}}"'
|
||||||
|
partials: { missing: 'Hello, world!' }
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Failed Lookup - Partial
|
||||||
|
desc: The empty string should be used when the named partial is not found.
|
||||||
|
data: { dynamic: 'content' }
|
||||||
|
template: '"{{>*dynamic}}"'
|
||||||
|
partials: { foobar: 'Hello, world!' }
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Context
|
||||||
|
desc: The dynamic partial should operate within the current context.
|
||||||
|
data: { text: 'Hello, world!', example: 'partial' }
|
||||||
|
template: '"{{>*example}}"'
|
||||||
|
partials: { partial: '*{{text}}*' }
|
||||||
|
expected: '"*Hello, world!*"'
|
||||||
|
|
||||||
|
- name: Dotted Names
|
||||||
|
desc: The dynamic partial should operate within the current context.
|
||||||
|
data: { text: 'Hello, world!', foo: { bar: { baz: 'partial' } } }
|
||||||
|
template: '"{{>*foo.bar.baz}}"'
|
||||||
|
partials: { partial: '*{{text}}*' }
|
||||||
|
expected: '"*Hello, world!*"'
|
||||||
|
|
||||||
|
- name: Dotted Names - Operator Precedence
|
||||||
|
desc: The dotted name should be resolved entirely before being dereferenced.
|
||||||
|
data:
|
||||||
|
text: 'Hello, world!'
|
||||||
|
foo: 'test'
|
||||||
|
test:
|
||||||
|
bar:
|
||||||
|
baz: 'partial'
|
||||||
|
template: '"{{>*foo.bar.baz}}"'
|
||||||
|
partials: { partial: '*{{text}}*' }
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Dotted Names - Failed Lookup
|
||||||
|
desc: The dynamic partial should operate within the current context.
|
||||||
|
data:
|
||||||
|
foo:
|
||||||
|
text: 'Hello, world!'
|
||||||
|
bar:
|
||||||
|
baz: 'partial'
|
||||||
|
template: '"{{>*foo.bar.baz}}"'
|
||||||
|
partials: { partial: '*{{text}}*' }
|
||||||
|
expected: '"**"'
|
||||||
|
|
||||||
|
- name: Dotted names - Context Stacking
|
||||||
|
desc: Dotted names should not push a new frame on the context stack.
|
||||||
|
data:
|
||||||
|
section1: { value: 'section1' }
|
||||||
|
section2: { dynamic: 'partial', value: 'section2' }
|
||||||
|
template: "{{#section1}}{{>*section2.dynamic}}{{/section1}}"
|
||||||
|
partials:
|
||||||
|
partial: '"{{value}}"'
|
||||||
|
expected: '"section1"'
|
||||||
|
|
||||||
|
- name: Dotted names - Context Stacking Under Repetition
|
||||||
|
desc: Dotted names should not push a new frame on the context stack.
|
||||||
|
data:
|
||||||
|
value: 'test'
|
||||||
|
section1: [ 1, 2 ]
|
||||||
|
section2: { dynamic: 'partial', value: 'section2' }
|
||||||
|
template: "{{#section1}}{{>*section2.dynamic}}{{/section1}}"
|
||||||
|
partials:
|
||||||
|
partial: "{{value}}"
|
||||||
|
expected: "testtest"
|
||||||
|
|
||||||
|
- name: Dotted names - Context Stacking Failed Lookup
|
||||||
|
desc: Dotted names should resolve against the proper context stack.
|
||||||
|
data:
|
||||||
|
section1: [ 1, 2 ]
|
||||||
|
section2: { dynamic: 'partial', value: 'section2' }
|
||||||
|
template: "{{#section1}}{{>*section2.dynamic}}{{/section1}}"
|
||||||
|
partials:
|
||||||
|
partial: '"{{value}}"'
|
||||||
|
expected: '""""'
|
||||||
|
|
||||||
|
- name: Recursion
|
||||||
|
desc: Dynamic partials should properly recurse.
|
||||||
|
data:
|
||||||
|
template: 'node'
|
||||||
|
content: 'X'
|
||||||
|
nodes: [ { content: 'Y', nodes: [] } ]
|
||||||
|
template: '{{>*template}}'
|
||||||
|
partials: { node: '{{content}}<{{#nodes}}{{>*template}}{{/nodes}}>' }
|
||||||
|
expected: 'X<Y<>>'
|
||||||
|
|
||||||
|
- name: Dynamic Names - Double Dereferencing
|
||||||
|
desc: Dynamic Names can't be dereferenced more than once.
|
||||||
|
data: { dynamic: 'test', 'test': 'content' }
|
||||||
|
template: '"{{>**dynamic}}"'
|
||||||
|
partials: { content: 'Hello, world!' }
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
- name: Dynamic Names - Composed Dereferencing
|
||||||
|
desc: Dotted Names are resolved entirely before dereferencing begins.
|
||||||
|
data: { foo: 'fizz', bar: 'buzz', fizz: { buzz: { content: null } } }
|
||||||
|
template: '"{{>*foo.*bar}}"'
|
||||||
|
partials: { content: 'Hello, world!' }
|
||||||
|
expected: '""'
|
||||||
|
|
||||||
|
# Whitespace Sensitivity
|
||||||
|
|
||||||
|
- name: Surrounding Whitespace
|
||||||
|
desc: |
|
||||||
|
A dynamic partial should not alter surrounding whitespace; any
|
||||||
|
whitespace preceding the tag should be treated as indentation while any
|
||||||
|
whitespace succeding the tag should be left untouched.
|
||||||
|
data: { partial: 'foobar' }
|
||||||
|
template: '| {{>*partial}} |'
|
||||||
|
partials: { foobar: "\t|\t" }
|
||||||
|
expected: "| \t|\t |"
|
||||||
|
|
||||||
|
- name: Inline Indentation
|
||||||
|
desc: |
|
||||||
|
Whitespace should be left untouched: whitespaces preceding the tag
|
||||||
|
should be treated as indentation.
|
||||||
|
data: { dynamic: 'partial', data: '|' }
|
||||||
|
template: " {{data}} {{>*dynamic}}\n"
|
||||||
|
partials: { partial: ">\n>" }
|
||||||
|
expected: " | >\n>\n"
|
||||||
|
|
||||||
|
- name: Standalone Line Endings
|
||||||
|
desc: '"\r\n" should be considered a newline for standalone tags.'
|
||||||
|
data: { dynamic: 'partial' }
|
||||||
|
template: "|\r\n{{>*dynamic}}\r\n|"
|
||||||
|
partials: { partial: ">" }
|
||||||
|
expected: "|\r\n>|"
|
||||||
|
|
||||||
|
- name: Standalone Without Previous Line
|
||||||
|
desc: Standalone tags should not require a newline to precede them.
|
||||||
|
data: { dynamic: 'partial' }
|
||||||
|
template: " {{>*dynamic}}\n>"
|
||||||
|
partials: { partial: ">\n>"}
|
||||||
|
expected: " >\n >>"
|
||||||
|
|
||||||
|
- name: Standalone Without Newline
|
||||||
|
desc: Standalone tags should not require a newline to follow them.
|
||||||
|
data: { dynamic: 'partial' }
|
||||||
|
template: ">\n {{>*dynamic}}"
|
||||||
|
partials: { partial: ">\n>" }
|
||||||
|
expected: ">\n >\n >"
|
||||||
|
|
||||||
|
- name: Standalone Indentation
|
||||||
|
desc: Each line of the partial should be indented before rendering.
|
||||||
|
data: { dynamic: 'partial', content: "<\n->" }
|
||||||
|
template: |
|
||||||
|
\
|
||||||
|
{{>*dynamic}}
|
||||||
|
/
|
||||||
|
partials:
|
||||||
|
partial: |
|
||||||
|
|
|
||||||
|
{{{content}}}
|
||||||
|
|
|
||||||
|
expected: |
|
||||||
|
\
|
||||||
|
|
|
||||||
|
<
|
||||||
|
->
|
||||||
|
|
|
||||||
|
/
|
||||||
|
|
||||||
|
# Whitespace Insensitivity
|
||||||
|
|
||||||
|
- name: Padding Whitespace
|
||||||
|
desc: Superfluous in-tag whitespace should be ignored.
|
||||||
|
data: { dynamic: 'partial', boolean: true }
|
||||||
|
template: "|{{> * dynamic }}|"
|
||||||
|
partials: { partial: "[]" }
|
||||||
|
expected: '|[]|'
|
||||||
@@ -0,0 +1,306 @@
|
|||||||
|
{
|
||||||
|
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
|
||||||
|
"overview": "Like partials, Parent tags are used to expand an external template into the\ncurrent template. Unlike partials, Parent tags may contain optional\narguments delimited by Block tags. For this reason, Parent tags may also be\nreferred to as Parametric Partials.\n\nThe Parent tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter; each Parent tag MUST be followed by\nan End Section tag with the same content within the matching Parent tag.\n\nThis tag's content names the Parent template to inject. Set Delimiter tags\nPreceding a Parent tag MUST NOT affect the parsing of the injected external\ntemplate. The Parent MUST be rendered against the context stack local to the\ntag. If the named Parent cannot be found, the empty string SHOULD be used\ninstead, as in interpolations.\n\nParent tags SHOULD be treated as standalone when appropriate. If this tag is\nused standalone, any whitespace preceding the tag should be treated as\nindentation, and prepended to each line of the Parent before rendering.\n\nThe Block tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter. Each Block tag MUST be followed by\nan End Section tag with the same content within the matching Block tag. This\ntag's content determines the parameter or argument name.\n\nBlock tags may appear both inside and outside of Parent tags. In both cases,\nthey specify a position within the template that can be overridden; it is a\nparameter of the containing template. The template text between the Block tag\nand its matching End Section tag defines the default content to render when\nthe parameter is not overridden from outside.\n\nIn addition, when used inside of a Parent tag, the template text between a\nBlock tag and its matching End Section tag defines content that replaces the\ndefault defined in the Parent template. This content is the argument passed\nto the Parent template.\n\nThe practice of injecting an external template using a Parent tag is referred\nto as inheritance. If the Parent tag includes a Block tag that overrides a\nparameter of the Parent template, this may also be referred to as\nsubstitution.\n\nParent templates are taken from the same namespace as regular Partial\ntemplates and in fact, injecting a regular Partial is exactly equivalent to\ninjecting a Parent without making any substitutions. Parameter and arguments\nnames live in a namespace that is distinct from both Partials and the context.\n",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "Default",
|
||||||
|
"desc": "Default content should be rendered if the block isn't overridden",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{$title}}Default title{{/title}}\n",
|
||||||
|
"expected": "Default title\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Variable",
|
||||||
|
"desc": "Default content renders variables",
|
||||||
|
"data": {
|
||||||
|
"bar": "baz"
|
||||||
|
},
|
||||||
|
"template": "{{$foo}}default {{bar}} content{{/foo}}\n",
|
||||||
|
"expected": "default baz content\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Triple Mustache",
|
||||||
|
"desc": "Default content renders triple mustache variables",
|
||||||
|
"data": {
|
||||||
|
"bar": "<baz>"
|
||||||
|
},
|
||||||
|
"template": "{{$foo}}default {{{bar}}} content{{/foo}}\n",
|
||||||
|
"expected": "default <baz> content\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Sections",
|
||||||
|
"desc": "Default content renders sections",
|
||||||
|
"data": {
|
||||||
|
"bar": {
|
||||||
|
"baz": "qux"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}\n",
|
||||||
|
"expected": "default qux content\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Negative Sections",
|
||||||
|
"desc": "Default content renders negative sections",
|
||||||
|
"data": {
|
||||||
|
"baz": "three"
|
||||||
|
},
|
||||||
|
"template": "{{$foo}}default {{^bar}}{{baz}}{{/bar}} content{{/foo}}\n",
|
||||||
|
"expected": "default three content\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Mustache Injection",
|
||||||
|
"desc": "Mustache injection in default content",
|
||||||
|
"data": {
|
||||||
|
"bar": {
|
||||||
|
"baz": "{{qux}}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}\n",
|
||||||
|
"expected": "default {{qux}} content\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Inherit",
|
||||||
|
"desc": "Default content rendered inside inherited templates",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<include}}{{/include}}\n",
|
||||||
|
"partials": {
|
||||||
|
"include": "{{$foo}}default content{{/foo}}"
|
||||||
|
},
|
||||||
|
"expected": "default content"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Overridden content",
|
||||||
|
"desc": "Overridden content",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<super}}{{$title}}sub template title{{/title}}{{/super}}",
|
||||||
|
"partials": {
|
||||||
|
"super": "...{{$title}}Default title{{/title}}..."
|
||||||
|
},
|
||||||
|
"expected": "...sub template title..."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Data does not override block",
|
||||||
|
"desc": "Context does not override argument passed into parent",
|
||||||
|
"data": {
|
||||||
|
"var": "var in data"
|
||||||
|
},
|
||||||
|
"template": "{{<include}}{{$var}}var in template{{/var}}{{/include}}",
|
||||||
|
"partials": {
|
||||||
|
"include": "{{$var}}var in include{{/var}}"
|
||||||
|
},
|
||||||
|
"expected": "var in template"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Data does not override block default",
|
||||||
|
"desc": "Context does not override default content of block",
|
||||||
|
"data": {
|
||||||
|
"var": "var in data"
|
||||||
|
},
|
||||||
|
"template": "{{<include}}{{/include}}",
|
||||||
|
"partials": {
|
||||||
|
"include": "{{$var}}var in include{{/var}}"
|
||||||
|
},
|
||||||
|
"expected": "var in include"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Overridden parent",
|
||||||
|
"desc": "Overridden parent",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "test {{<parent}}{{$stuff}}override{{/stuff}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{$stuff}}...{{/stuff}}"
|
||||||
|
},
|
||||||
|
"expected": "test override"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Two overridden parents",
|
||||||
|
"desc": "Two overridden parents with different content",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "test {{<parent}}{{$stuff}}override1{{/stuff}}{{/parent}} {{<parent}}{{$stuff}}override2{{/stuff}}{{/parent}}\n",
|
||||||
|
"partials": {
|
||||||
|
"parent": "|{{$stuff}}...{{/stuff}}{{$default}} default{{/default}}|"
|
||||||
|
},
|
||||||
|
"expected": "test |override1 default| |override2 default|\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Override parent with newlines",
|
||||||
|
"desc": "Override parent with newlines",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$ballmer}}\npeaked\n\n:(\n{{/ballmer}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{$ballmer}}peaking{{/ballmer}}"
|
||||||
|
},
|
||||||
|
"expected": "peaked\n\n:(\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Inherit indentation",
|
||||||
|
"desc": "Inherit indentation when overriding a parent",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$nineties}}hammer time{{/nineties}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "stop:\n {{$nineties}}collaborate and listen{{/nineties}}\n"
|
||||||
|
},
|
||||||
|
"expected": "stop:\n hammer time\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Only one override",
|
||||||
|
"desc": "Override one parameter but not the other",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$stuff2}}override two{{/stuff2}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{$stuff}}new default one{{/stuff}}, {{$stuff2}}new default two{{/stuff2}}"
|
||||||
|
},
|
||||||
|
"expected": "new default one, override two"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Parent template",
|
||||||
|
"desc": "Parent templates behave identically to partials when called with no parameters",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{>parent}}|{{<parent}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{$foo}}default content{{/foo}}"
|
||||||
|
},
|
||||||
|
"expected": "default content|default content"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Recursion",
|
||||||
|
"desc": "Recursion in inherited templates",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$foo}}override{{/foo}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{$foo}}default content{{/foo}} {{$bar}}{{<parent2}}{{/parent2}}{{/bar}}",
|
||||||
|
"parent2": "{{$foo}}parent2 default content{{/foo}} {{<parent}}{{$bar}}don't recurse{{/bar}}{{/parent}}"
|
||||||
|
},
|
||||||
|
"expected": "override override override don't recurse"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Multi-level inheritance",
|
||||||
|
"desc": "Top-level substitutions take precedence in multi-level inheritance",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$a}}c{{/a}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{<older}}{{$a}}p{{/a}}{{/older}}",
|
||||||
|
"older": "{{<grandParent}}{{$a}}o{{/a}}{{/grandParent}}",
|
||||||
|
"grandParent": "{{$a}}g{{/a}}"
|
||||||
|
},
|
||||||
|
"expected": "c"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Multi-level inheritance, no sub child",
|
||||||
|
"desc": "Top-level substitutions take precedence in multi-level inheritance",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{<older}}{{$a}}p{{/a}}{{/older}}",
|
||||||
|
"older": "{{<grandParent}}{{$a}}o{{/a}}{{/grandParent}}",
|
||||||
|
"grandParent": "{{$a}}g{{/a}}"
|
||||||
|
},
|
||||||
|
"expected": "p"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Text inside parent",
|
||||||
|
"desc": "Ignores text inside parent templates, but does parse $ tags",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}} asdfasd {{$foo}}hmm{{/foo}} asdfasdfasdf {{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{$foo}}default content{{/foo}}"
|
||||||
|
},
|
||||||
|
"expected": "hmm"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Text inside parent",
|
||||||
|
"desc": "Allows text inside a parent tag, but ignores it",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}} asdfasd asdfasdfasdf {{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{$foo}}default content{{/foo}}"
|
||||||
|
},
|
||||||
|
"expected": "default content"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Block scope",
|
||||||
|
"desc": "Scope of a substituted block is evaluated in the context of the parent template",
|
||||||
|
"data": {
|
||||||
|
"fruit": "apples",
|
||||||
|
"nested": {
|
||||||
|
"fruit": "bananas"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$block}}I say {{fruit}}.{{/block}}{{/parent}}",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{#nested}}{{$block}}You say {{fruit}}.{{/block}}{{/nested}}"
|
||||||
|
},
|
||||||
|
"expected": "I say bananas."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone parent",
|
||||||
|
"desc": "A parent's opening and closing tags need not be on separate lines in order to be standalone",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "Hi,\n {{<parent}}{{/parent}}\n",
|
||||||
|
"partials": {
|
||||||
|
"parent": "one\ntwo\n"
|
||||||
|
},
|
||||||
|
"expected": "Hi,\n one\n two\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Standalone block",
|
||||||
|
"desc": "A block's opening and closing tags need not be on separate lines in order to be standalone",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$block}}\none\ntwo{{/block}}\n{{/parent}}\n",
|
||||||
|
"partials": {
|
||||||
|
"parent": "Hi,\n {{$block}}{{/block}}\n"
|
||||||
|
},
|
||||||
|
"expected": "Hi,\n one\n two\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Block reindentation",
|
||||||
|
"desc": "Block indentation is removed at the site of definition and added at the site of expansion",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$block}}\n one\n two\n{{/block}}{{/parent}}\n",
|
||||||
|
"partials": {
|
||||||
|
"parent": "Hi,\n {{$block}}\n {{/block}}\n"
|
||||||
|
},
|
||||||
|
"expected": "Hi,\n one\n two\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Intrinsic indentation",
|
||||||
|
"desc": "When the block opening tag is standalone, indentation is determined by default content",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$block}}\none\ntwo\n{{/block}}{{/parent}}\n",
|
||||||
|
"partials": {
|
||||||
|
"parent": "Hi,\n{{$block}}\n default\n{{/block}}\n"
|
||||||
|
},
|
||||||
|
"expected": "Hi,\n one\n two\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Nested block reindentation",
|
||||||
|
"desc": "Nested blocks are reindented relative to the surrounding block",
|
||||||
|
"data": {
|
||||||
|
},
|
||||||
|
"template": "{{<parent}}{{$nested}}\nthree\n{{/nested}}{{/parent}}\n",
|
||||||
|
"partials": {
|
||||||
|
"parent": "{{<grandparent}}{{$block}}\n one\n {{$nested}}\n two\n {{/nested}}\n{{/block}}{{/grandparent}}\n",
|
||||||
|
"grandparent": "{{$block}}default{{/block}}"
|
||||||
|
},
|
||||||
|
"expected": "one\n three\n"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,326 @@
|
|||||||
|
overview: |
|
||||||
|
Like partials, Parent tags are used to expand an external template into the
|
||||||
|
current template. Unlike partials, Parent tags may contain optional
|
||||||
|
arguments delimited by Block tags. For this reason, Parent tags may also be
|
||||||
|
referred to as Parametric Partials.
|
||||||
|
|
||||||
|
The Parent tags' content MUST be a non-whitespace character sequence NOT
|
||||||
|
containing the current closing delimiter; each Parent tag MUST be followed by
|
||||||
|
an End Section tag with the same content within the matching Parent tag.
|
||||||
|
|
||||||
|
This tag's content names the Parent template to inject. Set Delimiter tags
|
||||||
|
Preceding a Parent tag MUST NOT affect the parsing of the injected external
|
||||||
|
template. The Parent MUST be rendered against the context stack local to the
|
||||||
|
tag. If the named Parent cannot be found, the empty string SHOULD be used
|
||||||
|
instead, as in interpolations.
|
||||||
|
|
||||||
|
Parent tags SHOULD be treated as standalone when appropriate. If this tag is
|
||||||
|
used standalone, any whitespace preceding the tag should be treated as
|
||||||
|
indentation, and prepended to each line of the Parent before rendering.
|
||||||
|
|
||||||
|
The Block tags' content MUST be a non-whitespace character sequence NOT
|
||||||
|
containing the current closing delimiter. Each Block tag MUST be followed by
|
||||||
|
an End Section tag with the same content within the matching Block tag. This
|
||||||
|
tag's content determines the parameter or argument name.
|
||||||
|
|
||||||
|
Block tags may appear both inside and outside of Parent tags. In both cases,
|
||||||
|
they specify a position within the template that can be overridden; it is a
|
||||||
|
parameter of the containing template. The template text between the Block tag
|
||||||
|
and its matching End Section tag defines the default content to render when
|
||||||
|
the parameter is not overridden from outside.
|
||||||
|
|
||||||
|
In addition, when used inside of a Parent tag, the template text between a
|
||||||
|
Block tag and its matching End Section tag defines content that replaces the
|
||||||
|
default defined in the Parent template. This content is the argument passed
|
||||||
|
to the Parent template.
|
||||||
|
|
||||||
|
The practice of injecting an external template using a Parent tag is referred
|
||||||
|
to as inheritance. If the Parent tag includes a Block tag that overrides a
|
||||||
|
parameter of the Parent template, this may also be referred to as
|
||||||
|
substitution.
|
||||||
|
|
||||||
|
Parent templates are taken from the same namespace as regular Partial
|
||||||
|
templates and in fact, injecting a regular Partial is exactly equivalent to
|
||||||
|
injecting a Parent without making any substitutions. Parameter and arguments
|
||||||
|
names live in a namespace that is distinct from both Partials and the context.
|
||||||
|
tests:
|
||||||
|
- name: Default
|
||||||
|
desc: Default content should be rendered if the block isn't overridden
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
{{$title}}Default title{{/title}}
|
||||||
|
expected: |
|
||||||
|
Default title
|
||||||
|
|
||||||
|
- name: Variable
|
||||||
|
desc: Default content renders variables
|
||||||
|
data: { bar: 'baz' }
|
||||||
|
template: |
|
||||||
|
{{$foo}}default {{bar}} content{{/foo}}
|
||||||
|
expected: |
|
||||||
|
default baz content
|
||||||
|
|
||||||
|
- name: Triple Mustache
|
||||||
|
desc: Default content renders triple mustache variables
|
||||||
|
data: { bar: '<baz>' }
|
||||||
|
template: |
|
||||||
|
{{$foo}}default {{{bar}}} content{{/foo}}
|
||||||
|
expected: |
|
||||||
|
default <baz> content
|
||||||
|
|
||||||
|
- name: Sections
|
||||||
|
desc: Default content renders sections
|
||||||
|
data: { bar: {baz: 'qux'} }
|
||||||
|
template: |
|
||||||
|
{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}
|
||||||
|
expected: |
|
||||||
|
default qux content
|
||||||
|
|
||||||
|
- name: Negative Sections
|
||||||
|
desc: Default content renders negative sections
|
||||||
|
data: { baz: 'three' }
|
||||||
|
template: |
|
||||||
|
{{$foo}}default {{^bar}}{{baz}}{{/bar}} content{{/foo}}
|
||||||
|
expected: |
|
||||||
|
default three content
|
||||||
|
|
||||||
|
- name: Mustache Injection
|
||||||
|
desc: Mustache injection in default content
|
||||||
|
data: {bar: {baz: '{{qux}}'} }
|
||||||
|
template: |
|
||||||
|
{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}
|
||||||
|
expected: |
|
||||||
|
default {{qux}} content
|
||||||
|
|
||||||
|
- name: Inherit
|
||||||
|
desc: Default content rendered inside inherited templates
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
{{<include}}{{/include}}
|
||||||
|
partials:
|
||||||
|
include: "{{$foo}}default content{{/foo}}"
|
||||||
|
expected: "default content"
|
||||||
|
|
||||||
|
- name: Overridden content
|
||||||
|
desc: Overridden content
|
||||||
|
data: { }
|
||||||
|
template: "{{<super}}{{$title}}sub template title{{/title}}{{/super}}"
|
||||||
|
partials:
|
||||||
|
super: "...{{$title}}Default title{{/title}}..."
|
||||||
|
expected: "...sub template title..."
|
||||||
|
|
||||||
|
- name: Data does not override block
|
||||||
|
desc: Context does not override argument passed into parent
|
||||||
|
data: { var: 'var in data' }
|
||||||
|
template: "{{<include}}{{$var}}var in template{{/var}}{{/include}}"
|
||||||
|
partials:
|
||||||
|
include: "{{$var}}var in include{{/var}}"
|
||||||
|
expected: "var in template"
|
||||||
|
|
||||||
|
- name: Data does not override block default
|
||||||
|
desc: Context does not override default content of block
|
||||||
|
data: { var: 'var in data' }
|
||||||
|
template: "{{<include}}{{/include}}"
|
||||||
|
partials:
|
||||||
|
include: "{{$var}}var in include{{/var}}"
|
||||||
|
expected: "var in include"
|
||||||
|
|
||||||
|
- name: Overridden parent
|
||||||
|
desc: Overridden parent
|
||||||
|
data: { }
|
||||||
|
template: "test {{<parent}}{{$stuff}}override{{/stuff}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{$stuff}}...{{/stuff}}"
|
||||||
|
expected: "test override"
|
||||||
|
|
||||||
|
- name: Two overridden parents
|
||||||
|
desc: Two overridden parents with different content
|
||||||
|
data: { }
|
||||||
|
template: |
|
||||||
|
test {{<parent}}{{$stuff}}override1{{/stuff}}{{/parent}} {{<parent}}{{$stuff}}override2{{/stuff}}{{/parent}}
|
||||||
|
partials:
|
||||||
|
parent: "|{{$stuff}}...{{/stuff}}{{$default}} default{{/default}}|"
|
||||||
|
expected: |
|
||||||
|
test |override1 default| |override2 default|
|
||||||
|
|
||||||
|
- name: Override parent with newlines
|
||||||
|
desc: Override parent with newlines
|
||||||
|
data: { }
|
||||||
|
template: "{{<parent}}{{$ballmer}}\npeaked\n\n:(\n{{/ballmer}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{$ballmer}}peaking{{/ballmer}}"
|
||||||
|
expected: "peaked\n\n:(\n"
|
||||||
|
|
||||||
|
- name: Inherit indentation
|
||||||
|
desc: Inherit indentation when overriding a parent
|
||||||
|
data: { }
|
||||||
|
template: "{{<parent}}{{$nineties}}hammer time{{/nineties}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: |
|
||||||
|
stop:
|
||||||
|
{{$nineties}}collaborate and listen{{/nineties}}
|
||||||
|
expected: |
|
||||||
|
stop:
|
||||||
|
hammer time
|
||||||
|
|
||||||
|
- name: Only one override
|
||||||
|
desc: Override one parameter but not the other
|
||||||
|
data: { }
|
||||||
|
template: "{{<parent}}{{$stuff2}}override two{{/stuff2}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{$stuff}}new default one{{/stuff}}, {{$stuff2}}new default two{{/stuff2}}"
|
||||||
|
expected: "new default one, override two"
|
||||||
|
|
||||||
|
- name: Parent template
|
||||||
|
desc: Parent templates behave identically to partials when called with no parameters
|
||||||
|
data: { }
|
||||||
|
template: "{{>parent}}|{{<parent}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{$foo}}default content{{/foo}}"
|
||||||
|
expected: "default content|default content"
|
||||||
|
|
||||||
|
- name: Recursion
|
||||||
|
desc: Recursion in inherited templates
|
||||||
|
data: {}
|
||||||
|
template: "{{<parent}}{{$foo}}override{{/foo}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{$foo}}default content{{/foo}} {{$bar}}{{<parent2}}{{/parent2}}{{/bar}}"
|
||||||
|
parent2: "{{$foo}}parent2 default content{{/foo}} {{<parent}}{{$bar}}don't recurse{{/bar}}{{/parent}}"
|
||||||
|
expected: "override override override don't recurse"
|
||||||
|
|
||||||
|
- name: Multi-level inheritance
|
||||||
|
desc: Top-level substitutions take precedence in multi-level inheritance
|
||||||
|
data: { }
|
||||||
|
template: "{{<parent}}{{$a}}c{{/a}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{<older}}{{$a}}p{{/a}}{{/older}}"
|
||||||
|
older: "{{<grandParent}}{{$a}}o{{/a}}{{/grandParent}}"
|
||||||
|
grandParent: "{{$a}}g{{/a}}"
|
||||||
|
expected: c
|
||||||
|
|
||||||
|
- name: Multi-level inheritance, no sub child
|
||||||
|
desc: Top-level substitutions take precedence in multi-level inheritance
|
||||||
|
data: { }
|
||||||
|
template: "{{<parent}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{<older}}{{$a}}p{{/a}}{{/older}}"
|
||||||
|
older: "{{<grandParent}}{{$a}}o{{/a}}{{/grandParent}}"
|
||||||
|
grandParent: "{{$a}}g{{/a}}"
|
||||||
|
expected: p
|
||||||
|
|
||||||
|
- name: Text inside parent
|
||||||
|
desc: Ignores text inside parent templates, but does parse $ tags
|
||||||
|
data: { }
|
||||||
|
template: "{{<parent}} asdfasd {{$foo}}hmm{{/foo}} asdfasdfasdf {{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{$foo}}default content{{/foo}}"
|
||||||
|
expected:
|
||||||
|
hmm
|
||||||
|
|
||||||
|
- name: Text inside parent
|
||||||
|
desc: Allows text inside a parent tag, but ignores it
|
||||||
|
data: {}
|
||||||
|
template: "{{<parent}} asdfasd asdfasdfasdf {{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{$foo}}default content{{/foo}}"
|
||||||
|
expected: default content
|
||||||
|
|
||||||
|
- name: Block scope
|
||||||
|
desc: Scope of a substituted block is evaluated in the context of the parent template
|
||||||
|
data:
|
||||||
|
fruit: apples
|
||||||
|
nested:
|
||||||
|
fruit: bananas
|
||||||
|
template: "{{<parent}}{{$block}}I say {{fruit}}.{{/block}}{{/parent}}"
|
||||||
|
partials:
|
||||||
|
parent: "{{#nested}}{{$block}}You say {{fruit}}.{{/block}}{{/nested}}"
|
||||||
|
expected: I say bananas.
|
||||||
|
|
||||||
|
- name: Standalone parent
|
||||||
|
desc: A parent's opening and closing tags need not be on separate lines in order to be standalone
|
||||||
|
data: {}
|
||||||
|
template: |
|
||||||
|
Hi,
|
||||||
|
{{<parent}}{{/parent}}
|
||||||
|
partials:
|
||||||
|
parent: |
|
||||||
|
one
|
||||||
|
two
|
||||||
|
expected: |
|
||||||
|
Hi,
|
||||||
|
one
|
||||||
|
two
|
||||||
|
|
||||||
|
- name: Standalone block
|
||||||
|
desc: A block's opening and closing tags need not be on separate lines in order to be standalone
|
||||||
|
data: {}
|
||||||
|
template: |
|
||||||
|
{{<parent}}{{$block}}
|
||||||
|
one
|
||||||
|
two{{/block}}
|
||||||
|
{{/parent}}
|
||||||
|
partials:
|
||||||
|
parent: |
|
||||||
|
Hi,
|
||||||
|
{{$block}}{{/block}}
|
||||||
|
expected: |
|
||||||
|
Hi,
|
||||||
|
one
|
||||||
|
two
|
||||||
|
|
||||||
|
- name: Block reindentation
|
||||||
|
desc: Block indentation is removed at the site of definition and added at the site of expansion
|
||||||
|
data: {}
|
||||||
|
template: |
|
||||||
|
{{<parent}}{{$block}}
|
||||||
|
one
|
||||||
|
two
|
||||||
|
{{/block}}{{/parent}}
|
||||||
|
partials:
|
||||||
|
parent: |
|
||||||
|
Hi,
|
||||||
|
{{$block}}
|
||||||
|
{{/block}}
|
||||||
|
expected: |
|
||||||
|
Hi,
|
||||||
|
one
|
||||||
|
two
|
||||||
|
|
||||||
|
- name: Intrinsic indentation
|
||||||
|
desc: When the block opening tag is standalone, indentation is determined by default content
|
||||||
|
data: {}
|
||||||
|
template: |
|
||||||
|
{{<parent}}{{$block}}
|
||||||
|
one
|
||||||
|
two
|
||||||
|
{{/block}}{{/parent}}
|
||||||
|
partials:
|
||||||
|
parent: |
|
||||||
|
Hi,
|
||||||
|
{{$block}}
|
||||||
|
default
|
||||||
|
{{/block}}
|
||||||
|
expected: |
|
||||||
|
Hi,
|
||||||
|
one
|
||||||
|
two
|
||||||
|
|
||||||
|
- name: Nested block reindentation
|
||||||
|
desc: Nested blocks are reindented relative to the surrounding block
|
||||||
|
data: {}
|
||||||
|
template: |
|
||||||
|
{{<parent}}{{$nested}}
|
||||||
|
three
|
||||||
|
{{/nested}}{{/parent}}
|
||||||
|
partials:
|
||||||
|
parent: |
|
||||||
|
{{<grandparent}}{{$block}}
|
||||||
|
one
|
||||||
|
{{$nested}}
|
||||||
|
two
|
||||||
|
{{/nested}}
|
||||||
|
{{/block}}{{/grandparent}}
|
||||||
|
grandparent: "{{$block}}default{{/block}}"
|
||||||
|
expected: |
|
||||||
|
one
|
||||||
|
three
|
||||||
@@ -0,0 +1,222 @@
|
|||||||
|
{
|
||||||
|
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
|
||||||
|
"overview": "Lambdas are a special-cased data type for use in interpolations and\nsections.\n\nWhen used as the data value for an Interpolation tag, the lambda MUST be\ntreatable as an arity 0 function, and invoked as such. The returned value\nMUST be rendered against the default delimiters, then interpolated in place\nof the lambda.\n\nWhen used as the data value for a Section tag, the lambda MUST be treatable\nas an arity 1 function, and invoked as such (passing a String containing the\nunprocessed section contents). The returned value MUST be rendered against\nthe current delimiters, then interpolated in place of the section.\n",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "Interpolation",
|
||||||
|
"desc": "A lambda's return value should be interpolated.",
|
||||||
|
"data": {
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { \"world\" }",
|
||||||
|
"raku": "sub { \"world\" }",
|
||||||
|
"perl": "sub { \"world\" }",
|
||||||
|
"js": "function() { return \"world\" }",
|
||||||
|
"php": "return \"world\";",
|
||||||
|
"python": "lambda: \"world\"",
|
||||||
|
"clojure": "(fn [] \"world\")",
|
||||||
|
"lisp": "(lambda () \"world\")",
|
||||||
|
"pwsh": "\"world\"",
|
||||||
|
"go": "func() string { return \"world\" }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "Hello, {{lambda}}!",
|
||||||
|
"expected": "Hello, world!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Interpolation - Expansion",
|
||||||
|
"desc": "A lambda's return value should be parsed.",
|
||||||
|
"data": {
|
||||||
|
"planet": "world",
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { \"{{planet}}\" }",
|
||||||
|
"raku": "sub { q+{{planet}}+ }",
|
||||||
|
"perl": "sub { \"{{planet}}\" }",
|
||||||
|
"js": "function() { return \"{{planet}}\" }",
|
||||||
|
"php": "return \"{{planet}}\";",
|
||||||
|
"python": "lambda: \"{{planet}}\"",
|
||||||
|
"clojure": "(fn [] \"{{planet}}\")",
|
||||||
|
"lisp": "(lambda () \"{{planet}}\")",
|
||||||
|
"pwsh": "\"{{planet}}\"",
|
||||||
|
"go": "func() string { return \"{{planet}}\" }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "Hello, {{lambda}}!",
|
||||||
|
"expected": "Hello, world!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Interpolation - Alternate Delimiters",
|
||||||
|
"desc": "A lambda's return value should parse with the default delimiters.",
|
||||||
|
"data": {
|
||||||
|
"planet": "world",
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { \"|planet| => {{planet}}\" }",
|
||||||
|
"raku": "sub { q+|planet| => {{planet}}+ }",
|
||||||
|
"perl": "sub { \"|planet| => {{planet}}\" }",
|
||||||
|
"js": "function() { return \"|planet| => {{planet}}\" }",
|
||||||
|
"php": "return \"|planet| => {{planet}}\";",
|
||||||
|
"python": "lambda: \"|planet| => {{planet}}\"",
|
||||||
|
"clojure": "(fn [] \"|planet| => {{planet}}\")",
|
||||||
|
"lisp": "(lambda () \"|planet| => {{planet}}\")",
|
||||||
|
"pwsh": "\"|planet| => {{planet}}\"",
|
||||||
|
"go": "func() string { return \"|planet| => {{planet}}\" }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{= | | =}}\nHello, (|&lambda|)!",
|
||||||
|
"expected": "Hello, (|planet| => world)!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Interpolation - Multiple Calls",
|
||||||
|
"desc": "Interpolated lambdas should not be cached.",
|
||||||
|
"data": {
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { $calls ||= 0; $calls += 1 }",
|
||||||
|
"raku": "sub { state $calls += 1 }",
|
||||||
|
"perl": "sub { no strict; $calls += 1 }",
|
||||||
|
"js": "function() { return (g=(function(){return this})()).calls=(g.calls||0)+1 }",
|
||||||
|
"php": "global $calls; return ++$calls;",
|
||||||
|
"python": "lambda: globals().update(calls=globals().get(\"calls\",0)+1) or calls",
|
||||||
|
"clojure": "(def g (atom 0)) (fn [] (swap! g inc))",
|
||||||
|
"lisp": "(let ((g 0)) (lambda () (incf g)))",
|
||||||
|
"pwsh": "if (($null -eq $script:calls) -or ($script:calls -ge 3)){$script:calls=0}; ++$script:calls; $script:calls",
|
||||||
|
"go": "func() func() int { g := 0; return func() int { g++; return g } }()"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{lambda}} == {{{lambda}}} == {{lambda}}",
|
||||||
|
"expected": "1 == 2 == 3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Escaping",
|
||||||
|
"desc": "Lambda results should be appropriately escaped.",
|
||||||
|
"data": {
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { \">\" }",
|
||||||
|
"raku": "sub { \">\" }",
|
||||||
|
"perl": "sub { \">\" }",
|
||||||
|
"js": "function() { return \">\" }",
|
||||||
|
"php": "return \">\";",
|
||||||
|
"python": "lambda: \">\"",
|
||||||
|
"clojure": "(fn [] \">\")",
|
||||||
|
"lisp": "(lambda () \">\")",
|
||||||
|
"pwsh": "\">\"",
|
||||||
|
"go": "func() string { return \">\" }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "<{{lambda}}{{{lambda}}}",
|
||||||
|
"expected": "<>>"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Section",
|
||||||
|
"desc": "Lambdas used for sections should receive the raw section string.",
|
||||||
|
"data": {
|
||||||
|
"x": "Error!",
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { |text| text == \"{{x}}\" ? \"yes\" : \"no\" }",
|
||||||
|
"raku": "sub { $^section eq q+{{x}}+ ?? \"yes\" !! \"no\" }",
|
||||||
|
"perl": "sub { $_[0] eq \"{{x}}\" ? \"yes\" : \"no\" }",
|
||||||
|
"js": "function(txt) { return (txt == \"{{x}}\" ? \"yes\" : \"no\") }",
|
||||||
|
"php": "return ($text == \"{{x}}\") ? \"yes\" : \"no\";",
|
||||||
|
"python": "lambda text: text == \"{{x}}\" and \"yes\" or \"no\"",
|
||||||
|
"clojure": "(fn [text] (if (= text \"{{x}}\") \"yes\" \"no\"))",
|
||||||
|
"lisp": "(lambda (text) (if (string= text \"{{x}}\") \"yes\" \"no\"))",
|
||||||
|
"pwsh": "if ($args[0] -eq \"{{x}}\") {\"yes\"} else {\"no\"}",
|
||||||
|
"go": "func(text string) string { if text == \"{{x}}\" { return \"yes\" } else { return \"no\" } }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "<{{#lambda}}{{x}}{{/lambda}}>",
|
||||||
|
"expected": "<yes>"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Section - Expansion",
|
||||||
|
"desc": "Lambdas used for sections should have their results parsed.",
|
||||||
|
"data": {
|
||||||
|
"planet": "Earth",
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { |text| \"#{text}{{planet}}#{text}\" }",
|
||||||
|
"raku": "sub { $^section ~ q+{{planet}}+ ~ $^section }",
|
||||||
|
"perl": "sub { $_[0] . \"{{planet}}\" . $_[0] }",
|
||||||
|
"js": "function(txt) { return txt + \"{{planet}}\" + txt }",
|
||||||
|
"php": "return $text . \"{{planet}}\" . $text;",
|
||||||
|
"python": "lambda text: \"%s{{planet}}%s\" % (text, text)",
|
||||||
|
"clojure": "(fn [text] (str text \"{{planet}}\" text))",
|
||||||
|
"lisp": "(lambda (text) (format nil \"~a{{planet}}~a\" text text))",
|
||||||
|
"pwsh": "\"$($args[0]){{planet}}$($args[0])\"",
|
||||||
|
"go": "func(text string) string { return text + \"{{planet}}\" + text }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "<{{#lambda}}-{{/lambda}}>",
|
||||||
|
"expected": "<-Earth->"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Section - Alternate Delimiters",
|
||||||
|
"desc": "Lambdas used for sections should parse with the current delimiters.",
|
||||||
|
"data": {
|
||||||
|
"planet": "Earth",
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { |text| \"#{text}{{planet}} => |planet|#{text}\" }",
|
||||||
|
"raku": "sub { $^section ~ q+{{planet}} => |planet|+ ~ $^section }",
|
||||||
|
"perl": "sub { $_[0] . \"{{planet}} => |planet|\" . $_[0] }",
|
||||||
|
"js": "function(txt) { return txt + \"{{planet}} => |planet|\" + txt }",
|
||||||
|
"php": "return $text . \"{{planet}} => |planet|\" . $text;",
|
||||||
|
"python": "lambda text: \"%s{{planet}} => |planet|%s\" % (text, text)",
|
||||||
|
"clojure": "(fn [text] (str text \"{{planet}} => |planet|\" text))",
|
||||||
|
"lisp": "(lambda (text) (format nil \"~a{{planet}} => |planet|~a\" text text))",
|
||||||
|
"pwsh": "\"$($args[0]){{planet}} => |planet|$($args[0])\"",
|
||||||
|
"go": "func(text string) string { return text + \"{{planet}} => |planet|\" + text }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{= | | =}}<|#lambda|-|/lambda|>",
|
||||||
|
"expected": "<-{{planet}} => Earth->"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Section - Multiple Calls",
|
||||||
|
"desc": "Lambdas used for sections should not be cached.",
|
||||||
|
"data": {
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { |text| \"__#{text}__\" }",
|
||||||
|
"raku": "sub { \"__\" ~ $^section ~ \"__\" }",
|
||||||
|
"perl": "sub { \"__\" . $_[0] . \"__\" }",
|
||||||
|
"js": "function(txt) { return \"__\" + txt + \"__\" }",
|
||||||
|
"php": "return \"__\" . $text . \"__\";",
|
||||||
|
"python": "lambda text: \"__%s__\" % (text)",
|
||||||
|
"clojure": "(fn [text] (str \"__\" text \"__\"))",
|
||||||
|
"lisp": "(lambda (text) (format nil \"__~a__\" text))",
|
||||||
|
"pwsh": "\"__$($args[0])__\"",
|
||||||
|
"go": "func(text string) string { return \"__\" + text + \"__\" }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "{{#lambda}}FILE{{/lambda}} != {{#lambda}}LINE{{/lambda}}",
|
||||||
|
"expected": "__FILE__ != __LINE__"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Inverted Section",
|
||||||
|
"desc": "Lambdas used for inverted sections should be considered truthy.",
|
||||||
|
"data": {
|
||||||
|
"static": "static",
|
||||||
|
"lambda": {
|
||||||
|
"__tag__": "code",
|
||||||
|
"ruby": "proc { |text| false }",
|
||||||
|
"raku": "sub { 0 }",
|
||||||
|
"perl": "sub { 0 }",
|
||||||
|
"js": "function(txt) { return false }",
|
||||||
|
"php": "return false;",
|
||||||
|
"python": "lambda text: 0",
|
||||||
|
"clojure": "(fn [text] false)",
|
||||||
|
"lisp": "(lambda (text) (declare (ignore text)) nil)",
|
||||||
|
"pwsh": "$false",
|
||||||
|
"go": "func(text string) bool { return false }"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"template": "<{{^lambda}}{{static}}{{/lambda}}>",
|
||||||
|
"expected": "<>"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,189 @@
|
|||||||
|
overview: |
|
||||||
|
Lambdas are a special-cased data type for use in interpolations and
|
||||||
|
sections.
|
||||||
|
|
||||||
|
When used as the data value for an Interpolation tag, the lambda MUST be
|
||||||
|
treatable as an arity 0 function, and invoked as such. The returned value
|
||||||
|
MUST be rendered against the default delimiters, then interpolated in place
|
||||||
|
of the lambda.
|
||||||
|
|
||||||
|
When used as the data value for a Section tag, the lambda MUST be treatable
|
||||||
|
as an arity 1 function, and invoked as such (passing a String containing the
|
||||||
|
unprocessed section contents). The returned value MUST be rendered against
|
||||||
|
the current delimiters, then interpolated in place of the section.
|
||||||
|
tests:
|
||||||
|
- name: Interpolation
|
||||||
|
desc: A lambda's return value should be interpolated.
|
||||||
|
data:
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { "world" }'
|
||||||
|
raku: 'sub { "world" }'
|
||||||
|
perl: 'sub { "world" }'
|
||||||
|
js: 'function() { return "world" }'
|
||||||
|
php: 'return "world";'
|
||||||
|
python: 'lambda: "world"'
|
||||||
|
clojure: '(fn [] "world")'
|
||||||
|
lisp: '(lambda () "world")'
|
||||||
|
pwsh: '"world"'
|
||||||
|
go: 'func() string { return "world" }'
|
||||||
|
template: "Hello, {{lambda}}!"
|
||||||
|
expected: "Hello, world!"
|
||||||
|
|
||||||
|
- name: Interpolation - Expansion
|
||||||
|
desc: A lambda's return value should be parsed.
|
||||||
|
data:
|
||||||
|
planet: "world"
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { "{{planet}}" }'
|
||||||
|
raku: 'sub { q+{{planet}}+ }'
|
||||||
|
perl: 'sub { "{{planet}}" }'
|
||||||
|
js: 'function() { return "{{planet}}" }'
|
||||||
|
php: 'return "{{planet}}";'
|
||||||
|
python: 'lambda: "{{planet}}"'
|
||||||
|
clojure: '(fn [] "{{planet}}")'
|
||||||
|
lisp: '(lambda () "{{planet}}")'
|
||||||
|
pwsh: '"{{planet}}"'
|
||||||
|
go: 'func() string { return "{{planet}}" }'
|
||||||
|
template: "Hello, {{lambda}}!"
|
||||||
|
expected: "Hello, world!"
|
||||||
|
|
||||||
|
- name: Interpolation - Alternate Delimiters
|
||||||
|
desc: A lambda's return value should parse with the default delimiters.
|
||||||
|
data:
|
||||||
|
planet: "world"
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { "|planet| => {{planet}}" }'
|
||||||
|
raku: 'sub { q+|planet| => {{planet}}+ }'
|
||||||
|
perl: 'sub { "|planet| => {{planet}}" }'
|
||||||
|
js: 'function() { return "|planet| => {{planet}}" }'
|
||||||
|
php: 'return "|planet| => {{planet}}";'
|
||||||
|
python: 'lambda: "|planet| => {{planet}}"'
|
||||||
|
clojure: '(fn [] "|planet| => {{planet}}")'
|
||||||
|
lisp: '(lambda () "|planet| => {{planet}}")'
|
||||||
|
pwsh: '"|planet| => {{planet}}"'
|
||||||
|
go: 'func() string { return "|planet| => {{planet}}" }'
|
||||||
|
template: "{{= | | =}}\nHello, (|&lambda|)!"
|
||||||
|
expected: "Hello, (|planet| => world)!"
|
||||||
|
|
||||||
|
- name: Interpolation - Multiple Calls
|
||||||
|
desc: Interpolated lambdas should not be cached.
|
||||||
|
data:
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { $calls ||= 0; $calls += 1 }'
|
||||||
|
raku: 'sub { state $calls += 1 }'
|
||||||
|
perl: 'sub { no strict; $calls += 1 }'
|
||||||
|
js: 'function() { return (g=(function(){return this})()).calls=(g.calls||0)+1 }'
|
||||||
|
php: 'global $calls; return ++$calls;'
|
||||||
|
python: 'lambda: globals().update(calls=globals().get("calls",0)+1) or calls'
|
||||||
|
clojure: '(def g (atom 0)) (fn [] (swap! g inc))'
|
||||||
|
lisp: '(let ((g 0)) (lambda () (incf g)))'
|
||||||
|
pwsh: 'if (($null -eq $script:calls) -or ($script:calls -ge 3)){$script:calls=0}; ++$script:calls; $script:calls'
|
||||||
|
go: 'func() func() int { g := 0; return func() int { g++; return g } }()'
|
||||||
|
template: '{{lambda}} == {{{lambda}}} == {{lambda}}'
|
||||||
|
expected: '1 == 2 == 3'
|
||||||
|
|
||||||
|
- name: Escaping
|
||||||
|
desc: Lambda results should be appropriately escaped.
|
||||||
|
data:
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { ">" }'
|
||||||
|
raku: 'sub { ">" }'
|
||||||
|
perl: 'sub { ">" }'
|
||||||
|
js: 'function() { return ">" }'
|
||||||
|
php: 'return ">";'
|
||||||
|
python: 'lambda: ">"'
|
||||||
|
clojure: '(fn [] ">")'
|
||||||
|
lisp: '(lambda () ">")'
|
||||||
|
pwsh: '">"'
|
||||||
|
go: 'func() string { return ">" }'
|
||||||
|
template: "<{{lambda}}{{{lambda}}}"
|
||||||
|
expected: "<>>"
|
||||||
|
|
||||||
|
- name: Section
|
||||||
|
desc: Lambdas used for sections should receive the raw section string.
|
||||||
|
data:
|
||||||
|
x: 'Error!'
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { |text| text == "{{x}}" ? "yes" : "no" }'
|
||||||
|
raku: 'sub { $^section eq q+{{x}}+ ?? "yes" !! "no" }'
|
||||||
|
perl: 'sub { $_[0] eq "{{x}}" ? "yes" : "no" }'
|
||||||
|
js: 'function(txt) { return (txt == "{{x}}" ? "yes" : "no") }'
|
||||||
|
php: 'return ($text == "{{x}}") ? "yes" : "no";'
|
||||||
|
python: 'lambda text: text == "{{x}}" and "yes" or "no"'
|
||||||
|
clojure: '(fn [text] (if (= text "{{x}}") "yes" "no"))'
|
||||||
|
lisp: '(lambda (text) (if (string= text "{{x}}") "yes" "no"))'
|
||||||
|
pwsh: 'if ($args[0] -eq "{{x}}") {"yes"} else {"no"}'
|
||||||
|
go: 'func(text string) string { if text == "{{x}}" { return "yes" } else { return "no" } }'
|
||||||
|
template: "<{{#lambda}}{{x}}{{/lambda}}>"
|
||||||
|
expected: "<yes>"
|
||||||
|
|
||||||
|
- name: Section - Expansion
|
||||||
|
desc: Lambdas used for sections should have their results parsed.
|
||||||
|
data:
|
||||||
|
planet: "Earth"
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { |text| "#{text}{{planet}}#{text}" }'
|
||||||
|
raku: 'sub { $^section ~ q+{{planet}}+ ~ $^section }'
|
||||||
|
perl: 'sub { $_[0] . "{{planet}}" . $_[0] }'
|
||||||
|
js: 'function(txt) { return txt + "{{planet}}" + txt }'
|
||||||
|
php: 'return $text . "{{planet}}" . $text;'
|
||||||
|
python: 'lambda text: "%s{{planet}}%s" % (text, text)'
|
||||||
|
clojure: '(fn [text] (str text "{{planet}}" text))'
|
||||||
|
lisp: '(lambda (text) (format nil "~a{{planet}}~a" text text))'
|
||||||
|
pwsh: '"$($args[0]){{planet}}$($args[0])"'
|
||||||
|
go: 'func(text string) string { return text + "{{planet}}" + text }'
|
||||||
|
template: "<{{#lambda}}-{{/lambda}}>"
|
||||||
|
expected: "<-Earth->"
|
||||||
|
|
||||||
|
- name: Section - Alternate Delimiters
|
||||||
|
desc: Lambdas used for sections should parse with the current delimiters.
|
||||||
|
data:
|
||||||
|
planet: "Earth"
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { |text| "#{text}{{planet}} => |planet|#{text}" }'
|
||||||
|
raku: 'sub { $^section ~ q+{{planet}} => |planet|+ ~ $^section }'
|
||||||
|
perl: 'sub { $_[0] . "{{planet}} => |planet|" . $_[0] }'
|
||||||
|
js: 'function(txt) { return txt + "{{planet}} => |planet|" + txt }'
|
||||||
|
php: 'return $text . "{{planet}} => |planet|" . $text;'
|
||||||
|
python: 'lambda text: "%s{{planet}} => |planet|%s" % (text, text)'
|
||||||
|
clojure: '(fn [text] (str text "{{planet}} => |planet|" text))'
|
||||||
|
lisp: '(lambda (text) (format nil "~a{{planet}} => |planet|~a" text text))'
|
||||||
|
pwsh: '"$($args[0]){{planet}} => |planet|$($args[0])"'
|
||||||
|
go: 'func(text string) string { return text + "{{planet}} => |planet|" + text }'
|
||||||
|
template: "{{= | | =}}<|#lambda|-|/lambda|>"
|
||||||
|
expected: "<-{{planet}} => Earth->"
|
||||||
|
|
||||||
|
- name: Section - Multiple Calls
|
||||||
|
desc: Lambdas used for sections should not be cached.
|
||||||
|
data:
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { |text| "__#{text}__" }'
|
||||||
|
raku: 'sub { "__" ~ $^section ~ "__" }'
|
||||||
|
perl: 'sub { "__" . $_[0] . "__" }'
|
||||||
|
js: 'function(txt) { return "__" + txt + "__" }'
|
||||||
|
php: 'return "__" . $text . "__";'
|
||||||
|
python: 'lambda text: "__%s__" % (text)'
|
||||||
|
clojure: '(fn [text] (str "__" text "__"))'
|
||||||
|
lisp: '(lambda (text) (format nil "__~a__" text))'
|
||||||
|
pwsh: '"__$($args[0])__"'
|
||||||
|
go: 'func(text string) string { return "__" + text + "__" }'
|
||||||
|
template: '{{#lambda}}FILE{{/lambda}} != {{#lambda}}LINE{{/lambda}}'
|
||||||
|
expected: '__FILE__ != __LINE__'
|
||||||
|
|
||||||
|
- name: Inverted Section
|
||||||
|
desc: Lambdas used for inverted sections should be considered truthy.
|
||||||
|
data:
|
||||||
|
static: 'static'
|
||||||
|
lambda: !code
|
||||||
|
ruby: 'proc { |text| false }'
|
||||||
|
raku: 'sub { 0 }'
|
||||||
|
perl: 'sub { 0 }'
|
||||||
|
js: 'function(txt) { return false }'
|
||||||
|
php: 'return false;'
|
||||||
|
python: 'lambda text: 0'
|
||||||
|
clojure: '(fn [text] false)'
|
||||||
|
lisp: '(lambda (text) (declare (ignore text)) nil)'
|
||||||
|
pwsh: '$false'
|
||||||
|
go: 'func(text string) bool { return false }'
|
||||||
|
template: "<{{^lambda}}{{static}}{{/lambda}}>"
|
||||||
|
expected: "<>"
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
data: {
|
||||||
|
name: "Kilgarvan"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Begin layout >>
|
||||||
|
{{content}}
|
||||||
|
<< End layout
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
Hello, this is {{name}}.
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
#+feature dynamic-literals
|
||||||
|
#+test
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:testing"
|
||||||
|
import mustache "mustache"
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_simple_substitution :: proc(t: ^testing.T) {
|
||||||
|
data := map[string]string{"name" = "World"}
|
||||||
|
result, err := mustache.render("Hello, {{name}}!", data)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "Hello, World!")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_bool_section :: proc(t: ^testing.T) {
|
||||||
|
data := map[string]bool{"show" = true}
|
||||||
|
result, err := mustache.render("{{#show}}visible{{/show}}", data)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "visible")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_inverted_section :: proc(t: ^testing.T) {
|
||||||
|
data := map[string]bool{"show" = false}
|
||||||
|
result, err := mustache.render("{{^show}}hidden{{/show}}", data)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "hidden")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_unescaped :: proc(t: ^testing.T) {
|
||||||
|
data := map[string]string{"html" = "<b>bold</b>"}
|
||||||
|
result, err := mustache.render("{{{html}}}", data)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "<b>bold</b>")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_array_iteration :: proc(t: ^testing.T) {
|
||||||
|
items := make([dynamic]map[string]string)
|
||||||
|
defer delete(items)
|
||||||
|
append(&items, map[string]string{"name" = "Alice"})
|
||||||
|
append(&items, map[string]string{"name" = "Bob"})
|
||||||
|
|
||||||
|
data := map[string][dynamic]map[string]string{"items" = items}
|
||||||
|
result, err := mustache.render("{{#items}}{{name}} {{/items}}", data)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "Alice Bob ")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_partial :: proc(t: ^testing.T) {
|
||||||
|
data := map[string]string{"name" = "Test"}
|
||||||
|
partials := map[string]string{"greeting" = "Hello, {{name}}!"}
|
||||||
|
result, err := mustache.render("{{>greeting}}", data, partials)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "Hello, Test!")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_mixed_types :: proc(t: ^testing.T) {
|
||||||
|
data := map[string]any{
|
||||||
|
"site_title" = "One Idiot Developer",
|
||||||
|
"has_date" = true,
|
||||||
|
"date" = "17 Jul 2025",
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := mustache.render(
|
||||||
|
"{{site_title}}: {{#has_date}}{{date}}{{/has_date}}",
|
||||||
|
data,
|
||||||
|
)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "One Idiot Developer: 17 Jul 2025")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_nested_context :: proc(t: ^testing.T) {
|
||||||
|
page := map[string]string{
|
||||||
|
"title" = "My Post",
|
||||||
|
}
|
||||||
|
data := map[string]any{
|
||||||
|
"site_title" = "One Idiot Developer",
|
||||||
|
"page" = page,
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := mustache.render(
|
||||||
|
"{{site_title}}: {{#page}}{{title}}{{/page}}",
|
||||||
|
data,
|
||||||
|
)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "One Idiot Developer: My Post")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_layout :: proc(t: ^testing.T) {
|
||||||
|
layout := `<html><body>{{{content}}}</body></html>`
|
||||||
|
template := "<p>Hello!</p>"
|
||||||
|
data := map[string]string{}
|
||||||
|
|
||||||
|
result, err := mustache.render_in_layout(template, data, layout)
|
||||||
|
testing.expect(t, err == nil)
|
||||||
|
testing.expect(t, result == "<html><body><p>Hello!</p></body></html>")
|
||||||
|
}
|
||||||
+164
-159
@@ -1,5 +1,8 @@
|
|||||||
|
#+feature dynamic-literals
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import mustache "mustache"
|
||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
@@ -9,6 +12,55 @@ MONTHS: [12]string = {
|
|||||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Page_Context :: struct {
|
||||||
|
permalink: string,
|
||||||
|
title: string,
|
||||||
|
star: string,
|
||||||
|
has_date: bool,
|
||||||
|
date_iso: string,
|
||||||
|
date_display: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
Year_Section :: struct {
|
||||||
|
year: string,
|
||||||
|
posts: [dynamic]Page_Context,
|
||||||
|
}
|
||||||
|
|
||||||
|
Year_Slice :: struct {
|
||||||
|
year: string,
|
||||||
|
posts: []Page_Context,
|
||||||
|
}
|
||||||
|
|
||||||
|
build_page_context :: proc(page: Page) -> Page_Context {
|
||||||
|
star := ""
|
||||||
|
if page.is_starred {
|
||||||
|
star = ICON_STAR
|
||||||
|
}
|
||||||
|
return Page_Context{
|
||||||
|
permalink = page.permalink,
|
||||||
|
title = page.title,
|
||||||
|
star = star,
|
||||||
|
has_date = page.date != "",
|
||||||
|
date_iso = page.date,
|
||||||
|
date_display = format_date(page.date),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
build_social_context :: proc(config: Site_Config) -> [dynamic]map[string]string {
|
||||||
|
social_ctx := make([dynamic]map[string]string)
|
||||||
|
for link in config.social {
|
||||||
|
append(
|
||||||
|
&social_ctx,
|
||||||
|
map[string]string{
|
||||||
|
"name" = link.name,
|
||||||
|
"url" = link.url,
|
||||||
|
"icon" = social_icon(link.name),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return social_ctx
|
||||||
|
}
|
||||||
|
|
||||||
render_site :: proc(pages: []Page, config: Site_Config) {
|
render_site :: proc(pages: []Page, config: Site_Config) {
|
||||||
sort_pages_by_date(pages)
|
sort_pages_by_date(pages)
|
||||||
|
|
||||||
@@ -65,154 +117,148 @@ render_site :: proc(pages: []Page, config: Site_Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render_page_html :: proc(page: Page, config: Site_Config) -> string {
|
render_page_html :: proc(page: Page, config: Site_Config) -> string {
|
||||||
comments := ""
|
social_ctx := build_social_context(config)
|
||||||
if page.type == .Post {
|
defer delete(social_ctx)
|
||||||
comments = `
|
|
||||||
<script src="https://utteranc.es/client.js"
|
data := map[string]any{
|
||||||
repo="sbrow/sbrow.github.io"
|
"title" = fmt.tprintf("%s | %s", page.title, config.title),
|
||||||
issue-term="pathname"
|
"page_title" = page.title,
|
||||||
label="Comment"
|
"body_html" = page.body_html,
|
||||||
theme="github-dark"
|
"has_date" = page.date != "",
|
||||||
crossorigin="anonymous"
|
"date_iso" = page.date,
|
||||||
async></script>`
|
"date_display" = format_date(page.date),
|
||||||
|
"is_post" = page.type == .Post,
|
||||||
|
"home_icon" = ICON_HOME,
|
||||||
|
"chevron_up" = ICON_CHEVRON_UP,
|
||||||
|
"year" = "2026",
|
||||||
|
"author" = config.author,
|
||||||
|
"social" = social_ctx[:],
|
||||||
}
|
}
|
||||||
|
|
||||||
body := fmt.aprintf(
|
partials := load_partials(config.layouts_dir)
|
||||||
`<main>
|
|
||||||
<article class="prose">
|
post_tpl, _ := os.read_entire_file_from_path( fmt.tprintf("%s/post.html", config.layouts_dir), context.allocator)
|
||||||
<h1>%s</h1>
|
base_tpl, _ := os.read_entire_file_from_path( fmt.tprintf("%s/base.html", config.layouts_dir), context.allocator)
|
||||||
%s %s
|
|
||||||
</article>%s
|
result, err := mustache.render_in_layout(
|
||||||
</main>
|
string(post_tpl),
|
||||||
`,
|
data,
|
||||||
page.title,
|
string(base_tpl),
|
||||||
render_date(page),
|
partials,
|
||||||
page.body_html,
|
|
||||||
comments,
|
|
||||||
)
|
)
|
||||||
title := fmt.tprintf("%s | %s", page.title, config.title)
|
if err != nil {
|
||||||
return render_chrome(title, body, config)
|
fmt.eprintfln("thor: mustache error rendering page: %v", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
load_partials :: proc(layouts_dir: string) -> map[string]string {
|
||||||
|
partials: map[string]string
|
||||||
|
|
||||||
|
nav, _ := os.read_entire_file_from_path( fmt.tprintf("%s/partials/nav.html", layouts_dir), context.allocator)
|
||||||
|
partials["nav"] = string(nav)
|
||||||
|
|
||||||
|
footer, _ := os.read_entire_file_from_path( fmt.tprintf("%s/partials/footer.html", layouts_dir), context.allocator)
|
||||||
|
partials["footer"] = string(footer)
|
||||||
|
|
||||||
|
return partials
|
||||||
}
|
}
|
||||||
|
|
||||||
render_home_html :: proc(home: Page, pages: []Page, config: Site_Config) -> string {
|
render_home_html :: proc(home: Page, pages: []Page, config: Site_Config) -> string {
|
||||||
items: [dynamic]string
|
list_pages := make([dynamic]Page_Context)
|
||||||
defer delete(items)
|
defer delete(list_pages)
|
||||||
|
|
||||||
for page in pages {
|
for page in pages {
|
||||||
if page.type == .Home {
|
if page.type == .Home {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
append(&items, render_post_item(page))
|
append(&list_pages, build_page_context(page))
|
||||||
}
|
}
|
||||||
|
|
||||||
post_list := strings.join(items[:], "\n")
|
social_ctx := build_social_context(config)
|
||||||
|
defer delete(social_ctx)
|
||||||
|
|
||||||
body := fmt.aprintf(
|
data := map[string]any{
|
||||||
`<main>
|
"title" = config.title,
|
||||||
<header class="text-center mb-28">
|
"home_body" = home.body_html,
|
||||||
%s </header>
|
"list_pages" = list_pages[:],
|
||||||
<ul>
|
"home_icon" = ICON_HOME,
|
||||||
%s
|
"chevron_up" = ICON_CHEVRON_UP,
|
||||||
</ul>
|
"year" = "2026",
|
||||||
</main>
|
"author" = config.author,
|
||||||
`,
|
"social" = social_ctx[:],
|
||||||
home.body_html,
|
}
|
||||||
post_list,
|
|
||||||
|
partials := load_partials(config.layouts_dir)
|
||||||
|
|
||||||
|
home_tpl, _ := os.read_entire_file_from_path( fmt.tprintf("%s/home.html", config.layouts_dir), context.allocator)
|
||||||
|
base_tpl, _ := os.read_entire_file_from_path( fmt.tprintf("%s/base.html", config.layouts_dir), context.allocator)
|
||||||
|
|
||||||
|
result, err := mustache.render_in_layout(
|
||||||
|
string(home_tpl),
|
||||||
|
data,
|
||||||
|
string(base_tpl),
|
||||||
|
partials,
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
return render_chrome(config.title, body, config)
|
fmt.eprintfln("thor: mustache error: %v", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
render_posts_html :: proc(pages: []Page, config: Site_Config) -> string {
|
render_posts_html :: proc(pages: []Page, config: Site_Config) -> string {
|
||||||
parts: [dynamic]string
|
// Group posts by year
|
||||||
defer delete(parts)
|
year_sections := make([dynamic]Year_Section)
|
||||||
|
defer delete(year_sections)
|
||||||
append(&parts, "<main>\n")
|
|
||||||
append(&parts, ` <h1 class="text-center">Posts</h1>` + "\n")
|
|
||||||
|
|
||||||
current_year := ""
|
current_year := ""
|
||||||
open := false
|
|
||||||
for page in pages {
|
for page in pages {
|
||||||
if page.type != .Post {
|
if page.type != .Post {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
year := get_year(page.date)
|
year := get_year(page.date)
|
||||||
if year != current_year {
|
if year != current_year {
|
||||||
if open {
|
append(&year_sections, Year_Section{year = year})
|
||||||
append(&parts, " </ul>\n </section>\n")
|
|
||||||
}
|
|
||||||
open = true
|
|
||||||
current_year = year
|
current_year = year
|
||||||
append(
|
|
||||||
&parts,
|
|
||||||
fmt.aprintf(" <section>\n <h2>%s</h2>\n <hr class=\"text-slate-800 mb-1\">\n <ul>\n", year),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
append(&parts, render_post_item(page))
|
append(&year_sections[len(year_sections) - 1].posts, build_page_context(page))
|
||||||
append(&parts, "\n")
|
|
||||||
}
|
}
|
||||||
if open {
|
// Convert [dynamic]Page_Context slices to []Page_Context for mustache
|
||||||
append(&parts, " </ul>\n </section>\n")
|
year_slices := make([dynamic]Year_Slice)
|
||||||
}
|
defer delete(year_slices)
|
||||||
append(&parts, "</main>\n")
|
for section in year_sections {
|
||||||
|
append(&year_slices, Year_Slice{year = section.year, posts = section.posts[:]})
|
||||||
body := strings.join(parts[:], "")
|
|
||||||
title := fmt.tprintf("Posts | %s", config.title)
|
|
||||||
return render_chrome(title, body, config)
|
|
||||||
}
|
|
||||||
|
|
||||||
render_chrome :: proc(page_title: string, body: string, config: Site_Config) -> string {
|
|
||||||
header := fmt.aprintf(HEADER, ICON_HOME)
|
|
||||||
|
|
||||||
// Build social links from config
|
|
||||||
social_parts: [dynamic]string
|
|
||||||
defer delete(social_parts)
|
|
||||||
for link in config.social {
|
|
||||||
append(
|
|
||||||
&social_parts,
|
|
||||||
fmt.aprintf(
|
|
||||||
`\n <a href="%s" target="_blank" rel="noopener noreferrer me" title="%s">%s</a>`,
|
|
||||||
link.url,
|
|
||||||
link.name,
|
|
||||||
social_icon(link.name),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
social_html := strings.join(social_parts[:], "")
|
|
||||||
|
|
||||||
copyright := "<p><small>©</small> 2026</p>"
|
|
||||||
if config.author != "" {
|
|
||||||
copyright = fmt.aprintf("<p><small>©</small> 2026 %s</p>", config.author)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.aprintf(
|
social_ctx := build_social_context(config)
|
||||||
`<!DOCTYPE html>
|
defer delete(social_ctx)
|
||||||
<html lang="en">
|
|
||||||
<head>
|
data := map[string]any{
|
||||||
<meta charset="utf-8">
|
"title" = fmt.tprintf("Posts | %s", config.title),
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
"year_sections" = year_slices[:],
|
||||||
<title>%s</title>
|
"home_icon" = ICON_HOME,
|
||||||
<link rel="stylesheet" href="/css/main.css?v=2">
|
"chevron_up" = ICON_CHEVRON_UP,
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
|
"year" = "2026",
|
||||||
<script src="/js/main.js?v=2" defer></script>
|
"author" = config.author,
|
||||||
</head>
|
"social" = social_ctx[:],
|
||||||
<body>
|
}
|
||||||
%s%s<footer>
|
|
||||||
<a class="goto-top opacity-0" href="#">%s</a>%s
|
partials := load_partials(config.layouts_dir)
|
||||||
<p class="pt-5 prose">Proudly built with <a href="https://odin-lang.org/">Odin</a> and <a href="https://tailwindcss.com/">Tailwindcss</a></p>
|
|
||||||
%s</footer>
|
posts_tpl, _ := os.read_entire_file_from_path( fmt.tprintf("%s/posts_list.html", config.layouts_dir), context.allocator)
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
|
base_tpl, _ := os.read_entire_file_from_path( fmt.tprintf("%s/base.html", config.layouts_dir), context.allocator)
|
||||||
<script>hljs.highlightAll();</script>
|
|
||||||
</body>
|
result, err := mustache.render_in_layout(
|
||||||
</html>
|
string(posts_tpl),
|
||||||
`,
|
data,
|
||||||
page_title,
|
string(base_tpl),
|
||||||
header,
|
partials,
|
||||||
body,
|
|
||||||
ICON_CHEVRON_UP,
|
|
||||||
social_html,
|
|
||||||
copyright,
|
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.eprintfln("thor: mustache error: %v", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
social_icon :: proc(name: string) -> string {
|
social_icon :: proc(name: string) -> string {
|
||||||
@@ -225,47 +271,6 @@ social_icon :: proc(name: string) -> string {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
HEADER :: `
|
|
||||||
<header>
|
|
||||||
<nav>
|
|
||||||
<ul>
|
|
||||||
<li class="mr-auto"><a href="/">%s</a></li>
|
|
||||||
<li><a href="/ideas/">Ideas</a></li>
|
|
||||||
<li><a href="/posts/">Posts</a></li>
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
</header>
|
|
||||||
`
|
|
||||||
|
|
||||||
render_post_item :: proc(page: Page) -> string {
|
|
||||||
date_html := ""
|
|
||||||
if page.date != "" {
|
|
||||||
date_html = fmt.aprintf(
|
|
||||||
"<time datetime=\"%s\">%s</time>",
|
|
||||||
page.date,
|
|
||||||
format_date(page.date),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
star := ""
|
|
||||||
if page.is_starred {
|
|
||||||
star = ICON_STAR
|
|
||||||
}
|
|
||||||
return fmt.aprintf(
|
|
||||||
` <li class="flex justify-between"><a href="%s">%s</a><span>%s%s</span></li>`,
|
|
||||||
page.permalink,
|
|
||||||
page.title,
|
|
||||||
star,
|
|
||||||
date_html,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
render_date :: proc(page: Page) -> string {
|
|
||||||
if page.date == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return fmt.aprintf(` <time datetime="%s">%s</time>` + "\n", page.date, format_date(page.date))
|
|
||||||
}
|
|
||||||
|
|
||||||
format_date :: proc(iso: string) -> string {
|
format_date :: proc(iso: string) -> string {
|
||||||
if len(iso) < 10 {
|
if len(iso) < 10 {
|
||||||
return iso
|
return iso
|
||||||
|
|||||||
Reference in New Issue
Block a user