phml.core.formats.format

 1from typing import Optional
 2
 3from phml.core.nodes import AST, NODE
 4from phml.types.config import Config
 5
 6
 7class Format:
 8    """Base class for built-in file formats. Each sub class contains a `parse` and
 9    `compile` method. The parse method should take a string or dict and return
10    """
11
12    extension: str | list[str] = "txt"
13    """The extension or extensions for the file format. When writing to a file and
14    extensions is a list then the first extensions in the list is used for the file
15    extension.
16    """
17
18    @classmethod
19    def suffix(cls) -> str:
20        """The prefered extension/suffix for the file format."""
21
22        if isinstance(cls.extension, list):
23            return f".{cls.extension[0]}"
24        return f".{cls.extension}"
25
26    @classmethod
27    def is_format(cls, _extension: str) -> bool:
28        """Determine if an extension is of the current format."""
29
30        if isinstance(cls.extension, list):
31            return _extension.lstrip(".") in cls.extension
32        return _extension.lstrip(".") == cls.extension
33
34    @classmethod
35    def parse(cls, data: ..., auto_close: bool = True) -> str:
36        """Parse the given data into a phml.core.nodes.AST."""
37        raise Exception("Base class Format's parse method should never be called")
38
39    @classmethod
40    def compile(
41        cls,
42        ast: AST,
43        config: Config,
44        components: Optional[dict[str, dict[str, list | NODE]]] = None,
45        **kwargs,
46    ) -> AST:
47        """Compile and process the given ast and return the resulting ast."""
48        raise Exception(f"{cls.__class__.__name__} \
49does not support compiling and returning a phml ast.")
50
51    @classmethod
52    def render(
53        cls,
54        ast: AST,
55        config: Config,
56        components: Optional[dict[str, dict[str, list | NODE]]] = None,
57        indent: int = 0,
58        **kwargs,
59    ) -> str:
60        """Compile the given phml.core.nodes.AST into string of a given format."""
61        raise Exception("Base class Format's render method should never be called")
class Format:
 8class Format:
 9    """Base class for built-in file formats. Each sub class contains a `parse` and
10    `compile` method. The parse method should take a string or dict and return
11    """
12
13    extension: str | list[str] = "txt"
14    """The extension or extensions for the file format. When writing to a file and
15    extensions is a list then the first extensions in the list is used for the file
16    extension.
17    """
18
19    @classmethod
20    def suffix(cls) -> str:
21        """The prefered extension/suffix for the file format."""
22
23        if isinstance(cls.extension, list):
24            return f".{cls.extension[0]}"
25        return f".{cls.extension}"
26
27    @classmethod
28    def is_format(cls, _extension: str) -> bool:
29        """Determine if an extension is of the current format."""
30
31        if isinstance(cls.extension, list):
32            return _extension.lstrip(".") in cls.extension
33        return _extension.lstrip(".") == cls.extension
34
35    @classmethod
36    def parse(cls, data: ..., auto_close: bool = True) -> str:
37        """Parse the given data into a phml.core.nodes.AST."""
38        raise Exception("Base class Format's parse method should never be called")
39
40    @classmethod
41    def compile(
42        cls,
43        ast: AST,
44        config: Config,
45        components: Optional[dict[str, dict[str, list | NODE]]] = None,
46        **kwargs,
47    ) -> AST:
48        """Compile and process the given ast and return the resulting ast."""
49        raise Exception(f"{cls.__class__.__name__} \
50does not support compiling and returning a phml ast.")
51
52    @classmethod
53    def render(
54        cls,
55        ast: AST,
56        config: Config,
57        components: Optional[dict[str, dict[str, list | NODE]]] = None,
58        indent: int = 0,
59        **kwargs,
60    ) -> str:
61        """Compile the given phml.core.nodes.AST into string of a given format."""
62        raise Exception("Base class Format's render method should never be called")

Base class for built-in file formats. Each sub class contains a parse and compile method. The parse method should take a string or dict and return

Format()
extension: str | list[str] = 'txt'

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 suffix(cls) -> str:
19    @classmethod
20    def suffix(cls) -> str:
21        """The prefered extension/suffix for the file format."""
22
23        if isinstance(cls.extension, list):
24            return f".{cls.extension[0]}"
25        return f".{cls.extension}"

The prefered extension/suffix for the file format.

@classmethod
def is_format(cls, _extension: str) -> bool:
27    @classmethod
28    def is_format(cls, _extension: str) -> bool:
29        """Determine if an extension is of the current format."""
30
31        if isinstance(cls.extension, list):
32            return _extension.lstrip(".") in cls.extension
33        return _extension.lstrip(".") == cls.extension

Determine if an extension is of the current format.

@classmethod
def parse(cls, data: Ellipsis, auto_close: bool = True) -> str:
35    @classmethod
36    def parse(cls, data: ..., auto_close: bool = True) -> str:
37        """Parse the given data into a phml.core.nodes.AST."""
38        raise Exception("Base class Format's parse method should never be called")

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

@classmethod
def compile( cls, ast: phml.core.nodes.AST.AST, config: dict[typing.Literal['enabled'], dict[typing.Literal['html', 'markdown'], bool]], 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, **kwargs) -> phml.core.nodes.AST.AST:
40    @classmethod
41    def compile(
42        cls,
43        ast: AST,
44        config: Config,
45        components: Optional[dict[str, dict[str, list | NODE]]] = None,
46        **kwargs,
47    ) -> AST:
48        """Compile and process the given ast and return the resulting ast."""
49        raise Exception(f"{cls.__class__.__name__} \
50does not support compiling and returning a phml ast.")

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

@classmethod
def render( cls, ast: phml.core.nodes.AST.AST, config: dict[typing.Literal['enabled'], dict[typing.Literal['html', 'markdown'], bool]], 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 = 0, **kwargs) -> str:
52    @classmethod
53    def render(
54        cls,
55        ast: AST,
56        config: Config,
57        components: Optional[dict[str, dict[str, list | NODE]]] = None,
58        indent: int = 0,
59        **kwargs,
60    ) -> str:
61        """Compile the given phml.core.nodes.AST into string of a given format."""
62        raise Exception("Base class Format's render method should never be called")

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