From 3a63ee1d372b1c5946e1b66fbe8d40bd3984d45d Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Mon, 14 Nov 2022 11:29:22 +0000 Subject: [PATCH] Accept colon(s) in YAML key names as it's ok according to the YAML specs and is widely used in the real world, f.e. in Puppet's Hiera (https://puppet.com/docs/puppet/7/hiera_quick.html#values_common_data) --- pygments/lexers/data.py | 2 +- tests/snippets/yaml/test_yaml_colon_in_key.txt | 11 +++++++++++ tests/snippets/yaml/test_yaml_colon_in_key_double.txt | 11 +++++++++++ tests/snippets/yaml/test_yaml_colon_in_key_start.txt | 11 +++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/snippets/yaml/test_yaml_colon_in_key.txt create mode 100644 tests/snippets/yaml/test_yaml_colon_in_key_double.txt create mode 100644 tests/snippets/yaml/test_yaml_colon_in_key_start.txt diff --git a/pygments/lexers/data.py b/pygments/lexers/data.py index e7d0a8436d..d1887304c7 100644 --- a/pygments/lexers/data.py +++ b/pygments/lexers/data.py @@ -231,7 +231,7 @@ def callback(lexer, match, context): # whitespaces separating tokens (r'[ ]+', Whitespace), # key with colon - (r'''([^#,:?\[\]{}"'\n]+)(:)(?=[ ]|$)''', + (r'''([^#,?\[\]{}"'\n]+)(:)(?=[ ]|$)''', bygroups(Name.Tag, set_indent(Punctuation, implicit=True))), # tags, anchors and aliases, include('descriptors'), diff --git a/tests/snippets/yaml/test_yaml_colon_in_key.txt b/tests/snippets/yaml/test_yaml_colon_in_key.txt new file mode 100644 index 0000000000..9f3d313666 --- /dev/null +++ b/tests/snippets/yaml/test_yaml_colon_in_key.txt @@ -0,0 +1,11 @@ +# Colon in the key name is accepted by the YAML specs too + +---input--- +foo:bar: value + +---tokens--- +'foo:bar' Name.Tag +':' Punctuation +' ' Text.Whitespace +'value' Literal.Scalar.Plain +'\n' Text.Whitespace diff --git a/tests/snippets/yaml/test_yaml_colon_in_key_double.txt b/tests/snippets/yaml/test_yaml_colon_in_key_double.txt new file mode 100644 index 0000000000..6f68b6d37e --- /dev/null +++ b/tests/snippets/yaml/test_yaml_colon_in_key_double.txt @@ -0,0 +1,11 @@ +# Colons in the key name is accepted by the YAML specs too + +---input--- +foo::bar: value + +---tokens--- +'foo::bar' Name.Tag +':' Punctuation +' ' Text.Whitespace +'value' Literal.Scalar.Plain +'\n' Text.Whitespace diff --git a/tests/snippets/yaml/test_yaml_colon_in_key_start.txt b/tests/snippets/yaml/test_yaml_colon_in_key_start.txt new file mode 100644 index 0000000000..60d4e2349a --- /dev/null +++ b/tests/snippets/yaml/test_yaml_colon_in_key_start.txt @@ -0,0 +1,11 @@ +# Colon at the beginning of the key name is accepted by the YAML specs too + +---input--- +:foo: value + +---tokens--- +':foo' Name.Tag +':' Punctuation +' ' Text.Whitespace +'value' Literal.Scalar.Plain +'\n' Text.Whitespace