Skip to content
Permalink
Browse files

REF: Add config.mako variables to all templates at render time

  • Loading branch information...
kernc committed Apr 22, 2019
1 parent 183c37f commit d1d19d3b9d3d1803d13c98fee953934803588435
Showing with 15 additions and 36 deletions.
  1. +13 −1 pdoc/__init__.py
  2. +2 −1 pdoc/templates/config.mako
  3. +0 −18 pdoc/templates/html.mako
  4. +0 −8 pdoc/templates/pdf.mako
  5. +0 −8 pdoc/templates/text.mako
@@ -359,6 +359,7 @@ def recursive_htmls(mod):

from mako.lookup import TemplateLookup
from mako.exceptions import TopLevelLookupException
from mako.template import Template

try:
from pdoc._version import version as __version__ # noqa: F401
@@ -426,6 +427,17 @@ def _render_template(template_name, **kwargs):
Returns the Mako template with the given name. If the template
cannot be found, a nicer error message is displayed.
"""
# Apply config.mako configuration
MAKO_INTERNALS = Template('').module.__dict__.keys()
DEFAULT_CONFIG = path.join(path.dirname(__file__), 'templates', 'config.mako')
config = {}
for config_module in (Template(filename=DEFAULT_CONFIG).module,
tpl_lookup.get_template('/config.mako').module):
config.update((var, getattr(config_module, var, None))
for var in config_module.__dict__
if var not in MAKO_INTERNALS)
config.update(kwargs)

try:
t = tpl_lookup.get_template(template_name)
except TopLevelLookupException:
@@ -434,7 +446,7 @@ def _render_template(template_name, **kwargs):
', '.join(path.join(p, template_name.lstrip("/"))
for p in tpl_lookup.directories)))
try:
return t.render(**kwargs).strip()
return t.render(**config).strip()
except Exception:
from mako import exceptions
print(exceptions.text_error_template().render(),
@@ -1,5 +1,6 @@
<%!
# Template configuration. Copy over and adapt as required.
# Template configuration. Copy over in your template directory
# (used with --template-dir) and adapt as required.
html_lang = 'en'
show_inherited_members = False
extract_module_toc_into_sidebar = True
@@ -16,24 +16,6 @@
return _to_html(text, module=module, link=link)
%>

## Import template configuration from potentially-overridden config.mako.
## It may override above imported/defined functions as well.
<%namespace file="config.mako" name="config"/>
<%
html_lang = getattr(config.attr, 'html_lang', 'en')
show_inherited_members = getattr(config.attr, 'show_inherited_members', True)
extract_module_toc_into_sidebar = getattr(config.attr, 'extract_module_toc_into_sidebar', True)
list_class_variables_in_index = getattr(config.attr, 'list_class_variables_in_index', False)
sort_identifiers = getattr(config.attr, 'sort_identifiers', True)
show_type_annotations = getattr(config.attr, 'show_type_annotations', False)
hljs_style = getattr(config.attr, 'hljs_style', 'github')
link = getattr(config.attr, 'link', link)
to_html = getattr(config.attr, 'to_html', to_html)
glimpse = getattr(config.attr, 'glimpse', glimpse)
extract_toc = getattr(config.attr, 'extract_toc', extract_toc)
%>

<%def name="ident(name)"><span class="ident">${name}</span></%def>

<%def name="show_source(d)">
@@ -25,14 +25,6 @@
return re.sub(r'\n(#+) +(.+)\n', r'\n%s\1 \2\n' % ('#' * level), text)
%>

## Import template configuration from potentially-overridden config.mako.
<%namespace file="config.mako" name="config"/>
<%
show_inherited_members = getattr(config.attr, 'show_inherited_members', True)
sort_identifiers = getattr(config.attr, 'sort_identifiers', True)
show_type_annotations = getattr(config.attr, 'show_type_annotations', False)
%>

<%def name="title(level, string, id=None)">
<% id = ' {#%s}' % id if id is not None else '' %>
${('#' * level) + ' ' + string + id}
@@ -6,14 +6,6 @@
return ' ' * spaces + new.strip()
%>

## Import template configuration from potentially-overridden config.mako.
<%namespace file="config.mako" name="config"/>
<%
show_inherited_members = getattr(config.attr, 'show_inherited_members', True)
sort_identifiers = getattr(config.attr, 'sort_identifiers', True)
show_type_annotations = getattr(config.attr, 'show_type_annotations', False)
%>

<%def name="deflist(s)">:${indent(s)[1:]}</%def>

<%def name="h3(s)">### ${s}

0 comments on commit d1d19d3

Please sign in to comment.
You can’t perform that action at this time.