Fix some sources type in book examples

This commit is contained in:
René Kijewski 2024-05-10 13:54:12 +02:00
parent faed11dccb
commit 621b1b69cf
3 changed files with 46 additions and 46 deletions

View File

@ -20,7 +20,7 @@ The resulting output will be printed to `stderr` during the compilation process.
The parse tree looks like this for the example template: The parse tree looks like this for the example template:
``` ```rust
[Lit("", "Hello,", " "), Expr(WS(false, false), Var("name")), [Lit("", "Hello,", " "), Expr(WS(false, false), Var("name")),
Lit("", "!", "\n")] Lit("", "!", "\n")]
``` ```

View File

@ -56,13 +56,13 @@ Enable it with Cargo features (see below for more information).
Returns the absolute value. Returns the absolute value.
``` ```jinja
{{ -2|abs }} {{ -2|abs }}
``` ```
Output: Output:
``` ```text
2 2
``` ```
@ -71,14 +71,14 @@ Output:
Creates a reference to the given argument. Creates a reference to the given argument.
``` ```jinja
{{ "a"|as_ref }} {{ "a"|as_ref }}
{{ self.x|as_ref }} {{ self.x|as_ref }}
``` ```
will become: will become:
``` ```rust
&"a" &"a"
&self.x &self.x
``` ```
@ -88,13 +88,13 @@ will become:
Capitalize a value. The first character will be uppercase, all others lowercase: Capitalize a value. The first character will be uppercase, all others lowercase:
``` ```jinja
{{ "hello"|capitalize }} {{ "hello"|capitalize }}
``` ```
Output: Output:
``` ```text
Hello Hello
``` ```
@ -103,12 +103,12 @@ Hello
Centers the value in a field of a given width: Centers the value in a field of a given width:
``` ```jinja
-{{ "a"|center(5) }}- -{{ "a"|center(5) }}-
``` ```
Output: Output:
``` ```text
- a - - a -
``` ```
@ -117,7 +117,7 @@ Output:
Dereferences the given argument. Dereferences the given argument.
``` ```jinja
{% let s = String::from("a")|as_ref %} {% let s = String::from("a")|as_ref %}
{% if s|deref == String::from("b") %} {% if s|deref == String::from("b") %}
{% endif %} {% endif %}
@ -125,7 +125,7 @@ Dereferences the given argument.
will become: will become:
``` ```rust
let s = &String::from("a"); let s = &String::from("a");
if *s == String::from("b") {} if *s == String::from("b") {}
``` ```
@ -135,13 +135,13 @@ if *s == String::from("b") {}
Escapes HTML characters in strings: Escapes HTML characters in strings:
``` ```jinja
{{ "Escape <>&"|e }} {{ "Escape <>&"|e }}
``` ```
Output: Output:
``` ```html
Escape &lt;&gt;&amp; Escape &lt;&gt;&amp;
``` ```
@ -175,12 +175,12 @@ Escape &lt;&gt;&amp;
Returns adequate string representation (in KB, ..) of number of bytes: Returns adequate string representation (in KB, ..) of number of bytes:
``` ```jinja
{{ 1000|filesizeformat }} {{ 1000|filesizeformat }}
``` ```
Output: Output:
``` ```text
1 KB 1 KB
``` ```
@ -195,14 +195,14 @@ Rust). The two arguments are passed through to [`format!()`] by
the Rinja code generator, but the order is swapped to support filter the Rinja code generator, but the order is swapped to support filter
composition. composition.
```text ```jinja
{{ value|fmt("{:?}") }} {{ value|fmt("{:?}") }}
``` ```
As an example, this allows filters to be composed like the following. As an example, this allows filters to be composed like the following.
Which is not possible using the `format` filter. Which is not possible using the `format` filter.
```text ```jinja
{{ value|capitalize|fmt("{:?}") }} {{ value|capitalize|fmt("{:?}") }}
``` ```
@ -215,7 +215,7 @@ The first argument to this filter must be a string literal (as in normal Rust).
All arguments are passed through to [`format!()`] by the Rinja code generator. All arguments are passed through to [`format!()`] by the Rinja code generator.
``` ```jinja
{{ "{:?}"|format(var) }} {{ "{:?}"|format(var) }}
``` ```
@ -226,13 +226,13 @@ All arguments are passed through to [`format!()`] by the Rinja code generator.
Indent newlines with width spaces. Indent newlines with width spaces.
``` ```jinja
{{ "hello\nfoo\nbar"|indent(4) }} {{ "hello\nfoo\nbar"|indent(4) }}
``` ```
Output: Output:
``` ```text
hello hello
foo foo
bar bar
@ -243,17 +243,17 @@ hello
Joins iterable into a string separated by provided argument. Joins iterable into a string separated by provided argument.
``` ```rust
array = &["foo", "bar", "bazz"] array = &["foo", "bar", "bazz"]
``` ```
``` ```jinja
{{ array|join(", ") }} {{ array|join(", ") }}
``` ```
Output: Output:
``` ```text
foo, bar, bazz foo, bar, bazz
``` ```
@ -264,13 +264,13 @@ Replaces line breaks in plain text with appropriate HTML.
A single newline becomes an HTML line break `<br>` and a new line followed by a blank line becomes a paragraph break `<p>`. A single newline becomes an HTML line break `<br>` and a new line followed by a blank line becomes a paragraph break `<p>`.
``` ```jinja
{{ "hello\nworld\n\nfrom\nrinja"|linebreaks }} {{ "hello\nworld\n\nfrom\nrinja"|linebreaks }}
``` ```
Output: Output:
``` ```html
<p>hello<br />world</p><p>from<br />rinja</p> <p>hello<br />world</p><p>from<br />rinja</p>
``` ```
@ -279,13 +279,13 @@ Output:
Converts all newlines in a piece of plain text to HTML line breaks. Converts all newlines in a piece of plain text to HTML line breaks.
``` ```jinja
{{ "hello\nworld\n\nfrom\nrinja"|linebreaks }} {{ "hello\nworld\n\nfrom\nrinja"|linebreaks }}
``` ```
Output: Output:
``` ```html
hello<br />world<br /><br />from<br />rinja hello<br />world<br /><br />from<br />rinja
``` ```
@ -298,13 +298,13 @@ Consecutive double line breaks will be reduced down to a single paragraph break.
This is useful in contexts where changing single line breaks to line break tags would interfere with other HTML elements, such as lists and nested `<div>` tags. This is useful in contexts where changing single line breaks to line break tags would interfere with other HTML elements, such as lists and nested `<div>` tags.
``` ```jinja
{{ "hello\nworld\n\nfrom\n\n\n\nrinja"|paragraphbreaks }} {{ "hello\nworld\n\nfrom\n\n\n\nrinja"|paragraphbreaks }}
``` ```
Output: Output:
``` ```html
<p>hello\nworld</p><p>from</p><p>rinja</p> <p>hello\nworld</p><p>from</p><p>rinja</p>
``` ```
@ -313,13 +313,13 @@ Output:
Converts to lowercase. Converts to lowercase.
``` ```jinja
{{ "HELLO"|lower }} {{ "HELLO"|lower }}
``` ```
Output: Output:
``` ```text
hello hello
``` ```
@ -344,13 +344,13 @@ Output:
Return a title cased version of the value. Words will start with uppercase letters, all Return a title cased version of the value. Words will start with uppercase letters, all
remaining characters are lowercase. remaining characters are lowercase.
``` ```jinja
{{ "hello WORLD"|title }} {{ "hello WORLD"|title }}
``` ```
Output: Output:
``` ```text
Hello World Hello World
``` ```
@ -359,13 +359,13 @@ Hello World
Strip leading and trailing whitespace. Strip leading and trailing whitespace.
``` ```jinja
{{ " hello "|trim }} {{ " hello "|trim }}
``` ```
Output: Output:
``` ```text
hello hello
``` ```
@ -375,13 +375,13 @@ hello
Limit string length, appends '...' if truncated. Limit string length, appends '...' if truncated.
``` ```jinja
{{ "hello"|truncate(2) }} {{ "hello"|truncate(2) }}
``` ```
Output: Output:
``` ```text
he... he...
``` ```
@ -390,13 +390,13 @@ he...
Converts to uppercase. Converts to uppercase.
``` ```jinja
{{ "hello"|upper }} {{ "hello"|upper }}
``` ```
Output: Output:
``` ```text
HELLO HELLO
``` ```
@ -405,13 +405,13 @@ HELLO
Percent encodes the string. Replaces reserved characters with the % escape character followed by a byte value as two hexadecimal digits. Percent encodes the string. Replaces reserved characters with the % escape character followed by a byte value as two hexadecimal digits.
``` ```text
hello?world hello?world
``` ```
Output: Output:
``` ```text
hello%3Fworld hello%3Fworld
``` ```
@ -420,13 +420,13 @@ hello%3Fworld
Count the words in that string. Count the words in that string.
``` ```jinja
{{ "rinja is sort of cool"|wordcount }} {{ "rinja is sort of cool"|wordcount }}
``` ```
Output: Output:
``` ```text
5 5
``` ```
@ -436,7 +436,7 @@ Output:
The following filters can be enabled by requesting the respective feature in the Cargo.toml The following filters can be enabled by requesting the respective feature in the Cargo.toml
[dependencies section](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html), e.g. [dependencies section](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html), e.g.
``` ```toml
[dependencies] [dependencies]
rinja = { version = "0.11.2", features = "serde-json" } rinja = { version = "0.11.2", features = "serde-json" }
``` ```
@ -454,7 +454,7 @@ In HTML attributes, you can either use it in quotation marks `"{{data|json}}"` a
or in apostrophes with the (optional) safe filter `'{{data|json|safe}}'`. or in apostrophes with the (optional) safe filter `'{{data|json|safe}}'`.
In HTML texts the output of e.g. `<pre>{{data|json|safe}}</pre>` is safe, too. In HTML texts the output of e.g. `<pre>{{data|json|safe}}</pre>` is safe, too.
``` ```jinja
Good: <li data-extra="{{data|json}}"></li> Good: <li data-extra="{{data|json}}"></li>
Good: <li data-extra='{{data|json|safe}}'></li> Good: <li data-extra='{{data|json|safe}}'></li>
Good: <pre>{{data|json|safe}}</pre> Good: <pre>{{data|json|safe}}</pre>

View File

@ -10,7 +10,7 @@ rinja = "0.12.1"
Now create a directory called `templates` in your crate root. Now create a directory called `templates` in your crate root.
In it, create a file called `hello.html`, containing the following: In it, create a file called `hello.html`, containing the following:
``` ```jinja
Hello, {{ name }}! Hello, {{ name }}!
``` ```