Package: comprehemd
🤔 CompreheMDCompreheMDis a Python package for parsing Markdown documents.InstallationMarkdownParser classParsing a streamParsing chunksBlocks classesBlockCodeBlockEmptyBlockHeadingBlockProjectContributingLicenceAuthorAcknowledgementsInstallationCompreheMD requires Python 3.8 or later.Install CompreheMD via pip:pipinstallcomprehemdMarkdownParser classParsing a streamThe Markdown document parsed in this example isexample.md.To read an entire text stream, callread(). The method yieldsblocksuntil the stream ends.fromcomprehemdimportMarkdownParserwithopen("docs/example.md","r")asfp:forblockinMarkdownParser().read(fp):print(block)HeadingBlock (1): An Example Document
EmptyBlock
HeadingBlock (2): Introduction
EmptyBlock
Block: This is just a short example document.
EmptyBlock
HeadingBlock (2): Block examples
EmptyBlock
Block: Here's some backtick-fenced code:
EmptyBlock
CodeBlock (python): print("Hello, world!")
EmptyBlock
Block: Here's some tilde-fenced code:
EmptyBlock
CodeBlock (python): print("Hello, galaxy!")
EmptyBlock
Block: Here's some indented code:
EmptyBlock
CodeBlock (<None>): print("Hello, multiverse!")
EmptyBlock
Block: That's your lot!Parsing chunksThe parser can be fed ad-hoc chunks of Markdown. Thefeed()method yields all theblocksthat the chunk completed.After feeding the final chunk, you must callclose()to flush and yield any buffered blocks.fromcomprehemdimportCodeBlock,HeadingBlock,MarkdownParserdeftease(chunk:str)->None:escaped=chunk.replace("\n","\\n")forblockinparser.feed(chunk):print(f'After "{escaped}", the parser yielded:')print(block)print()else:print(f'After "{escaped}", the parser did not yield.')print()parser=MarkdownParser()tease("# Feeding exam")tease("ple\n\nThis de")tease("monstrates chu")tease("nked feeding.")forblockinparser.close():print("After closing, the parser yielded:")print(block)print()After "# Feeding exam", the parser did not yield.

After "ple\n\nThis de", the parser yielded:
HeadingBlock (1): Feeding example

After "ple\n\nThis de", the parser yielded:
EmptyBlock

After "ple\n\nThis de", the parser did not yield.

After "monstrates chu", the parser did not yield.

After "nked feeding.", the parser did not yield.

After closing, the parser yielded:
Block: This demonstrates chunked feeding.Blocks classesBlockTheBlockclass is the base of all blocks.sourcereturns the original Markdown source for the block.textreturns the meaningful text representation of the block.CodeBlockTheCodeBlockclass represents a code block.languagereturns the language hint if one was specified.The block can be rendered back to Markdown by callingrender().EmptyBlockEmptyBlockrepresents an empty line.HeadingBlockTheHeadingBlockclass represents a heading.levelreturns the heading's level (i.e. 1 for the top-most heading, down to 6 for the lowest).ProjectContributingTo contribute a bug report, enhancement or feature request, please raise an issue atgithub.com/cariad/comprehemd/issues.If you want to contribute a code change, please raise an issue first so we can chat about the direction you want to take.LicenceCompreheMD is released atgithub.com/cariad/comprehemdunder the MIT Licence.SeeLICENSEfor more information.AuthorHello! 👋 I'mCariad Ecclestonand I'm a freelance DevOps and backend engineer. My contact details are available on my personal wiki atcariad.earth.Please consider supporting my open source projects bysponsoring me on GitHub.AcknowledgementsEpic ❤️ to John Gruber for developingthe original Markdown specification.This documentation was pressed byEdition.