-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
Well, have you already seen the documentation on creating lexers? https://pygments.org/docs/lexerdevelopment/ |
Beta Was this translation helpful? Give feedback.
-
I managed to get it to work.... kinda. It's most likely a regex issue on my end, so I would appreciate if someone could take a look here and give me a clue on what to change. """A Pygments lexer for the command examples"""
import re
from pygments.lexer import RegexLexer, bygroups
from pygments.token import *
__all__ = ("CommandLexer",)
class CommandLexer(RegexLexer):
"""Simple lexer to highlight arguments of commands."""
name = 'Command'
aliases = ['command']
filenames = ['*.command']
tokens = {
'root': [
(r'^(/)([^\S]+)', bygroups(Name.Tag, Text)),
(r'[\[\{<]', Punctuation, 'option'),
(r'\w', Text)
],
'option': [
(r'\|', Punctuation),
(r'[^\[\{<>\}\|]+', Name.Tag),
(r'[\[\{<]', Punctuation, '#push'),
(r'[\]\}>]', Punctuation, '#pop')
]
} And while I'm at it, I am wondering what the recommended way is to register this lexer for Pygments to use. I'm looking for a setup that I could use in GitHub Actions. |
Beta Was this translation helpful? Give feedback.
-
I'm still looking for a proper setup to register my lexer in Pygments to be used in other projects... My current setup uses deprecated stuff, which I would like to avoid. The current setup I use is to run Would the equivalent for this be similar to what the Pygments docs mention ( Any suggestions? |
Beta Was this translation helpful? Give feedback.
-
To give a complete answer to this for everyone, I managed to create the following lexer: """A Pygments lexer for the command examples"""
import re
from pygments.lexer import RegexLexer, bygroups
from pygments.token import *
__all__ = ("CommandLexer",)
class CommandLexer(RegexLexer):
"""Simple lexer to highlight arguments of commands."""
name = 'Command'
aliases = ['command']
filenames = ['*.command']
tokens = {
'root': [
(r'^(\/)([^\S]+)', bygroups(Name.Tag, Text)),
(r'[\[\{<]', Punctuation, 'option'),
(r'\w', Text)
],
'option': [
(r'\|', Punctuation),
(r'[^\[\{<>\}\|\]]+', Name.Tag),
(r'[\[\{<]', Punctuation, '#push'),
(r'[\]\}>]', Punctuation, '#pop')
]
} Also made an empty And I setup this [build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "commandlexer"
description = "A custom RegexLexer for DecentHolograms commands highlighting"
version = "1.0.0"
authors = [
{ name = "Andre601" }
]
dependencies = ["pygments"]
[project.entry-points."pygments.lexers"]
commandlexer = "commandlexer.command_lexer:CommandLexer" Finally did I use Thanks everyone who helped me getting this to work! |
Beta Was this translation helpful? Give feedback.
To give a complete answer to this for everyone, I managed to create the following lexer: