API Directory
Modules
Name | Description |
---|
AnnotationType | Provides an enumeration of annotations that can be used in template design files with the .pymd extension. |
Classes
Name | Description |
---|
Position | Used to locate positions in the original template string. |
TemplateBase | Backend class for Pymaidol templates, serves as the base class for all Pymaidol template classes. |
TemplateRenderer | Class that renders template strings into strings. |
1 - AnnotationType Module
Overview
The AnnotationType
module provides an enumeration of annotations that can be used in template design files with the .pymd
extension.
Module: pymaidol
Import
from pymaidol import AnnotationType
Classes
Static Variables
Name | Type | Description |
---|
FULL_ANNOTATION_TYPE | list[AnnotationTypeEnum] | A list that contains all the enumeration values of the annotation type enumerations. |
1.1 - AnnotationTypeEnum Enumeration
Overview
The AnnotationTypeEnum
enumeration is the base class for all annotation type enumerations. It has no internal enumeration values and is only used for inheritance.
Module: pymaidol.AnnotationType
Inherits from: enum.Enum
Import
from pymaidol import AnnotationTypeEnum
or
from pymaidol.AnnotationType import AnnotationTypeEnum
1.2 - MultiLineAnnotationTypeEnum Enumeration
Overview
Contains the types of multiline annotations that can be used in template design files .pymd
.
Module: pymaidol.AnnotationType
Inherits from: pymaidol.AnnotationType.AnnotationTypeEnum
Import
from pymaidol import MultiLineAnnotationTypeEnum
or
from pymaidol.AnnotationType import MultiLineAnnotationTypeEnum
Enum Items
Name | Value | Description |
---|
Python | ’'' | Multiline comment in Python, which starts and ends with ''' . |
C | /* | Multiline comment in C, which starts with /\* and ends with */ . |
HTML | <!– | Multiline comment in HTML, which starts with <!-- and ends with --> . |
1.3 - SingleLineAnnotationTypeEnum Enumeration
Overview
Contains the types of single-line annotations that can be used in template design files .pymd
.
Module: pymaidol.AnnotationType
Inherits from: pymaidol.AnnotationType.AnnotationTypeEnum
Import
from pymaidol import SingleLineAnnotationTypeEnum
or
from pymaidol.AnnotationType import SingleLineAnnotationTypeEnum
Enum Items
Name | Value | Description |
---|
Python | # | Single-line comment for Python, which starts with # . |
C | // | Single-line comment for C, which starts with // . |
See Also
AnnotationTypeEnum Enumeration
2 - Position Class
Overview
The Position
class is used to locate positions in the original template string.
The properties of the Position
object are read-only and cannot be modified after initialization.
Note: When the Position
class is used for the end position, it includes the character at that position. For example, if start.total = 20
and end.total = 30
, the slice of the original template string should be template[20:31]
.
Module: pymaidol.Positions
Import
from pymaidol import Position
or
from pymaidol.Positions import Position
Constructor
Position(line_index, char_index, total)
Parameters
line_index
(int): Line index. Starting from 0.char_index
(int): Character index of the current line. Starting from 0.total
(int): Total character index. Starting from 0.
Properties
line_index
(int, readonly)
Line index. Starting from 0.
char_index
(int, readonly)
Character index of the current line. Starting from 0.
total
(int, readonly)
Total character index. Starting from 0.
full_description
(str, readonly)
Complete and human-readable position description.
Methods
Position.Default()
@ classmethod
Create and return a default Position
object with all properties set to 0.
Parameters
None.
Returns (Position
)
Default Position
object.
Copy()
Copy and return a new Position
object.
Parameters
None.
Returns (Position
)
New Position
object with the same properties as the original object.
3 - TemplateBase Class
Overview
The TemplateBase
class is the backend class for Pymaidol templates and serves as the base class for all Pymaidol template classes. It is an abstract class that cannot be instantiated and needs to be inherited for use.
Module: pymaidol.TemplateBase
Inherits from: abc.ABC
Import
from pymaidol import TemplateBase
or
from pymaidol.TemplateBase import TemplateBase
Constructor
TemplateBase(template, template_file_path, supported_annotation_types, disable_annotation_types)
(virtual)
Parameters
template
(str, optional): The template string. Default is None
.template_file_path
(str, optional): The template file path. Default is None
.supported_annotation_types
(list[AnnotationTypeEnum], optional): The list of supported annotation types. Default is all annotation types (Python, C single-line and multi-line comments, HTML comments).disable_annotation_types
(list[AnnotationTypeEnum], optional): The list of disabled annotation types, making these annotations appear in the rendered text. Default is empty. Disabled annotations take precedence over supported annotations. For example, if AnnotationTypeEnum.SingleLineAnnotationTypeEnum.Python
(Python single-line comments) is included in disable_annotation_types
, it will be disabled regardless of whether it is included in supported_annotation_types
.
When both template
and template_file_path
are None
, the TemplateBase
class and its subclasses will attempt to read the template file .pymd
with the same name in the same directory as itself. If both are not empty, template
will be used as a priority.
Attributes
rendered
(str, readonly)
The rendered string. If Render()
method has not been called since self-initialization or calling HotReload()
method, it will be None
.
template
(str, readonly)
The template string.
Methods
HotReload(template, template_file_path)
(final)
Reloads the template.
Parameters
template
(str, optional): The template string. Default is None
.template_file_path
(str, optional): The template file path. Default is None
.
When both template
and template_file_path
are None
, the TemplateBase
class and its subclasses will attempt to read the template file .pymd
with the same name in the same directory as itself. If both are not empty, template
will be used as a priority.
Returns (None
)
None.
Render(inject_kwargs)
(final)
Renders the template string with the given data.
Parameters
inject_kwargs
(dict, Optional): The externally injected variables used for rendering the template string. Default is None
. The injected variables will be treated as local variables.
Returns (str
)
The rendered string.
4 - TemplateRenderer Class
Overview
A class that renders template strings into strings.
Module: pymaidol.TemplateRenderer
Import
from pymaidol import TemplateRenderer
or
from pymaidol.TemplateRenderer import TemplateRenderer
Constructor
TemplateRenderer(template, supported_annotation_types)
Parameters
template
(str): The template string.supported_annotation_types
(list[AnnotationTypeEnum], optional): The list of supported annotation types. Defaults to all annotation types (Python, C single-line and multi-line comments, HTML comments).
Attributes
template
(str, readonly)
The template string.
Methods
TemplateRenderer.ReadFromFile(template_file_path, supported_annotation_types)
(classmethod)
Given a template file path, returns a TemplateRenderer
object.
Parameters
template_file_path
(str): The template file path.supported_annotation_types
(list[AnnotationTypeEnum]): The list of supported annotation types.
Returns
TemplateRenderer
: The TemplateRenderer
object.
Render(local_vars, global_vars)
(final)
Renders the template string with the given data.
Parameters
local_vars
(dict): The local variables used for rendering the template string.global_vars
(dict, optional): The global variables used for rendering the template string. Defaults to None
.
Returns
str
: The rendered string.