diff --git a/read_more_link/Readme.md b/read_more_link/Readme.md new file mode 100644 index 0000000..cc9b98b --- /dev/null +++ b/read_more_link/Readme.md @@ -0,0 +1,26 @@ +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 +--- + # 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