@@ -985,7 +985,7 @@ def init_plugins(self, commands_only=False, load_all=False):
985
985
self .disabled_compilers [p [- 1 ].name ] = p
986
986
utils .LOGGER .debug ('Not loading unneeded compiler {}' , p [- 1 ].name )
987
987
if p [- 1 ].name not in self .config ['COMPILERS' ] and \
988
- p [- 1 ].details .has_option ('Nikola' , 'plugincategory' ) and p [- 1 ].details .get ('Nikola' , 'PluginCategory' ) == 'Compiler' :
988
+ p [- 1 ].details .has_option ('Nikola' , 'plugincategory' ) and p [- 1 ].details .get ('Nikola' , 'PluginCategory' ) in ( 'Compiler' , 'PageCompiler' ) :
989
989
bad_candidates .add (p )
990
990
self .disabled_compilers [p [- 1 ].name ] = p
991
991
utils .LOGGER .debug ('Not loading unneeded compiler {}' , p [- 1 ].name )
@@ -1290,7 +1290,7 @@ def get_compiler(self, source_name):
1290
1290
1291
1291
return compiler
1292
1292
1293
- def render_template (self , template_name , output_name , context , url_type = None ):
1293
+ def render_template (self , template_name , output_name , context , url_type = None , is_fragment = False ):
1294
1294
"""Render a template with the global context.
1295
1295
1296
1296
If ``output_name`` is None, will return a string and all URL
@@ -1301,6 +1301,9 @@ def render_template(self, template_name, output_name, context, url_type=None):
1301
1301
1302
1302
The argument ``url_type`` allows to override the ``URL_TYPE``
1303
1303
configuration.
1304
+
1305
+ If ``is_fragment`` is set to ``True``, a HTML fragment will
1306
+ be rendered and not a whole HTML document.
1304
1307
"""
1305
1308
local_context = {}
1306
1309
local_context ["template_name" ] = template_name
@@ -1309,6 +1312,7 @@ def render_template(self, template_name, output_name, context, url_type=None):
1309
1312
for k in self ._GLOBAL_CONTEXT_TRANSLATABLE :
1310
1313
local_context [k ] = local_context [k ](local_context ['lang' ])
1311
1314
local_context ['is_rtl' ] = local_context ['lang' ] in LEGAL_VALUES ['RTL_LANGUAGES' ]
1315
+ local_context ['url_type' ] = self .config ['URL_TYPE' ] if url_type is None else url_type
1312
1316
# string, arguments
1313
1317
local_context ["formatmsg" ] = lambda s , * a : s % a
1314
1318
for h in local_context ['template_hooks' ].values ():
@@ -1336,9 +1340,18 @@ def render_template(self, template_name, output_name, context, url_type=None):
1336
1340
1337
1341
utils .makedirs (os .path .dirname (output_name ))
1338
1342
parser = lxml .html .HTMLParser (remove_blank_text = True )
1339
- doc = lxml .html .document_fromstring (data , parser )
1343
+ if is_fragment :
1344
+ doc = lxml .html .fragment_fromstring (data , parser )
1345
+ else :
1346
+ doc = lxml .html .document_fromstring (data , parser )
1340
1347
self .rewrite_links (doc , src , context ['lang' ], url_type )
1341
- data = b'<!DOCTYPE html>\n ' + lxml .html .tostring (doc , encoding = 'utf8' , method = 'html' , pretty_print = True )
1348
+ if is_fragment :
1349
+ # doc.text contains text before the first HTML, or None if there was no text
1350
+ # The text after HTML elements is added by tostring() (because its implicit
1351
+ # argument with_tail has default value True).
1352
+ data = (doc .text or '' ).encode ('utf-8' ) + b'' .join ([lxml .html .tostring (child , encoding = 'utf-8' , method = 'html' ) for child in doc .iterchildren ()])
1353
+ else :
1354
+ data = lxml .html .tostring (doc , encoding = 'utf8' , method = 'html' , pretty_print = True , doctype = '<!DOCTYPE html>' )
1342
1355
with open (output_name , "wb+" ) as post_file :
1343
1356
post_file .write (data )
1344
1357
@@ -1348,7 +1361,7 @@ def rewrite_links(self, doc, src, lang, url_type=None):
1348
1361
doc .rewrite_links (lambda dst : self .url_replacer (src , dst , lang , url_type ), resolve_base_href = False )
1349
1362
1350
1363
# lxml ignores srcset in img and source elements, so do that by hand
1351
- objs = list (doc .xpath ('(* //img|* //source)' ))
1364
+ objs = list (doc .xpath ('(//img|//source)' ))
1352
1365
for obj in objs :
1353
1366
if 'srcset' in obj .attrib :
1354
1367
urls = [u .strip () for u in obj .attrib ['srcset' ].split (',' )]
@@ -1479,9 +1492,16 @@ def _make_renderfunc(self, t_data, fname=None):
1479
1492
keyword argument dict and then the latter provides the template
1480
1493
context.
1481
1494
1495
+ Global context keys are made available as part of the context,
1496
+ respecting locale.
1497
+
1498
+ As a special quirk, the "data" key from global_context is made
1499
+ available as "global_data" because of name clobbering.
1500
+
1482
1501
"""
1483
1502
def render_shortcode (* args , ** kw ):
1484
1503
context = self .GLOBAL_CONTEXT .copy ()
1504
+ context ['global_data' ] = context ['data' ]
1485
1505
context .update (kw )
1486
1506
context ['_args' ] = args
1487
1507
context ['lang' ] = utils .LocaleBorg ().current_lang
@@ -1999,7 +2019,7 @@ def scan_posts(self, really=False, ignore_quit=False, quiet=False):
1999
2019
sys .exit (1 )
2000
2020
signal ('scanned' ).send (self )
2001
2021
2002
- 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 ):
2022
+ 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 ):
2003
2023
"""Helper function for rendering pages and post lists and other related pages.
2004
2024
2005
2025
lang is the current language.
@@ -2011,7 +2031,8 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
2011
2031
context (optional) a dict used as a basis for the template context. The lang parameter will always be added.
2012
2032
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.
2013
2033
post_deps_dict (optional) is a dict merged into the copy of context which is used as an uptodate dependency.
2014
- url_type (optional) allows to override the ``URL_TYPE`` configuration
2034
+ url_type (optional) allows to override the ``URL_TYPE`` configuration.
2035
+ is_fragment (optional) allows to write a HTML fragment instead of a HTML document.
2015
2036
"""
2016
2037
utils .LocaleBorg ().set_locale (lang )
2017
2038
@@ -2045,7 +2066,7 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
2045
2066
'targets' : [output_name ],
2046
2067
'file_dep' : file_deps ,
2047
2068
'actions' : [(self .render_template , [template_name , output_name ,
2048
- context , url_type ])],
2069
+ context , url_type , is_fragment ])],
2049
2070
'clean' : True ,
2050
2071
'uptodate' : [config_changed (deps_dict , 'nikola.nikola.Nikola.generic_renderer' )] + ([] if uptodate_deps is None else uptodate_deps )
2051
2072
}
@@ -2088,7 +2109,8 @@ def generic_page_renderer(self, lang, post, filters, context=None):
2088
2109
uptodate_deps = uptodate_deps ,
2089
2110
context = context ,
2090
2111
context_deps_remove = ['post' ],
2091
- post_deps_dict = deps_dict )
2112
+ post_deps_dict = deps_dict ,
2113
+ url_type = post .url_type )
2092
2114
2093
2115
def generic_post_list_renderer (self , lang , posts , output_name , template_name , filters , extra_context ):
2094
2116
"""Render pages with lists of posts."""
0 commit comments