Skip to content

Commit d3441d0

Browse files
authored
Merge pull request #3225 from jvoisin/caddy
Add a lexer for Caddy
2 parents 8549c5a + c593f3f commit d3441d0

13 files changed

Lines changed: 611 additions & 2 deletions

‎CHANGES‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Version 2.21.0
1010
- New lexers:
1111

1212
* BitBake (#3103)
13+
* Caddyfile
1314
* CEL (#3048)
1415
* PureScript (#1077, #3054)
1516

‎pygments/lexers/_mapping.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
'CSharpAspxLexer': ('pygments.lexers.dotnet', 'aspx-cs', ('aspx-cs',), ('*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd'), ()),
7474
'CSharpLexer': ('pygments.lexers.dotnet', 'C#', ('csharp', 'c#', 'cs'), ('*.cs',), ('text/x-csharp',)),
7575
'Ca65Lexer': ('pygments.lexers.asm', 'ca65 assembler', ('ca65',), ('*.s',), ()),
76+
'CaddyfileLexer': ('pygments.lexers.configs', 'Caddyfile', ('caddyfile', 'caddy'), ('Caddyfile',), ()),
7677
'CadlLexer': ('pygments.lexers.archetype', 'cADL', ('cadl',), ('*.cadl',), ()),
7778
'CapDLLexer': ('pygments.lexers.esoteric', 'CapDL', ('capdl',), ('*.cdl',), ()),
7879
'CapnProtoLexer': ('pygments.lexers.capnproto', "Cap'n Proto", ('capnp',), ('*.capnp',), ()),

‎pygments/lexers/configs.py‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import re
1212

1313
from pygments.lexer import ExtendedRegexLexer, RegexLexer, default, words, \
14-
bygroups, include, using, line_re
14+
bygroups, include, using, this, line_re
1515
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
1616
Number, Punctuation, Whitespace, Literal, Error, Generic
1717
from pygments.lexers.shell import BashLexer
@@ -22,7 +22,8 @@
2222
'NginxConfLexer', 'LighttpdConfLexer', 'DockerLexer',
2323
'TerraformLexer', 'TermcapLexer', 'TerminfoLexer',
2424
'PkgConfigLexer', 'PacmanConfLexer', 'AugeasLexer', 'TOMLLexer',
25-
'NestedTextLexer', 'SingularityLexer', 'UnixConfigLexer']
25+
'NestedTextLexer', 'SingularityLexer', 'UnixConfigLexer',
26+
'CaddyfileLexer']
2627

2728

2829
class IniLexer(RegexLexer):
@@ -1431,3 +1432,68 @@ class UnixConfigLexer(RegexLexer):
14311432
(r'[^:\n]+', String),
14321433
],
14331434
}
1435+
1436+
1437+
class CaddyfileLexer(RegexLexer):
1438+
"""
1439+
Lexer for Caddyfile, the configuration file format of the Caddy web server.
1440+
"""
1441+
1442+
name = 'Caddyfile'
1443+
url = 'https://caddyserver.com/docs/caddyfile'
1444+
aliases = ['caddyfile', 'caddy']
1445+
filenames = ['Caddyfile']
1446+
version_added = '2.21'
1447+
1448+
# The standard HTTP directives shipped with Caddy.
1449+
directives = (
1450+
'abort', 'acme_server', 'basic_auth', 'bind', 'encode', 'error',
1451+
'file_server', 'forward_auth', 'handle', 'handle_errors',
1452+
'handle_path', 'header', 'import', 'invoke', 'log', 'map', 'metrics',
1453+
'php_fastcgi', 'push', 'redir', 'request_body', 'request_header',
1454+
'respond', 'reverse_proxy', 'rewrite', 'root', 'route', 'templates',
1455+
'tls', 'tracing', 'try_files', 'uri', 'vars',
1456+
)
1457+
1458+
tokens = {
1459+
'root': [
1460+
# Directives are only keywords as the first token on a line, so
1461+
# anchor them to the start of the (possibly indented) line. This
1462+
# consumes the preceding newline(s)/indentation itself, otherwise
1463+
# the generic whitespace rule below would swallow it first.
1464+
(r'((?:\A|\s*\n)[ \t]*)(' + '|'.join(directives) + r')\b',
1465+
bygroups(Whitespace, Keyword)),
1466+
(r'\s+', Whitespace),
1467+
(r'#.*', Comment.Single),
1468+
# Heredoc: <<LABEL <args> ... LABEL. The remainder of the opening
1469+
# line (e.g. a status code) is not part of the body, so re-lex it;
1470+
# the closing label must stand alone on its line.
1471+
(r'(<<)([a-zA-Z_]\w*)(.*\n)((?:.*\n)*?)([ \t]*)(\2)(?=[ \t]*$)',
1472+
bygroups(Operator, String.Delimiter, using(this),
1473+
String.Heredoc, Whitespace, String.Delimiter)),
1474+
# Placeholders: {path}, {http.request.uri}, {$ENV}, {args[0]}, ...
1475+
(r'\{[^{}\s]+\}', Name.Variable),
1476+
# Blocks and snippet parentheses
1477+
(r'[(){}]', Punctuation),
1478+
# Named matcher, e.g. @api
1479+
(r'@[\w.-]+', Name.Decorator),
1480+
(r'"', String.Double, 'string'),
1481+
(r'`', String.Backtick, 'backtick'),
1482+
# Numbers, durations and sizes (443, 200, 30s, 1.5m, 10MB); a
1483+
# trailing dot or more digits/letters means it is an address or
1484+
# hostname, which must stay Text.
1485+
(r'\d+(?:\.\d+)?[a-zA-Z]*(?=[\s#{}()"`]|$)', Number),
1486+
(r'[^\s#{}()"`]+', Text),
1487+
],
1488+
'string': [
1489+
(r'\\.', String.Escape),
1490+
(r'\{[^{}\s]+\}', Name.Variable),
1491+
(r'[^"\\{]+', String.Double),
1492+
(r'"', String.Double, '#pop'),
1493+
(r'.', String.Double),
1494+
],
1495+
'backtick': [
1496+
(r'[^`]+', String.Backtick),
1497+
(r'`', String.Backtick, '#pop'),
1498+
],
1499+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Global options block
2+
{
3+
4+
admin off
5+
http_port 80
6+
https_port 443
7+
default_sni example.com
8+
log {
9+
output file /var/log/caddy/access.log
10+
format json
11+
level INFO
12+
}
13+
}
14+
15+
# Reusable snippet
16+
(logging) {
17+
log {
18+
output file /var/log/caddy/{args[0]}.log
19+
}
20+
}
21+
22+
# Simple static site
23+
example.com {
24+
root * /srv/www
25+
file_server
26+
encode gzip zstd
27+
28+
import logging example
29+
30+
@api {
31+
path /api/*
32+
method GET POST
33+
}
34+
handle @api {
35+
reverse_proxy localhost:8080 {
36+
header_up Host {http.request.host}
37+
header_up X-Real-IP {remote_host}
38+
lb_policy round_robin
39+
}
40+
}
41+
42+
@websockets {
43+
header Connection *Upgrade*
44+
header Upgrade websocket
45+
}
46+
47+
redir /old /new permanent
48+
49+
respond /health 200
50+
51+
respond <<HTML 200
52+
<html>
53+
<body>Hello from {http.request.uri}</body>
54+
</html>
55+
HTML
56+
57+
header {
58+
Strict-Transport-Security "max-age=31536000;"
59+
-Server
60+
}
61+
62+
tls {$ACME_EMAIL} {
63+
protocols tls1.2 tls1.3
64+
}
65+
66+
basic_auth {
67+
alice $2a$14$hashedpasswordhere
68+
}
69+
}
70+
71+
# Redirect www to apex
72+
www.example.com {
73+
redir https://example.com{uri} permanent
74+
}
75+
76+
:8080 {
77+
metrics /metrics
78+
respond "It works!" 200
79+
}

0 commit comments

Comments
 (0)