Skip to content

Commit

Permalink
Simplifying.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Oct 23, 2016
1 parent 0280de3 commit 8785681
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions nikola/nikola.py
Expand Up @@ -1341,25 +1341,21 @@ def render_template(self, template_name, output_name, context, url_type=None, is
doc = lxml.html.fragment_fromstring(data, parser)
else:
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()])
else:
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, 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)'))
else:
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(',')]
Expand Down

0 comments on commit 8785681

Please sign in to comment.