Default to a basic DFHack-specific pygments lexer (supporting comments and prompts)

develop
lethosor 2020-10-09 00:27:22 -04:00
parent 09f262a0c2
commit a5f85e256d
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
2 changed files with 36 additions and 1 deletions

@ -195,6 +195,7 @@ needs_sphinx = '1.8'
extensions = [
'sphinx.ext.extlinks',
'dfhack.changelog',
'dfhack.lexer',
]
# This config value must be a dictionary of external sites, mapping unique
@ -283,7 +284,7 @@ default_role = 'ref'
pygments_style = 'sphinx'
# The default language to highlight source code in.
highlight_language = 'none'
highlight_language = 'dfhack'
# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False

@ -0,0 +1,34 @@
# adapted from:
# https://stackoverflow.com/a/16470058
# https://pygments.org/docs/lexerdevelopment/
import re
from pygments.lexer import RegexLexer
from pygments.token import Comment, Generic, Text
from sphinx.highlighting import lexers
class DFHackLexer(RegexLexer):
name = 'DFHack'
aliases = ['dfhack']
flags = re.IGNORECASE | re.MULTILINE
tokens = {
'root': [
(r'\#.+$', Comment.Single),
(r'^\[[a-z]+\]\# ', Generic.Prompt),
(r'.+?', Text),
]
}
def register_lexer(app):
lexers['dfhack'] = DFHackLexer()
def setup(app):
app.connect('builder-inited', register_lexer)
return {
'version': '0.1',
'parallel_read_safe': True,
'parallel_write_safe': True,
}