Back to index

moin  1.9.0~rc2
Namespaces | Classes | Functions | Variables
MoinMoin.support.pygments.lexers Namespace Reference

Namespaces

namespace  _clbuiltins
namespace  _luabuiltins
namespace  _mapping
namespace  _phpbuiltins
namespace  _vimbuiltins
namespace  agile
namespace  asm
namespace  compiled
namespace  dotnet
namespace  functional
namespace  math
namespace  other
namespace  parsers
namespace  special
namespace  templates
namespace  text
namespace  web

Classes

class  _automodule

Functions

def _load_lexers
def get_all_lexers
def find_lexer_class
def get_lexer_by_name
def get_lexer_for_filename
def get_lexer_for_mimetype
def _iter_lexerclasses
def guess_lexer_for_filename
def guess_lexer

Variables

list __all__
dictionary _lexer_cache = {}
list oldmod = sys.modules['pygments.lexers']
tuple newmod = _automodule('pygments.lexers')

Function Documentation

Return an iterator over all lexer classes.

Definition at line 142 of file __init__.py.

00142 
00143 def _iter_lexerclasses():
00144     """
00145     Return an iterator over all lexer classes.
00146     """
00147     for module_name, name, _, _, _ in LEXERS.itervalues():
00148         if name not in _lexer_cache:
00149             _load_lexers(module_name)
00150         yield _lexer_cache[name]
00151     for lexer in find_plugin_lexers():
00152         yield lexer
00153 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.pygments.lexers._load_lexers (   module_name) [private]
Load a lexer (and all others in the module too).

Definition at line 32 of file __init__.py.

00032 
00033 def _load_lexers(module_name):
00034     """
00035     Load a lexer (and all others in the module too).
00036     """
00037     mod = __import__(module_name, None, None, ['__all__'])
00038     for lexer_name in mod.__all__:
00039         cls = getattr(mod, lexer_name)
00040         _lexer_cache[cls.name] = cls
00041 

Here is the caller graph for this function:

Lookup a lexer class by name. Return None if not found.

Definition at line 53 of file __init__.py.

00053 
00054 def find_lexer_class(name):
00055     """
00056     Lookup a lexer class by name. Return None if not found.
00057     """
00058     if name in _lexer_cache:
00059         return _lexer_cache[name]
00060     # lookup builtin lexers
00061     for module_name, lname, aliases, _, _ in LEXERS.itervalues():
00062         if name == lname:
00063             _load_lexers(module_name)
00064             return _lexer_cache[name]
00065     # continue with lexers from setuptools entrypoints
00066     for cls in find_plugin_lexers():
00067         if cls.name == name:
00068             return cls
00069 

Here is the call graph for this function:

Here is the caller graph for this function:

Return a generator of tuples in the form ``(name, aliases,
filenames, mimetypes)`` of all know lexers.

Definition at line 42 of file __init__.py.

00042 
00043 def get_all_lexers():
00044     """
00045     Return a generator of tuples in the form ``(name, aliases,
00046     filenames, mimetypes)`` of all know lexers.
00047     """
00048     for item in LEXERS.itervalues():
00049         yield item[1:]
00050     for lexer in find_plugin_lexers():
00051         yield lexer.name, lexer.aliases, lexer.filenames, lexer.mimetypes
00052 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.pygments.lexers.get_lexer_by_name (   _alias,
  options 
)
Get a lexer by an alias.

Definition at line 70 of file __init__.py.

00070 
00071 def get_lexer_by_name(_alias, **options):
00072     """
00073     Get a lexer by an alias.
00074     """
00075     # lookup builtin lexers
00076     for module_name, name, aliases, _, _ in LEXERS.itervalues():
00077         if _alias in aliases:
00078             if name not in _lexer_cache:
00079                 _load_lexers(module_name)
00080             return _lexer_cache[name](**options)
00081     # continue with lexers from setuptools entrypoints
00082     for cls in find_plugin_lexers():
00083         if _alias in cls.aliases:
00084             return cls(**options)
00085     raise ClassNotFound('no lexer for alias %r found' % _alias)
00086 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.pygments.lexers.get_lexer_for_filename (   _fn,
  code = None,
  options 
)
Get a lexer for a filename.  If multiple lexers match the filename
pattern, use ``analyze_text()`` to figure out which one is more
appropriate.

Definition at line 87 of file __init__.py.

00087 
00088 def get_lexer_for_filename(_fn, code=None, **options):
00089     """
00090     Get a lexer for a filename.  If multiple lexers match the filename
00091     pattern, use ``analyze_text()`` to figure out which one is more
00092     appropriate.
00093     """
00094     matches = []
00095     fn = basename(_fn)
00096     for modname, name, _, filenames, _ in LEXERS.itervalues():
00097         for filename in filenames:
00098             if fnmatch.fnmatch(fn, filename):
00099                 if name not in _lexer_cache:
00100                     _load_lexers(modname)
00101                 matches.append(_lexer_cache[name])
00102     for cls in find_plugin_lexers():
00103         for filename in cls.filenames:
00104             if fnmatch.fnmatch(fn, filename):
00105                 matches.append(cls)
00106 
00107     if sys.version_info > (3,) and isinstance(code, bytes):
00108         # decode it, since all analyse_text functions expect unicode
00109         code = code.decode('latin1')
00110 
00111     def get_rating(cls):
00112         # The class _always_ defines analyse_text because it's included in
00113         # the Lexer class.  The default implementation returns None which
00114         # gets turned into 0.0.  Run scripts/detect_missing_analyse_text.py
00115         # to find lexers which need it overridden.
00116         d = cls.analyse_text(code)
00117         #print "Got %r from %r" % (d, cls)
00118         return d
00119 
00120     if code:
00121         matches.sort(key=get_rating)
00122     if matches:
00123         #print "Possible lexers, after sort:", matches
00124         return matches[-1](**options)
00125     raise ClassNotFound('no lexer for filename %r found' % _fn)
00126 

Here is the call graph for this function:

Here is the caller graph for this function:

Get a lexer for a mimetype.

Definition at line 127 of file __init__.py.

00127 
00128 def get_lexer_for_mimetype(_mime, **options):
00129     """
00130     Get a lexer for a mimetype.
00131     """
00132     for modname, name, _, _, mimetypes in LEXERS.itervalues():
00133         if _mime in mimetypes:
00134             if name not in _lexer_cache:
00135                 _load_lexers(modname)
00136             return _lexer_cache[name](**options)
00137     for cls in find_plugin_lexers():
00138         if _mime in cls.mimetypes:
00139             return cls(**options)
00140     raise ClassNotFound('no lexer for mimetype %r found' % _mime)
00141 

Here is the call graph for this function:

def MoinMoin.support.pygments.lexers.guess_lexer (   _text,
  options 
)
Guess a lexer by strong distinctions in the text (eg, shebang).

Definition at line 197 of file __init__.py.

00197 
00198 def guess_lexer(_text, **options):
00199     """
00200     Guess a lexer by strong distinctions in the text (eg, shebang).
00201     """
00202     best_lexer = [0.0, None]
00203     for lexer in _iter_lexerclasses():
00204         rv = lexer.analyse_text(_text)
00205         if rv == 1.0:
00206             return lexer(**options)
00207         if rv > best_lexer[0]:
00208             best_lexer[:] = (rv, lexer)
00209     if not best_lexer[0] or best_lexer[1] is None:
00210         raise ClassNotFound('no lexer matching the text found')
00211     return best_lexer[1](**options)
00212 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.pygments.lexers.guess_lexer_for_filename (   _fn,
  _text,
  options 
)
Lookup all lexers that handle those filenames primary (``filenames``)
or secondary (``alias_filenames``). Then run a text analysis for those
lexers and choose the best result.

usage::

    >>> from pygments.lexers import guess_lexer_for_filename
    >>> guess_lexer_for_filename('hello.html', '<%= @foo %>')
    <pygments.lexers.templates.RhtmlLexer object at 0xb7d2f32c>
    >>> guess_lexer_for_filename('hello.html', '<h1>{{ title|e }}</h1>')
    <pygments.lexers.templates.HtmlDjangoLexer object at 0xb7d2f2ac>
    >>> guess_lexer_for_filename('style.css', 'a { color: <?= $link ?> }')
    <pygments.lexers.templates.CssPhpLexer object at 0xb7ba518c>

Definition at line 154 of file __init__.py.

00154 
00155 def guess_lexer_for_filename(_fn, _text, **options):
00156     """
00157     Lookup all lexers that handle those filenames primary (``filenames``)
00158     or secondary (``alias_filenames``). Then run a text analysis for those
00159     lexers and choose the best result.
00160 
00161     usage::
00162 
00163         >>> from pygments.lexers import guess_lexer_for_filename
00164         >>> guess_lexer_for_filename('hello.html', '<%= @foo %>')
00165         <pygments.lexers.templates.RhtmlLexer object at 0xb7d2f32c>
00166         >>> guess_lexer_for_filename('hello.html', '<h1>{{ title|e }}</h1>')
00167         <pygments.lexers.templates.HtmlDjangoLexer object at 0xb7d2f2ac>
00168         >>> guess_lexer_for_filename('style.css', 'a { color: <?= $link ?> }')
00169         <pygments.lexers.templates.CssPhpLexer object at 0xb7ba518c>
00170     """
00171     fn = basename(_fn)
00172     primary = None
00173     matching_lexers = set()
00174     for lexer in _iter_lexerclasses():
00175         for filename in lexer.filenames:
00176             if fnmatch.fnmatch(fn, filename):
00177                 matching_lexers.add(lexer)
00178                 primary = lexer
00179         for filename in lexer.alias_filenames:
00180             if fnmatch.fnmatch(fn, filename):
00181                 matching_lexers.add(lexer)
00182     if not matching_lexers:
00183         raise ClassNotFound('no lexer for filename %r found' % fn)
00184     if len(matching_lexers) == 1:
00185         return matching_lexers.pop()(**options)
00186     result = []
00187     for lexer in matching_lexers:
00188         rv = lexer.analyse_text(_text)
00189         if rv == 1.0:
00190             return lexer(**options)
00191         result.append((rv, lexer))
00192     result.sort()
00193     if not result[-1][0] and primary is not None:
00194         return primary(**options)
00195     return result[-1][1](**options)
00196 

Here is the call graph for this function:


Variable Documentation

Initial value:
00001 ['get_lexer_by_name', 'get_lexer_for_filename', 'find_lexer_class',
00002            'guess_lexer']

Definition at line 26 of file __init__.py.

Definition at line 29 of file __init__.py.

Definition at line 228 of file __init__.py.

list MoinMoin.support.pygments.lexers.oldmod = sys.modules['pygments.lexers']

Definition at line 227 of file __init__.py.