Skip to content
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

Closed
brainchild0 opened this issue Feb 3, 2020 · 2 comments
Closed

templating in defaults file #6108

brainchild0 opened this issue Feb 3, 2020 · 2 comments

Comments

@brainchild0
Copy link

brainchild0 commented Feb 3, 2020

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:

  • Familiar: Idioms derive from established patterns supported by existing tools and languages.
  • Noninvasive: Uses not involving the feature are minimally affected by its inclusion into the application.
  • Flexible: Users are able to achieve different effects accommodating a variety of simple or sophisticated cases.
  • Comprehensive: Most cases demanded by users are supported.
  • Extensible: Support for further use cases can be added in the future without breaking compatibility or adding convolutions.

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 the foo key in the context. Similarly, {{foo.bar}} evaluates to the value found by the bar key in a dictionary corresponding the the foo key in the context; and {{foo.0}} evaluates to the first element of a list corresponding to the foo key in the context. The text {{#func}}val{{/func}} evaluates to the value returned from passing the string val to the lambda found with the key func.

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 variable varname.
  • {{md.title}}: Evaluates to the metadata variable title.
  • {{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 and op represent data related to the user's configuration and the current application invocation. The namespaces env and md show views of the environment variables and metadata fields. The namespace pd 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.

@iandol
Copy link
Contributor

iandol commented May 21, 2020

This would be a flexible "enhanced" solution to the issue I raised in #5982 with a {{usr:datadir}} added to the option list.

@jgm jgm closed this as completed in 6dd7520 Mar 5, 2021
@brainchild0
Copy link
Author

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants