diff options
| author | Jean Abou Samra <jean@abou-samra.fr> | 2023-03-30 22:35:57 +0200 |
|---|---|---|
| committer | Jean Abou Samra <jean@abou-samra.fr> | 2023-03-30 22:35:57 +0200 |
| commit | a208ea553b17dc50fc2e3498244c74a75909888b (patch) | |
| tree | 036e0f6712d5d34975e3579dc498f7c198f460f7 /pygments/lexers | |
| parent | 242e56e9c0ab98f5e51a1eb765a79b84e44125ce (diff) | |
| download | pygments-git-a208ea553b17dc50fc2e3498244c74a75909888b.tar.gz | |
Use autodoc for pygments.lexers
Diffstat (limited to 'pygments/lexers')
| -rw-r--r-- | pygments/lexers/__init__.py | 67 |
1 files changed, 42 insertions, 25 deletions
diff --git a/pygments/lexers/__init__.py b/pygments/lexers/__init__.py index 35986b7d..efe99a0c 100644 --- a/pygments/lexers/__init__.py +++ b/pygments/lexers/__init__.py @@ -62,9 +62,9 @@ def get_all_lexers(plugins=True): def find_lexer_class(name): - """Lookup a lexer class by name. - - Return None if not found. + """ + Return the `Lexer` subclass that with the *name* attribute as given by + the *name* argument. """ if name in _lexer_cache: return _lexer_cache[name] @@ -80,10 +80,15 @@ def find_lexer_class(name): def find_lexer_class_by_name(_alias): - """Lookup a lexer class by alias. + """ + Return the `Lexer` subclass that has `alias` in its aliases list, without + instantiating it. Like `get_lexer_by_name`, but does not instantiate the class. + Will raise :exc:`pygments.util.ClassNotFound` if no lexer with that alias is + found. + .. versionadded:: 2.2 """ if not _alias: @@ -102,9 +107,13 @@ def find_lexer_class_by_name(_alias): def get_lexer_by_name(_alias, **options): - """Get a lexer by an alias. + """ + Return an instance of a `Lexer` subclass that has `alias` in its + aliases list. The lexer is given the `options` at its + instantiation. - Raises ClassNotFound if not found. + Will raise :exc:`pygments.util.ClassNotFound` if no lexer with that alias is + found. """ if not _alias: raise ClassNotFound('no lexer for alias %r found' % _alias) @@ -203,10 +212,15 @@ def find_lexer_class_for_filename(_fn, code=None): def get_lexer_for_filename(_fn, code=None, **options): """Get a lexer for a filename. - If multiple lexers match the filename pattern, use ``analyse_text()`` to - figure out which one is more appropriate. + Return a `Lexer` subclass instance that has a filename pattern + matching `fn`. The lexer is given the `options` at its + instantiation. - Raises ClassNotFound if not found. + Raise :exc:`pygments.util.ClassNotFound` if no lexer for that filename + is found. + + If multiple lexers match the filename pattern, use their ``analyse_text()`` + methods to figure out which one is more appropriate. """ res = find_lexer_class_for_filename(_fn, code) if not res: @@ -215,9 +229,12 @@ def get_lexer_for_filename(_fn, code=None, **options): def get_lexer_for_mimetype(_mime, **options): - """Get a lexer for a mimetype. + """ + Return a `Lexer` subclass instance that has `mime` in its mimetype + list. The lexer is given the `options` at its instantiation. - Raises ClassNotFound if not found. + Will raise :exc:`pygments.util.ClassNotFound` if not lexer for that mimetype + is found. """ for modname, name, _, _, mimetypes in LEXERS.values(): if _mime in mimetypes: @@ -243,19 +260,11 @@ def _iter_lexerclasses(plugins=True): 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 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> + As :func:`guess_lexer()`, but only lexers which have a pattern in `filenames` + or `alias_filenames` that matches `filename` are taken into consideration. + + :exc:`pygments.util.ClassNotFound` is raised if no lexer thinks it can + handle the content. """ fn = basename(_fn) primary = {} @@ -293,7 +302,15 @@ def guess_lexer_for_filename(_fn, _text, **options): def guess_lexer(_text, **options): - """Guess a lexer by strong distinctions in the text (eg, shebang).""" + """ + Return a `Lexer` subclass instance that's guessed from the text in + `text`. For that, the :meth:`.analyse_text()` method of every known lexer + class is called with the text as argument, and the lexer which returned the + highest value will be instantiated and returned. + + :exc:`pygments.util.ClassNotFound` is raised if no lexer thinks it can + handle the content. + """ if not isinstance(_text, str): inencoding = options.get('inencoding', options.get('encoding')) |
