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

 

 

 

Preprocessing and Postprocessing

DocOnce allows preprocessor commands for, e.g., including files, leaving out text, or inserting special text depending on the format. These commands are run prior to translation of the document. After translation, there are doconce split_* commands available for splitting HTML and Sphinx documents into smaller pieces (web pages) as a postprocess.

The Preprocess and Mako Preprocessors

Two preprocessors are supported: preprocess (http://code.google.com/p/preprocess) and mako (http://www.makotemplates.org/). The former allows include and if-else statements much like the well-known preprocessor in C and C++ (but it does not allow sophisticated macro substitutions). The latter preprocessor is a very powerful template system. With Mako you can automatically generate various type of text and steer the generation through Python code embedded in the DocOnce document. An arbitrary set of name=value command-line arguments (at the end of the command line) automatically define Mako variables that are substituted in the document.

DocOnce will detect if preprocess or Mako commands are used and run the relevant preprocessor prior to translating the DocOnce source to a specific format.

The preprocess and mako programs always have the variable FORMAT defined as the desired output format of DocOnce (html, latex, plain, rst, sphinx, epydoc, st). It is then easy to test on the value of FORMAT and take different actions for different formats. Below is an example:

First some math:

!bt
\begin{align}
x &= 3
label{x:eq1}\\
y &= 5
label{y:eq1}
\end{align}
!et
Let us now reason about this.

# Sphinx cannot refer to labels in align environments

# #if FORMAT in ("latex", "pdflatex", "html")
From (\ref{x:eq})-(\ref{y:eq1}) we get that
# #elif FORMAT == "sphinx"
From
!bt
\[ x = 3 \]
!et
and
!bt
\[ y= 5 \]
!et
it follows that
# #else
From the above equations it follows that
# #endif
$x+y$ is 8.

A variable DEVICE is also defined. It equals screen by default, but the command-line argument --device=paper can set DEVICE to paper (or another value). Testing on DEVICE inside the document makes it possible to test if the output is on paper media, a sreen, or a particular device.

Other user-defined variables for the preprocessor can be set at the command line as explained in the section From DocOnce to Other Formats.

More advanced use of mako can include Python code that may automate the writing of parts of the document.

On the type of a Mako variable from the command line. You define Mako variables as in this example:

Terminal> doconce format html mydoc LANG=C NO=4 COMMENTS=False

The variable LANG gets the value 'C' as a Python string inside the document. All values are treated as strings, except if the value is True or False or if it is an integer. Therefore, NO becomes the integer 4 and you can test like % if NO > 4:. The variable COMMENTS gets the boolean value False and % if not COMMENT: is a positive test. With Mako such variables set on the command-line you can easily leave out portions of the document or choose between different versions of the text in a very flexible and fine-tuned way.

Advanced setting of Mako variables. Mako variables can also be run through eval before being sent to Mako. For example,

Terminal> doconce format latex mydoc \
          SOMEVAR="eval(['problem', 'data', 'results'])"

Now, SOMEVAR will be defined by

SOMEVAR = eval("['problem', 'data', 'results']")

and result in the list ['problem', 'data', 'results']. In a DocOnce document, we can write

% for element in SOMEVAR:
 * "`${element}.pdf`": "http://some.net/pdf/${element}.pdf"
% endfor

and produce the following LaTeX code:

\begin{itemize}
 \item \href{{http://some.net/pdf/problem.pdf}}{\nolinkurl{problem.pdf}}

 \item \href{{http://some.net/pdf/data.pdf}}{\nolinkurl{data.pdf}}

 \item \href{{http://some.net/pdf/results.pdf}}{\nolinkurl{results.pdf}}
\end{itemize}

Instead of giving the SOMEVAR list on the command line, we can hardcode it inside the document:

<%
SOMEVAR = ['problem', 'data', 'results']
%>

The flexibility enabled by Mako variables and statements is one of the major reasons to adopt DocOnce.