phml.core.formats.phml_format

 1from typing import Optional
 2
 3from phml.core.nodes import AST, NODE
 4
 5from .compile import ASTRenderer
 6from .format import Format
 7from .parse import parse_hypertest_markup
 8
 9
10def parse_markup(data: str, class_name: str, auto_close: bool = True) -> AST:
11    """Parse a string as a markup document."""
12    return parse_hypertest_markup(data, class_name, auto_close)
13
14
15class PHMLFormat(Format):
16    """Logic for parsing and compiling html files."""
17
18    extension: str = "phml"
19
20    @classmethod
21    def parse(cls, data: str, auto_close: bool = True) -> str:
22        return parse_markup(data, cls.__name__, auto_close)
23
24    @classmethod
25    def compile(
26        cls,
27        ast: AST,
28        components: Optional[dict[str, dict[str, list | NODE]]] = None,
29        **kwargs,
30    ) -> AST:
31        """Compile and process the given ast and return the resulting ast."""
32        return ast
33
34    @classmethod
35    def render(
36        cls,
37        ast: AST,
38        components: Optional[dict[str, dict[str, list | NODE]]] = None,
39        indent: int = 4,
40        **kwargs,
41    ) -> str:
42        indent = indent or 4
43
44        return ASTRenderer(ast, indent).compile()
def parse_markup( data: str, class_name: str, auto_close: bool = True) -> phml.core.nodes.AST.AST:
11def parse_markup(data: str, class_name: str, auto_close: bool = True) -> AST:
12    """Parse a string as a markup document."""
13    return parse_hypertest_markup(data, class_name, auto_close)

Parse a string as a markup document.

class PHMLFormat(phml.core.formats.format.Format):
16class PHMLFormat(Format):
17    """Logic for parsing and compiling html files."""
18
19    extension: str = "phml"
20
21    @classmethod
22    def parse(cls, data: str, auto_close: bool = True) -> str:
23        return parse_markup(data, cls.__name__, auto_close)
24
25    @classmethod
26    def compile(
27        cls,
28        ast: AST,
29        components: Optional[dict[str, dict[str, list | NODE]]] = None,
30        **kwargs,
31    ) -> AST:
32        """Compile and process the given ast and return the resulting ast."""
33        return ast
34
35    @classmethod
36    def render(
37        cls,
38        ast: AST,
39        components: Optional[dict[str, dict[str, list | NODE]]] = None,
40        indent: int = 4,
41        **kwargs,
42    ) -> str:
43        indent = indent or 4
44
45        return ASTRenderer(ast, indent).compile()

Logic for parsing and compiling html files.

PHMLFormat()
extension: str = 'phml'

The extension or extensions for the file format. When writing to a file and extensions is a list then the first extensions in the list is used for the file extension.

@classmethod
def parse(cls, data: str, auto_close: bool = True) -> str:
21    @classmethod
22    def parse(cls, data: str, auto_close: bool = True) -> str:
23        return parse_markup(data, cls.__name__, auto_close)

Parse the given data into a phml.core.nodes.AST.

25    @classmethod
26    def compile(
27        cls,
28        ast: AST,
29        components: Optional[dict[str, dict[str, list | NODE]]] = None,
30        **kwargs,
31    ) -> AST:
32        """Compile and process the given ast and return the resulting ast."""
33        return ast

Compile and process the given ast and return the resulting ast.

@classmethod
def render( cls, ast: phml.core.nodes.AST.AST, components: Optional[dict[str, dict[str, list | phml.core.nodes.nodes.Root | phml.core.nodes.nodes.Element | phml.core.nodes.nodes.Text | phml.core.nodes.nodes.Comment | phml.core.nodes.nodes.DocType | phml.core.nodes.nodes.Parent | phml.core.nodes.nodes.Node | phml.core.nodes.nodes.Literal]]] = None, indent: int = 4, **kwargs) -> str:
35    @classmethod
36    def render(
37        cls,
38        ast: AST,
39        components: Optional[dict[str, dict[str, list | NODE]]] = None,
40        indent: int = 4,
41        **kwargs,
42    ) -> str:
43        indent = indent or 4
44
45        return ASTRenderer(ast, indent).compile()

Compile the given phml.core.nodes.AST into string of a given format.