Skip to content
Myr

jinjaext.py

This file

Path

docs/jinjaext.py

216 lines · 7.5 KiB · Python

Last changed by

Fixed a typoArmin Ronacherbefore adoption2bc1ef7

History of this file

Take it

The walk stopped after 200 commits: lines older than b217886 point at it.

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 1–26why, one step back
1# -*- coding: utf-8 -*-
2"""3 Jinja Documentation Extensions4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56 Support for automatically documenting filters and tests.78 :copyright: Copyright 2008 by Armin Ronacher.9 :license: BSD.10"""11import os12import re13import inspect14import jinja215from itertools import islice16from types import BuiltinFunctionType17from docutils import nodes18from docutils.statemachine import ViewList19from sphinx.ext.autodoc import prepare_docstring20from sphinx.application import TemplateBridge21from pygments.style import Style22from pygments.token import Keyword, Name, Comment, String, Error, \23 Number, Operator, Generic24from jinja2 import Environment, FileSystemLoader2526

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

lines 27–39why, one step back
27def parse_rst(state, content_offset, doc):
28 node = nodes.section()29 # hack around title style bookkeeping30 surrounding_title_styles = state.memo.title_styles31 surrounding_section_level = state.memo.section_level32 state.memo.title_styles = []33 state.memo.section_level = 034 state.nested_parse(doc, content_offset, node, match_titles=1)35 state.memo.title_styles = surrounding_title_styles36 state.memo.section_level = surrounding_section_level37 return node.children3839

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 40–50why, one step back
40class JinjaStyle(Style):
41 title = 'Jinja Style'42 default_style = ""43 styles = {44 Comment: 'italic #aaaaaa',45 Comment.Preproc: 'noitalic #B11414',46 Comment.Special: 'italic #505050',4748 Keyword: 'bold #B80000',49 Keyword.Type: '#808080',50

even more tests, fixed severe bug with autoescaping.Armin Ronacherbefore adoption5411ce7

line 51why, one step back
51 Operator.Word: 'bold #B80000',

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 52–148why, one step back
52
53 Name.Builtin: '#333333',54 Name.Function: '#333333',55 Name.Class: 'bold #333333',56 Name.Namespace: 'bold #333333',57 Name.Entity: 'bold #363636',58 Name.Attribute: '#686868',59 Name.Tag: 'bold #686868',60 Name.Decorator: '#686868',6162 String: '#AA891C',63 Number: '#444444',6465 Generic.Heading: 'bold #000080',66 Generic.Subheading: 'bold #800080',67 Generic.Deleted: '#aa0000',68 Generic.Inserted: '#00aa00',69 Generic.Error: '#aa0000',70 Generic.Emph: 'italic',71 Generic.Strong: 'bold',72 Generic.Prompt: '#555555',73 Generic.Output: '#888888',74 Generic.Traceback: '#aa0000',7576 Error: '#F00 bg:#FAA'77 }787980class Jinja2Bridge(TemplateBridge):8182 def init(self, builder):83 path = builder.config.templates_path84 self.env = Environment(loader=FileSystemLoader(path))8586 def render(self, template, context):87 return self.env.get_template(template).render(context)888990_sig_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*(\(.*?\))')919293def format_function(name, aliases, func):94 lines = inspect.getdoc(func).splitlines()95 signature = '()'96 if isinstance(func, BuiltinFunctionType):97 match = _sig_re.match(lines[0])98 if match is not None:99 del lines[:1 + bool(lines and not lines[0])]100 signature = match.group(1)101 else:102 try:103 argspec = inspect.getargspec(func)104 if getattr(func, 'environmentfilter', False) or \105 getattr(func, 'contextfilter', False):106 del argspec[0][0]107 signature = inspect.formatargspec(*argspec)108 except:109 pass110 result = ['.. function:: %s%s' % (name, signature), '']111 result.extend(' ' + line for line in lines)112 if aliases:113 result.extend(('', ' :aliases: %s' % ', '.join(114 '``%s``' % x for x in sorted(aliases))))115 return result116117118def dump_functions(mapping):119 def directive(dirname, arguments, options, content, lineno,120 content_offset, block_text, state, state_machine):121 reverse_mapping = {}122 for name, func in mapping.iteritems():123 reverse_mapping.setdefault(func, []).append(name)124 filters = []125 for func, names in reverse_mapping.iteritems():126 aliases = sorted(names, key=lambda x: len(x))127 name = aliases.pop()128 filters.append((name, aliases, func))129 filters.sort()130131 result = ViewList()132 for name, aliases, func in filters:133 for item in format_function(name, aliases, func):134 result.append(item, '<jinjaext>')135136 node = nodes.paragraph()137 state.nested_parse(result, content_offset, node)138 return node.children139 return directive140141142def jinja_changelog(dirname, arguments, options, content, lineno,143 content_offset, block_text, state, state_machine):144 doc = ViewList()145 changelog = file(os.path.join(os.path.dirname(jinja2.__file__), '..',146 'CHANGES'))147 try:148 for line in islice(changelog, 3, None):

Fixed a typoArmin Ronacherbefore adoption2bc1ef7

line 149why, one step back
149 doc.append(line.rstrip().decode('utf-8'), '<jinjaext>')

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 150–151why, one step back
150 finally:
151 changelog.close()

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

line 152why, one step back
152 return parse_rst(state, content_offset, doc)

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 153–159why, one step back
153
154155from jinja2.defaults import DEFAULT_FILTERS, DEFAULT_TESTS156jinja_filters = dump_functions(DEFAULT_FILTERS)157jinja_tests = dump_functions(DEFAULT_TESTS)158159

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

lines 160–170why, one step back
160def jinja_nodes(dirname, arguments, options, content, lineno,
161 content_offset, block_text, state, state_machine):162 from jinja2.nodes import Node163 doc = ViewList()164 def walk(node, indent):165 p = ' ' * indent166 sig = ', '.join(node.fields)167 doc.append(p + '.. autoclass:: %s(%s)' % (node.__name__, sig), '')168 if node.abstract:169 members = []170 for key, name in node.__dict__.iteritems():

improved variable exporting, added TODO fileArmin Ronacherbefore adoption69e12db

lines 171–172why, one step back
171 if not key.startswith('_') and \
172 not hasattr(node.__base__, key) and callable(name):

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

lines 173–176why, one step back
173 members.append(key)
174 if members:175 members.sort()176 doc.append('%s :members: %s' % (p, ', '.join(members)), '')

improved variable exporting, added TODO fileArmin Ronacherbefore adoption69e12db

line 177why, one step back
177 if node.__base__ != object:

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

line 178why, one step back
178 doc.append('', '')

improved variable exporting, added TODO fileArmin Ronacherbefore adoption69e12db

lines 179–180why, one step back
179 doc.append('%s :Node type: :class:`%s`' %
180 (p, node.__base__.__name__), '')

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

lines 181–189why, one step back
181 doc.append('', '')
182 children = node.__subclasses__()183 children.sort(key=lambda x: x.__name__.lower())184 for child in children:185 walk(child, indent)186 walk(Node, 0)187 return parse_rst(state, content_offset, doc)188189

updated documentationArmin Ronacherbefore adoption3ef2043

lines 190–209why, one step back
190def inject_toc(app, doctree, docname):
191 titleiter = iter(doctree.traverse(nodes.title))192 try:193 # skip first title, we are not interested in that one194 titleiter.next()195 title = titleiter.next()196 # and check if there is at least another title197 titleiter.next()198 except StopIteration:199 return200 tocnode = nodes.section('')201 tocnode['classes'].append('toc')202 toctitle = nodes.section('')203 toctitle['classes'].append('toctitle')204 toctitle.append(nodes.title(text='Table Of Contents'))205 tocnode.append(toctitle)206 tocnode += doctree.document.settings.env.get_toc_for(docname)[0][1]207 title.parent.insert(title.parent.children.index(title), tocnode)208209

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 210–213why, one step back
210def setup(app):
211 app.add_directive('jinjafilters', jinja_filters, 0, (0, 0, 0))212 app.add_directive('jinjatests', jinja_tests, 0, (0, 0, 0))213 app.add_directive('jinjachangelog', jinja_changelog, 0, (0, 0, 0))

First extension interface documentation and updates in that interfaceArmin Ronacherbefore adoption023b5e9

line 214why, one step back
214 app.add_directive('jinjanodes', jinja_nodes, 0, (0, 0, 0))

updated documentationArmin Ronacherbefore adoption3ef2043

lines 215–216why, one step back
215 # uncomment for inline toc. links are broken unfortunately
216 ##app.connect('doctree-resolved', inject_toc)