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