def get_formatter_for_filename(fn, **options): _init_formatter_cache() fn = os.path.basename(fn) for pattern, cls in _formatter_filename_cache: if fnmatch.fnmatch(fn, pattern): return cls(**options) raise ClassNotFound("No formatter found for file name %r" % fn)
def get_filter_by_name(filtername, **options): """ Return an instantiated filter. Options are passed to the filter initializer if wanted. Raise a ClassNotFound if not found. """ cls = find_filter_class(filtername) if cls: return cls(**options) else: raise ClassNotFound('filter %r not found' % filtername)
def get_lexer_for_mimetype(_mime, **options): """ Get a lexer for a mimetype. """ for modname, name, _, _, mimetypes in LEXERS.values(): if _mime in mimetypes: if name not in _lexer_cache: _load_lexers(modname) return _lexer_cache[name](**options) for cls in find_plugin_lexers(): if _mime in cls.mimetypes: return cls(**options) raise ClassNotFound('no lexer for mimetype %r found' % _mime)
def guess_lexer(_text, **options): """ Guess a lexer by strong distinctions in the text (eg, shebang). """ best_lexer = [0.0, None] for lexer in _iter_lexerclasses(): rv = lexer.analyse_text(_text) if rv == 1.0: return lexer(**options) if rv > best_lexer[0]: best_lexer[:] = (rv, lexer) if not best_lexer[0] or best_lexer[1] is None: raise ClassNotFound('no lexer matching the text found') return best_lexer[1](**options)
def get_style_by_name(name): if name in STYLE_MAP: mod, cls = STYLE_MAP[name].split('::') builtin = "yes" else: for found_name, style in find_plugin_styles(): if name == found_name: return style # perhaps it got dropped into our styles package builtin = "" mod = name cls = name.title() + "Style" try: mod = __import__('pygments3.styles.' + mod, None, None, [cls]) except ImportError: raise ClassNotFound("Could not find style module %r" % mod + (builtin and ", though it should be builtin") + ".") try: return getattr(mod, cls) except AttributeError: raise ClassNotFound("Could not find style class %r in style module." % cls)
def get_lexer_by_name(_alias, **options): """ Get a lexer by an alias. """ # lookup builtin lexers for module_name, name, aliases, _, _ in LEXERS.values(): if _alias in aliases: if name not in _lexer_cache: _load_lexers(module_name) return _lexer_cache[name](**options) # continue with lexers from setuptools entrypoints for cls in find_plugin_lexers(): if _alias in cls.aliases: return cls(**options) raise ClassNotFound('no lexer for alias %r found' % _alias)
def 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 pygments3.lexers import guess_lexer_for_filename >>> guess_lexer_for_filename('hello.html', '<%= @foo %>') <pygments3.lexers.templates.RhtmlLexer object at 0xb7d2f32c> >>> guess_lexer_for_filename('hello.html', '<h1>{{ title|e }}</h1>') <pygments3.lexers.templates.HtmlDjangoLexer object at 0xb7d2f2ac> >>> guess_lexer_for_filename('style.css', 'a { color: <?= $link ?> }') <pygments3.lexers.templates.CssPhpLexer object at 0xb7ba518c> """ fn = basename(_fn) primary = None matching_lexers = set() for lexer in _iter_lexerclasses(): for filename in lexer.filenames: if fnmatch.fnmatch(fn, filename): matching_lexers.add(lexer) primary = lexer for filename in lexer.alias_filenames: if fnmatch.fnmatch(fn, filename): matching_lexers.add(lexer) if not matching_lexers: raise ClassNotFound('no lexer for filename %r found' % fn) if len(matching_lexers) == 1: return matching_lexers.pop()(**options) result = [] for lexer in matching_lexers: rv = lexer.analyse_text(_text) if rv == 1.0: return lexer(**options) result.append((rv, lexer)) result.sort() if not result[-1][0] and primary is not None: return primary(**options) return result[-1][1](**options)
def 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. """ matches = [] fn = basename(_fn) for modname, name, _, filenames, _ in LEXERS.values(): for filename in filenames: if fnmatch.fnmatch(fn, filename): if name not in _lexer_cache: _load_lexers(modname) matches.append(_lexer_cache[name]) for cls in find_plugin_lexers(): for filename in cls.filenames: if fnmatch.fnmatch(fn, filename): matches.append(cls) if sys.version_info > (3, ) and isinstance(code, bytes): # decode it, since all analyse_text functions expect unicode code = code.decode('latin1') 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)
def get_formatter_by_name(name, **options): _init_formatter_cache() cls = _formatter_alias_cache.get(name, None) if not cls: raise ClassNotFound("No formatter found for name %r" % name) return cls(**options)