Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 123 lines (99 sloc) 4.112 kb
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
1 # -*- coding: utf-8 -*-
2 """
3 Jinja Documentation Extensions
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 Support for automatically documenting filters and tests.
7
8 :copyright: Copyright 2008 by Armin Ronacher.
9 :license: BSD.
10 """
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
11 import re
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
12 import inspect
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
13 from types import BuiltinFunctionType
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
14 from docutils import nodes
15 from docutils.statemachine import ViewList
16 from sphinx.ext.autodoc import prepare_docstring
17
18
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
19 from pygments.style import Style
20 from pygments.token import Keyword, Name, Comment, String, Error, \
21 Number, Operator, Generic
22
23
24 class JinjaStyle(Style):
25 title = 'Jinja Style'
26 default_style = ""
27 styles = {
28 Comment: 'italic #aaaaaa',
29 Comment.Preproc: 'noitalic #B11414',
30 Comment.Special: 'italic #505050',
31
32 Keyword: 'bold #B80000',
33 Keyword.Type: '#808080',
34
35 Operator.Word: '#333333',
36
37 Name.Builtin: '#333333',
38 Name.Function: '#333333',
39 Name.Class: 'bold #333333',
40 Name.Namespace: 'bold #333333',
41 Name.Entity: 'bold #363636',
42 Name.Attribute: '#686868',
43 Name.Tag: 'bold #686868',
44 Name.Decorator: '#686868',
45
46 String: '#BE9B5D',
b2a36aa @mitsuhiko updated jinja docs
authored
47 Number: '#444444',
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
48
49 Generic.Heading: 'bold #000080',
50 Generic.Subheading: 'bold #800080',
51 Generic.Deleted: '#aa0000',
52 Generic.Inserted: '#00aa00',
53 Generic.Error: '#aa0000',
54 Generic.Emph: 'italic',
55 Generic.Strong: 'bold',
56 Generic.Prompt: '#555555',
57 Generic.Output: '#888888',
58 Generic.Traceback: '#aa0000',
59
60 Error: '#F00 bg:#FAA'
61 }
62
63 _sig_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*(\(.*?\))')
64
65
66 def format_function(name, aliases, func):
67 lines = inspect.getdoc(func).splitlines()
68 signature = '()'
69 if isinstance(func, BuiltinFunctionType):
70 match = _sig_re.match(lines[0])
71 if match is not None:
72 del lines[:1 + bool(lines and not lines[0])]
73 signature = match.group(1)
74 else:
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
75 try:
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
76 argspec = inspect.getargspec(func)
77 if getattr(func, 'environmentfilter', False) or \
78 getattr(func, 'contextfilter', False):
79 del argspec[0][0]
80 signature = inspect.formatargspec(*argspec)
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
81 except:
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
82 pass
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
83 result = ['.. function:: %s%s' % (name, signature), '']
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
84 result.extend(' ' + line for line in lines)
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
85 if aliases:
86 result.extend(('', ' :aliases: %s' % ', '.join(
87 '``%s``' % x for x in sorted(aliases))))
88 return result
89
90
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
91 def dump_functions(mapping):
92 def directive(dirname, arguments, options, content, lineno,
93 content_offset, block_text, state, state_machine):
94 reverse_mapping = {}
95 for name, func in mapping.iteritems():
96 reverse_mapping.setdefault(func, []).append(name)
97 filters = []
98 for func, names in reverse_mapping.iteritems():
99 aliases = sorted(names, key=lambda x: len(x))
100 name = aliases.pop()
101 filters.append((name, aliases, func))
102 filters.sort()
103
104 result = ViewList()
105 for name, aliases, func in filters:
106 for item in format_function(name, aliases, func):
107 result.append(item, '<jinjaext>')
108
109 node = nodes.paragraph()
110 state.nested_parse(result, content_offset, node)
111 return node.children
112 return directive
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
113
114
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
115 from jinja2.defaults import DEFAULT_FILTERS, DEFAULT_TESTS
116 jinja_filters = dump_functions(DEFAULT_FILTERS)
117 jinja_tests = dump_functions(DEFAULT_TESTS)
3c8b7ad @mitsuhiko first version of the jinja2 docs
authored
118
119
120 def setup(app):
157531b @mitsuhiko more docs and fixed bug in parser that assigned lineno for ExprStmt w…
authored
121 app.add_directive('jinjafilters', jinja_filters, 0, (0, 0, 0))
122 app.add_directive('jinjatests', jinja_tests, 0, (0, 0, 0))
Something went wrong with that request. Please try again.