phml.core.parser

phml.core.parser

The core parsing module for phml. Handles parsing html and phmls strings along with json.

Exposes phml.core.parser.Parser which handles all parsing functionality.

 1"""phml.core.parser
 2
 3The core parsing module for phml. Handles parsing html and phmls strings
 4along with json.
 5
 6Exposes phml.core.parser.Parser which handles all parsing functionality.
 7"""
 8
 9from pathlib import Path
10from typing import Optional
11
12from phml.core.formats import Format, Formats
13from phml.core.nodes import AST
14
15__all__ = ["Parser"]
16
17
18class Parser:
19    """Primary logic to handle everything with a phml file.
20
21    This class can parse files as phml files and create an ast.
22    The ast and the nodes themselfs can translate themselves to;
23    html, phml, and json. The ast can recursively return itself as
24    an html string. However, only this class can process the python
25    blocks inside of the phml file.
26
27    Call Parser.convert() and pass any kwargs you wish to be exposed to
28    the process that processes the python. You may also use Parser.util to
29    pass extensions to convert and manipulate the html along with the python
30    processing.
31    """
32
33    ast: AST
34    """The recursive node tree of the phml ast."""
35
36    def __init__(self):
37        self.ast = None
38
39    def load(self, path: str | Path, from_format: Optional[Format] = None, auto_close: bool = True):
40        """Parse a given phml file to AST following hast and unist.
41
42        When finished the PHML.ast variable will be populated with the
43        resulting ast.
44
45        Args:
46            path (str | Path): The path to the file that should be parsed.
47            from_format (Format): phml.core.formats.Format class that will parse the given source.
48        """
49
50        path = Path(path)
51        if from_format is None:
52            for file_format in Formats():
53                if file_format.is_format(path.suffix):
54                    with open(path, "r", encoding="utf-8") as source:
55                        src = source.read()
56
57                    self.ast = file_format.parse(src, auto_close=auto_close)
58                    return self
59        else:
60            with open(path, "r", encoding="utf-8") as source:
61                src = source.read()
62            self.ast = from_format.parse(src, auto_close=auto_close)
63            return self
64
65        raise Exception(f"Could not parse unknown filetype {path.suffix.lstrip('.')!r}")
66
67    def parse(
68        self,
69        data: str | dict,
70        from_format: Format = Formats.PHML,
71        auto_close: bool = True
72    ):
73        """Parse data from a phml/html string or from a dict to a phml ast.
74
75        Args:
76            data (str | dict): Data to parse in to a ast
77            data_type (str): Can be `HTML`, `PHML`, `MARKDOWN`, or `JSON` which
78            tells parser how to parse the data. Otherwise it will assume
79            str data to be html/phml and dict as `json`.
80            from_format (Format): phml.core.formats.Format class that will parse the given source.
81        """
82
83        if isinstance(data, dict):
84            self.ast = Formats.JSON.parse(data)
85        else:
86            self.ast = from_format.parse(data, auto_close=auto_close)
87
88        return self
class Parser:
19class Parser:
20    """Primary logic to handle everything with a phml file.
21
22    This class can parse files as phml files and create an ast.
23    The ast and the nodes themselfs can translate themselves to;
24    html, phml, and json. The ast can recursively return itself as
25    an html string. However, only this class can process the python
26    blocks inside of the phml file.
27
28    Call Parser.convert() and pass any kwargs you wish to be exposed to
29    the process that processes the python. You may also use Parser.util to
30    pass extensions to convert and manipulate the html along with the python
31    processing.
32    """
33
34    ast: AST
35    """The recursive node tree of the phml ast."""
36
37    def __init__(self):
38        self.ast = None
39
40    def load(self, path: str | Path, from_format: Optional[Format] = None, auto_close: bool = True):
41        """Parse a given phml file to AST following hast and unist.
42
43        When finished the PHML.ast variable will be populated with the
44        resulting ast.
45
46        Args:
47            path (str | Path): The path to the file that should be parsed.
48            from_format (Format): phml.core.formats.Format class that will parse the given source.
49        """
50
51        path = Path(path)
52        if from_format is None:
53            for file_format in Formats():
54                if file_format.is_format(path.suffix):
55                    with open(path, "r", encoding="utf-8") as source:
56                        src = source.read()
57
58                    self.ast = file_format.parse(src, auto_close=auto_close)
59                    return self
60        else:
61            with open(path, "r", encoding="utf-8") as source:
62                src = source.read()
63            self.ast = from_format.parse(src, auto_close=auto_close)
64            return self
65
66        raise Exception(f"Could not parse unknown filetype {path.suffix.lstrip('.')!r}")
67
68    def parse(
69        self,
70        data: str | dict,
71        from_format: Format = Formats.PHML,
72        auto_close: bool = True
73    ):
74        """Parse data from a phml/html string or from a dict to a phml ast.
75
76        Args:
77            data (str | dict): Data to parse in to a ast
78            data_type (str): Can be `HTML`, `PHML`, `MARKDOWN`, or `JSON` which
79            tells parser how to parse the data. Otherwise it will assume
80            str data to be html/phml and dict as `json`.
81            from_format (Format): phml.core.formats.Format class that will parse the given source.
82        """
83
84        if isinstance(data, dict):
85            self.ast = Formats.JSON.parse(data)
86        else:
87            self.ast = from_format.parse(data, auto_close=auto_close)
88
89        return self

Primary logic to handle everything with a phml file.

This class can parse files as phml files and create an ast. The ast and the nodes themselfs can translate themselves to; html, phml, and json. The ast can recursively return itself as an html string. However, only this class can process the python blocks inside of the phml file.

Call Parser.convert() and pass any kwargs you wish to be exposed to the process that processes the python. You may also use Parser.util to pass extensions to convert and manipulate the html along with the python processing.

Parser()
37    def __init__(self):
38        self.ast = None

The recursive node tree of the phml ast.

def load( self, path: str | pathlib.Path, from_format: Optional[phml.core.formats.format.Format] = None, auto_close: bool = True):
40    def load(self, path: str | Path, from_format: Optional[Format] = None, auto_close: bool = True):
41        """Parse a given phml file to AST following hast and unist.
42
43        When finished the PHML.ast variable will be populated with the
44        resulting ast.
45
46        Args:
47            path (str | Path): The path to the file that should be parsed.
48            from_format (Format): phml.core.formats.Format class that will parse the given source.
49        """
50
51        path = Path(path)
52        if from_format is None:
53            for file_format in Formats():
54                if file_format.is_format(path.suffix):
55                    with open(path, "r", encoding="utf-8") as source:
56                        src = source.read()
57
58                    self.ast = file_format.parse(src, auto_close=auto_close)
59                    return self
60        else:
61            with open(path, "r", encoding="utf-8") as source:
62                src = source.read()
63            self.ast = from_format.parse(src, auto_close=auto_close)
64            return self
65
66        raise Exception(f"Could not parse unknown filetype {path.suffix.lstrip('.')!r}")

Parse a given phml file to AST following hast and unist.

When finished the PHML.ast variable will be populated with the resulting ast.

Arguments:
  • path (str | Path): The path to the file that should be parsed.
  • from_format (Format): phml.core.formats.Format class that will parse the given source.
def parse( self, data: str | dict, from_format: phml.core.formats.format.Format = <class 'phml.core.formats.phml_format.PHMLFormat'>, auto_close: bool = True):
68    def parse(
69        self,
70        data: str | dict,
71        from_format: Format = Formats.PHML,
72        auto_close: bool = True
73    ):
74        """Parse data from a phml/html string or from a dict to a phml ast.
75
76        Args:
77            data (str | dict): Data to parse in to a ast
78            data_type (str): Can be `HTML`, `PHML`, `MARKDOWN`, or `JSON` which
79            tells parser how to parse the data. Otherwise it will assume
80            str data to be html/phml and dict as `json`.
81            from_format (Format): phml.core.formats.Format class that will parse the given source.
82        """
83
84        if isinstance(data, dict):
85            self.ast = Formats.JSON.parse(data)
86        else:
87            self.ast = from_format.parse(data, auto_close=auto_close)
88
89        return self

Parse data from a phml/html string or from a dict to a phml ast.

Arguments:
  • data (str | dict): Data to parse in to a ast
  • data_type (str): Can be HTML, PHML, MARKDOWN, or JSON which
  • tells parser how to parse the data. Otherwise it will assume
  • str data to be html/phml and dict as json.
  • from_format (Format): phml.core.formats.Format class that will parse the given source.