From 42b5a94660d635989248de5c2b93fa608ab2b861 Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Tue, 9 Feb 2016 13:26:40 +0100 Subject: Varnish Lexer: Add analyse_text() It looks for documents starting with 'vcl 4.0;', optionally with comments preceding it. As all valid VCL as of Varnish 4.0 starts with this (even Varnish 4.1), this is pretty much guaranteed to work. --- pygments/lexers/varnish.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pygments/lexers/varnish.py b/pygments/lexers/varnish.py index 0162f55f..d2d81f6d 100644 --- a/pygments/lexers/varnish.py +++ b/pygments/lexers/varnish.py @@ -28,6 +28,20 @@ class VCLLexer(RegexLexer): filenames = [ '*.vcl' ] mimetypes = ['text/x-vclsrc'] + def analyse_text(text): + # If the very first line is 'vcl 4.0;' it's pretty much guaranteed + # that this is VCL + if re.search('^vcl 4\.0;\n', text): + return 1.0 + + # Skip over comments and blank lines + # This is accurate enough that returning 0.9 is reasonable. + # Almost no VCL files start without some comments. + if re.search('^((\s+)|(#[^\n]*\n)|(\n)|(\s*//[^\n]*\n)|(/\*[^*/]*\*/))*vcl 4\.0;', text): + return 0.9 + + return 0.0 + tokens = { 'probe': [ (r'(\s*\.\w+)(\s*=\s*)([^;]*)(;)', @@ -100,7 +114,7 @@ class VCLLexer(RegexLexer): bygroups(Keyword,Name.Variable.Global,Punctuation),'acl'), (r'[();]', Punctuation), (r'(client|server)\.(ip|identity)\b',Name.Variable), - (r'^(vcl )(4.0)(;)$', + (r'(vcl )(4.0)(;)$', bygroups(Keyword.Reserved,Name.Constant,Punctuation)), (r'(include\s+)("[^"]+"\s*)(;)', bygroups(Keyword,String,Punctuation)) -- cgit v1.2.1