From 943e590c24892ce6fac4fdfd4bc4d581c216064d Mon Sep 17 00:00:00 2001 From: Vuong Nguyen Date: Wed, 4 Dec 2013 23:21:19 -0500 Subject: [PATCH 1/3] Read more link plugin -- initial commit --- read_more_link/Readme.md | 27 ++++++++++++ read_more_link/__init__.py | 1 + read_more_link/read_more_link.py | 71 ++++++++++++++++++++++++++++++++ read_more_link/requirements.txt | 1 + 4 files changed, 100 insertions(+) create mode 100644 read_more_link/Readme.md create mode 100644 read_more_link/__init__.py create mode 100644 read_more_link/read_more_link.py create mode 100644 read_more_link/requirements.txt diff --git a/read_more_link/Readme.md b/read_more_link/Readme.md new file mode 100644 index 0000000..7a8cfee --- /dev/null +++ b/read_more_link/Readme.md @@ -0,0 +1,27 @@ +Read More Link +=== + +**Author**: Vuong Nguyen (http://vuongnguyen.com) + +This plugin inserts an inline "read more" or "continue" link into the last html element of the object summary. + +For more information, please visit: http://vuongnguyen.com/creating-inline-read-more-link-python-pelican-lxml.html + +Requirements +--- + + `lxml` - for parsing html elements + +Settings +--- + :::python + # This settings indicates that you want to create summary at a certain length + SUMMARY_MAX_LENGTH = 50 + + # This indicates what goes inside the read more link + READ_MORE_LINK = None (ex: 'continue') + + # This is the format of the read more link + READ_MORE_LINK_FORMAT = '{text}' + + diff --git a/read_more_link/__init__.py b/read_more_link/__init__.py new file mode 100644 index 0000000..6b3673b --- /dev/null +++ b/read_more_link/__init__.py @@ -0,0 +1 @@ +from .read_more_link import * \ No newline at end of file diff --git a/read_more_link/read_more_link.py b/read_more_link/read_more_link.py new file mode 100644 index 0000000..bf6f094 --- /dev/null +++ b/read_more_link/read_more_link.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +""" +Read More Link +=========================== + +This plugin inserts an inline "read more" or "continue" link into the last html element of the object summary. + +For more information, please visit: http://vuongnguyen.com/creating-inline-read-more-link-python-pelican-lxml.html + +""" + +from pelican import signals, contents +from pelican.utils import truncate_html_words + +try: + from lxml.html import fragment_fromstring, fragments_fromstring, tostring + from lxml.etree import ParserError +except ImportError: + raise Exception("Unable to find lxml. To use READ_MORE_LINK, you need lxml") + + +def insert_into_last_element(html, element): + """ + function to insert an html element into another html fragment + example: + html = '

paragraph1

paragraph2...

' + element = 'read more' + ---> '

paragraph1

paragraph2...read more

' + """ + try: + item = fragment_fromstring(element) + except ParserError, TypeError: + item = fragment_fromstring('') + + try: + doc = fragments_fromstring(html) + doc[-1].append(item) + + return ''.join(tostring(e) for e in doc) + except ParserError, TypeError: + return '' + +def insert_read_more_link(instance): + """ + Insert an inline "read more" link into the last element of the summary + :param instance: + :return: + """ + + # only deals with Article type + if type(instance) != contents.Article: return + + + SUMMARY_MAX_LENGTH = instance.settings.get('SUMMARY_MAX_LENGTH') + READ_MORE_LINK = instance.settings.get('READ_MORE_LINK', None) + READ_MORE_LINK_FORMAT = instance.settings.get('READ_MORE_LINK_FORMAT', + '{text}') + + if not (SUMMARY_MAX_LENGTH and READ_MORE_LINK and READ_MORE_LINK_FORMAT): return + + if hasattr(instance, '_summary') and instance._summary: + summary = instance._summary + else: + summary = truncate_html_words(instance.content, SUMMARY_MAX_LENGTH) + + if summary=3.2.1 \ No newline at end of file From 1cf85f7a06a5b718779c3d76df10e582c3f87a30 Mon Sep 17 00:00:00 2001 From: Vuong Nguyen Date: Mon, 16 Dec 2013 17:01:57 -0500 Subject: [PATCH 2/3] Update Readme.md --- read_more_link/Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/read_more_link/Readme.md b/read_more_link/Readme.md index 7a8cfee..42074ba 100644 --- a/read_more_link/Readme.md +++ b/read_more_link/Readme.md @@ -10,11 +10,11 @@ For more information, please visit: http://vuongnguyen.com/creating-inline-read- Requirements --- - `lxml` - for parsing html elements + lxml - for parsing html elements Settings --- - :::python + ```python # This settings indicates that you want to create summary at a certain length SUMMARY_MAX_LENGTH = 50 From 30638d8ac4563ccebe9b4129fcfd9770cabe6f20 Mon Sep 17 00:00:00 2001 From: Vuong Nguyen Date: Mon, 16 Dec 2013 17:02:16 -0500 Subject: [PATCH 3/3] Update Readme.md --- read_more_link/Readme.md | 1 - 1 file changed, 1 deletion(-) diff --git a/read_more_link/Readme.md b/read_more_link/Readme.md index 42074ba..cc9b98b 100644 --- a/read_more_link/Readme.md +++ b/read_more_link/Readme.md @@ -14,7 +14,6 @@ Requirements Settings --- - ```python # This settings indicates that you want to create summary at a certain length SUMMARY_MAX_LENGTH = 50