Skip to content

Commit a1eb111

Browse files
committedOct 23, 2016
Allowing to write HTML fragments instead of only whole documents.
1 parent 926bcc8 commit a1eb111

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed
 

‎CHANGES.txt

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Features
1616
translated (Issue #2116)
1717
* Pass ``post`` object and ``lang`` to post compilers (Issue #2531)
1818
* Pass ``url_type`` into template's context.
19+
* ``render_template`` and ``generic_renderer`` can now create HTML
20+
fragments.
1921

2022
New in v7.8.1
2123
=============

‎nikola/nikola.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ def get_compiler(self, source_name):
12871287

12881288
return compiler
12891289

1290-
def render_template(self, template_name, output_name, context, url_type=None):
1290+
def render_template(self, template_name, output_name, context, url_type=None, is_fragment=False):
12911291
"""Render a template with the global context.
12921292
12931293
If ``output_name`` is None, will return a string and all URL
@@ -1298,6 +1298,9 @@ def render_template(self, template_name, output_name, context, url_type=None):
12981298
12991299
The argument ``url_type`` allows to override the ``URL_TYPE``
13001300
configuration.
1301+
1302+
If ``is_fragment`` is set to ``True``, a HTML fragment will
1303+
be rendered and not a whole HTML document.
13011304
"""
13021305
local_context = {}
13031306
local_context["template_name"] = template_name
@@ -1334,19 +1337,25 @@ def render_template(self, template_name, output_name, context, url_type=None):
13341337

13351338
utils.makedirs(os.path.dirname(output_name))
13361339
parser = lxml.html.HTMLParser(remove_blank_text=True)
1337-
doc = lxml.html.document_fromstring(data, parser)
1338-
self.rewrite_links(doc, src, context['lang'], url_type)
1339-
data = b'<!DOCTYPE html>\n' + lxml.html.tostring(doc, encoding='utf8', method='html', pretty_print=True)
1340+
if is_fragment:
1341+
doc = lxml.html.fragment_fromstring(data, parser)
1342+
else:
1343+
doc = lxml.html.document_fromstring(data, parser)
1344+
self.rewrite_links(doc, src, context['lang'], url_type, is_fragment=is_fragment)
1345+
if is_fragment:
1346+
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()])
1347+
else:
1348+
data = b'<!DOCTYPE html>\n' + lxml.html.tostring(doc, encoding='utf8', method='html', pretty_print=True)
13401349
with open(output_name, "wb+") as post_file:
13411350
post_file.write(data)
13421351

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

13481357
# lxml ignores srcset in img and source elements, so do that by hand
1349-
objs = list(doc.xpath('(*//img|*//source)'))
1358+
objs = list(doc.xpath('({}//img|{}//source)'.format('' if is_fragment else '*')))
13501359
for obj in objs:
13511360
if 'srcset' in obj.attrib:
13521361
urls = [u.strip() for u in obj.attrib['srcset'].split(',')]
@@ -1997,7 +2006,7 @@ def scan_posts(self, really=False, ignore_quit=False, quiet=False):
19972006
sys.exit(1)
19982007
signal('scanned').send(self)
19992008

2000-
def generic_renderer(self, lang, output_name, template_name, filters, file_deps=None, uptodate_deps=None, context=None, context_deps_remove=None, post_deps_dict=None, url_type=None):
2009+
def generic_renderer(self, lang, output_name, template_name, filters, file_deps=None, uptodate_deps=None, context=None, context_deps_remove=None, post_deps_dict=None, url_type=None, is_fragment=False):
20012010
"""Helper function for rendering pages and post lists and other related pages.
20022011
20032012
lang is the current language.
@@ -2009,7 +2018,8 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
20092018
context (optional) a dict used as a basis for the template context. The lang parameter will always be added.
20102019
context_deps_remove (optional) is a list of keys to remove from the context after using it as an uptodate dependency. This should name all keys containing non-trivial Python objects; they can be replaced by adding JSON-style dicts in post_deps_dict.
20112020
post_deps_dict (optional) is a dict merged into the copy of context which is used as an uptodate dependency.
2012-
url_type (optional) allows to override the ``URL_TYPE`` configuration
2021+
url_type (optional) allows to override the ``URL_TYPE`` configuration.
2022+
is_fragment (optional) allows to write a HTML fragment instead of a HTML document.
20132023
"""
20142024
utils.LocaleBorg().set_locale(lang)
20152025

@@ -2043,7 +2053,7 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
20432053
'targets': [output_name],
20442054
'file_dep': file_deps,
20452055
'actions': [(self.render_template, [template_name, output_name,
2046-
context, url_type])],
2056+
context, url_type, is_fragment])],
20472057
'clean': True,
20482058
'uptodate': [config_changed(deps_dict, 'nikola.nikola.Nikola.generic_renderer')] + ([] if uptodate_deps is None else uptodate_deps)
20492059
}

0 commit comments

Comments
 (0)
Failed to load comments.