RST backtick refactor (all *.rst except manual.rst and rst_examples.rst) (#17258)
Co-authored-by: quantimnot <quantimnot@users.noreply.github.com>
This commit is contained in:
parent
15586c7a7a
commit
83ae70cb54
30 changed files with 1402 additions and 1350 deletions
|
|
@ -1,3 +1,5 @@
|
|||
.. default-role:: code
|
||||
|
||||
===================
|
||||
Source Code Filters
|
||||
===================
|
||||
|
|
@ -8,7 +10,7 @@ A `Source Code Filter (SCF)` transforms the input character stream to an in-mem
|
|||
output stream before parsing. A filter can be used to provide templating
|
||||
systems or preprocessors.
|
||||
|
||||
To use a filter for a source file the ``#?`` notation is used::
|
||||
To use a filter for a source file the `#?` notation is used::
|
||||
|
||||
#? stdtmpl(subsChar = '$', metaChar = '#')
|
||||
#proc generateXML(name, age: string): string =
|
||||
|
|
@ -21,9 +23,9 @@ To use a filter for a source file the ``#?`` notation is used::
|
|||
As the example shows, passing arguments to a filter can be done
|
||||
just like an ordinary procedure call with named or positional arguments. The
|
||||
available parameters depend on the invoked filter. Before version 0.12.0 of
|
||||
the language ``#!`` was used instead of ``#?``.
|
||||
the language `#!` was used instead of `#?`.
|
||||
|
||||
**Hint:** With ``--hint[codeBegin]:on`` or ``--verbosity:2``
|
||||
**Hint:** With `--hint[codeBegin]:on` or `--verbosity:2`
|
||||
(or higher) while compiling or `nim check`, Nim lists the processed code after
|
||||
each filter application.
|
||||
|
||||
|
|
@ -32,8 +34,8 @@ Usage
|
|||
|
||||
First, put your SCF code in a separate file with filters specified in the first line.
|
||||
**Note:** You can name your SCF file with any file extension you want, but the
|
||||
conventional extension is ``.nimf``
|
||||
(it used to be ``.tmpl`` but that was too generic, for example preventing github to
|
||||
conventional extension is `.nimf`
|
||||
(it used to be `.tmpl` but that was too generic, for example preventing github to
|
||||
recognize it as Nim source file).
|
||||
|
||||
If we use `generateXML` code shown above and call the SCF file `xmlGen.nimf`
|
||||
|
|
@ -47,7 +49,7 @@ In your `main.nim`:
|
|||
Pipe operator
|
||||
=============
|
||||
|
||||
Filters can be combined with the ``|`` pipe operator::
|
||||
Filters can be combined with the `|` pipe operator::
|
||||
|
||||
#? strip(startswith="<") | stdtmpl
|
||||
#proc generateXML(name, age: string): string =
|
||||
|
|
@ -68,10 +70,10 @@ The replace filter replaces substrings in each line.
|
|||
|
||||
Parameters and their defaults:
|
||||
|
||||
``sub: string = ""``
|
||||
`sub: string = ""`
|
||||
the substring that is searched for
|
||||
|
||||
``by: string = ""``
|
||||
`by: string = ""`
|
||||
the string the substring is replaced with
|
||||
|
||||
|
||||
|
|
@ -83,14 +85,14 @@ each line.
|
|||
|
||||
Parameters and their defaults:
|
||||
|
||||
``startswith: string = ""``
|
||||
`startswith: string = ""`
|
||||
strip only the lines that start with *startswith* (ignoring leading
|
||||
whitespace). If empty every line is stripped.
|
||||
|
||||
``leading: bool = true``
|
||||
`leading: bool = true`
|
||||
strip leading whitespace
|
||||
|
||||
``trailing: bool = true``
|
||||
`trailing: bool = true`
|
||||
strip trailing whitespace
|
||||
|
||||
|
||||
|
|
@ -99,25 +101,25 @@ StdTmpl filter
|
|||
|
||||
The stdtmpl filter provides a simple templating engine for Nim. The
|
||||
filter uses a line based parser: Lines prefixed with a *meta character*
|
||||
(default: ``#``) contain Nim code, other lines are verbatim. Because
|
||||
(default: `#`) contain Nim code, other lines are verbatim. Because
|
||||
indentation-based parsing is not suited for a templating engine, control flow
|
||||
statements need ``end X`` delimiters.
|
||||
statements need `end X` delimiters.
|
||||
|
||||
Parameters and their defaults:
|
||||
|
||||
``metaChar: char = '#'``
|
||||
`metaChar: char = '#'`
|
||||
prefix for a line that contains Nim code
|
||||
|
||||
``subsChar: char = '$'``
|
||||
`subsChar: char = '$'`
|
||||
prefix for a Nim expression within a template line
|
||||
|
||||
``conc: string = " & "``
|
||||
`conc: string = " & "`
|
||||
the operation for concatenation
|
||||
|
||||
``emit: string = "result.add"``
|
||||
`emit: string = "result.add"`
|
||||
the operation to emit a string literal
|
||||
|
||||
``toString: string = "$"``
|
||||
`toString: string = "$"`
|
||||
the operation that is applied to each expression
|
||||
|
||||
Example::
|
||||
|
|
@ -174,18 +176,18 @@ The filter transforms this into:
|
|||
|
||||
|
||||
Each line that does not start with the meta character (ignoring leading
|
||||
whitespace) is converted to a string literal that is added to ``result``.
|
||||
whitespace) is converted to a string literal that is added to `result`.
|
||||
|
||||
The substitution character introduces a Nim expression *e* within the
|
||||
string literal. *e* is converted to a string with the *toString* operation
|
||||
which defaults to ``$``. For strong type checking, set ``toString`` to the
|
||||
which defaults to `$`. For strong type checking, set `toString` to the
|
||||
empty string. *e* must match this PEG pattern::
|
||||
|
||||
e <- [a-zA-Z\128-\255][a-zA-Z0-9\128-\255_.]* / '{' x '}'
|
||||
x <- '{' x+ '}' / [^}]*
|
||||
|
||||
To produce a single substitution character it has to be doubled: ``$$``
|
||||
produces ``$``.
|
||||
To produce a single substitution character it has to be doubled: `$$`
|
||||
produces `$`.
|
||||
|
||||
The template engine is quite flexible. It is easy to produce a procedure that
|
||||
writes the template code directly to a file::
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue