|
11 | 11 | import re |
12 | 12 |
|
13 | 13 | from pygments.lexer import ExtendedRegexLexer, RegexLexer, default, words, \ |
14 | | - bygroups, include, using, line_re |
| 14 | + bygroups, include, using, this, line_re |
15 | 15 | from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
16 | 16 | Number, Punctuation, Whitespace, Literal, Error, Generic |
17 | 17 | from pygments.lexers.shell import BashLexer |
|
22 | 22 | 'NginxConfLexer', 'LighttpdConfLexer', 'DockerLexer', |
23 | 23 | 'TerraformLexer', 'TermcapLexer', 'TerminfoLexer', |
24 | 24 | 'PkgConfigLexer', 'PacmanConfLexer', 'AugeasLexer', 'TOMLLexer', |
25 | | - 'NestedTextLexer', 'SingularityLexer', 'UnixConfigLexer'] |
| 25 | + 'NestedTextLexer', 'SingularityLexer', 'UnixConfigLexer', |
| 26 | + 'CaddyfileLexer'] |
26 | 27 |
|
27 | 28 |
|
28 | 29 | class IniLexer(RegexLexer): |
@@ -1431,3 +1432,68 @@ class UnixConfigLexer(RegexLexer): |
1431 | 1432 | (r'[^:\n]+', String), |
1432 | 1433 | ], |
1433 | 1434 | } |
| 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 | + } |
0 commit comments