Skip to content

Commit

Permalink
Allowing to override url_type in render_template and rewrite_links me…
Browse files Browse the repository at this point in the history
…thods.
  • Loading branch information
felixfontein committed Oct 2, 2016
1 parent 785c3e8 commit e083e07
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions nikola/nikola.py
Expand Up @@ -1277,14 +1277,17 @@ def get_compiler(self, source_name):

return compile_html

def render_template(self, template_name, output_name, context):
def render_template(self, template_name, output_name, context, url_type=None):
"""Render a template with the global context.
If ``output_name`` is None, will return a string and all URL
normalization will be ignored (including the link:// scheme).
If ``output_name`` is a string, URLs will be normalized and
the resultant HTML will be saved to the named file (path must
start with OUTPUT_FOLDER).
The argument ``url_type`` allows to override the ``URL_TYPE``
configuration.
"""
local_context = {}
local_context["template_name"] = template_name
Expand Down Expand Up @@ -1321,22 +1324,22 @@ def render_template(self, template_name, output_name, context):
utils.makedirs(os.path.dirname(output_name))
parser = lxml.html.HTMLParser(remove_blank_text=True)
doc = lxml.html.document_fromstring(data, parser)
self.rewrite_links(doc, src, context['lang'])
self.rewrite_links(doc, src, context['lang'], url_type)
data = b'<!DOCTYPE html>\n' + lxml.html.tostring(doc, encoding='utf8', method='html', pretty_print=True)
with open(output_name, "wb+") as post_file:
post_file.write(data)

def rewrite_links(self, doc, src, lang):
def rewrite_links(self, doc, src, lang, url_type=None):
"""Replace links in document to point to the right places."""
# First let lxml replace most of them
doc.rewrite_links(lambda dst: self.url_replacer(src, dst, lang), resolve_base_href=False)
doc.rewrite_links(lambda dst: self.url_replacer(src, dst, lang, url_type), resolve_base_href=False)

# lxml ignores srcset in img and source elements, so do that by hand
objs = list(doc.xpath('(*//img|*//source)'))
for obj in objs:
if 'srcset' in obj.attrib:
urls = [u.strip() for u in obj.attrib['srcset'].split(',')]
urls = [self.url_replacer(src, dst, lang) for dst in urls]
urls = [self.url_replacer(src, dst, lang, url_type) for dst in urls]
obj.set('srcset', ', '.join(urls))

def url_replacer(self, src, dst, lang=None, url_type=None):
Expand Down

0 comments on commit e083e07

Please sign in to comment.