qmp_lexer.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # QEMU Monitor Protocol Lexer Extension
  2. #
  3. # Copyright (C) 2019, Red Hat Inc.
  4. #
  5. # Authors:
  6. # Eduardo Habkost <ehabkost@redhat.com>
  7. # John Snow <jsnow@redhat.com>
  8. #
  9. # This work is licensed under the terms of the GNU GPLv2 or later.
  10. # See the COPYING file in the top-level directory.
  11. """qmp_lexer is a Sphinx extension that provides a QMP lexer for code blocks."""
  12. from pygments.lexer import RegexLexer, DelegatingLexer
  13. from pygments.lexers.data import JsonLexer
  14. from pygments import token
  15. from sphinx import errors
  16. class QMPExampleMarkersLexer(RegexLexer):
  17. """
  18. QMPExampleMarkersLexer lexes QMP example annotations.
  19. This lexer adds support for directionality flow and elision indicators.
  20. """
  21. tokens = {
  22. 'root': [
  23. (r'-> ', token.Generic.Prompt),
  24. (r'<- ', token.Generic.Prompt),
  25. (r'\.{3}( .* \.{3})?', token.Comment.Multiline),
  26. ]
  27. }
  28. class QMPExampleLexer(DelegatingLexer):
  29. """QMPExampleLexer lexes annotated QMP examples."""
  30. def __init__(self, **options):
  31. super(QMPExampleLexer, self).__init__(JsonLexer, QMPExampleMarkersLexer,
  32. token.Error, **options)
  33. def setup(sphinx):
  34. """For use by the Sphinx extensions API."""
  35. try:
  36. sphinx.require_sphinx('2.1')
  37. sphinx.add_lexer('QMP', QMPExampleLexer)
  38. except errors.VersionRequirementError:
  39. sphinx.add_lexer('QMP', QMPExampleLexer())
  40. return dict(
  41. parallel_read_safe = True,
  42. parallel_write_safe = True
  43. )