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

Method convert

markdown/core.py:322–399  ·  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

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)

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
testYamlMetaDataMethod · 0.80

Calls 2

parseDocumentMethod · 0.80
runMethod · 0.45

Tested by 15

assertMarkdownRendersMethod · 0.76
testComplexSettingsMethod · 0.76
testWikilinksMetaDataMethod · 0.76
testURLCallbackMethod · 0.76
test_abbr_resetMethod · 0.76
testTOCResetMethod · 0.76
testBasicMetaDataMethod · 0.64
testYamlMetaDataMethod · 0.64
testMissingMetaDataMethod · 0.64