commonmark_wrapper.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #! /usr/bin/env python3
  2. # :Copyright: © 2022 Günter Milde.
  3. # :License: Released under the terms of the `2-Clause BSD license`_, in short:
  4. #
  5. # Copying and distribution of this file, with or without modification,
  6. # are permitted in any medium without royalty provided the copyright
  7. # notice and this notice are preserved.
  8. # This file is offered as-is, without any warranty.
  9. #
  10. # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
  11. #
  12. # Revision: $Revision: 9048 $
  13. # Date: $Date: 2022-03-29 23:50:15 +0200 (Di, 29. Mär 2022) $
  14. """
  15. An interface for parsing CommonMark input.
  16. Select a locally installed parser from the following 3rd-party
  17. parser packages:
  18. :pycmark: https://pypi.org/project/pycmark/
  19. :myst: https://pypi.org/project/pycmark/
  20. :recommonmark: https://pypi.org/project/pycmark/ (unmaintained, deprecated)
  21. The first parser class that can be successfully imported is mapped to
  22. `commonmark_wrapper.Parser`.
  23. This module is provisional:
  24. the API is not settled and may change with any minor Docutils version.
  25. """
  26. import docutils.parsers
  27. commonmark_parser_names = ('pycmark', 'myst', 'recommonmark')
  28. """Names of compatible drop-in CommonMark parsers"""
  29. Parser = None
  30. parser_name = ''
  31. for name in commonmark_parser_names:
  32. try:
  33. Parser = docutils.parsers.get_parser_class(name)
  34. except ImportError:
  35. continue
  36. parser_name = name
  37. break
  38. if Parser is None:
  39. raise ImportError(
  40. 'Parsing "CommonMark" requires one of the packages\n'
  41. f'{commonmark_parser_names} available at https://pypi.org')
  42. if parser_name == 'myst':
  43. if not Parser.settings_defaults:
  44. Parser.settings_defaults = {}
  45. Parser.settings_defaults['myst_commonmark_only'] = True