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