$$ \newcommand{\beqa}{\begin{eqnarray}} \newcommand{\eeqa}{\end{eqnarray}} \newcommand{\ep}{\thinspace . } \newcommand{\uvec}{\vec u} \newcommand{\Q}{\pmb{Q}} $$

 

 

 

Mako Programming

The Mako templating engine is used as preprocessor for DocOnce documents, but the Preprocess is run prior to Mako and is recommended for including other files via # #include "filename". Preprocess is also sufficient for if-else tests to steer which parts of the text that are to be compiled. For more demanding tasks, use Mako, which resembles a real programming language.

Warning. Unfortunately, the combination of Mako and LaTeX mathematics may lead to problems because Mako applies syntax like ${var} to extract variables or call functions, while LaTeX mathematics sometimes applies the same syntax, e.g., ${\cal O}(\Delta x^2)$ which looks like a Mako function call. This problem can give rise to strange error messages from Mako, usually that a variable is not defined. The solution is to avoid such Mako-style syntax when writing LaTeX mathematics, e.g., by defining newcommands if it is otherwise problematic.

The Basics of Mako

Just a preliminary sketch of some Mako code (next example is better!):

# Define variables
<%
mycounter = 1
mydict = {}
%>

# Assume MYVAR is given on the command line as MYVAR=mytext (e.g.)
% if MYVAR is not UNDEFINED:
The value of MYVAR is ${MYVAR}.
% endif

<%
## Manipulation of variables
mycounter += 1
%>

% if MYVAR in (2,4,6):
MYVAR is even integer in [2,6].
% elif MYVAR > 1000000:
MYVAR is big.
% else:
MYVAR=${MYVAR}, mycounter=${mycounter}.
% endif

# Function
<%
# Define Python function: FORMAT and DEVICE
# are always defined

def link(filename):
    url = "https://github.com/some/path/to/' + filename + '"'
    if DEVICE == 'screen':
        # make link to url
        return '"filename":' + url
    elif DEVICE == 'paper':
        # write URL explicit on paper
        return 'URL:' + url
%>

<%doc>
This
is
a
block
comment in Mako.
<%doc/>

Debugging Python code in Mako

Although it is easy to put in Python code inside <% and %> directives, it may quickly become a nightmare to debug the Python code. If Mako reports a syntax error in the Python code, it is recommended to debug the Python code outside of Mako. That is, copy all Python code to a separate file and run it as standard Python code. This will give much more precise (and familiar) error messages.

A setup that is easy for developing and maintaining the Python code used in Mako goes as follows. Different pieces of Python code in Mako is placed in separate files and included via the Preprocess preprocessor in the DocOnce document. For instance,

## Define Mako variables and basic functions
<%
# #include "variables.py"
# #include "basic_functions.py"
%>

Some DocOnce text ....

## Define more functions
<%
# #include "more_functions.py"
%>

Here, we have separated the Python code in Mako into three files: variables.py, basic_functions.py, and more_functions.py. With the # #include statements, Mako sees the .py files inserted as text inside the <% and %> directives (check the file tmp_preprocess__mydoc.do.txt for the result of running Preprocess and the complete DocOnce file that Mako sees).

To debug the .py files, collect all their text in one single .py file and run it. The recommended way is to use Preprocess to copy all the text in the .py files into a single file, named (say) all_code.p.py:

# --*-- coding: utf-8 -*-
# (Encoding info cannot be in mako code, but required here
# in Python code if we use non-ascii characters)

# #include "variables.py"
# #include "basic_functions.py"
# #include "more_functions.py"

# Test the code included above
print some_func(1, 'arg')

Note that after including the .py files one can insert calls to functions, print variables, etc., as desired to debug and experiment.

Run Preprocess on all_code.p.py to produce an ordinary Python file all_code.py and run that code to test and debug. A little shell script all_code.sh doing both these things is convenient:

#!/bin/sh
preprocess all_code.p.py > all_code.py
python all_code.py

Especially when the Python code in Mako grows and becomes non-trivial, this setup for having the code external to the DocOnce document is very effective.

Things to remember when programming Mako. And to be repeated: keep the Python code in separate files!

One can use Mako to extend the DocOnce syntax. Four examples are given next.