@@ -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,19 +1337,25 @@ 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 )
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 )
1340
1349
with open (output_name , "wb+" ) as post_file :
1341
1350
post_file .write (data )
1342
1351
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 ):
1344
1353
"""Replace links in document to point to the right places."""
1345
1354
# First let lxml replace most of them
1346
1355
doc .rewrite_links (lambda dst : self .url_replacer (src , dst , lang , url_type ), resolve_base_href = False )
1347
1356
1348
1357
# 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 '*' ) ))
1350
1359
for obj in objs :
1351
1360
if 'srcset' in obj .attrib :
1352
1361
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):
1997
2006
sys .exit (1 )
1998
2007
signal ('scanned' ).send (self )
1999
2008
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 ):
2001
2010
"""Helper function for rendering pages and post lists and other related pages.
2002
2011
2003
2012
lang is the current language.
@@ -2009,7 +2018,8 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
2009
2018
context (optional) a dict used as a basis for the template context. The lang parameter will always be added.
2010
2019
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.
2011
2020
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.
2013
2023
"""
2014
2024
utils .LocaleBorg ().set_locale (lang )
2015
2025
@@ -2043,7 +2053,7 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
2043
2053
'targets' : [output_name ],
2044
2054
'file_dep' : file_deps ,
2045
2055
'actions' : [(self .render_template , [template_name , output_name ,
2046
- context , url_type ])],
2056
+ context , url_type , is_fragment ])],
2047
2057
'clean' : True ,
2048
2058
'uptodate' : [config_changed (deps_dict , 'nikola.nikola.Nikola.generic_renderer' )] + ([] if uptodate_deps is None else uptodate_deps )
2049
2059
}
0 commit comments