diff options
-rw-r--r-- | docs/src/formatters.txt | 18 | ||||
-rw-r--r-- | docs/src/lexers.txt | 24 | ||||
-rw-r--r-- | docs/src/styles.txt | 14 | ||||
-rw-r--r-- | pygments/formatter.py | 3 | ||||
-rw-r--r-- | pygments/formatters/__init__.py | 8 | ||||
-rw-r--r-- | pygments/formatters/html.py | 15 | ||||
-rw-r--r-- | pygments/lexers/agile.py | 2 | ||||
-rw-r--r-- | pygments/styles/__init__.py | 9 |
8 files changed, 87 insertions, 6 deletions
diff --git a/docs/src/formatters.txt b/docs/src/formatters.txt index 7cd8c012..062a59c7 100644 --- a/docs/src/formatters.txt +++ b/docs/src/formatters.txt @@ -96,6 +96,23 @@ All these classes are importable from `pygments.formatters`. td .code .cm { color: #999999 } ... + If you have pygments > 0.5.1 you can also pass a list of tuple to the + `get_style_defs` method to request multiple prefixes for the tokens: + + .. sourcecode:: python + + formatter.get_style_defs(['div.syntax pre', 'pre.syntax']) + + The output would then look like this: + + .. sourcecode:: css + + div.syntax pre .kw, + pre.syntax .kw { font-weight: bold; color: #00FF00 } + div.syntax pre .cm, + pre.syntax .cm { color: #999999 } + ... + Additional options accepted by the `HtmlFormatter`: `nowrap` @@ -257,4 +274,3 @@ All these classes are importable from `pygments.formatters`. :Aliases: ``text``, ``null`` :Filename pattern: ``*.txt`` - diff --git a/docs/src/lexers.txt b/docs/src/lexers.txt index ab0af16b..a39e7749 100644 --- a/docs/src/lexers.txt +++ b/docs/src/lexers.txt @@ -574,3 +574,27 @@ Text lexers :Aliases: ``tex``, ``latex`` :Filename patterns: ``*.tex``, ``*.aux``, ``*.toc`` + + +Iterating over all Lexers +========================= + +*requires pygments > 0.5.1* + +To get all lexers (both the builtin and the plugin ones) you can +use the `get_all_lexers` function from the `pygments.lexers` +module: + +.. sourcecode:: pycon + + >>> from pygments.lexers import get_all_lexers + >>> i = get_all_lexers() + >>> i.next() + ('Diff', ('diff',), ('*.diff', '*.patch'), ('text/x-diff', 'text/x-patch')) + >>> i.next() + ('Delphi', ('delphi', 'objectpascal', 'pas', 'pascal'), ('*.pas',), ('text/x-pascal',)) + >>> i.next() + ('XML+Ruby', ('xml+erb', 'xml+ruby'), (), ()) + +As you can see the return value is an iterator which yields tuples +in the form ``(name, aliases, filetypes, mimetypes)`` diff --git a/docs/src/styles.txt b/docs/src/styles.txt index 4fd6c297..cda19155 100644 --- a/docs/src/styles.txt +++ b/docs/src/styles.txt @@ -117,3 +117,17 @@ To get a list of known styles you can use this snippet: >>> from pygments.styles import STYLE_MAP >>> STYLE_MAP.keys() ['default', 'emacs', 'friendly', 'colorful'] + + +Getting a list of available Styles +================================== + +*requires pygments > 0.5.1* + +Because it could be that a plugin registered a style there is +a way to iterate over all styles: + +.. sourcecode:: pycon + + >>> from pygments.styles import get_all_styles + >>> styles = list(get_all_styles()) diff --git a/pygments/formatter.py b/pygments/formatter.py index fe7ccc97..47477f34 100644 --- a/pygments/formatter.py +++ b/pygments/formatter.py @@ -56,7 +56,8 @@ class Formatter(object): Return the style definitions for the current style as a string. ``arg`` is an additional argument whose meaning depends on the - formatter used. + formatter used. Note that ``arg`` can also be a list or tuple + for some formatters like the html formatter. """ return '' diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py index 74557440..ecbc7b41 100644 --- a/pygments/formatters/__init__.py +++ b/pygments/formatters/__init__.py @@ -77,3 +77,11 @@ def get_formatter_for_filename(fn, **options): if not cls: raise ValueError("No formatter found for file name %r" % fn) return cls(**options) + + +def get_all_formatters(): + """Return a generator for all formatters.""" + for formatter in FORMATTERS: + yield formatter + for _, formatter in find_plugin_formatters(): + yield formatter diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py index 4df6c732..f342bf6a 100644 --- a/pygments/formatters/html.py +++ b/pygments/formatters/html.py @@ -165,13 +165,22 @@ class HtmlFormatter(Formatter): current highlighting style. ``arg`` can be a string of selectors to insert before the token type classes. """ - if arg: - arg += ' ' + if isinstance(arg, basestring): + args = [arg] + else: + args = list(arg) + + def prefix(cls): + tmp = [] + for arg in args: + tmp.append((arg and arg + ' ' or '') + '.' + cls) + return ', '.join(tmp) + styles = [(level, ttype, cls, style) for cls, (style, ttype, level) in self.class2style.iteritems() if cls and style] styles.sort() - lines = ['%s.%s { %s } /* %s */' % (arg, cls, style, repr(ttype)[6:]) + lines = ['%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:]) for level, ttype, cls, style in styles] if arg and not self.nobackground and \ self.style.background_color is not None: diff --git a/pygments/lexers/agile.py b/pygments/lexers/agile.py index b7fb3189..ab219aab 100644 --- a/pygments/lexers/agile.py +++ b/pygments/lexers/agile.py @@ -48,7 +48,7 @@ class PythonLexer(RegexLexer): (r'!=|==|<<|>>|[-+/*%=<>&^|]', Operator), (r'(assert|break|continue|del|elif|else|except|exec|' r'finally|for|global|if|lambda|pass|print|raise|' - r'return|try|while|yield)\b', Keyword), + r'return|try|while|yield|as|with)\b', Keyword), (r'(def)(\s+)', bygroups(Keyword, Text), 'funcname'), (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'), (r'(from)(\s+)', bygroups(Keyword, Text), 'fromimport'), diff --git a/pygments/styles/__init__.py b/pygments/styles/__init__.py index c92456d5..a8a502eb 100644 --- a/pygments/styles/__init__.py +++ b/pygments/styles/__init__.py @@ -38,3 +38,12 @@ def get_style_by_name(name): mod, cls = STYLE_MAP[name].split('::') mod = __import__('pygments.styles.' + mod, None, None, [cls]) return getattr(mod, cls) + + +def get_all_styles(): + """Return an generator for all styles by name. + Both builtin and plugin.""" + for name in STYLE_MAP: + yield name + for name, _ in find_plugin_styles(): + yield name |