| 5 | |
| 6 | |
| 7 | class Parser: |
| 8 | __slots__ = ("__raw", "__state") |
| 9 | |
| 10 | def __init__( |
| 11 | self, |
| 12 | options: typing.Union[ |
| 13 | _rustlib.HtmlOptions, |
| 14 | _rustlib.XmlOptions, |
| 15 | typing.Literal["html"], |
| 16 | typing.Literal["xml"], |
| 17 | ] = "html", |
| 18 | ): |
| 19 | """ |
| 20 | An HTML/XML parser, ready to receive unicode input. |
| 21 | |
| 22 | This is very easy to use and allows you to stream input using `.process()` method; By this way |
| 23 | you are don't worry about memory usages of huge inputs. |
| 24 | |
| 25 | for `options`, If your input is a HTML document, pass a `HtmlOptions`; |
| 26 | If your input is a XML document, pass `XmlOptions`. |
| 27 | """ |
| 28 | if isinstance(options, str): |
| 29 | if options == "html": |
| 30 | options = _rustlib.HtmlOptions() |
| 31 | elif options == "xml": |
| 32 | options = _rustlib.XmlOptions() |
| 33 | else: |
| 34 | raise ValueError(f"invalid parser options: {options!r}") |
| 35 | |
| 36 | self.__raw = _rustlib.Parser(options) |
| 37 | |
| 38 | # 0 - processing |
| 39 | # 1 - finished |
| 40 | # 2 - converted |
| 41 | self.__state = 0 |
| 42 | |
| 43 | def writable(self) -> bool: |
| 44 | """ |
| 45 | Shorthand for `not Parser.is_finished`. |
| 46 | |
| 47 | This function exists to make `Parser` like a `BytesIO` and `StringIO`. |
| 48 | You can pass the `Parser` to each function which needs a writable buffer or IO. |
| 49 | """ |
| 50 | return not self.is_finished |
| 51 | |
| 52 | def write(self, content: typing.Union[str, bytes]) -> int: |
| 53 | """ |
| 54 | Same as `Parser.process`. |
| 55 | |
| 56 | This function exists to make `Parser` like a `BytesIO` and `StringIO`. |
| 57 | You can pass the `Parser` to each function which needs a writable buffer or IO. |
| 58 | """ |
| 59 | self.__raw.process(content) |
| 60 | return len(content) |
| 61 | |
| 62 | def process(self, content: typing.Union[str, bytes]) -> "Parser": |
| 63 | """ |
| 64 | Processes an input. |
no outgoing calls
no test coverage detected