Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eeb494c

Browse files
committed
Switched templating to jinja2
1 parent d02008f commit eeb494c

File tree

6 files changed

+99
-11
lines changed

6 files changed

+99
-11
lines changed

babel.cfg

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
[extractors]
2-
jinja = sphinx._jinja.babel_extract
31
[python: **.py]
4-
[jinja: **/templates/**.html]
5-
[jinja: **/templates/**.xml]
2+
[jinja2: **/templates/**.html]
3+
[jinja2: **/templates/**.xml]
64
[javascript: **.js]

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- coding: utf-8 -*-
1+
0;115;0c# -*- coding: utf-8 -*-
22
import ez_setup
33
ez_setup.use_setuptools()
44

@@ -36,7 +36,7 @@
3636
and inclusion of appropriately formatted docstrings.
3737
'''
3838

39-
requires = ['Pygments>=0.8', 'Jinja>=1.1', 'docutils>=0.4']
39+
requires = ['Pygments>=0.8', 'Jinja2>=2.0', 'docutils>=0.4']
4040

4141
if sys.version_info < (2, 4):
4242
print 'ERROR: Sphinx requires at least Python 2.4 to run.'

sphinx/_jinja2.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
sphinx._jinja2
5+
==============
6+
7+
Glue code for jinja2.
8+
9+
:author: Sebastian Wiesner
10+
11+
:copyright: 2008 by Sebastian Wiesner
12+
:license: MIT
13+
"""
14+
15+
import codecs
16+
from os import path
17+
18+
import jinja2
19+
20+
from sphinx.util import mtimes_of_files
21+
from sphinx.application import TemplateBridge
22+
23+
24+
class SphinxLoader(jinja2.BaseLoader):
25+
"""
26+
A jinja2 reimplementation of `sphinx._jinja.SphinxFileSystemLoader`.
27+
"""
28+
29+
def __init__(self, basepath, extpaths, encoding='utf-8'):
30+
"""
31+
Creates a new loader for sphinx.
32+
33+
``extpaths`` is a list of directories, which provide additional
34+
templates to sphinx.
35+
36+
``encoding`` is used to decode the templates into unicode strings.
37+
Defaults to utf-8.
38+
39+
If ``basepath`` is set, this path is used to load sphinx core
40+
templates. If False, these templates are loaded from the sphinx
41+
package.
42+
"""
43+
self.core_loader = jinja2.FileSystemLoader(basepath)
44+
self.all_loaders = jinja2.ChoiceLoader(
45+
[jinja2.FileSystemLoader(extpath) for extpath in extpaths] +
46+
[self.core_loader])
47+
48+
def get_source(self, environment, template):
49+
# exclamation mark forces loading from core
50+
if template.startswith('!'):
51+
return self.core_loader.get_source(environment, template[1:])
52+
# check if the template is probably an absolute path
53+
fs_path = template.replace('/', path.sep)
54+
if path.isabs(fs_path):
55+
if not path.exists(fs_path):
56+
raise jinja2.TemplateNotFound(template)
57+
f = codecs.open(fs_path, 'r', self.encoding)
58+
try:
59+
mtime = path.getmtime(path)
60+
return (f.read(), fs_path,
61+
lambda: mtime == path.getmtime(path))
62+
finally:
63+
f.close()
64+
# finally try to load from custom templates
65+
return self.all_loaders.get_source(environment, template)
66+
67+
68+
class BuiltinTemplates(TemplateBridge):
69+
"""
70+
Interfaces the rendering environment of jinja2 for use in sphinx.
71+
"""
72+
73+
def init(self, builder):
74+
base_templates_path = path.join(path.dirname(__file__), 'templates')
75+
ext_templates_path = [path.join(builder.confdir, dir)
76+
for dir in builder.config.templates_path]
77+
self.templates_path = [base_templates_path] + ext_templates_path
78+
loader = SphinxLoader(base_templates_path, ext_templates_path)
79+
use_i18n = builder.translator is not None
80+
extensions = use_i18n and ['jinja2.ext.i18n'] or []
81+
self.environment = jinja2.Environment(loader=loader,
82+
extensions=extensions)
83+
if use_i18n:
84+
self.environment.install_gettext_translations(builder.translator)
85+
86+
def render(self, template, context):
87+
return self.environment.get_template(template).render(context)
88+
89+
def newest_template_mtime(self):
90+
return max(mtimes_of_files(self.templates_path, '.html'))

sphinx/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def init_templates(self):
9898
self.templates = self.app.import_object(
9999
self.config.template_bridge, 'template_bridge setting')()
100100
else:
101-
from sphinx._jinja import BuiltinTemplates
101+
from sphinx._jinja2 import BuiltinTemplates
102102
self.templates = BuiltinTemplates()
103103
self.templates.init(self)
104104

sphinx/templates/layout.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{%- endblock %}
55
{%- set reldelim1 = reldelim1 is not defined and ' &raquo;' or reldelim1 %}
66
{%- set reldelim2 = reldelim2 is not defined and ' |' or reldelim2 %}
7-
{%- macro relbar %}
7+
{%- macro relbar() %}
88
<div class="related">
99
<h3>{{ _('Navigation') }}</h3>
1010
<ul>
@@ -24,7 +24,7 @@ <h3>{{ _('Navigation') }}</h3>
2424
</ul>
2525
</div>
2626
{%- endmacro %}
27-
{%- macro sidebar %}
27+
{%- macro sidebar() %}
2828
{%- if builder != 'htmlhelp' %}
2929
<div class="sphinxsidebar">
3030
<div class="sphinxsidebarwrapper">
@@ -64,7 +64,7 @@ <h3>{{ _('This Page') }}</h3>
6464
</ul>
6565
{%- endif %}
6666
{%- if customsidebar %}
67-
{{ rendertemplate(customsidebar) }}
67+
{% include customsidebar %}
6868
{%- endif %}
6969
{%- block sidebarsearch %}
7070
{%- if pagename != "search" %}

sphinx/templates/modindex.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h1 id="global-module-index">{{ _('Global Module Index') }}</h1>
5252
{% if fname %}<a href="{{ fname }}">{% endif -%}
5353
<tt class="xref">{{ modname|e }}</tt>
5454
{%- if fname %}</a>{% endif %}
55-
{%- if pform[0] %} <em>({{ pform|join(', ') }})</em>{% endif -%}
55+
{%- if pform and pform[0] %} <em>({{ pform|join(', ') }})</em>{% endif -%}
5656
</td><td>{% if dep %}<strong>{{ _('Deprecated')}}:</strong>{% endif %}
5757
<em>{{ synops|e }}</em></td></tr>
5858
{%- endif -%}

0 commit comments

Comments
 (0)