Convert a Markdown string to a string in the specified output format. Arguments: source: Markdown formatted text as Unicode or ASCII string. Returns: A string in the specified output format. Markdown parsing takes place in five steps:
(self, source: str)
| 320 | return False |
| 321 | |
| 322 | def convert(self, source: str) -> str: |
| 323 | """ |
| 324 | Convert a Markdown string to a string in the specified output format. |
| 325 | |
| 326 | Arguments: |
| 327 | source: Markdown formatted text as Unicode or ASCII string. |
| 328 | |
| 329 | Returns: |
| 330 | A string in the specified output format. |
| 331 | |
| 332 | Markdown parsing takes place in five steps: |
| 333 | |
| 334 | 1. A bunch of [`preprocessors`][markdown.preprocessors] munge the input text. |
| 335 | 2. A [`BlockParser`][markdown.blockparser.BlockParser] parses the high-level structural elements of the |
| 336 | pre-processed text into an [`ElementTree`][xml.etree.ElementTree.ElementTree] object. |
| 337 | 3. A bunch of [`treeprocessors`][markdown.treeprocessors] are run against the |
| 338 | [`ElementTree`][xml.etree.ElementTree.ElementTree] object. One such `treeprocessor` |
| 339 | ([`markdown.treeprocessors.InlineProcessor`][]) runs [`inlinepatterns`][markdown.inlinepatterns] |
| 340 | against the [`ElementTree`][xml.etree.ElementTree.ElementTree] object, parsing inline markup. |
| 341 | 4. Some [`postprocessors`][markdown.postprocessors] are run against the text after the |
| 342 | [`ElementTree`][xml.etree.ElementTree.ElementTree] object has been serialized into text. |
| 343 | 5. The output is returned as a string. |
| 344 | |
| 345 | !!! warning |
| 346 | The Python-Markdown library does ***not*** sanitize its HTML output. |
| 347 | If you are processing Markdown input from an untrusted source, it is your |
| 348 | responsibility to ensure that it is properly sanitized. For more |
| 349 | information see [Sanitizing HTML Output](../../sanitization.md). |
| 350 | |
| 351 | """ |
| 352 | |
| 353 | # Fix up the source text |
| 354 | if not source.strip(): |
| 355 | return '' # a blank Unicode string |
| 356 | |
| 357 | try: |
| 358 | source = str(source) |
| 359 | except UnicodeDecodeError as e: # pragma: no cover |
| 360 | # Customize error message while maintaining original traceback |
| 361 | e.reason += '. -- Note: Markdown only accepts Unicode input!' |
| 362 | raise |
| 363 | |
| 364 | # Split into lines and run the line preprocessors. |
| 365 | self.lines = source.split("\n") |
| 366 | for prep in self.preprocessors: |
| 367 | self.lines = prep.run(self.lines) |
| 368 | |
| 369 | # Parse the high-level elements. |
| 370 | root = self.parser.parseDocument(self.lines).getroot() |
| 371 | |
| 372 | # Run the tree-processors |
| 373 | for treeprocessor in self.treeprocessors: |
| 374 | newRoot = treeprocessor.run(root) |
| 375 | if newRoot is not None: |
| 376 | root = newRoot |
| 377 | |
| 378 | # Serialize _properly_. Strip top-level tags. |
| 379 | output = self.serializer(root) |