Simple templating system
Project description
Nancy
logo by Silvia Polverini
© 2002–2024 Reuben Thomas rrt@sc3d.org
https://github.com/rrthomas/nancy
Nancy is a simple macro processor that copies a file or directory, filling in templates as it goes. It has just one non-trivial construct: context-dependent file inclusion. A file can either be included literally, or run as a command and its output included.
Nancy was originally designed to build simple static web sites, but can be used for all sorts of other tasks, similar to more complicated systems like AutoGen and TXR.
Nancy is free software, licensed under the GNU GPL version 3 (or, at your option, any later version), and written in TypeScript.
See the Cookbook for examples.
Please send questions, comments, and bug reports to the maintainer, or report them on the project’s web page (see above for addresses).
Installation
Install Nancy with pip (part of Python):
$ pip install nancy
Invocation
nancy [-h] [--path PATH] [--version] INPUT-PATH OUTPUT
A simple templating system.
positional arguments:
INPUT-PATH list of input directories, or a single file
OUTPUT output directory, or file ('-' for stdout)
options:
-h, --help show this help message and exit
--path PATH path to build relative to input tree [default: '']
--version show program's version number and exit
The INPUT-PATH is a ':'-separated list; the inputs are merged
in left-to-right order.
OUTPUT cannot be in any input directory.
Operation
Nancy starts by combining the list of inputs given as its input path. If the same file or directory exists in more than one of the directories on the input path, the left-most takes precedence. The result is called the “input tree”, and all paths are relative to it.
Next, Nancy traverses the input tree, or the tree given by the --path
argument, if any, which is a relative path denoting a subtree of the
input tree.
For each directory in the input tree, Nancy creates a corresponding directory, if it does not already exist.
For each file, Nancy looks at its name, and:
- If the name contains the suffix
.nancy
, the file’s contents is expanded (see below), and the result is then written to a file of the same name, but with.nancy
removed, in the corresponding place in the output directory. - Else, if the name contains the suffix
.in
, the file is skipped. (It may be used by macros in other files.) - Otherwise, the file is copied verbatim to the corresponding place in the output.
Files and directories in the output have the same name as in the input tree,
except for the root directory (or file), which is called OUTPUT
.
The special suffixes need not end the file name; they can be used as infixes before the file type suffix.
Special cases
- If the input path is a single file, and no
--path
argument is given, then Nancy acts as if the input path were the current directory and the--path
argument were the file name. This makes it convenient to expand a single file using the command:nancy INPUT-FILE OUTPUT-FILE
- When the output is a single file, the special filename
-
may be used to cause Nancy to print the result to standard output instead of writing it to a file.
Template expansion
Nancy expands a template file as follows:
- Scan the text for commands. Expand any arguments to the command, run each
command, and replace the command by the result, eliding any final
newline. (This elision may look tricky, but it almost always does what
you want, and makes
$include
behave better in various contexts.) - Output the resultant text.
A command takes the form $COMMAND
or $COMMAND{ARGUMENT, ...}
.
Built-in commands
Nancy recognises these commands:
$include{FILE}
Look up the given source file in the input tree (see below); read its contents, then expand them (that is, execute any commands it contains) and return the result.$paste{FILE}
Like$include
, but does not expand its result before returning it.$path
Expands to the file currently being expanded, relative to the input tree.$realpath
Expands to the real path of the file currently being expanded.
The last two commands are mostly useful as arguments to external programs (see below).
To find the file specified by a $include{FILE}
command, Nancy proceeds
thus:
- Set
path
to the value of$path
. - See whether
path/FILE
is a file (or a symbolic link to a file). If so, return the file path, unless we are already in the middle of expanding this file. - If
path
is empty, stop. Otherwise, remove the last directory frompath
and go to step 2.
If no file is found, Nancy stops with an error message.
For example, if Nancy is trying to find file.html
, starting in the
subdirectory foo/bar/baz
, it will try the following files, in order:
foo/bar/baz/file.html
foo/bar/file.html
foo/file.html
file.html
See the website example in the Cookbook for a worked example.
Running other programs
In addition to the rules given above, Nancy also allows $include
and
$paste
to take their input from programs. This can be useful in a variety
of ways: to insert the current date or time, to make a calculation, or to
convert a file to a different format.
Nancy can run a program in two ways:
-
If a file found by an
$include
or$paste
command has the “execute” permission, it is run. -
If no file of the given name can be found using the rules in the previous section, Nancy looks for an executable file on the user’s
PATH
(the list of directories specified by thePATH
environment variable). If one is found, it is run.
In either case, arguments may be passed to the program: use
$include{FILE,ARGUMENT_1,ARGUMENT_2,…}
, or the equivalent for $paste
.
For example, to insert the current date:
$paste{date,+%Y-%m-%d}
See the date example in the Cookbook for more detail.
When commands that run programs are nested inside each other, the order in which they are run may matter. Nancy only guarantees that if one command is nested inside another, the inner command will be processed first.
Escaping
To prevent a comma from being interpreted as an argument separator, put a backslash in front of it:
$include{cat,I\, Robot.txt,3 Rules of Robotics.txt}
This will run the $include
command with the following arguments:
cat
I, Robot.txt
3 Rules of Robotics.txt
Note that the filenames supplied to cat
refer not to the input tree, but
to the file system.
Similarly, a command can be treated as literal text by putting a backslash in front of it:
Now I can talk about \$paste.
This will output:
Now I can talk about $paste.
Development
Check out the git repository with:
git clone https://github.com/rrthomas/nancy
To run the tests:
make test
You will need the tree
utility to build the documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file nancy-8.0.0.tar.gz
.
File metadata
- Download URL: nancy-8.0.0.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1a5e5f0f7d42e8c6c8d1d2cc92f5ecbbba9f1e19f7a837950c7a5e3d92756a7 |
|
MD5 | 4ebb476f7e75fee75a3706b3176a569e |
|
BLAKE2b-256 | 2c4e0c3c4d1405fe45d95d1bb80527888dc99f198c894501d6ac71ebf2f2fa28 |
File details
Details for the file nancy-8.0.0-py3-none-any.whl
.
File metadata
- Download URL: nancy-8.0.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc8e547d4633d8e705077944c4f1db7134ddb54ac94b73e317e712f22f725b63 |
|
MD5 | 7e7af7216222bd1818e85a06eefecc93 |
|
BLAKE2b-256 | 188fac31dd19a7c6b4de65a3d1658f9bad8418b28e6ffb5e979050cff43949a5 |