-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
templating in defaults file #6108
Comments
This would be a flexible "enhanced" solution to the issue I raised in #5982 with a |
Suggest keeping open, since 6dd7520 has much more narrow scope than immediate topic. |
jgm
added a commit
that referenced
this issue
Mar 8, 2021
This allows the syntax `${HOME}` to be used, in fields that expect file paths only. Any environment variable may be interpolated in this way. A warning will be raised for undefined variables. The special variable `USERDATA` is automatically set to the user data directory in force when the defaults file is parsed. (Note: it may be different from the eventual user data directory, if the defaults file or further command line options change that.) Closes #5982. Closes #5977. Closes #6108 (path not taken).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem
Since defaults files were introduced, the utility of interpolation of variables in options values has been raised several times (see #5790, #5977).
Further discussion has surrounded functional needs that may be satisfied through the addition of interpolative processing (see #5871).
Overview
An optimal solution would carry the following features:
Design considerations might by divided into two categories, syntax and semantics. Syntax, in this context, relates to how expressions are demarcated from normal text, and how they are represented through a character sequence. Semantics relates to which data and operations are available and how they are referenced using syntax.
Concept
Syntax
Template language Mustache appears to offer an effective solution for syntax. Its advantages include simplicity, context agnosticism, rigorous specification, cross-language support, compound data types, and lambdas.
A Haskell implementation of Mustache is available in Hackage.
Each evaluation of a Mustache template involves a dictionary mapping data object, called the context, with keys typed to strings. Supported types for dictionary values are primitives, lists, functions, and dictionaries. Within a template, interpolated expressions are delimited by two pairs of squiggly braces. The text
{{foo}}
evaluates to the value corresponding to thefoo
key in the context. Similarly,{{foo.bar}}
evaluates to the value found by thebar
key in a dictionary corresponding the thefoo
key in the context; and{{foo.0}}
evaluates to the first element of a list corresponding to thefoo
key in the context. The text{{#func}}val{{/func}}
evaluates to the value returned from passing the stringval
to the lambda found with the keyfunc
.Semantics
Using basic building blocks offered by Mustache, it is easy to imagine how to build powerful yet plain semantics.
A few examples might be:
{{usr.home}}
: Evaluates to the user home directory.{{op.cwd}}
: Evaluates to the current working directory.{{env.varname}}
: Evaluates to environment variablevarname
.{{md.title}}
: Evaluates to the metadata variabletitle
.{{op.infiles.0}}
: Evaluates to the name of the first input file.{{#pd.filesafe}}{{md.title}}{{/pd.filesafe}}.pdf
: Evaluates to a string derived from the title, but safe for file names and without whitespace, followed by a.pdf
suffix.{{#pd.basename}}{{op.infile.0}}{{/pd.basename}}.pdf
: Evaluates to the name of the first input file, with the original extension replaced by.pdf
.These examples are based on the assumption that the context would be populated with a few predefined tables, each representing conceptually a namespace of related data or functions. The namespaces
usr
andop
represent data related to the user's configuration and the current application invocation. The namespacesenv
andmd
show views of the environment variables and metadata fields. The namespacepd
provides a set of functions useful for data transformations.Remarks
Mustache syntax makes the solution familiar and noninvasive. Separation of data and functions into distinctive namespaces makes the solution flexible, comprehensive, and extensible.
The text was updated successfully, but these errors were encountered: