diff --git a/Cargo.lock b/Cargo.lock index 37db7b87..83044cc0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -219,6 +219,7 @@ dependencies = [ "clap", "clap-stdin", "css-colors", + "detect-newline-style", "encoding_rs_io", "indexmap", "itertools", @@ -422,6 +423,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22c2bbfc5708f23437b074ba4e699b14fd6d7181a61695bccc8d944b78739236" +[[package]] +name = "detect-newline-style" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1124f25c3615ab547669f878088cef84850679327f79eccc70412c25a6643749" +dependencies = [ + "regex", +] + [[package]] name = "deunicode" version = "1.4.3" diff --git a/whiskers/Cargo.toml b/whiskers/Cargo.toml index 45427771..e9acb10a 100644 --- a/whiskers/Cargo.toml +++ b/whiskers/Cargo.toml @@ -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" diff --git a/whiskers/examples/demo/input.tera b/whiskers/examples/demo/input.tera index 7fc4e425..6ae918fa 100644 --- a/whiskers/examples/demo/input.tera +++ b/whiskers/examples/demo/input.tera @@ -25,4 +25,4 @@ whiskers: ### Conditionals -this is a {{ if(cond=flavor.dark, t="dark", f="light") }} theme \ No newline at end of file +this is a {{ if(cond=flavor.dark, t="dark", f="light") }} theme diff --git a/whiskers/examples/demo/justfile b/whiskers/examples/demo/justfile index d3b99b3f..77c8272d 100644 --- a/whiskers/examples/demo/justfile +++ b/whiskers/examples/demo/justfile @@ -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") \ No newline at end of file +all: setup (gen "latte") (gen "frappe") (gen "macchiato") (gen "mocha") diff --git a/whiskers/examples/demo/output/frappe.md b/whiskers/examples/demo/output/frappe.md index 6befc15d..eb0ed5d7 100644 --- a/whiskers/examples/demo/output/frappe.md +++ b/whiskers/examples/demo/output/frappe.md @@ -15,4 +15,4 @@ ### Conditionals -this is a dark theme \ No newline at end of file +this is a dark theme diff --git a/whiskers/examples/demo/output/latte.md b/whiskers/examples/demo/output/latte.md index 72db3a18..bfc15b5b 100644 --- a/whiskers/examples/demo/output/latte.md +++ b/whiskers/examples/demo/output/latte.md @@ -15,4 +15,4 @@ ### Conditionals -this is a light theme \ No newline at end of file +this is a light theme diff --git a/whiskers/examples/demo/output/macchiato.md b/whiskers/examples/demo/output/macchiato.md index 32bd5a56..c737dc58 100644 --- a/whiskers/examples/demo/output/macchiato.md +++ b/whiskers/examples/demo/output/macchiato.md @@ -15,4 +15,4 @@ ### Conditionals -this is a dark theme \ No newline at end of file +this is a dark theme diff --git a/whiskers/examples/demo/output/mocha.md b/whiskers/examples/demo/output/mocha.md index 3a0174b0..a165cffa 100644 --- a/whiskers/examples/demo/output/mocha.md +++ b/whiskers/examples/demo/output/mocha.md @@ -15,4 +15,4 @@ ### Conditionals -this is a dark theme \ No newline at end of file +this is a dark theme diff --git a/whiskers/examples/frontmatter/input.tera b/whiskers/examples/frontmatter/input.tera index 964b8a74..0138ec89 100644 --- a/whiskers/examples/frontmatter/input.tera +++ b/whiskers/examples/frontmatter/input.tera @@ -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 \ No newline at end of file +this is a {{ if(cond=flavor.dark, t="dark", f="light") }} theme diff --git a/whiskers/examples/frontmatter/justfile b/whiskers/examples/frontmatter/justfile index fd20d5a3..f5376b29 100644 --- a/whiskers/examples/frontmatter/justfile +++ b/whiskers/examples/frontmatter/justfile @@ -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") \ No newline at end of file +all: setup (gen "latte") (gen "frappe") (gen "macchiato") (gen "mocha") diff --git a/whiskers/examples/frontmatter/output/frappe.md b/whiskers/examples/frontmatter/output/frappe.md index 50b8f1a9..f3eda606 100644 --- a/whiskers/examples/frontmatter/output/frappe.md +++ b/whiskers/examples/frontmatter/output/frappe.md @@ -23,4 +23,4 @@ This file also contains variables that have been defined in the frontmatter, as ### Conditionals -this is a dark theme \ No newline at end of file +this is a dark theme diff --git a/whiskers/examples/frontmatter/output/latte.md b/whiskers/examples/frontmatter/output/latte.md index b0780759..d70acc42 100644 --- a/whiskers/examples/frontmatter/output/latte.md +++ b/whiskers/examples/frontmatter/output/latte.md @@ -23,4 +23,4 @@ This file also contains variables that have been defined in the frontmatter, as ### Conditionals -this is a light theme \ No newline at end of file +this is a light theme diff --git a/whiskers/examples/frontmatter/output/macchiato.md b/whiskers/examples/frontmatter/output/macchiato.md index 18f9345c..6cd7c5a5 100644 --- a/whiskers/examples/frontmatter/output/macchiato.md +++ b/whiskers/examples/frontmatter/output/macchiato.md @@ -23,4 +23,4 @@ This file also contains variables that have been defined in the frontmatter, as ### Conditionals -this is a dark theme \ No newline at end of file +this is a dark theme diff --git a/whiskers/examples/frontmatter/output/mocha.md b/whiskers/examples/frontmatter/output/mocha.md index 70301bc1..5cc128e7 100644 --- a/whiskers/examples/frontmatter/output/mocha.md +++ b/whiskers/examples/frontmatter/output/mocha.md @@ -23,4 +23,4 @@ This file also contains variables that have been defined in the frontmatter, as ### Conditionals -this is a dark theme \ No newline at end of file +this is a dark theme diff --git a/whiskers/examples/multi-output/single-accent/output-frappe-red.txt b/whiskers/examples/multi-output/single-accent/output-frappe-red.txt index 0daaefcd..7a1865c9 100644 --- a/whiskers/examples/multi-output/single-accent/output-frappe-red.txt +++ b/whiskers/examples/multi-output/single-accent/output-frappe-red.txt @@ -1 +1 @@ -Frappé accent red \ No newline at end of file +Frappé accent red diff --git a/whiskers/examples/multi-output/single-accent/output-latte-red.txt b/whiskers/examples/multi-output/single-accent/output-latte-red.txt index c422ede9..d5f61fed 100644 --- a/whiskers/examples/multi-output/single-accent/output-latte-red.txt +++ b/whiskers/examples/multi-output/single-accent/output-latte-red.txt @@ -1 +1 @@ -Latte accent red \ No newline at end of file +Latte accent red diff --git a/whiskers/examples/multi-output/single-accent/output-macchiato-red.txt b/whiskers/examples/multi-output/single-accent/output-macchiato-red.txt index cb7892fe..d2cf71d6 100644 --- a/whiskers/examples/multi-output/single-accent/output-macchiato-red.txt +++ b/whiskers/examples/multi-output/single-accent/output-macchiato-red.txt @@ -1 +1 @@ -Macchiato accent red \ No newline at end of file +Macchiato accent red diff --git a/whiskers/examples/multi-output/single-accent/output-mocha-red.txt b/whiskers/examples/multi-output/single-accent/output-mocha-red.txt index dbb95666..6ac30bf8 100644 --- a/whiskers/examples/multi-output/single-accent/output-mocha-red.txt +++ b/whiskers/examples/multi-output/single-accent/output-mocha-red.txt @@ -1 +1 @@ -Mocha accent red \ No newline at end of file +Mocha accent red diff --git a/whiskers/examples/multi-output/single-flavor/output-a.txt b/whiskers/examples/multi-output/single-flavor/output-a.txt index b1ca0449..c6224c19 100644 --- a/whiskers/examples/multi-output/single-flavor/output-a.txt +++ b/whiskers/examples/multi-output/single-flavor/output-a.txt @@ -1 +1 @@ -Latte variant a \ No newline at end of file +Latte variant a diff --git a/whiskers/examples/multi-output/single-flavor/output-b.txt b/whiskers/examples/multi-output/single-flavor/output-b.txt index 47ee6665..4c75fab8 100644 --- a/whiskers/examples/multi-output/single-flavor/output-b.txt +++ b/whiskers/examples/multi-output/single-flavor/output-b.txt @@ -1 +1 @@ -Latte variant b \ No newline at end of file +Latte variant b diff --git a/whiskers/examples/single-file/overrides/justfile b/whiskers/examples/single-file/overrides/justfile index c9a610c2..f655d8c8 100644 --- a/whiskers/examples/single-file/overrides/justfile +++ b/whiskers/examples/single-file/overrides/justfile @@ -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 \ No newline at end of file + @{{whiskers_cmd}} input.tera > output.md diff --git a/whiskers/examples/single-file/simple/justfile b/whiskers/examples/single-file/simple/justfile index c9a610c2..f655d8c8 100644 --- a/whiskers/examples/single-file/simple/justfile +++ b/whiskers/examples/single-file/simple/justfile @@ -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 \ No newline at end of file + @{{whiskers_cmd}} input.tera > output.md diff --git a/whiskers/src/frontmatter.rs b/whiskers/src/frontmatter.rs index cc627e19..581fde42 100644 --- a/whiskers/src/frontmatter.rs +++ b/whiskers/src/frontmatter.rs @@ -1,3 +1,4 @@ +use detect_newline_style::LineEnding; use std::collections::HashMap; #[derive(Debug)] @@ -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)) } diff --git a/whiskers/tests/cli.rs b/whiskers/tests/cli.rs index 119009b5..4f2b4b20 100644 --- a/whiskers/tests/cli.rs +++ b/whiskers/tests/cli.rs @@ -42,7 +42,9 @@ 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 @@ -50,7 +52,9 @@ mod happy_path { 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 @@ -58,7 +62,9 @@ mod happy_path { 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 @@ -66,7 +72,9 @@ mod happy_path { 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!")); } } diff --git a/whiskers/tests/fixtures/encodings/README.md b/whiskers/tests/fixtures/encodings/README.md index 0c6ab973..26d9f9fa 100644 --- a/whiskers/tests/fixtures/encodings/README.md +++ b/whiskers/tests/fixtures/encodings/README.md @@ -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. \ No newline at end of file +There are tests in `tests/encodings.rs` that ensure these fixtures are not unintentionally changed. diff --git a/whiskers/tests/fixtures/encodings/utf16be.tera b/whiskers/tests/fixtures/encodings/utf16be.tera index d1de7a99..55ce7e49 100644 Binary files a/whiskers/tests/fixtures/encodings/utf16be.tera and b/whiskers/tests/fixtures/encodings/utf16be.tera differ diff --git a/whiskers/tests/fixtures/encodings/utf16le.tera b/whiskers/tests/fixtures/encodings/utf16le.tera index cab6764a..040a6cdc 100644 Binary files a/whiskers/tests/fixtures/encodings/utf16le.tera and b/whiskers/tests/fixtures/encodings/utf16le.tera differ diff --git a/whiskers/tests/fixtures/encodings/utf8.tera b/whiskers/tests/fixtures/encodings/utf8.tera index e9dc9fda..0fc071ec 100644 --- a/whiskers/tests/fixtures/encodings/utf8.tera +++ b/whiskers/tests/fixtures/encodings/utf8.tera @@ -2,4 +2,4 @@ whiskers: version: "2.0" --- -it worked! \ No newline at end of file +it worked! diff --git a/whiskers/tests/fixtures/encodings/utf8bom.tera b/whiskers/tests/fixtures/encodings/utf8bom.tera index a4254f91..06b50b70 100644 --- a/whiskers/tests/fixtures/encodings/utf8bom.tera +++ b/whiskers/tests/fixtures/encodings/utf8bom.tera @@ -2,4 +2,4 @@ whiskers: version: "2.0" --- -it worked! \ No newline at end of file +it worked! diff --git a/whiskers/tests/fixtures/multi/multi.tera b/whiskers/tests/fixtures/multi/multi.tera index b1bda761..aa7a9306 100644 --- a/whiskers/tests/fixtures/multi/multi.tera +++ b/whiskers/tests/fixtures/multi/multi.tera @@ -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 %} \ No newline at end of file +{% endfor -%} diff --git a/whiskers/tests/fixtures/multifile.tera b/whiskers/tests/fixtures/multifile.tera index ebb365ea..651ab185 100644 --- a/whiskers/tests/fixtures/multifile.tera +++ b/whiskers/tests/fixtures/multifile.tera @@ -9,4 +9,4 @@ whiskers: --- # Catppuccin {{flavor.name}}{% if variant == "no-italics" %} (no italics){% endif %} [theme] -{{accent}}: #{{flavor.colors[accent].hex}} \ No newline at end of file +{{accent}}: #{{flavor.colors[accent].hex}} diff --git a/whiskers/tests/fixtures/single/single.md b/whiskers/tests/fixtures/single/single.md index 01cb25f9..9498a8ab 100644 --- a/whiskers/tests/fixtures/single/single.md +++ b/whiskers/tests/fixtures/single/single.md @@ -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%) \ No newline at end of file +dark green: #205016 // hsl(109, 58%, 20%) diff --git a/whiskers/tests/fixtures/single/single.tera b/whiskers/tests/fixtures/single/single.tera index 00028db5..80d18dc4 100644 --- a/whiskers/tests/fixtures/single/single.tera +++ b/whiskers/tests/fixtures/single/single.tera @@ -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) }} \ No newline at end of file +dark green: #{{ darkgreen.hex }} // {{ self::css_hsl(v=darkgreen.hsl) }} diff --git a/whiskers/tests/fixtures/singlefile-multiflavor.tera b/whiskers/tests/fixtures/singlefile-multiflavor.tera index 9ce6243b..0700ca2a 100644 --- a/whiskers/tests/fixtures/singlefile-multiflavor.tera +++ b/whiskers/tests/fixtures/singlefile-multiflavor.tera @@ -7,4 +7,4 @@ accent: mauve # Catppuccin {{flavor.name}} [theme] {{accent}}: #{{flavor.colors[accent].hex}} -{% endfor %} \ No newline at end of file +{% endfor %} diff --git a/whiskers/tests/fixtures/singlefile-singleflavor.tera b/whiskers/tests/fixtures/singlefile-singleflavor.tera index c0465900..8508a1ac 100644 --- a/whiskers/tests/fixtures/singlefile-singleflavor.tera +++ b/whiskers/tests/fixtures/singlefile-singleflavor.tera @@ -5,4 +5,4 @@ accent: mauve --- # Catppuccin {{flavor.name}} [theme] -{{accent}}: #{{flavor.colors[accent].hex}} \ No newline at end of file +{{accent}}: #{{flavor.colors[accent].hex}}