summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-12-28 23:23:18 +0100
committerGeorg Brandl <georg@python.org>2008-12-28 23:23:18 +0100
commit577a475e8cbb39e4b8ecf396e7507f18df91cff4 (patch)
tree8c52e5081ad821578e82bba18d129c3bb6f51ec2
parentaa99b75cdaacfe265bc434670f19ea7c8b79f1a6 (diff)
parent8b4cc586c71986158f0170ab342b6076e8bbeaf7 (diff)
downloadsphinx-577a475e8cbb39e4b8ecf396e7507f18df91cff4.tar.gz
merge in http://www.bitbucket.org/lunar/sphinx-jinja2/
-rw-r--r--babel.cfg6
-rw-r--r--setup.py2
-rw-r--r--sphinx/_jinja2.py90
-rw-r--r--sphinx/builders/__init__.py2
-rw-r--r--sphinx/templates/layout.html10
-rw-r--r--sphinx/templates/modindex.html2
6 files changed, 100 insertions, 12 deletions
diff --git a/babel.cfg b/babel.cfg
index 5f5188b1..e53a462d 100644
--- a/babel.cfg
+++ b/babel.cfg
@@ -1,6 +1,4 @@
-[extractors]
-jinja = sphinx._jinja.babel_extract
[python: **.py]
-[jinja: **/templates/**.html]
-[jinja: **/templates/**.xml]
+[jinja2: **/templates/**.html]
+[jinja2: **/templates/**.xml]
[javascript: **.js]
diff --git a/setup.py b/setup.py
index e747e1b1..37cdd1e8 100644
--- a/setup.py
+++ b/setup.py
@@ -36,7 +36,7 @@ are already present, work fine and can be seen "in action" in the Python docs:
and inclusion of appropriately formatted docstrings.
'''
-requires = ['Pygments>=0.8', 'Jinja>=1.1', 'docutils>=0.4']
+requires = ['Pygments>=0.8', 'Jinja2>=2.0', 'docutils>=0.4']
if sys.version_info < (2, 4):
print 'ERROR: Sphinx requires at least Python 2.4 to run.'
diff --git a/sphinx/_jinja2.py b/sphinx/_jinja2.py
new file mode 100644
index 00000000..a6f23e28
--- /dev/null
+++ b/sphinx/_jinja2.py
@@ -0,0 +1,90 @@
+# -*- coding: utf-8 -*-
+
+"""
+ sphinx._jinja2
+ ==============
+
+ Glue code for jinja2.
+
+ :author: Sebastian Wiesner
+ :contact: basti.wiesner@gmx.net
+ :copyright: 2008 by Sebastian Wiesner
+ :license: MIT
+"""
+
+import codecs
+from os import path
+
+import jinja2
+
+from sphinx.util import mtimes_of_files
+from sphinx.application import TemplateBridge
+
+
+class SphinxLoader(jinja2.BaseLoader):
+ """
+ A jinja2 reimplementation of `sphinx._jinja.SphinxFileSystemLoader`.
+ """
+
+ def __init__(self, basepath, extpaths, encoding='utf-8'):
+ """
+ Creates a new loader for sphinx.
+
+ ``extpaths`` is a list of directories, which provide additional
+ templates to sphinx.
+
+ ``encoding`` is used to decode the templates into unicode strings.
+ Defaults to utf-8.
+
+ If ``basepath`` is set, this path is used to load sphinx core
+ templates. If False, these templates are loaded from the sphinx
+ package.
+ """
+ self.core_loader = jinja2.FileSystemLoader(basepath)
+ self.all_loaders = jinja2.ChoiceLoader(
+ [jinja2.FileSystemLoader(extpath) for extpath in extpaths] +
+ [self.core_loader])
+
+ def get_source(self, environment, template):
+ # exclamation mark forces loading from core
+ if template.startswith('!'):
+ return self.core_loader.get_source(environment, template[1:])
+ # check if the template is probably an absolute path
+ fs_path = template.replace('/', path.sep)
+ if path.isabs(fs_path):
+ if not path.exists(fs_path):
+ raise jinja2.TemplateNotFound(template)
+ f = codecs.open(fs_path, 'r', self.encoding)
+ try:
+ mtime = path.getmtime(path)
+ return (f.read(), fs_path,
+ lambda: mtime == path.getmtime(path))
+ finally:
+ f.close()
+ # finally try to load from custom templates
+ return self.all_loaders.get_source(environment, template)
+
+
+class BuiltinTemplates(TemplateBridge):
+ """
+ Interfaces the rendering environment of jinja2 for use in sphinx.
+ """
+
+ def init(self, builder):
+ base_templates_path = path.join(path.dirname(__file__), 'templates')
+ ext_templates_path = [path.join(builder.confdir, dir)
+ for dir in builder.config.templates_path]
+ self.templates_path = [base_templates_path] + ext_templates_path
+ loader = SphinxLoader(base_templates_path, ext_templates_path)
+ use_i18n = builder.translator is not None
+ extensions = use_i18n and ['jinja2.ext.i18n'] or []
+ self.environment = jinja2.Environment(loader=loader,
+ extensions=extensions)
+ if use_i18n:
+ self.environment.install_gettext_translations(builder.translator)
+
+ def render(self, template, context):
+ return self.environment.get_template(template).render(context)
+
+ def newest_template_mtime(self):
+ return max(mtimes_of_files(self.templates_path, '.html'))
diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py
index c9286a27..cb2f51f7 100644
--- a/sphinx/builders/__init__.py
+++ b/sphinx/builders/__init__.py
@@ -80,7 +80,7 @@ class Builder(object):
self.templates = self.app.import_object(
self.config.template_bridge, 'template_bridge setting')()
else:
- from sphinx._jinja import BuiltinTemplates
+ from sphinx._jinja2 import BuiltinTemplates
self.templates = BuiltinTemplates()
self.templates.init(self)
diff --git a/sphinx/templates/layout.html b/sphinx/templates/layout.html
index e49a0c81..510fef7b 100644
--- a/sphinx/templates/layout.html
+++ b/sphinx/templates/layout.html
@@ -4,7 +4,7 @@
{%- endblock %}
{%- set reldelim1 = reldelim1 is not defined and ' &raquo;' or reldelim1 %}
{%- set reldelim2 = reldelim2 is not defined and ' |' or reldelim2 %}
-{%- macro relbar %}
+{%- macro relbar() %}
<div class="related">
<h3>{{ _('Navigation') }}</h3>
<ul>
@@ -24,7 +24,7 @@
</ul>
</div>
{%- endmacro %}
-{%- macro sidebar %}
+{%- macro sidebar() %}
{%- if builder != 'htmlhelp' %}
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
@@ -52,7 +52,7 @@
{%- endif %}
{%- endblock %}
{%- block sidebarsourcelink %}
- {%- if show_source and has_source %}
+ {%- if show_source and has_source and sourcename %}
<h3>{{ _('This Page') }}</h3>
<ul class="this-page-menu">
<li><a href="{{ pathto('_sources/' + sourcename, true)|e }}" rel="nofollow">{{ _('Show Source') }}</a></li>
@@ -60,7 +60,7 @@
{%- endif %}
{%- endblock %}
{%- if customsidebar %}
- {{ rendertemplate(customsidebar) }}
+ {% include customsidebar %}
{%- endif %}
{%- block sidebarsearch %}
{%- if pagename != "search" %}
@@ -83,7 +83,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
{{ metatags }}
{%- if builder != 'htmlhelp' %}
- {%- set titlesuffix = " &mdash; " + docstitle|e %}
+ {%- set titlesuffix = " &mdash; "|safe + docstitle|e %}
{%- endif %}
<title>{{ title|striptags }}{{ titlesuffix }}</title>
<link rel="stylesheet" href="{{ pathto('_static/' + style, 1) }}" type="text/css" />
diff --git a/sphinx/templates/modindex.html b/sphinx/templates/modindex.html
index 7a83eb46..2c632c38 100644
--- a/sphinx/templates/modindex.html
+++ b/sphinx/templates/modindex.html
@@ -32,7 +32,7 @@
{% if fname %}<a href="{{ fname }}">{% endif -%}
<tt class="xref">{{ modname|e }}</tt>
{%- if fname %}</a>{% endif %}
- {%- if pform[0] %} <em>({{ pform|join(', ') }})</em>{% endif -%}
+ {%- if pform and pform[0] %} <em>({{ pform|join(', ') }})</em>{% endif -%}
</td><td>{% if dep %}<strong>{{ _('Deprecated')}}:</strong>{% endif %}
<em>{{ synops|e }}</em></td></tr>
{%- endif -%}