Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add World of Warcraft TOC file lexer #2244

Merged
merged 11 commits into from Sep 21, 2022
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -522,6 +522,7 @@
'WatLexer': ('pygments.lexers.webassembly', 'WebAssembly', ('wast', 'wat'), ('*.wat', '*.wast'), ()),
'WebIDLLexer': ('pygments.lexers.webidl', 'Web IDL', ('webidl',), ('*.webidl',), ()),
'WhileyLexer': ('pygments.lexers.whiley', 'Whiley', ('whiley',), ('*.whiley',), ('text/x-whiley',)),
'WoWTocLexer': ('pygments.lexers.wowtoc', 'World of Warcraft TOC', ('wowtoc',), ('*.toc',), ()),
'X10Lexer': ('pygments.lexers.x10', 'X10', ('x10', 'xten'), ('*.x10',), ('text/x-x10',)),
'XMLUL4Lexer': ('pygments.lexers.ul4', 'XML+UL4', ('xml+ul4',), ('*.xmlul4',), ()),
'XQueryLexer': ('pygments.lexers.webmisc', 'XQuery', ('xquery', 'xqy', 'xq', 'xql', 'xqm'), ('*.xqy', '*.xquery', '*.xq', '*.xql', '*.xqm'), ('text/xquery', 'application/xquery')),
Expand Down
82 changes: 82 additions & 0 deletions pygments/lexers/wowtoc.py
@@ -0,0 +1,82 @@
"""
pygments.lexers.wowtoc
~~~~~~~~~~~~~~~~~~~~~~

Lexer for World of Warcraft TOC files, which describe game addon metadata.

:copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

from pygments.lexer import RegexLexer, bygroups
from pygments.token import Comment, Name, Text, Punctuation, String, Keyword

__all__ = ["WoWTocLexer"]

def _create_tag_line_token(inner_pattern, inner_token, ignore_case=False):
# this function template-izes the tag line for a specific type of tag, which will
# have a different pattern and different token. otherwise, everything about a tag
# line is the same
return (
(r"(?i)" if ignore_case else r"")
+ r"^(##)( *)"
+ inner_pattern
+ r"( *)(:)( *)([^\n]*?)( *)$",
t-mart marked this conversation as resolved.
Show resolved Hide resolved
bygroups(
Keyword.Declaration,
Text.Whitespace,
inner_token,
Text.Whitespace,
Punctuation,
Text.Whitespace,
String,
Text.Whitespace,
),
)


class WoWTocLexer(RegexLexer):
"""
Lexer for World of Warcraft TOC files.

.. versionadded:: 2.13
"""

name = "World of Warcraft TOC"
aliases = ["wowtoc"]
filenames = ["*.toc"]

tokens = {
"root": [
# official localized tags, Notes and Title
# (normal part is insensitive, locale part is sensitive)
_create_tag_line_token(
r"((?:[nN][oO][tT][eE][sS]|[tT][iI][tT][lL][eE])-(?:ptBR|zhCN|enCN|frFR|deDE|itIT|esMX|ptPT|koKR|ruRU|esES|zhTW|enTW|enGB|enUS))",
Name.Builtin,
),
# other official tags
_create_tag_line_token(
r"(Interface|Title|Notes|RequiredDeps|Dep[^: ]*|OptionalDeps|LoadOnDemand|LoadWith|LoadManagers|SavedVariablesPerCharacter|SavedVariables|DefaultState|Secure|Author|Version)",
Name.Builtin,
ignore_case=True
),
# user-defined tags
_create_tag_line_token(
r"(x-[^: ]*)",
Name.Variable,
ignore_case=True
),
# non-conforming tags, but still valid
_create_tag_line_token(
r"([^: ]*)",
Name.Other,
ignore_case=True
t-mart marked this conversation as resolved.
Show resolved Hide resolved
),

# Comments
(r"^#[^\n]*$", Comment),
t-mart marked this conversation as resolved.
Show resolved Hide resolved

# Addon Files
(r"^[^\n]+$", Name),
t-mart marked this conversation as resolved.
Show resolved Hide resolved
]
}
6 changes: 6 additions & 0 deletions tests/examplefiles/wowtoc/comments.toc
@@ -0,0 +1,6 @@
#
#a
# a comment
# a comment with a # in it
## no comma, thus a comment
## has space: and is thus, a comment
17 changes: 17 additions & 0 deletions tests/examplefiles/wowtoc/comments.toc.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/examplefiles/wowtoc/files.toc
@@ -0,0 +1,3 @@
a
Foo.lua
Spaces allowed.lua
8 changes: 8 additions & 0 deletions tests/examplefiles/wowtoc/files.toc.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions tests/examplefiles/wowtoc/official_tags.toc
@@ -0,0 +1,63 @@
## TiTlE-ptBR: value
## TiTlE-zhCN: value
## TiTlE-enCN: value
## TiTlE-frFR: value
## TiTlE-deDE: value
## TiTlE-itIT: value
## TiTlE-esMX: value
## TiTlE-ptPT: value
## TiTlE-koKR: value
## TiTlE-ruRU: value
## TiTlE-esES: value
## TiTlE-zhTW: value
## TiTlE-enTW: value
## TiTlE-enGB: value
## TiTlE-enUS: value
## NoTeS-ptBR: value
## NoTeS-zhCN: value
## NoTeS-enCN: value
## NoTeS-frFR: value
## NoTeS-deDE: value
## NoTeS-itIT: value
## NoTeS-esMX: value
## NoTeS-ptPT: value
## NoTeS-koKR: value
## NoTeS-ruRU: value
## NoTeS-esES: value
## NoTeS-zhTW: value
## NoTeS-enTW: value
## NoTeS-enGB: value
## NoTeS-enUS: value
## Interface: value
## interface: value
## Title: value
## title: value
## Notes: value
## notes: value
## RequiredDeps: value
## requireddeps: value
## Dependencies: value
## dependencies: value
## OptionalDeps: value
## optionaldeps: value
## LoadOnDemand: value
## loadondemand: value
## LoadWith: value
## loadwith: value
## LoadManagers: value
## loadmanagers: value
## SavedVariablesPerCharacter: value
## savedvariablespercharacter: value
## SavedVariables: value
## savedvariables: value
## DefaultState: value
## defaultstate: value
## Secure: value
## secure: value
## Author: value
## author: value
## Version: value
## version: value
## Dep: value
## dep: value
## DepSomething: value