Skip to content
Browse files

Implement and use filter to show the first N paragraphs: intro.

This filter replaces the use of the summarize filter. With the latter
it is harder to control what is shown in the entry listings; sometimes
headings also appear in the summary if the first paragraph is short
enough. What I want is to shown the first paragraph of an article. The
new filter does this.
  • Loading branch information...
1 parent f045e80 commit 775c675cbaef98383a9b3e5021b1bfe5ccd7f738 @markvl committed
Showing with 74 additions and 3 deletions.
  1. +4 −3 conf.py
  2. +70 −0 filters/intro.py
View
7 conf.py
@@ -20,16 +20,16 @@
FILTERS = ['markdown', 'typography', 'h1']
VIEWS = {
# Article indexes
- '/': {'filters': 'summarize+16', 'view': 'index',
+ '/': {'filters': 'intro', 'view': 'index',
'pagination': '/page/:num'},
- '/weblog/': {'filters': 'summarize+16', 'view': 'index',
+ '/weblog/': {'filters': 'intro', 'view': 'index',
'pagination': '/weblog/page/:num'},
# Articles
'/weblog/:year/:month/:day/:slug/': {'view': 'entry'},
# Tag indexes
- '/weblog/tag/:name/': {'filters': 'summarize+16', 'view': 'tag',
+ '/weblog/tag/:name/': {'filters': 'intro', 'view': 'tag',
'pagination': '/weblog/tag/:name/:num'},
# Atom feeds
@@ -43,6 +43,7 @@
# Pages
'/:slug/': {'view': 'page'},
}
+FILTERS_DIR = 'filters/'
STATIC = ['assets', ]
DATE_FORMAT = '%Y-%m-%d %H:%M'
OUTPUT_IGNORE = ['/css/*', ]
View
70 filters/intro.py
@@ -0,0 +1,70 @@
+from acrylamid import log
+from acrylamid.filters import Filter
+from acrylamid.lib.html import HTMLParser, HTMLParseError
+
+
+class Introducer(HTMLParser):
+
+ def __init__(self, html, maxparagraphs=1):
+ self.maxparagraphs = maxparagraphs
+ self.paragraphs = 0
+ super(Introducer, self).__init__(html)
+
+ def handle_starttag(self, tag, attrs):
+ if self.paragraphs < self.maxparagraphs:
+ super(Introducer, self).handle_starttag(tag, attrs)
+
+ def handle_data(self, data):
+ if len(self.stack) < 1 or self.stack[0] != 'p':
+ pass
+ elif self.paragraphs >= self.maxparagraphs:
+ pass
+ else:
+ self.result.append(data)
+
+ def handle_endtag(self, tag):
+ if self.paragraphs < self.maxparagraphs:
+ if tag == 'p':
+ self.paragraphs += 1
+ super(Introducer, self).handle_endtag(tag)
+
+ def handle_startendtag(self, tag, attrs):
+ if self.paragraphs < self.maxparagraphs:
+ super(Introducer, self).handle_startendtag(tag, attrs)
+
+ def handle_entityref(self, name):
+ if self.paragraphs < self.maxparagraphs:
+ super(Introducer, self).handle_entityref(name)
+
+ def handle_charref(self, char):
+ if self.paragraphs < self.maxparagraphs:
+ super(Introducer, self).handle_charref(char)
+
+ def handle_comment(self, comment):
+ if self.paragraphs < self.maxparagraphs:
+ super(Introducer, self).handle_comment(comment)
+
+
+class Introduction(Filter):
+
+ match = ['intro', ]
+
+ def transform(self, content, entry, *args):
+ try:
+ maxparagraphs = int(entry.intro.maxparagraphs)
+ except (AttributeError, KeyError, ValueError) as e:
+ try:
+ maxparagraphs = int(args[0])
+ except (ValueError, IndexError) as e:
+ if e.__class__.__name__ == 'ValueError':
+ log.warn('Introduction: invalid maxparagraphs argument %r',
+ args[0])
+ maxparagraphs = 1
+
+ try:
+ return ''.join(Introducer(content, maxparagraphs).result)
+ except HTMLParseError as e:
+ log.warn('%s: %s in %s' % (e.__class__.__name__, e.msg,
+ entry.filename))
+ return content
+ return content

0 comments on commit 775c675

Please sign in to comment.
Something went wrong with that request. Please try again.