diff options
author | Katharina Fey <kookie@spacekookie.de> | 2017-10-12 15:13:13 +0200 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2017-10-12 15:13:13 +0200 |
commit | 964d22f599ab7f300663e4ef3dac0f8749b1076d (patch) | |
tree | 5ab4ded280c12e230fdb34bef1839ec683e854c0 /plugins | |
parent | b004cfd65b993a92a24767a9b022fb4e04554e7d (diff) |
Moving all of the website into the public repo. This is a mess
Diffstat (limited to 'plugins')
-rwxr-xr-x | plugins/summary/Readme.rst | 53 | ||||
-rwxr-xr-x | plugins/summary/__init__.py | 1 | ||||
-rw-r--r-- | plugins/summary/__pycache__/__init__.cpython-36.pyc | bin | 0 -> 165 bytes | |||
-rw-r--r-- | plugins/summary/__pycache__/summary.cpython-36.pyc | bin | 0 -> 1968 bytes | |||
-rwxr-xr-x | plugins/summary/summary.py | 90 | ||||
-rwxr-xr-x | plugins/summary/test_summary.py | 75 |
6 files changed, 219 insertions, 0 deletions
diff --git a/plugins/summary/Readme.rst b/plugins/summary/Readme.rst new file mode 100755 index 0000000..3aec268 --- /dev/null +++ b/plugins/summary/Readme.rst @@ -0,0 +1,53 @@ +Summary +------- + +This plugin allows easy, variable length summaries directly embedded into the +body of your articles. It introduces two new settings: ``SUMMARY_BEGIN_MARKER`` +and ``SUMMARY_END_MARKER``: strings which can be placed directly into an article +to mark the beginning and end of a summary. When found, the standard +``SUMMARY_MAX_LENGTH`` setting will be ignored. The markers themselves will also +be removed from your articles before they are published. The default values +are ``<!-- PELICAN_BEGIN_SUMMARY -->`` and ``<!-- PELICAN_END_SUMMARY -->``. +For example:: + + Title: My super title + Date: 2010-12-03 10:20 + Tags: thats, awesome + Category: yeah + Slug: my-super-post + Author: Alexis Metaireau + + This is the content of my super blog post. + <!-- PELICAN_END_SUMMARY --> + and this content occurs after the summary. + +Here, the summary is taken to be the first line of the post. Because no +beginning marker was found, it starts at the top of the body. It is possible +to leave out the end marker instead, in which case the summary will start at the +beginning marker and continue to the end of the body. + +The plugin also sets a ``has_summary`` attribute on every article. It is True +for articles with an explicitly-defined summary, and False otherwise. (It is +also False for an article truncated by ``SUMMARY_MAX_LENGTH``.) Your templates +can use this e.g. to add a link to the full text at the end of the summary. + +reST example +~~~~~~~~~~~~ + +Inserting the markers into a reStructuredText document makes use of the +comment directive, because raw HTML is automatically escaped. The reST equivalent of the above Markdown example looks like this:: + + My super title + ############## + + :date: 2010-12-03 10:20 + :tags: thats, awesome + :category: yeah + :slug: my-super-post + :author: Alexis Metaireau + + This is the content of my super blog post. + + .. PELICAN_END_SUMMARY + + and this content occurs after the summary. diff --git a/plugins/summary/__init__.py b/plugins/summary/__init__.py new file mode 100755 index 0000000..afe9311 --- /dev/null +++ b/plugins/summary/__init__.py @@ -0,0 +1 @@ +from .summary import * diff --git a/plugins/summary/__pycache__/__init__.cpython-36.pyc b/plugins/summary/__pycache__/__init__.cpython-36.pyc Binary files differnew file mode 100644 index 0000000..a017b5d --- /dev/null +++ b/plugins/summary/__pycache__/__init__.cpython-36.pyc diff --git a/plugins/summary/__pycache__/summary.cpython-36.pyc b/plugins/summary/__pycache__/summary.cpython-36.pyc Binary files differnew file mode 100644 index 0000000..690703c --- /dev/null +++ b/plugins/summary/__pycache__/summary.cpython-36.pyc diff --git a/plugins/summary/summary.py b/plugins/summary/summary.py new file mode 100755 index 0000000..365f08f --- /dev/null +++ b/plugins/summary/summary.py @@ -0,0 +1,90 @@ +""" +Summary +------- + +This plugin allows easy, variable length summaries directly embedded into the +body of your articles. +""" + +from __future__ import unicode_literals +from pelican import signals +from pelican.generators import ArticlesGenerator, StaticGenerator, PagesGenerator + +def initialized(pelican): + from pelican.settings import DEFAULT_CONFIG + DEFAULT_CONFIG.setdefault('SUMMARY_BEGIN_MARKER', + '<!-- PELICAN_BEGIN_SUMMARY -->') + DEFAULT_CONFIG.setdefault('SUMMARY_END_MARKER', + '<!-- PELICAN_END_SUMMARY -->') + if pelican: + pelican.settings.setdefault('SUMMARY_BEGIN_MARKER', + '<!-- PELICAN_BEGIN_SUMMARY -->') + pelican.settings.setdefault('SUMMARY_END_MARKER', + '<!-- PELICAN_END_SUMMARY -->') + +def extract_summary(instance): + # if summary is already specified, use it + # if there is no content, there's nothing to do + if hasattr(instance, '_summary'): + instance.has_summary = True + return + + if not instance._content: + instance.has_summary = False + return + + begin_marker = instance.settings['SUMMARY_BEGIN_MARKER'] + end_marker = instance.settings['SUMMARY_END_MARKER'] + + content = instance._content + begin_summary = -1 + end_summary = -1 + if begin_marker: + begin_summary = content.find(begin_marker) + if end_marker: + end_summary = content.find(end_marker) + + if begin_summary == -1 and end_summary == -1: + instance.has_summary = False + return + + # skip over the begin marker, if present + if begin_summary == -1: + begin_summary = 0 + else: + begin_summary = begin_summary + len(begin_marker) + + if end_summary == -1: + end_summary = None + + summary = content[begin_summary:end_summary] + + # remove the markers from the content + if begin_summary: + content = content.replace(begin_marker, '', 1) + if end_summary: + content = content.replace(end_marker, '', 1) + + instance._content = content + instance._summary = summary + instance.has_summary = True + + +def run_plugin(generators): + for generator in generators: + if isinstance(generator, ArticlesGenerator): + for article in generator.articles: + extract_summary(article) + elif isinstance(generator, PagesGenerator): + for page in generator.pages: + extract_summary(page) + + +def register(): + signals.initialized.connect(initialized) + try: + signals.all_generators_finalized.connect(run_plugin) + except AttributeError: + # NOTE: This results in #314 so shouldn't really be relied on + # https://github.com/getpelican/pelican-plugins/issues/314 + signals.content_object_init.connect(extract_summary) diff --git a/plugins/summary/test_summary.py b/plugins/summary/test_summary.py new file mode 100755 index 0000000..c995106 --- /dev/null +++ b/plugins/summary/test_summary.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- + +import unittest + +from jinja2.utils import generate_lorem_ipsum + +# generate one paragraph, enclosed with <p> +TEST_CONTENT = str(generate_lorem_ipsum(n=1)) +TEST_SUMMARY = generate_lorem_ipsum(n=1, html=False) + + +from pelican.contents import Page + +import summary + +class TestSummary(unittest.TestCase): + def setUp(self): + super(TestSummary, self).setUp() + + summary.register() + summary.initialized(None) + self.page_kwargs = { + 'content': TEST_CONTENT, + 'context': { + 'localsiteurl': '', + }, + 'metadata': { + 'summary': TEST_SUMMARY, + 'title': 'foo bar', + 'author': 'Blogger', + }, + } + + def _copy_page_kwargs(self): + # make a deep copy of page_kwargs + page_kwargs = dict([(key, self.page_kwargs[key]) for key in + self.page_kwargs]) + for key in page_kwargs: + if not isinstance(page_kwargs[key], dict): + break + page_kwargs[key] = dict([(subkey, page_kwargs[key][subkey]) + for subkey in page_kwargs[key]]) + + return page_kwargs + + def test_end_summary(self): + page_kwargs = self._copy_page_kwargs() + del page_kwargs['metadata']['summary'] + page_kwargs['content'] = ( + TEST_SUMMARY + '<!-- PELICAN_END_SUMMARY -->' + TEST_CONTENT) + page = Page(**page_kwargs) + # test both the summary and the marker removal + self.assertEqual(page.summary, TEST_SUMMARY) + self.assertEqual(page.content, TEST_SUMMARY + TEST_CONTENT) + + def test_begin_summary(self): + page_kwargs = self._copy_page_kwargs() + del page_kwargs['metadata']['summary'] + page_kwargs['content'] = ( + 'FOOBAR<!-- PELICAN_BEGIN_SUMMARY -->' + TEST_CONTENT) + page = Page(**page_kwargs) + # test both the summary and the marker removal + self.assertEqual(page.summary, TEST_CONTENT) + self.assertEqual(page.content, 'FOOBAR' + TEST_CONTENT) + + def test_begin_end_summary(self): + page_kwargs = self._copy_page_kwargs() + del page_kwargs['metadata']['summary'] + page_kwargs['content'] = ( + 'FOOBAR<!-- PELICAN_BEGIN_SUMMARY -->' + TEST_SUMMARY + + '<!-- PELICAN_END_SUMMARY -->' + TEST_CONTENT) + page = Page(**page_kwargs) + # test both the summary and the marker removal + self.assertEqual(page.summary, TEST_SUMMARY) + self.assertEqual(page.content, 'FOOBAR' + TEST_SUMMARY + TEST_CONTENT) |