Skip to main content

Expand macros within a text string. Somewhere between formatting and templating.

Project description

macro_expander

This is a python module for doing basic macro expansion in text, which is a simpler form of template rendering. The module provides a class named MacroProcessor that will parse text for macros and call a handler (callback) for each. Users can easily define their own macros by writing handler functions in a file named macros.py.

Installing

$ pip install macro-expander

Usage

The module provides a command line script named expand-macros.py that takes a text file as its first argument and an output file as it second argument. The script reads the text file, searches for "macros", and then replaces each macro with its expansion. The syntax/pattern for specifying a macro in the text is predefined by the module and is similar to LaTeX commands, but the macro expansion is performed by a python function, which can be supplied by the user. This allows a user to easily add new macros by simply writing a function with the correct signature.

Several macros are already provided. For example, say we have a text files named input.txt with the text

The `ls` command is used to list the files and directories in a directory. For example, running `ls` in your home directory will produce output similar to this:
$ ls
\shell{ls}

If we run

$ expand-macros.py input.txt output.txt
``
The `expand-macros.py` script will find the `shell` macro, run the command(s) inside the `{}`, and replace it with the output of the commands, producing something like

The ls command is used to list the files and directories in a directory. For example, running ls in your home directory will produce output similar to this:

$ ls
Audio Conan Desktop Documents
Downloads Pictures Videos

in a file named output.txt.

Macros

The syntax for macros is inspired by LaTeX commands. Macro names are prefixed with a \, followed by an optional set of square brackets [], followed by one or more sets of curly brackets {}.

\macro_name[optional text]{argument text}{more argument text}

If the macro does not take any arguments, it still must include one set of empty curly brackets to identify the end of the macro name. The syntax may be a little clumsy to those not familiar with LaTeX, but it is flexible.

Defining new macros

To define a new macro, you just need to provide a callback function in a file named macros.py somewhere in python's import path. The module will attempt to import macros and load all callback functions. The callback function should accept arguments

def example_macro(args, opts):
  ...
  return new_text

The macro_expander will take care of parsing macros in the input text, look for a callback function to expand each macro it finds. The list of arguments following the macro name will be passed as the first argument, and the text inside the optional [] will be passed as the second argument. Arguments and the optional text are passed in as strings. The callback function is responsible for processing these for any special meaning (to support key1=value1,key2=value2 style options to be set inside the [] for example). The callback return the text that should be used to replace the macro, and the macro_expander module handles the expansion.

Included macros

mathimg

Replaces a LaTeX snippet with an image of the rendered LaTeX. Useful for inserting LaTeX math into markdown files for example. Requires tex2im, a command line tool for converting LaTeX snippets to PNG.

Example:

Coulomb's Law is:

\mathimg{\vec{F} = \frac{k q_1 q_2}{r^2} \hat{r}}

will produce

Coulomb's Law is:

![](./mathimg-a12c90dd39e0ee8cb5025183d44ec431cad68d6d-image.png)

The file mathimg-a12c90dd39e0ee8cb5025183d44ec431cad68d6d-image.png is created by tex2im and contains an image of the equation. The mathimg also accepts an output format option. For example, to specify HTML

Coulomb's Law is:

\mathimg[o="html"]{\vec{F} = \frac{k q_1 q_2}{r^2} \hat{r}}

which will produce

Coulomb's Law is:

<img src="data:image/png;base64,{IMAGE}" >

where {IMAGE} will be the binary representation of the image. This gives a method for directly embedding the image into an HTML page for example.

scriptimg

Similar to mathimg, but instead of LaTeX, an arbitrary script is ran to produce the image that will be inserted. The script just needs to create a file named out.png with the image, and it will be inserted in the same way that mathimg inserts it. For example, to insert a graph using gnuplot

This is a graph of sine and cosine:
\scriptimg{#! /usr/bin/gnuplot
set term png
set output 'out.png'

set xlabel "x"
set grid
set xrange[0:2*pi]

plot sin(x), cos(x)
}

Note the shebang. The argument text will be written to a temporary file, made executable, and executed. The shebang is necessary to make gnuplot run the script. Obviously you should not run expand-macros.py on text from untrusted sources.

shell

Writes the argument text to a temporary file, runs it as a shell script, and replaces the macro with the output of the script. This is useful for writing documentation about a command line utilities.

file

Inserts the contents of a file.

Consider the following snippet of code:

\file{snippet.cpp}

This will insert the text from snippet.cpp into the output file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

macro_expander-0.4.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

macro_expander-0.4.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file macro_expander-0.4.0.tar.gz.

File metadata

  • Download URL: macro_expander-0.4.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for macro_expander-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ef8a8a79e40a6216f98d8a5d9d8ecdcfc2a4e344abab6d5593b931acc6ca3ea5
MD5 36d592ffeda881f7113ad8ed673c19de
BLAKE2b-256 d65a1e3f7a41424fce6a675654bc77f4a1df2c0055b2dbef0d720031a60c6218

See more details on using hashes here.

File details

Details for the file macro_expander-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for macro_expander-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f275ca4d6da6dcd187081d6831901bb5375f70f5f9a8bbe40245ffe9a74cb80
MD5 7f7cf876304adde973eb9ef74bc07011
BLAKE2b-256 ec2a6396c8d1730a6439498e734e3ff74a17efaa6dee959dbb005dc210a7c084

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page