1
# -*- coding: utf-8 -*-
8
:copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
:license: BSD, see LICENSE for details.
14
from pygments.formatters._mapping import FORMATTERS
15
from pygments.plugin import find_plugin_formatters
16
from pygments.util import docstring_headline, ClassNotFound
19
for fcls in FORMATTERS:
20
ns[fcls.__name__] = fcls
23
__all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
24
'get_all_formatters'] + [cls.__name__ for cls in FORMATTERS]
27
_formatter_alias_cache = {}
28
_formatter_filename_cache = []
30
def _init_formatter_cache():
31
if _formatter_alias_cache:
33
for cls in get_all_formatters():
34
for alias in cls.aliases:
35
_formatter_alias_cache[alias] = cls
36
for fn in cls.filenames:
37
_formatter_filename_cache.append((fn, cls))
40
def find_formatter_class(name):
41
_init_formatter_cache()
42
cls = _formatter_alias_cache.get(name, None)
46
def get_formatter_by_name(name, **options):
47
_init_formatter_cache()
48
cls = _formatter_alias_cache.get(name, None)
50
raise ClassNotFound("No formatter found for name %r" % name)
54
def get_formatter_for_filename(fn, **options):
55
_init_formatter_cache()
56
fn = os.path.basename(fn)
57
for pattern, cls in _formatter_filename_cache:
58
if fnmatch.fnmatch(fn, pattern):
60
raise ClassNotFound("No formatter found for file name %r" % fn)
63
def get_all_formatters():
64
"""Return a generator for all formatters."""
65
for formatter in FORMATTERS:
67
for _, formatter in find_plugin_formatters():