summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthatch <devnull@localhost>2008-12-31 10:25:01 -0600
committerthatch <devnull@localhost>2008-12-31 10:25:01 -0600
commit6bb013d7840510b4adf910269efb7f053d074bb5 (patch)
tree880dc5cc821e971dc0a913fdfcb62e222fe4a912
parentdcee51e8a7559ea221a35e0ae233e35140286662 (diff)
downloadpygments-6bb013d7840510b4adf910269efb7f053d074bb5.tar.gz
Support guessing even with a filename (#355)
This allows multiple lexers to support the same file extensions (say .pl for prolog or perl). When the file text is not provided, it will probably do the wrong thing (so -N mode of pygmentize will still be a wild guess).
-rw-r--r--pygments/cmdline.py2
-rw-r--r--pygments/lexers/__init__.py26
-rw-r--r--tests/examplefiles/matlab_sample (renamed from tests/examplefiles/sample.m)0
3 files changed, 23 insertions, 5 deletions
diff --git a/pygments/cmdline.py b/pygments/cmdline.py
index d1d21412..b9fe870a 100644
--- a/pygments/cmdline.py
+++ b/pygments/cmdline.py
@@ -366,7 +366,7 @@ def main(args=sys.argv):
if not lexer:
try:
- lexer = get_lexer_for_filename(infn, **parsed_opts)
+ lexer = get_lexer_for_filename(infn, code, **parsed_opts)
except ClassNotFound, err:
if '-g' in opts:
try:
diff --git a/pygments/lexers/__init__.py b/pygments/lexers/__init__.py
index 255fa022..fc242ae8 100644
--- a/pygments/lexers/__init__.py
+++ b/pygments/lexers/__init__.py
@@ -83,21 +83,39 @@ def get_lexer_by_name(_alias, **options):
raise ClassNotFound('no lexer for alias %r found' % _alias)
-def get_lexer_for_filename(_fn, **options):
+def get_lexer_for_filename(_fn, code=None, **options):
"""
- Get a lexer for a filename.
+ Get a lexer for a filename. If multiple lexers match the filename
+ pattern, use ``analyze_text()`` to figure out which one is more
+ appropriate.
"""
+ matches = []
fn = basename(_fn)
for modname, name, _, filenames, _ in LEXERS.itervalues():
for filename in filenames:
if fnmatch.fnmatch(fn, filename):
if name not in _lexer_cache:
_load_lexers(modname)
- return _lexer_cache[name](**options)
+ matches.append(_lexer_cache[name])
for cls in find_plugin_lexers():
for filename in cls.filenames:
if fnmatch.fnmatch(fn, filename):
- return cls(**options)
+ matches.append(cls)
+
+ def get_rating(cls):
+ # The class _always_ defines analyse_text because it's included in
+ # the Lexer class. The default implementation returns None which
+ # gets turned into 0.0. Run scripts/detect_missing_analyse_text.py
+ # to find lexers which need it overridden.
+ d = cls.analyse_text(code)
+ #print "Got %r from %r" % (d, cls)
+ return d
+
+ if code:
+ matches.sort(key=get_rating)
+ if matches:
+ #print "Possible lexers, after sort:", matches
+ return matches[-1](**options)
raise ClassNotFound('no lexer for filename %r found' % _fn)
diff --git a/tests/examplefiles/sample.m b/tests/examplefiles/matlab_sample
index 1834050c..1834050c 100644
--- a/tests/examplefiles/sample.m
+++ b/tests/examplefiles/matlab_sample