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

Fix: Issues with .properties format using whitespace delimited key #2241

Merged
merged 5 commits into from Sep 25, 2022
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
28 changes: 20 additions & 8 deletions pygments/lexers/configs.py
Expand Up @@ -128,14 +128,26 @@ class PropertiesLexer(RegexLexer):

tokens = {
'root': [
(r'^(\w+)([ \t])(\w+\s*)$', bygroups(Name.Attribute, Whitespace, String)),
(r'^\w+(\\[ \t]\w*)*$', Name.Attribute),
(r'(^ *)([#!].*)', bygroups(Whitespace, Comment)),
# More controversial comments
(r'(^ *)((?:;|//).*)', bygroups(Whitespace, Comment)),
(r'(.*?)([ \t]*)([=:])([ \t]*)(.*(?:(?<=\\)\n.*)*)',
bygroups(Name.Attribute, Whitespace, Operator, Whitespace, String)),
(r'\s', Whitespace),
(r'\s+', Whitespace),
(r'[!#].*|/{2}.*', Comment.Single),
# search for first separator
(r'([^\\\n]|\\.)*?(?=[ \f\t=:])', Name.Attribute, "separator"),
# empty key
(r'.+?$', Name.Attribute),
],
'separator': [
# search for line continuation escape
(r'([ \f\t]*)([=:]*)([ \f\t]*)(.*(?<!\\)(?:\\{2})*)(\\)(?!\\)$',
bygroups(Whitespace, Operator, Whitespace, String, Text), "value", "#pop"),
(r'([ \f\t]*)([=:]*)([ \f\t]*)(.*)',
bygroups(Whitespace, Operator, Whitespace, String), "#pop"),
],
'value': [ # line continuation
(r'\s+', Whitespace),
# search for line continuation escape
(r'(\s*)(.*(?<!\\)(?:\\{2})*)(\\)(?!\\)([ \t]*)',
bygroups(Whitespace, String, Text, Whitespace)),
(r'.*$', String, "#pop"),
],
}

Expand Down
38 changes: 23 additions & 15 deletions tests/examplefiles/properties/java.properties
@@ -1,16 +1,24 @@
foo = bar
foo: bar
foo.oof: \
bar=baz; \
asdf
#https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Properties.htm
# mixing spaces
Truth = Beauty
Truth:Beauty
Truth Beauty
Truth :Beauty

! line continuations and escapes
fruits apple, banana, pear, \
cantaloupe, watermelon, \
kiwi, mango
key = \
value1 \\\
and value2\\
key\ 2 = value
key\\ 3 = value3

// comment
# comment
; comment

x:a\
b
x: a \
b

x = \
! empty keys and edge cases
key1 =
key2
key3 the value3
key4 the:value4
key5 the=value5
key6=the value6
88 changes: 66 additions & 22 deletions tests/examplefiles/properties/java.properties.output

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

4 changes: 2 additions & 2 deletions tests/snippets/properties/test_comments.txt
Expand Up @@ -5,8 +5,8 @@
# also a comment

---tokens---
'! a comment' Comment
'! a comment' Comment.Single
'\n' Text.Whitespace

'# also a comment' Comment
'# also a comment' Comment.Single
'\n' Text.Whitespace
Expand Up @@ -2,5 +2,5 @@
# comment

---tokens---
'# comment' Comment
'# comment' Comment.Single
'\n' Text.Whitespace
3 changes: 2 additions & 1 deletion tests/snippets/properties/test_space_delimited_kv_pair.txt
Expand Up @@ -4,4 +4,5 @@ key value
---tokens---
'key' Name.Attribute
' ' Text.Whitespace
'value\n' Literal.String
'value' Literal.String
'\n' Text.Whitespace