1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-19 21:10:15 +02:00

Fix IP address as name detection in nginx conf (#1060)

Adds lexer rule to detect IP address _without_ a port.
I've created two commits for this PR. The first generates the test
snapshot files with the existing ruleset, the second adds the new rule
and updates the snapshot.

Noticed as part of the [NGINX
docs](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#proxying-http-traffic-to-a-group-of-servers),
you can see the `192` being highlighted by itself.

Let me know if there are any conventions I missed, or tweaks I should
make. Thanks!
This commit is contained in:
Jack Hickey 2025-03-15 23:17:47 +00:00 committed by GitHub
parent 0bf0e9f9ae
commit e0c774731c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 171 additions and 0 deletions

View File

@ -58,6 +58,9 @@
<rule pattern="\$[^\s;#()]+"> <rule pattern="\$[^\s;#()]+">
<token type="NameVariable"/> <token type="NameVariable"/>
</rule> </rule>
<rule pattern="(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)">
<token type="Name"/>
</rule>
<rule pattern="([a-z0-9.-]+)(:)([0-9]+)"> <rule pattern="([a-z0-9.-]+)(:)([0-9]+)">
<bygroups> <bygroups>
<token type="Name"/> <token type="Name"/>

31
lexers/testdata/nginx.actual vendored Normal file
View File

@ -0,0 +1,31 @@
http {
upstream app_backend {
server 127.0.0.1;
server 127.0.0.1:8082;
server backend.com;
server backend-2.com weight=5;
server backend-backup.org backup;
}
map $http_host $site_root {
default /var/www/default; # Default root
example.com /var/www/example_com;
example.org /var/www/example_org;
}
server {
listen 80 default_server;
server_name _;
root /var/www/default;
location / {
index index.html;
}
location /status {
return 200 "Default server status: OK\n";
add_header Content-Type text/plain;
}
}
}

137
lexers/testdata/nginx.expected vendored Normal file
View File

@ -0,0 +1,137 @@
[
{"type":"Keyword","value":"http"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"upstream"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"app_backend"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"server"},
{"type":"Text","value":" "},
{"type":"Name","value":"127.0.0.1"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"server"},
{"type":"Text","value":" "},
{"type":"Name","value":"127.0.0.1"},
{"type":"Punctuation","value":":"},
{"type":"LiteralNumberInteger","value":"8082"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"server"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"backend.com"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"server"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"backend-2.com"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"weight=5"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"server"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"backend-backup.org"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"backup"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordNamespace","value":"map"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"$http_host"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"$site_root"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"default"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"/var/www/default"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"# Default root\n"},
{"type":"Text","value":" "},
{"type":"KeywordNamespace","value":"example.com"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"/var/www/example_com"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"example.org"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"/var/www/example_org"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordNamespace","value":"server"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"listen"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"80"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"default_server"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"server_name"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"_"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordNamespace","value":"root"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"/var/www/default"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordNamespace","value":"location"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"/"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"index"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"index.html"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordNamespace","value":"location"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"/status"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"return"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"200"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"\"Default"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"server"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"status:"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"OK\\n\""},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordNamespace","value":"add_header"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"Content-Type"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"text/plain"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"}
]