Skip to content
Permalink
Browse files

Merge branch 'master' into fixing-post-dependencies

  • Loading branch information
felixfontein committed Oct 29, 2016
2 parents 83ffc8b + 48fa3bd commit 7904138fc74077de1ee3cce9631df680bcfd2965
Showing with 24 additions and 11 deletions.
  1. +2 −0 CHANGES.txt
  2. +2 −1 docs/creating-a-site.txt
  3. +20 −7 nikola/nikola.py
  4. +0 −3 snapcraft/stable/build.sh
@@ -12,6 +12,8 @@ Features

* Improving handling of .dep files, and allowing compilers to specify
additional targets for the render_posts task (Issue #2536)
* ``render_template`` and ``generic_renderer`` can now create HTML
fragments.
* Allow posts to set custom ``URL_TYPE`` by using the ``url_type``
meta tag (useful for HTML fragments inserted using JavaScript)
* Plugins can depend on other plugins being installed (Issue #2533)
@@ -48,8 +48,9 @@ configuration file:
# Some things in the middle you don't really need to change...
#

POSTS = ()
# you can also keep the current content of POSTS if you want a blog with your site
POSTS = ()
# remove destination directory to generate pages in the root directory
PAGES = (
("pages/*.rst", "", "story.tmpl"),
("pages/*.txt", "", "story.tmpl"),
@@ -1287,7 +1287,7 @@ def get_compiler(self, source_name):

return compiler

def render_template(self, template_name, output_name, context, url_type=None):
def render_template(self, template_name, output_name, context, url_type=None, is_fragment=False):
"""Render a template with the global context.
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):
The argument ``url_type`` allows to override the ``URL_TYPE``
configuration.
If ``is_fragment`` is set to ``True``, a HTML fragment will
be rendered and not a whole HTML document.
"""
local_context = {}
local_context["template_name"] = template_name
@@ -1334,9 +1337,18 @@ def render_template(self, template_name, output_name, context, url_type=None):

utils.makedirs(os.path.dirname(output_name))
parser = lxml.html.HTMLParser(remove_blank_text=True)
doc = lxml.html.document_fromstring(data, parser)
if is_fragment:
doc = lxml.html.fragment_fromstring(data, parser)
else:
doc = lxml.html.document_fromstring(data, parser)
self.rewrite_links(doc, src, context['lang'], url_type)
data = b'<!DOCTYPE html>\n' + lxml.html.tostring(doc, encoding='utf8', method='html', pretty_print=True)
if is_fragment:
# doc.text contains text before the first HTML, or None if there was no text
# The text after HTML elements is added by tostring() (because its implicit
# argument with_tail has default value True).
data = (doc.text or '').encode('utf-8') + b''.join([lxml.html.tostring(child, encoding='utf-8', method='html') for child in doc.iterchildren()])
else:
data = lxml.html.tostring(doc, encoding='utf8', method='html', pretty_print=True, doctype='<!DOCTYPE html>')
with open(output_name, "wb+") as post_file:
post_file.write(data)

@@ -1346,7 +1358,7 @@ def rewrite_links(self, doc, src, lang, url_type=None):
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
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(',')]
@@ -2004,7 +2016,7 @@ def scan_posts(self, really=False, ignore_quit=False, quiet=False):
sys.exit(1)
signal('scanned').send(self)

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):
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):
"""Helper function for rendering pages and post lists and other related pages.
lang is the current language.
@@ -2016,7 +2028,8 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
context (optional) a dict used as a basis for the template context. The lang parameter will always be added.
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.
post_deps_dict (optional) is a dict merged into the copy of context which is used as an uptodate dependency.
url_type (optional) allows to override the ``URL_TYPE`` configuration
url_type (optional) allows to override the ``URL_TYPE`` configuration.
is_fragment (optional) allows to write a HTML fragment instead of a HTML document.
"""
utils.LocaleBorg().set_locale(lang)

@@ -2050,7 +2063,7 @@ def generic_renderer(self, lang, output_name, template_name, filters, file_deps=
'targets': [output_name],
'file_dep': file_deps,
'actions': [(self.render_template, [template_name, output_name,
context, url_type])],
context, url_type, is_fragment])],
'clean': True,
'uptodate': [config_changed(deps_dict, 'nikola.nikola.Nikola.generic_renderer')] + ([] if uptodate_deps is None else uptodate_deps)
}
@@ -1,6 +1,3 @@
#!/bin/sh
rm -rf parts/ stage/ prime/
snapcraft pull
pip3 wheel --wheel-dir parts/nikola/packages --disable-pip-version-check --no-index --find-links parts/nikola/packages pillow
snapcraft

0 comments on commit 7904138

Please sign in to comment.
You can’t perform that action at this time.