Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
b95df04
(doc) mention no_escape() in the "Escaping" section
vadz Feb 18, 2021
ecaf6a6
Merge pull request #418 from vadz/document-no-escape
sunng87 Feb 19, 2021
cb1ab6c
(test) add failing case for #416
sunng87 Feb 19, 2021
1453fdc
(fix) issue when up level block has base value
sunng87 Feb 19, 2021
d27a972
Merge pull request #419 from sunng87/fix/416
sunng87 Feb 20, 2021
1cf56b8
(doc) update changelog
sunng87 Feb 20, 2021
740db0a
(refactor) rename boolean helpers module to extra
sunng87 Mar 4, 2021
7558656
(feat) add len helper to extra module
sunng87 Mar 4, 2021
039ec51
(chore) add to web-programming category
sunng87 Mar 5, 2021
9604486
(test) add test for len helper
sunng87 Mar 5, 2021
b21352b
Merge pull request #421 from sunng87/feature/helper-len
sunng87 Mar 7, 2021
fc611e5
(doc) refresh changelog
sunng87 Mar 7, 2021
a8392df
(test) add test to reproduce #422
sunng87 Mar 14, 2021
4e12c39
(cargo-release) version 4.0.0-beta.1
sunng87 Mar 16, 2021
de39b97
(fix) json string literal rule
sunng87 Mar 26, 2021
dda5576
(fix) allow upper_case_acronyms
sunng87 Mar 26, 2021
9dcb05a
(fix) fix upper-case acronyms where it is possible
sunng87 Mar 26, 2021
3394484
Merge pull request #423 from sunng87/fix/remove-string-escape
sunng87 Mar 26, 2021
16fdbd1
Merge pull request #424 from sunng87/fix/upper-case-acronyms
sunng87 Mar 26, 2021
19d2b39
(doc) maintaining changelog
sunng87 Mar 28, 2021
b9614b6
(cargo-release) version 4.0.0-beta.2
sunng87 Mar 28, 2021
260d2f7
Fix Actix example link is dead
Zageron Mar 30, 2021
1a00402
Merge pull request #425 from Zageron/bug/actix-example-link-incorrect…
sunng87 Mar 31, 2021
7b373f7
(test) adopt break changes in rhai 0.19.15
sunng87 Apr 6, 2021
39589d9
Merge branch 'master' of github.com:sunng87/handlebars-rust
sunng87 Apr 6, 2021
4c5aa7d
(fix) panic on invalid tag name #427
sunng87 May 2, 2021
0b87103
(feat) improve error message for mismatching close tag
sunng87 May 3, 2021
6534e67
(test) add assertion for error message
sunng87 May 3, 2021
880486b
Merge pull request #429 from sunng87/fix/panic-within-errors
sunng87 May 3, 2021
1e98496
(fix) index out of bounds panic in json path
May 3, 2021
472ffa5
Merge pull request #430 from 5225225/json-path-fix
sunng87 May 3, 2021
2943eed
(fuzz) add fuzzing harness
May 3, 2021
d8b6c3f
Merge pull request #431 from 5225225/add-fuzzing
sunng87 May 4, 2021
cc15d8b
(test) move template tests into test module
sunng87 May 13, 2021
6959de0
(test) adding test for #433
sunng87 May 19, 2021
187b3f2
(fix) zero-arity subexpression support
sunng87 May 20, 2021
cb6654e
(fix) remove collect when not needed
sunng87 May 20, 2021
0ce070c
(feat) update rhai to 0.20
sunng87 May 21, 2021
bcf9000
(fix) load partial from source when accessing partial in dev mode
sunng87 May 19, 2021
d5bb7db
(fix) lifetime for partial resolver
sunng87 May 21, 2021
37b9b2e
(fix) clippy warnings
sunng87 May 21, 2021
c9a1f19
Merge pull request #434 from sunng87/fix/partials-in-dev-mode
sunng87 May 22, 2021
3b39f22
(refactor) call_inner helper API to remove Option from return type
sunng87 May 22, 2021
7d85921
(fix) clippy warnings
sunng87 May 23, 2021
f6fef6c
(fix) update return type of script helper
sunng87 May 23, 2021
b6745b1
(doc) update changelog
sunng87 May 23, 2021
0e6da88
(doc) update code example in README
sunng87 May 23, 2021
7a34a1f
(doc) update minimal rust version for 4.0
sunng87 May 23, 2021
ab7eb21
Merge pull request #437 from sunng87/refacor/call_inner_provided
sunng87 May 24, 2021
9f549f8
(doc) docstring and changelog update
sunng87 May 24, 2021
bb68f45
(cargo-release) version 4.0.0
sunng87 May 25, 2021
8703df7
(cargo-release) start next development iteration 4.0.1-alpha.0
sunng87 May 25, 2021
f9d45cd
(doc) add len to our doc #439
sunng87 May 26, 2021
71b3ed9
(doc) add changelog for #404
sunng87 May 28, 2021
23d3012
(fix) example of partials
sunng87 May 30, 2021
a1c2de3
(doc) update changelog to reflect breaking changes in #441
sunng87 May 30, 2021
4962d7e
(chore) setup github ci
sunng87 Jun 4, 2021
bb70a73
Merge branch 'master' into feature/publish-source-api
mkantor Jun 6, 2021
46b2cf0
(test) add a test for register_template_source
mkantor Jun 7, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: CI
on:
# Only run when merging to master, or open/synchronize/reopen a PR.
push:
branches:
- master
pull_request:

jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
build: [stable, beta, nightly, macos, windows]
include:
- build: stable
os: ubuntu-latest
rust: stable
- build: beta
os: ubuntu-latest
rust: beta
- build: nightly
os: ubuntu-latest
rust: nightly
- build: macos
os: macos-latest
rust: stable
- build: windows
os: windows-latest
rust: stable
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
components: clippy
override: true
- name: Lint
run: cargo clippy --all-features
- name: Build and run tests
run: cargo test --all-features

rustfmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: rustfmt
override: true
- run: cargo fmt -- --check

msrv:
name: MSRV
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- run: cargo build --all-features
44 changes: 39 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
# Change Log

## [Unreleased](https://github.com/sunng87/handlebars-rust/compare/3.4.0...Unreleased) - ReleaseDate
## [4.0.0](https://github.com/sunng87/handlebars-rust/compare/3.4.0...4.0.0) - 2021-05-25

* [Added] `dev_mode` for registry: templates and scripts loaded from file are always
reloaded when dev mode enabled [#395]
* [Added] Registry is now `Clone` [#395]
* [Changed] Updated `rhai` to 0.19.6 and above [#391]
* [Added] New built-in helper `len` [#421]
* [Changed] Updated `rhai` to 0.19 and then 0.20 [#391]
* [Changed] `#each` helper now renders else block for non-iterable data [#380]
* [Changed] `TemplateError` and `ScriptError` is now a cause of `RenderError` [#395]
* [Changed] `RenderContext::get_partial` now returns `Option<&Template>`
* [Changed] Empty lines around block helpers are now stripped [#404]
* [Changed] **Breaking** `RenderContext::get_partial` now returns `Option<&Template>`
* [Changed] **Breaking** Capitalize names like `HtmlExpression` and `IoError` based on clippy recommendations [#424]
* [Changed] **Breaking** Improved return type of `call_inner` from `HelperDef` to avoid misleading [#437]
* [Fixed] reference starts with `null`, `true` and `false` were parsed incorrectly [#382]
* [Fixed] dir source path separator bug on windows [#389] [#405]
* [Removed] option to disable source map is removed [#395]
* [Removed] `TemplateFileError` and `TemplateRenderError` are removed and merged into
* [Fixed] stack overflow with nested `@partial-block` [#401]
* [Fixed] value access issue when upper block has a base value [#419]
* [Fixed] escape rules for Json string literal [#423]
* [Fixed] **Breaking** zero-arity subexpressions support [#433]
Zero-arity subexpression no longer resolved as variable. The behaviour is now aligned with handlebarsjs.
* [Removed] **Breaking** option to disable source map is removed [#395]
* [Removed] **Breaking** `TemplateFileError` and `TemplateRenderError` are removed and merged into
`TemplateError` and `RenderError` [#395]

## [3.5.5](https://github.com/sunng87/handlebars-rust/compare/3.5.4...3.5.5) - 2021-05-03

* [Fixed] Panic on reporting invalid tag name [#427]

## [3.5.4](https://github.com/sunng87/handlebars-rust/compare/3.5.3...3.5.4) - 2021-03-27

* [Fixed] Json string literal with escape char [#422]

## [3.5.3](https://github.com/sunng87/handlebars-rust/compare/3.5.2...3.5.3) - 2021-02-20

* [Fixed] value access issue when upper block has a base value [#419]

## [3.5.2](https://github.com/sunng87/handlebars-rust/compare/3.5.1...3.5.2) - 2020-12-29

* [Fixed] allow `/` as trailing separator on Windows, backported from master [#405]

## [3.5.1](https://github.com/sunng87/handlebars-rust/compare/3.5.0...3.5.1) - 2020-10-25

* [Fixed] dir source path separator bug on windows [#389]

## [3.5.0](https://github.com/sunng87/handlebars-rust/compare/3.4.0...3.5.0) - 2020-09-23

* [Changed] `#each` helper now renders else block for non-iterable data [#380]
* [Fixed] reference starts with `null`, `true` and `false` were parsed incorrectly [#382]

## [3.4.0](https://github.com/sunng87/handlebars-rust/compare/3.3.0...3.4.0) - 2020-08-14

* [Added] Debug log that can be turned on by using envlog or other implementation, to trace data resolution during rendering [#369]
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "handlebars"
version = "4.0.0-alpha.0"
version = "4.0.1-alpha.0"
authors = ["Ning Sun <sunng@pm.me>"]
description = "Handlebars templating implemented in Rust."
license = "MIT"
keywords = ["handlebars", "templating", "web"]
categories = ["template-engine"]
categories = ["template-engine", "web-programming"]
homepage = "https://github.com/sunng87/handlebars-rust"
repository = "https://github.com/sunng87/handlebars-rust"
documentation = "https://docs.rs/crate/handlebars/"
Expand All @@ -28,7 +28,7 @@ pest_derive = "2.1.0"
serde = "1.0.0"
serde_json = "1.0.39"
walkdir = { version = "2.2.3", optional = true }
rhai = { version = "0.19.6", optional = true, features = ["sync", "serde"] }
rhai = { version = "0.20", optional = true, features = ["sync", "serde"] }

[dev-dependencies]
env_logger = "0.8"
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ book](https://doc.rust-lang.org/book/).
### Quick Start

```rust
extern crate handlebars;
#[macro_use]
extern crate serde_json;

use handlebars::Handlebars;
use serde_json::json;

fn main() -> Result<(), Box<dyn Error>> {
let mut reg = Handlebars::new();
Expand All @@ -38,6 +35,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// register template using given name
reg.register_template_string("tpl_1", "Good afternoon, {{name}}")?;
println!("{}", reg.render("tpl_1", &json!({"name": "foo"}))?);

Ok(())
}
```
Expand Down Expand Up @@ -80,6 +78,7 @@ and clarify in CHANGELOG.

| Handlebars version range | Minimum Rust version |
| --- | --- |
| ~4.0.0 | 1.45 |
| ~3.0.0 | 1.32 |
| ~2.0.0 | 1.32 |
| ~1.1.0 | 1.30 |
Expand Down Expand Up @@ -185,7 +184,7 @@ Handlebars 3.0 can be used in WebAssembly projects.
example](https://github.com/seanmonstar/warp/blob/master/examples/handlebars_template.rs)
* Tower-web: [Built-in](https://github.com/carllerche/tower-web)
* Actix: [handlebars
example](https://github.com/actix/examples/blob/master/template_handlebars/src/main.rs)
example](https://github.com/actix/examples/blob/master/template_engines/handlebars/src/main.rs)
* Tide: [tide-handlebars](https://github.com/No9/tide-handlebars)

### Adopters
Expand Down
2 changes: 1 addition & 1 deletion examples/partials/template2.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#*inline "page"}}
<p>Rendered in partial, parent is {{parent}}</p>
{{/inline}}
{{> (parent)}}
{{> (lookup this "parent")}}
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

target
corpus
artifacts
26 changes: 26 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

[package]
name = "handlebars-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"

[dependencies.handlebars]
path = ".."

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "render_template_no_data"
path = "fuzz_targets/render_template_no_data.rs"
test = false
doc = false
8 changes: 8 additions & 0 deletions fuzz/fuzz_targets/render_template_no_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &str| {
let tpl = handlebars::Handlebars::new();

let _ = tpl.render_template(&data, &Vec::<u32>::new());
});
17 changes: 9 additions & 8 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,20 @@ fn parse_json_visitor<'a, 'reg>(
}
None => {
if path_context_depth > 0 {
if let Some(ref context_base_path) = block_contexts
let blk = block_contexts
.get(path_context_depth as usize)
.map(|blk| blk.base_path())
{
extend(&mut path_stack, context_base_path);
.or_else(|| block_contexts.front());

if let Some(base_value) = blk.and_then(|blk| blk.base_value()) {
merge_json_path(&mut path_stack, relative_path);
ResolvedPath::LocalValue(path_stack, base_value)
} else {
// TODO: is this correct behaviour?
if let Some(ref base_path) = block_contexts.front().map(|blk| blk.base_path()) {
if let Some(base_path) = blk.map(|blk| blk.base_path()) {
extend(&mut path_stack, base_path);
}
merge_json_path(&mut path_stack, relative_path);
ResolvedPath::AbsolutePath(path_stack)
}
merge_json_path(&mut path_stack, relative_path);
ResolvedPath::AbsolutePath(path_stack)
} else if from_root {
merge_json_path(&mut path_stack, relative_path);
ResolvedPath::AbsolutePath(path_stack)
Expand Down
22 changes: 16 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use walkdir::Error as WalkdirError;
use rhai::{EvalAltResult, ParseError};

/// Error when rendering data on template.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct RenderError {
pub desc: String,
pub template_name: Option<String>,
pub line_no: Option<usize>,
pub column_no: Option<usize>,
cause: Option<Box<dyn Error + Send + Sync + 'static>>,
unimplemented: bool,
}

impl fmt::Display for RenderError {
Expand Down Expand Up @@ -93,10 +94,14 @@ impl RenderError {
pub fn new<T: AsRef<str>>(desc: T) -> RenderError {
RenderError {
desc: desc.as_ref().to_owned(),
template_name: None,
line_no: None,
column_no: None,
cause: None,
..Default::default()
}
}

pub(crate) fn unimplemented() -> RenderError {
RenderError {
unimplemented: true,
..Default::default()
}
}

Expand All @@ -117,6 +122,11 @@ impl RenderError {

e
}

#[inline]
pub(crate) fn is_unimplemented(&self) -> bool {
self.unimplemented
}
}

quick_error! {
Expand Down Expand Up @@ -250,7 +260,7 @@ impl fmt::Display for TemplateError {
quick_error! {
#[derive(Debug)]
pub enum ScriptError {
IOError(err: IOError) {
IoError(err: IOError) {
from()
source(err)
}
Expand Down
8 changes: 7 additions & 1 deletion src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ literal = { string_literal |
null_literal = @{ "null" ~ !symbol_char }
boolean_literal = @{ ("true"|"false") ~ !symbol_char }
number_literal = @{ "-"? ~ ASCII_DIGIT+ ~ "."? ~ ASCII_DIGIT* ~ ("E" ~ "-"? ~ ASCII_DIGIT+)? }
string_literal = @{ ("\"" ~ (!"\"" ~ ("\\\"" | ANY))* ~ "\"") | ("'" ~ (!"'" ~ ("\\'" | ANY))* ~ "'") }
json_char = {
!("\"" | "\\") ~ ANY
| "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
| "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
}
string_inner = @{ json_char* }
string_literal = ${ "\"" ~ string_inner ~ "\"" }
array_literal = { "[" ~ literal? ~ ("," ~ literal)* ~ "]" }
object_literal = { "{" ~ (string_literal ~ ":" ~ literal)?
~ ("," ~ string_literal ~ ":" ~ literal)* ~ "}" }
Expand Down
3 changes: 1 addition & 2 deletions src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ mod test {
let s = vec![
"\"json string\"",
"\"quot: \\\"\"",
"'json string'",
"'quot: \\''",
"[]",
"[\"hello\"]",
"[1,2,3,4,true]",
Expand Down Expand Up @@ -174,6 +172,7 @@ mod test {
"{{exp 1}}",
"{{exp \"literal\"}}",
"{{exp \"literal with space\"}}",
r#"{{exp "literal with escape \\\\"}}"#,
"{{exp ref}}",
"{{exp (sub)}}",
"{{exp (sub 123)}}",
Expand Down
Loading