263 Commits

Author SHA1 Message Date
Guillaume Gomez
0372dac003 Implement is (not) defined 2024-07-24 10:27:35 +02:00
René Kijewski
55774ff36e Every used template gets referenced exactly once
Once per `#[template]`, that is. Still multiple times per crate.
2024-07-22 23:52:15 +02:00
René Kijewski
2ad6104a47 derive: prettier error messages for expected output tests 2024-07-22 23:51:07 +02:00
René Kijewski
5163e38355 filters: proper escaping for |linebreaks and friends
The filters `|linebreaks`, `|linebreaksbr` and `|paragraphbreaks`
generate HTML code to be embedded in a page. Having to specify that the
output of these filters is `|safe` is cumbersome. Also, these filters
need to operate on already escaped HTML data. This could be done by
writing `{{ s|escape|linebreaks|safe }}`.

This PR does the input and output formatting escaping for the user. The
input gets escaped for HTML (invariant of the selected escaper), and the
output gets marked as HTML safe.
2024-07-13 14:44:28 +02:00
René Kijewski
10e67fa312 Reimplement {% filter %} block
This PR reimplements the code generation for `{% filter %}` blocks, so
that the data is written directly into its destination writer, without
using a buffer. This way it behaves like a specialized
`{{ expr|filter1|filter2 }}` would, if the `expr` was a `Template` that
contained the body of the filter block.
2024-07-13 11:11:04 +02:00
René Kijewski
760fa7d4ca Use named W: fmt::Writer template argument
In my opinion it's much easier to read than `&mut (impl …)`. Also, it
might come in handy to have access to the type in the generator.
2024-07-13 10:50:03 +02:00
Guillaume Gomez
597ef12750 Fix filter block feature 2024-07-13 10:29:11 +02:00
René Kijewski
34073642b1 Run typos to fix typos 2024-07-12 07:07:00 +02:00
René Kijewski
2f5a637400 Use once_map instead of quick_cache
`quick_cache` is the wrong tool for this job. It want to evict items
after a time, which must not happen. It has too many options and
features we don't need.
2024-07-11 03:43:39 +02:00
René Kijewski
c67b74ee9f Caching reading Config and make it lifetime-free 2024-07-11 03:40:33 +02:00
René Kijewski
57bb4a67cb Cache read template sources 2024-07-11 03:40:33 +02:00
René Kijewski
501249fba9 Move get_template_source into input.rs
It's only used in this file.
2024-07-11 03:40:33 +02:00
René Kijewski
f15abb3d7c Cache parsing templates 2024-07-11 03:40:33 +02:00
René Kijewski
b295ff62ef derive: Replace Rcs with Arcs 2024-07-11 03:40:33 +02:00
René Kijewski
85330b353a Raise the MSRV to 1.71 2024-07-10 21:38:43 +02:00
René Kijewski
a84678c46d Move #[derive(Template)] comment to the macro itself
Less searching for an explanation.

I also restructured the text to use sections instead of bullet points.

And lastly I made `rinja_derive_standalone` export `derive_template`,
too, instead of `derive_template2`. This way we have less macro-magic.
2024-07-09 17:23:22 +02:00
Guillaume Gomez
69671826cc Remove links to discord server 2024-07-09 13:46:58 +02:00
René Kijewski
277cff4ebf Reset all version numbers to 0.2.0 2024-07-09 12:48:41 +02:00
René Kijewski
e3948ed436 Add HTML safe types
This PR adds an `HtmlSafeMarker` trait. Types that implement this marker
are know to never generate strings containing the characters `< > & " '`,
so they don't have to be escaped.

All glory goes to \@dtolnay's ["Autoref-based stable specialization"][1]
case study / blog entry.

[1]: <0a9f083f33/autoref-specialization/README.md>
2024-07-08 20:23:08 +02:00
René Kijewski
9beca5ed10 Add MsgValidEscapers to show list of configured escapers 2024-07-07 22:51:57 +02:00
René Kijewski
6b0ea27742 More useful error message and UI tests for missing escaper 2024-07-07 22:51:23 +02:00
René Kijewski
52915f4ce3 Simplify |escape: make it a "normal" filter 2024-07-07 22:51:19 +02:00
René Kijewski
9987770dda Store template source in Rc<str> 2024-07-06 00:40:34 +02:00
René Kijewski
7b016b6f7c Refactor add_to_check 2024-07-06 00:25:07 +02:00
René Kijewski
aa93b2a986 Implement Hash + PartialEq for more parser + derive types 2024-07-05 03:31:17 +02:00
René Kijewski
6a6994670d Show error reason when template could not be read 2024-07-04 11:02:58 +02:00
René Kijewski
55e8ff1d46 Dedup error message creation 2024-07-04 11:00:08 +02:00
René Kijewski
7bc362fcdc Remove a #[allow(clippy::match_wild_err_arm)] 2024-07-04 11:00:08 +02:00
René Kijewski
ee3a9e675a Add .rustfmt.toml configuration
This PR configures the formatting with a few defaults that aid
readability, in my opinion. One drawback of adding this file is that
rustfmt uses unstable features, now, and you have to use nightly to run
it:

```sh
cargo +nightly fmt --all
```
2024-07-04 07:05:57 +02:00
Guillaume Gomez
37c300d483
Merge pull request #42 from GuillaumeGomez/top-level-nodes-errors
Generate better errors for top level nodes
2024-07-03 10:39:33 +02:00
René Kijewski
d4b363b9a4 Dedup top-level check in heritage 2024-07-03 01:18:36 +02:00
René Kijewski
bf20db307e FileInfo<'a, 'b, 'c>FileInfo<'a> 2024-07-03 00:26:03 +02:00
Guillaume Gomez
fa467172dd Prevent jinja let to move variables 2024-07-02 22:39:28 +02:00
Guillaume Gomez
79e7331ccc Generate better errors for top level nodes 2024-07-02 21:35:41 +02:00
René Kijewski
145127e241
Merge pull request #40 from GuillaumeGomez/rename-as_ref
Rename `as_ref` filter into `ref`
2024-07-02 15:49:17 +02:00
Guillaume Gomez
f2dcd1737e Favor methods over closure fields 2024-07-02 12:14:20 +02:00
Guillaume Gomez
b37e7d3703 Rename as_ref filter into ref 2024-07-01 21:09:18 +02:00
Guillaume Gomez
6be20d5782 Prevent jinja macros to move variables 2024-07-01 18:05:12 +02:00
René Kijewski
e84e1a6af2 Move Target into its own file 2024-06-28 22:56:34 +02:00
René Kijewski
e94525f483 Add support for .. in let pattern matching for structs 2024-06-28 05:25:21 +02:00
René Kijewski
64a8094aaf Split pretty and ugly JSON filter impl 2024-06-27 14:25:22 +02:00
René Kijewski
462c04c0a0 Make JSON prettifying optional
This PR adds an optional argument to the `|tojson` filter, which
controls if the serialized JSON data gets prettified or not. The
arguments works the same as flask's [`|tojson`][flask] filter, which
passes the argument to python's [`json.dumps()`][python]:

* Omitting the argument, providing a negative integer, or `None`, then
  compact JSON data is generated.
* Providing a non-negative integer, then this amount of ASCII spaces is
  used to indent the data. (Capped to 16 characters.)
* Providing a string, then this string is used as prefix. I attempts are
  made to ensure that the prefix actually consists of whitespaces,
  because chances are, that if you provide e.g. `&nsbp;`, then you are
  doing it intentionally.

This is a breaking change, because it changes the default behavior to
not prettify the data. This is done intentionally, because this is how
it works in flask.

[flask]: https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.tojson
[python]: https://docs.python.org/3/library/json.html#json.dump
2024-06-25 00:12:25 +02:00
René Kijewski
4e88a308c2
Merge pull request #24 from GuillaumeGomez/simplify-kws
Remove duplicated code to detect Rust keywords/identifiers
2024-06-23 15:06:28 +02:00
René Kijewski
1ce549d260 derive: add benchmark
This PR adds the crate "rinja_derive_standalone", which is just like
"rinja_derive", though not a "proc_macro". This way we can easily expose
it's internals for testing and benchmarking.

Right now, the PR is more or less a prove of concept, and it probably
needs a handful more useful benchmark use cases to be worth the hassle.
2024-06-22 02:49:01 +02:00
Guillaume Gomez
d4e36fa59e Remove duplicated code to detect Rust keywords/identifiers 2024-06-20 23:16:52 +02:00
René Kijewski
bdc3c79e95 Test UTF-8-ness only in #[test]s 2024-06-20 22:02:39 +02:00
René Kijewski
bf93f4a755 serde-json is actually called serde_json 2024-06-20 19:03:04 +02:00
René Kijewski
ad645e7a21 Don't capture std::fmt::Error in rinja::Error
`std::fmt::Error` does not know why it failed, only that it failed, as
it has a single value. This PR removes the captured value, to make the
code a bit more dense.
2024-06-19 00:12:51 +02:00
René Kijewski
435ecc2050
Merge pull request #5 from GuillaumeGomez/non-existent-feature
Fix cfg using non-existing feature
2024-06-18 23:48:54 +02:00
Guillaume Gomez
c4f7fed830 Fix cfg using non-existing feature 2024-06-18 23:45:39 +02:00