MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / convert

Method convert

markdown/core.py:328–408  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 15

assertMarkdownRendersMethod · 0.95
convertFileMethod · 0.95
markdownFunction · 0.95
testComplexSettingsMethod · 0.95
testWikilinksMetaDataMethod · 0.95
testURLCallbackMethod · 0.95
test_abbr_resetMethod · 0.95
testTOCResetMethod · 0.95
testBasicMetaDataMethod · 0.80

Calls 2

parseDocumentMethod · 0.80
runMethod · 0.45