Skip to content

Commit

Permalink
fix(whiskers): avoid excessive whitespace trimming (#216)
Browse files Browse the repository at this point in the history
* fix(whiskers): only trim template start

* style: trailing newlines

* test(whiskers): update cli tests to match newlines

* fix: detect line ending for windows support

* test(whiskers): use contains
  • Loading branch information
uncenter authored May 19, 2024
1 parent 5ab5d87 commit 59bc0f6
Show file tree
Hide file tree
Showing 35 changed files with 59 additions and 38 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions whiskers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ catppuccin = { version = "2.2", features = ["serde", "css-colors"] }
clap = { version = "4.5.4", features = ["derive"] }
clap-stdin = "0.4"
css-colors = "1.0.1"
detect-newline-style = "0.1.2"
encoding_rs_io = "0.1.7"
indexmap = { version = "2.2.6", features = ["serde"] }
itertools = "0.12.1"
Expand Down
2 changes: 1 addition & 1 deletion whiskers/examples/demo/input.tera
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ whiskers:

### Conditionals

this is a {{ if(cond=flavor.dark, t="dark", f="light") }} theme
this is a {{ if(cond=flavor.dark, t="dark", f="light") }} theme
2 changes: 1 addition & 1 deletion whiskers/examples/demo/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ gen flavor:
@{{whiskers_cmd}} input.tera -f {{flavor}} > {{output}}/{{flavor}}.md

# Generate all four flavors
all: setup (gen "latte") (gen "frappe") (gen "macchiato") (gen "mocha")
all: setup (gen "latte") (gen "frappe") (gen "macchiato") (gen "mocha")
2 changes: 1 addition & 1 deletion whiskers/examples/demo/output/frappe.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

### Conditionals

this is a dark theme
this is a dark theme
2 changes: 1 addition & 1 deletion whiskers/examples/demo/output/latte.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

### Conditionals

this is a light theme
this is a light theme
2 changes: 1 addition & 1 deletion whiskers/examples/demo/output/macchiato.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

### Conditionals

this is a dark theme
this is a dark theme
2 changes: 1 addition & 1 deletion whiskers/examples/demo/output/mocha.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

### Conditionals

this is a dark theme
this is a dark theme
2 changes: 1 addition & 1 deletion whiskers/examples/frontmatter/input.tera
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ This file also contains variables that have been defined in the frontmatter, as

### Conditionals

this is a {{ if(cond=flavor.dark, t="dark", f="light") }} theme
this is a {{ if(cond=flavor.dark, t="dark", f="light") }} theme
2 changes: 1 addition & 1 deletion whiskers/examples/frontmatter/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ gen flavor:
@{{whiskers_cmd}} input.tera --flavor {{flavor}} > {{output}}/{{flavor}}.md

# Generate all four flavors
all: setup (gen "latte") (gen "frappe") (gen "macchiato") (gen "mocha")
all: setup (gen "latte") (gen "frappe") (gen "macchiato") (gen "mocha")
2 changes: 1 addition & 1 deletion whiskers/examples/frontmatter/output/frappe.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ This file also contains variables that have been defined in the frontmatter, as

### Conditionals

this is a dark theme
this is a dark theme
2 changes: 1 addition & 1 deletion whiskers/examples/frontmatter/output/latte.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ This file also contains variables that have been defined in the frontmatter, as

### Conditionals

this is a light theme
this is a light theme
2 changes: 1 addition & 1 deletion whiskers/examples/frontmatter/output/macchiato.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ This file also contains variables that have been defined in the frontmatter, as

### Conditionals

this is a dark theme
this is a dark theme
2 changes: 1 addition & 1 deletion whiskers/examples/frontmatter/output/mocha.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ This file also contains variables that have been defined in the frontmatter, as

### Conditionals

this is a dark theme
this is a dark theme
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Frappé accent red
Frappé accent red
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Latte accent red
Latte accent red
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Macchiato accent red
Macchiato accent red
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Mocha accent red
Mocha accent red
2 changes: 1 addition & 1 deletion whiskers/examples/multi-output/single-flavor/output-a.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Latte variant a
Latte variant a
2 changes: 1 addition & 1 deletion whiskers/examples/multi-output/single-flavor/output-b.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Latte variant b
Latte variant b
2 changes: 1 addition & 1 deletion whiskers/examples/single-file/overrides/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ whiskers_cmd := "cargo run --bin whiskers --"

# Generate a single file containing all four flavors
gen:
@{{whiskers_cmd}} input.tera > output.md
@{{whiskers_cmd}} input.tera > output.md
2 changes: 1 addition & 1 deletion whiskers/examples/single-file/simple/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ whiskers_cmd := "cargo run --bin whiskers --"

# Generate a single file containing all four flavors
gen:
@{{whiskers_cmd}} input.tera > output.md
@{{whiskers_cmd}} input.tera > output.md
12 changes: 7 additions & 5 deletions whiskers/src/frontmatter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use detect_newline_style::LineEnding;
use std::collections::HashMap;

#[derive(Debug)]
Expand Down Expand Up @@ -39,13 +40,14 @@ fn split(template: &str) -> Option<(&str, &str)> {
// we consider a template to possibly have frontmatter iff:
// * line 0 is "---"
// * there is another "---" on another line
let template = template.trim();
let sep = "---";
if !template.starts_with(sep) {
let template = template.trim_start();
let eol = LineEnding::find(template, LineEnding::LF).to_string();
let sep = "---".to_string() + &eol;
if !template.starts_with(&sep) {
return None;
}

template[sep.len()..]
.split_once(sep)
.map(|(a, b)| (a.trim(), b.trim()))
.split_once(&sep)
.map(|(a, b)| (a.trim(), b))
}
16 changes: 12 additions & 4 deletions whiskers/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,39 @@ mod happy_path {
fn test_utf8() {
let mut cmd = Command::cargo_bin("whiskers").expect("binary exists");
let assert = cmd.args(["tests/fixtures/encodings/utf8.tera"]).assert();
assert.success().stdout("it worked!");
assert
.success()
.stdout(predicate::str::contains("it worked!"));
}

/// Test that the CLI can render a UTF-8 with BOM template file
#[test]
fn test_utf8_bom() {
let mut cmd = Command::cargo_bin("whiskers").expect("binary exists");
let assert = cmd.args(["tests/fixtures/encodings/utf8bom.tera"]).assert();
assert.success().stdout("it worked!");
assert
.success()
.stdout(predicate::str::contains("it worked!"));
}

/// Test that the CLI can render a UTF-16 BE template file
#[test]
fn test_utf16be() {
let mut cmd = Command::cargo_bin("whiskers").expect("binary exists");
let assert = cmd.args(["tests/fixtures/encodings/utf16be.tera"]).assert();
assert.success().stdout("it worked!");
assert
.success()
.stdout(predicate::str::contains("it worked!"));
}

/// Test that the CLI can render a UTF-16 LE template file
#[test]
fn test_utf16le() {
let mut cmd = Command::cargo_bin("whiskers").expect("binary exists");
let assert = cmd.args(["tests/fixtures/encodings/utf16le.tera"]).assert();
assert.success().stdout("it worked!");
assert
.success()
.stdout(predicate::str::contains("it worked!"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/encodings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ The fixtures in this directory are encoded in various formats to test the encodi

Some text editors like to normalize the encoding of files when saving them. Please be careful not to change them unintentionally.

There are tests in `tests/encodings.rs` that ensure these fixtures are not unintentionally changed.
There are tests in `tests/encodings.rs` that ensure these fixtures are not unintentionally changed.
Binary file modified whiskers/tests/fixtures/encodings/utf16be.tera
Binary file not shown.
Binary file modified whiskers/tests/fixtures/encodings/utf16le.tera
Binary file not shown.
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/encodings/utf8.tera
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
whiskers:
version: "2.0"
---
it worked!
it worked!
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/encodings/utf8bom.tera
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
whiskers:
version: "2.0"
---
it worked!
it worked!
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/multi/multi.tera
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ hsl({{ v.h | round }}, {{ v.s * 100 | round }}%, {{ v.l * 100 | round }}%)
{{ color.name }} | #{{ color.hex }} | {{ self::css_rgb(v=color.rgb) }} | {{ self::css_hsl(v=color.hsl) }} |

{%- endfor %}
{% endfor %}
{% endfor -%}
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/multifile.tera
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ whiskers:
---
# Catppuccin {{flavor.name}}{% if variant == "no-italics" %} (no italics){% endif %}
[theme]
{{accent}}: #{{flavor.colors[accent].hex}}
{{accent}}: #{{flavor.colors[accent].hex}}
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/single/single.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ Crust | #dce0e8 | rgb(220, 224, 232) | hsl(220, 21%, 89%) |
red: #d20f39 / hsl(347, 87%, 44%)
orangey: #d3470f / hsl(17, 87%, 44%)
green: #40a02b / hsl(109, 58%, 40%)
dark green: #205016 // hsl(109, 58%, 20%)
dark green: #205016 // hsl(109, 58%, 20%)
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/single/single.tera
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ hsl({{ v.h | round }}, {{ v.s * 100 | round }}%, {{ v.l * 100 | round }}%)
red: #{{ red.hex }} / {{ self::css_hsl(v=red.hsl) }}
orangey: #{{ orange.hex }} / {{ self::css_hsl(v=orange.hsl) }}
green: #{{ green.hex }} / {{ self::css_hsl(v=green.hsl) }}
dark green: #{{ darkgreen.hex }} // {{ self::css_hsl(v=darkgreen.hsl) }}
dark green: #{{ darkgreen.hex }} // {{ self::css_hsl(v=darkgreen.hsl) }}
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/singlefile-multiflavor.tera
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ accent: mauve
# Catppuccin {{flavor.name}}
[theme]
{{accent}}: #{{flavor.colors[accent].hex}}
{% endfor %}
{% endfor %}
2 changes: 1 addition & 1 deletion whiskers/tests/fixtures/singlefile-singleflavor.tera
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ accent: mauve
---
# Catppuccin {{flavor.name}}
[theme]
{{accent}}: #{{flavor.colors[accent].hex}}
{{accent}}: #{{flavor.colors[accent].hex}}

0 comments on commit 59bc0f6

Please sign in to comment.