API reference

class lina.Formatter(formatterType)[source]

Bases: object

Base class for all formatters.

A formatter can be used to transform blocks/values during expansion.

Format(text)[source]

Format a value or a complete block.

IsBlockFormatter()[source]

Check if this formatter is a block formatter.

IsValueFormatter()[source]

Check if this formatter is a value formatter.

OnBlockBegin(isFirst)[source]

Called before a block is expanded.

Parameters:isFirstTrue if this is the first expansion of the block.
Returns:String or None. If a string is returned, it is prepended before the current block expansion.
OnBlockEnd(isLast)[source]

Called after a block has been expanded.

Parameters:isLastTrue if this is the last expansion of the block.
Returns:String or None. If a string is returned, it is appended after the current block expansion.
class lina.FormatterType[source]

Bases: enum.Enum

The formatter type, either Block or Value.

Block = 0
Value = 1
class lina.IncludeHandler[source]

Bases: object

Base interface for include handlers.

Get(name)[source]
exception lina.InvalidBlock(message, position)[source]

Bases: lina.TemplateException

An invalid block was encountered.

exception lina.InvalidFormatter(message, position)[source]

Bases: lina.TemplateException

An invalid formatter was encountered.

This exception is raised when a formatter could not be found or instantiated.

exception lina.InvalidNamedCharacterToken(message, position)[source]

Bases: lina.InvalidWhitespaceToken

An invalid named character token was encountered.

exception lina.InvalidToken(message, position)[source]

Bases: lina.TemplateException

An invalid token was encountered.

exception lina.InvalidWhitespaceToken(message, position)[source]

Bases: lina.TemplateException

Only for backwards compatibility. Will be removed in 2.x.

class lina.Template(template, includeHandler=None, *, filename=None)[source]

Bases: object

The main template class.

Render(context)[source]

Render the template using the provided context.

RenderSimple(**items)[source]

Simple rendering function.

This is just a convenience function which creates the context from the passed items and forwards them to Template.Render().

exception lina.TemplateException(message, position)[source]

Bases: Exception

Base class for all exceptions thrown by Lina.

GetPosition()[source]

Get the position where the exception occurred.

Returns:An object with two fields, line and column.
class lina.TemplateRepository(templateDirectory, suffix='')[source]

Bases: lina.IncludeHandler

A file template repository.

This template repository will load files from a specified folder.

Get(name)[source]
class lina.TextStream(text, *, filename=None)[source]

Bases: object

A read-only text stream.

The text stream is used for input only and keeps track of the current read pointer position in terms of line/column numbers.

Get()[source]

Get a character.

If the end of the stream has been reached, None is returned.

GetOffset()[source]

Get the current read offset in characters from the beginning of the stream.

GetPosition()[source]

Get the current read position as a pair (line, column).

IsAtEnd()[source]

Check if the end of the stream has been reached.

Peek()[source]

Peek at the next character in the stream if possible. Returns None if the end of the stream has been reached.

Reset()[source]

Reset back to the beginning of the stream.

Skip(length)[source]

Skip a number of characters starting from the current position.

Substring(start, end)[source]

Get a substring of the stream.

Unget()[source]

Move one character back in the input stream.

class lina.Token(name, start, end, position)[source]

Bases: object

Represents a single token.

Each token may contain an optional list of flags, separated by colons. The grammar implemented here is:

[prefix]?[^:}]+(:[^:})+, for example:
{{#Foo}} -> name = Foo, prefix = #
{{Bar:width=8}} -> name = Bar, prefix = None,
                                        flags = {width:8}

The constructor checks if the formatter matches the token type. A block formatter can be only applied to a block token, and a value formatter only to a value.

EvaluateNamedCharacterToken(position)[source]

Get the content of this token if this token is an escape character token.

If the content is not a valid character name, this function will raise InvalidSpecialCharacterToken.

GetEnd()[source]

Get the end offset.

GetFormatters()[source]

Get all active formatters for this token.

GetName()[source]

Get the name of this token.

GetPosition()[source]

Get the position as a (line, column) pair.

GetStart()[source]

Get the start offset.

IsBlockClose()[source]

Return true if this token is a block-close token.

IsBlockStart()[source]

Return true if this token is a block-start token.

IsInclude()[source]

Return true if this token is an include directive.

IsNamedCharacter()[source]

Return true if this token is a named character token.

IsNegatedBlockStart()[source]

Return true if this token is a negated block-start token.

IsSelfReference()[source]

Return true if this token is a self-reference.

IsValue()[source]