Skip to content
/ mlt2 Public

Simple textprocessing tool to mix freely text and python code

License

Notifications You must be signed in to change notification settings

MLBN/mlt2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mlt2

A simple text processing (templating) engine which allows to mix freely python and text. Its intended use is personal text processing. It does not have (and will never have) security measures for usage in a web server.

Introduction

Motivation

There are many python templating systems. They tend to be overloaded with features and huge. There are obvious drawbacks. Systems like mako, jinja and the like have a steep learning curve, they are not available on every system and local installation requires quite some knowledge about pythons internals. On our departmental computers, e.g., python is available, but mako and jinja are not.

I played a little with m4, which is a bit out of date, but available everywhere. Coming from tex/latex it is easy to deal with. However, macro logic is very cumbersome to code. To make long story short, I wrote my own templating script.

What I want

  • Small footprint with little or no dependencies
  • Full mixing of text, python, and expression evaluation.
  • No restrictions what a template may do or not do. This is intentionally unsuitable for dynamic web, but very suitable for daily text processing needs.
  • Everything is evaluated in one global scope. It is the template designers responsibility to take care of scoping if she/he wishes.
  • Easy commandline usage, as e.g. the m4 macro processor.

Usage

Download and install the package. Inside mlt2 there is a little script cli.py. Copy it as, say mlt2, to your local directory of executables, e.g. /usr/local/bin:

mlt2 [file list]

renders the file list. If no argument is given, this doc string is printed.

Syntax

Ingredients

A template consists of text, python blocks, embedded python expressions, and comments lines. Parsing is regex driven, and the template syntax can easily be changed by changing a few regular expressions in the core file.

  1. Normal text is just rendered unaltered
  2. Everything inside <? ... ?> is run as python code, within a fixed environment env. The environment corresponds to the globals inside the python code.
  3. Everything inside <!...!> is evaluated as python code, also within env. Variables declared in previous python blocks are visible.
  4. Everything inside <# ... #> is a comment.
  5. Auxiliary user tag: <$ ... $&gt;. By default it evaluates to a fixed with decimal expression. As syntactic sugar one may write &lt;foo[+-]$[expression]> to assign the expression to the variable foo, with + or - it will be added/subtracted to foo. New in v2.2: <> may be omitted.

The Blocks described in 1.-5. correspond to tokens of the language. Before such a token is rendered, it is preprocessed as follows. Depending on the value of the variable __ML_subst it is either

'': rendered as is '$': Expressions $ followed by a python identifier must refer to a valid variable. They are substituted by the value of this var '#{}': Same but ruby type #{var} ...

The template has access to the runtime environment. This is uncommented at the moment and may change without notice. See core.py.

License

MIT license 2019 Matthias Lesch <ml@matthiaslesch.de>

About

Simple textprocessing tool to mix freely text and python code

Resources

License

Stars

Watchers

Forks

Packages

No packages published