@@ -1287,7 +1287,7 @@ def get_compiler(self, source_name):
1287
1287
1288
1288
return compiler
1289
1289
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 ):
1291
1291
"""Render a template with the global context.
1292
1292
1293
1293
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):
1298
1298
1299
1299
The argument ``url_type`` allows to override the ``URL_TYPE``
1300
1300
configuration.
1301
+
1302
+ If ``is_fragment`` is set to ``True``, a HTML fragment will
1303
+ be rendered and not a whole HTML document.
1301
1304
"""
1302
1305
local_context = {}
1303
1306
local_context ["template_name" ] = template_name
@@ -1334,9 +1337,18 @@ def render_template(self, template_name, output_name, context, url_type=None):
1334
1337
1335
1338
utils .makedirs (os .path .dirname (output_name ))
1336
1339
parser = lxml .html .HTMLParser (remove_blank_text = True )
1337
- doc = lxml .html .document_fromstring (data , parser )
1340
+ if is_fragment :
1341
+ doc = lxml .html .fragment_fromstring (data , parser )
1342
+ else :
1343
+ doc = lxml .html .document_fromstring (data , parser )
1338
1344
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 )
1345
+ if is_fragment :
1346
+ # doc.text contains text before the first HTML, or None if there was no text
1347
+ # The text after HTML elements is added by tostring() (because its implicit
1348
+ # argument with_tail has default value True).
1349
+ data = (doc .text or '' ).encode ('utf-8' ) + b'' .join ([lxml .html .tostring (child , encoding = 'utf-8' , method = 'html' ) for child in doc .iterchildren ()])
1350
+ else :
1351
+ data = lxml .html .tostring (doc , encoding = 'utf8' , method = 'html' , pretty_print = True , doctype = '<!DOCTYPE html>' )
1340
1352
with open (output_name , "wb+" ) as post_file :
1341
1353
post_file .write (data )
1342
1354
@@ -1346,7 +1358,7 @@ def rewrite_links(self, doc, src, lang, url_type=None):
1346
1358
doc .rewrite_links (lambda dst : self .url_replacer (src , dst , lang , url_type ), resolve_base_href = False )
1347
1359
1348
1360
# lxml ignores srcset in img and source elements, so do that by hand
1349
- objs = list (doc .xpath ('(* //img|* //source)' ))
1361
+ objs = list (doc .xpath ('(//img|//source)' ))
1350
1362
for obj in objs :
1351
1363
if 'srcset' in obj .attrib :
1352
1364
urls = [u .strip () for u in obj .attrib ['srcset' ].split (',' )]
@@ -2004,7 +2016,7 @@ def scan_posts(self, really=False, ignore_quit=False, quiet=False):
2004
2016
sys .exit (1 )
2005
2017
signal ('scanned' ).send (self )
2006
2018
2007
- 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 ):
2019
+ 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 ):
2008
2020
"""Helper function for rendering pages and post lists and other related pages.
2009
2021
2010
2022
lang is the current language.
@@ -2016,7 +2028,8 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
2016
2028
context (optional) a dict used as a basis for the template context. The lang parameter will always be added.
2017
2029
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.
2018
2030
post_deps_dict (optional) is a dict merged into the copy of context which is used as an uptodate dependency.
2019
- url_type (optional) allows to override the ``URL_TYPE`` configuration
2031
+ url_type (optional) allows to override the ``URL_TYPE`` configuration.
2032
+ is_fragment (optional) allows to write a HTML fragment instead of a HTML document.
2020
2033
"""
2021
2034
utils .LocaleBorg ().set_locale (lang )
2022
2035
@@ -2050,7 +2063,7 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
2050
2063
'targets' : [output_name ],
2051
2064
'file_dep' : file_deps ,
2052
2065
'actions' : [(self .render_template , [template_name , output_name ,
2053
- context , url_type ])],
2066
+ context , url_type , is_fragment ])],
2054
2067
'clean' : True ,
2055
2068
'uptodate' : [config_changed (deps_dict , 'nikola.nikola.Nikola.generic_renderer' )] + ([] if uptodate_deps is None else uptodate_deps )
2056
2069
}
0 commit comments