Skip to content

Commit

Permalink
Add IRootPage interface and make featured posts available to it.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed May 22, 2018
1 parent 380d5df commit d268276
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Expand Up @@ -12,6 +12,12 @@
- Add a view for getting CSS data. Currently it has
``context/@@post_css/pagekind_class``.
- Update to Nikola 8; drop support for Python 2.7.
- Add ``interfaces.IRootPage`` which is added to pages for which the
metadata field ``nti-extra-page-kind`` is set to ``root``.
- Make the ``featured`` list available to all pages, not just index
pages. Together with the IRootPage this can be used to promote blog
posts to the root index.html.
- Add ``embedded_content`` to the ``@@post_text`` view.

0.0.1a2 (2017-10-14)
====================
Expand Down
7 changes: 4 additions & 3 deletions docs/path_helpers.rst
Expand Up @@ -74,9 +74,10 @@ Post Text

The ``text`` method on a post can take an optional parameter telling
it to truncate to a teaser length. This is configurable by the user
for index pages.
for index pages, but repeatedly accessing the configuration is tedious.
The :class:`post_text <nti.nikola_chameleon.view.PostTextView>` view
provides helpers to automatically take this into account.
provides helpers to automatically take this into account for indexes.
It also helps when embedding posts into other pages.

Typical example::

Expand Down Expand Up @@ -120,7 +121,7 @@ Typical usage::
CSS
---

The :class:`post_css <nti.nikola_chameleon.views.PostCssKindView>` has
The :class:`post_css <nti.nikola_chameleon.view.PostCssKindView>` has
helpers to produce CSS, typically based on the kind of page being
rendered.

Expand Down
10 changes: 9 additions & 1 deletion src/nti/nikola_chameleon/interfaces.py
Expand Up @@ -205,11 +205,19 @@ class IBookPageKind(IStoryPageKind):

class IPostPage(IPost, IPageKind):
"""
A page being rendered by a post.
A page being rendered for a post.
"""

interface.taggedValue('__pagekinds__', ())

class IRootPage(interface.Interface):
"""
The page that forms the root of the site, e.g., index.html.
The context takes this interface on when it specifies
the custom metadata field ``nti-extra-page-kind`` as ``"root"``.
"""

def _build(iface, result, tag='__pagekinds__', tx=lambda x: x):
__traceback_info__ = iface, tag
if not getattr(iface, '__module__', None) == __name__:
Expand Down
6 changes: 6 additions & 0 deletions src/nti/nikola_chameleon/plugin.py
Expand Up @@ -213,6 +213,10 @@ def render_template_to_string(self, template, context):
context = options.get('post')

if context is not None:
# Make the 'featured' list available to all pages, not just
# indexes. Added in nikola 8, but only for indexes.
if 'featured' not in options:
options['featured'] = [p for p in self.site.posts if p.post_status == 'featured']
if template == 'gallery.tmpl':
# Some galleries can have posts
context = _GalleryContext(options)
Expand All @@ -226,6 +230,8 @@ def render_template_to_string(self, template, context):
# post https://getnikola.com/handbook.html#post-types
if context.has_math:
interface.alsoProvides(context, interfaces.IMathJaxPost)
if context.meta[context.default_lang].get('nti-extra-page-kind', '') == 'root':
interface.alsoProvides(context, interfaces.IRootPage)
elif 'posts' in options:
context = _PostListContext(options['posts'])
for post in context:
Expand Down
3 changes: 2 additions & 1 deletion src/nti/nikola_chameleon/template.py
Expand Up @@ -151,5 +151,6 @@ def __call__(self, msgid, domain=None, mapping=None, context=None,
target_language=None, default=None):
try:
return self._messages(msgid, target_language)
except KeyError:
except (KeyError, TypeError):
# TypeError if the object is unhashable, e.g., a dict
return default or msgid
17 changes: 13 additions & 4 deletions src/nti/nikola_chameleon/view.py
Expand Up @@ -34,7 +34,7 @@ def __init__(self, context, request):

class View(BaseView):
"""
The default view when be begin rendering a template.
The default view when we begin rendering a template.
It may have "layers" applied to it to adjust the
rendering conditions.
Expand All @@ -59,7 +59,7 @@ class PostTextView(BaseView):
@property
def teaser(self):
"""
The string 'teaser' if the content is a teaser, otherwise
The string 'teaser' if the content is a teaser in an index, otherwise
None.
"""
return 'teaser' if self.request.options['index_teasers'] else None
Expand All @@ -68,15 +68,16 @@ def teaser(self):
def content_kind(self):
"""
Either 'summary' or 'content'. Intended to be used in
CSS class names.
CSS class names in indices.
"""
return 'summary' if self.teaser else 'content'


@property
def content(self):
"""
The full text or teaser text of the post.
The full text or teaser text of the post, as appropriate, in
an index.
"""
return self.context.text(teaser_only=bool(self.teaser))

Expand All @@ -87,6 +88,14 @@ def preview(self):
"""
return 'post-preview' if self.teaser else ''

@property
def embedded_content(self):
"""
The teaser for a post, stripped of html. Works regardless of index
status.
"""
return self.context.text(teaser_only=True, strip_html=True)

class PostCssKindView(BaseView):
"""
For getting various strings useful in CSS.
Expand Down

0 comments on commit d268276

Please sign in to comment.