Follow on GitHub

Texthon

Half Text, Half Python

«  Documentation   ::   Contents

Python API

class texthon.engine.Engine

Primary entrance for the Texthon library. This provides methods to load, compile, and evaluate templates.

Use the modules attribute (dict) to locate compiled or parsed modules.

add_includes(includes)

Add a list of include paths to resolve load directives with.

load_file(self, path, parser = Parser())

Loads a template module defined in a file.

Parameters:
  • path – path to the file
  • parser – the template parser to use
Return type:

the module definition

load_module(self, text, name, parser = Parser())

Loads a template module defined from an input stream

Parameters:
  • input_stream – the stream to read from
  • path – path identifier for the module
  • current – the current directory (used to resolve load directives)
  • parser – the template parser to use
Return type:

the module definition

load_text(self, text, name, parser = Parser())

Loads a template module defined in a string.

Parameters:
  • text – the definition string
  • name – path identifier for the module
  • current – the current directory (used to resolve load directives)
  • parser – the template parser to use
Return type:

the module definition

make()

Compile all loaded modules.

After the function is called, the entire modules dictionary is replaced with callable modules.

set_verbose(verbose)

Set to true for additional debug output.

class texthon.engine.Template_Function

Compiled template function object. Returns a string that’s the result of the template evaluation

The callable object takes a variable number of arguments, but it will perform additional checks against the declared parameters of the template function definition.

class texthon.engine.Template_Module

Provides access to template functions as attributes. It’s also a callable object that returns a shallow copy of itself on return.

Call this object with a list of modules to create mixins.

class texthon.parser.Module_Definition

A parsed template module.

dump()

Dump detailed module information including generated function code

class texthon.parser.Parser(directive_token='#', sub_ch='$')

The parser used to process template files.

Parameters:
  • directive_token – the prefix that indicates the start of a directive line.
  • sub_ch – the character that indicates the start of a placeholder
class texthon.utils.Indent(columns=4, use_tab=True)

Utility class for controlling text indentation.

Parameters:
  • columns – the number of columns per indent level
  • use_tab – whether to use tab characters for formatting
deindent(block, level=1)

returns a new block where each line has the specified level of indent removed

get_column(line)

returns the number of columns the line has for indent

indent(block, level=1)

returns a new block where each line has additional indent of the specified level

normalize(block)

returns a new block where each line is normalized with proper tabs or spaces, preserving the indent column

set_column(line, col)

returns the line with indent to the specified column

«  Documentation   ::   Contents