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

Improve IniLexer #1624

Merged
merged 1 commit into from Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pygments/lexers/configs.py
Expand Up @@ -40,7 +40,7 @@ class IniLexer(RegexLexer):
(r'\s+', Text),
(r'[;#].*', Comment.Single),
(r'\[.*?\]$', Keyword),
(r'(.*?)([ \t]*)(=)([ \t]*)(.*(?:\n[ \t].+)*)',
(r'(.*?)([ \t]*)(=)([ \t]*)([^\t\n]*)',
bygroups(Name.Attribute, Text, Operator, Text, String)),
# standalone option, supported by some INI parsers
(r'(.+?)$', Name.Attribute),
Expand Down
81 changes: 81 additions & 0 deletions tests/test_ini_lexer.py
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
"""
Pygments INI lexer tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

import pytest
from pygments.lexers.configs import IniLexer
from pygments.token import Token, String, Keyword, Name, Operator


@pytest.fixture(scope='module')
def lexer():
yield IniLexer()


def test_indented_entries(lexer):
fragment = \
'[section]\n' \
' key1=value1\n' \
' key2=value2\n'
tokens = [
(Keyword, '[section]'),
(Token.Text, '\n '),
(Name.Attribute, 'key1'),
(Operator, '='),
(String, 'value1'),
(Token.Text, '\n '),
(Name.Attribute, 'key2'),
(Operator, '='),
(String, 'value2'),
(Token.Text, '\n'),
]
assert list(lexer.get_tokens(fragment)) == tokens

fragment = \
'[section]\n' \
' key1 = value1\n' \
' key2 = value2\n'
tokens = [
(Keyword, '[section]'),
(Token.Text, '\n '),
(Name.Attribute, 'key1'),
(Token.Text, ' '),
(Operator, '='),
(Token.Text, ' '),
(String, 'value1'),
(Token.Text, '\n '),
(Name.Attribute, 'key2'),
(Token.Text, ' '),
(Operator, '='),
(Token.Text, ' '),
(String, 'value2'),
(Token.Text, '\n'),
]
assert list(lexer.get_tokens(fragment)) == tokens

fragment = \
'[section]\n' \
' key 1 = value 1\n' \
' key 2 = value 2\n'
tokens = [
(Keyword, '[section]'),
(Token.Text, '\n '),
(Name.Attribute, 'key 1'),
(Token.Text, ' '),
(Operator, '='),
(Token.Text, ' '),
(String, 'value 1'),
(Token.Text, '\n '),
(Name.Attribute, 'key 2'),
(Token.Text, ' '),
(Operator, '='),
(Token.Text, ' '),
(String, 'value 2'),
(Token.Text, '\n'),
]
assert list(lexer.get_tokens(fragment)) == tokens