Skip to content
Browse files
  • Loading branch information
felixfontein committed Oct 23, 2016
1 parent 0280de3 commit 878568128e2a1391305c929b03fb5d869727c61a
Showing with 3 additions and 7 deletions.
  1. +3 −7 nikola/
@@ -1341,25 +1341,21 @@ def render_template(self, template_name, output_name, context, url_type=None, is
doc = lxml.html.fragment_fromstring(data, parser)
doc = lxml.html.document_fromstring(data, parser)
self.rewrite_links(doc, src, context['lang'], url_type, is_fragment=is_fragment)
self.rewrite_links(doc, src, context['lang'], url_type)
if is_fragment:
data = (doc.text or '').encode('utf-8') + ''.encode('utf-8').join([lxml.html.tostring(child, encoding='utf-8', method='html') for child in doc.iterchildren()])
data = b'<!DOCTYPE html>\n' + lxml.html.tostring(doc, encoding='utf8', method='html', pretty_print=True)
with open(output_name, "wb+") as post_file:

def rewrite_links(self, doc, src, lang, url_type=None, is_fragment=False):
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, url_type), resolve_base_href=False)

# lxml ignores srcset in img and source elements, so do that by hand
if is_fragment:
# With the asterisk, top-level <img> and <source> tags are not found
objs = list(doc.xpath('(//img|//source)'))
objs = list(doc.xpath('(*//img|*//source)'))
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(',')]

0 comments on commit 8785681

Please sign in to comment.