Skip to content

Add trivial lexer for output (using Token.Generic.Output for the complete code block) #1835

@doerwalter

Description

@doerwalter

If I have the following Sphinx source:

	Do the following in your Python shell:

	.. sourcecode:: pycon

		>>> for i in range(3):
		...	print(i)
		...
		0
		1
		2

	You can also run the following code as a script:

	.. sourcecode:: pycon

		for i in range(3):
			print(i)

	This will output:

	.. sourcecode:: text

		0
		1
		2

the output in the first code snippet is marked up with Token.Generic.Output, but the output of the second code snippet is marked up with Token.Text (in fact it isn't marked up at all).

In my current style the result looks like this:

grafik

i.e. both outputs look different. I could of course use the console lexer for the second output and hope that the lexer doesn't find anything that it will interpret as a prompt. The better solution however would be to have a new lexer that simply outputs everything marked up with the token Token.Generic.Output.

Code for that might look something like this:

class OutputLexer(lexer.Lexer):
	"""
	Simple lexer that highlights everything as output.
	"""
	name = 'Text output'
	aliases = ['output']
	filenames = ['*.txt']
	mimetypes = ['text/plain']
	priority = 0.005

	def get_tokens_unprocessed(self, text):
		yield 0, token.Generic.Output, text

	def analyse_text(text):
		return OutpuLexer.priority

If this is accepted I can work on a pull request implementing OutputLexer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions