-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtruncate.py
47 lines (40 loc) · 1.27 KB
/
truncate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# rawdog plugin to truncate article descriptions to N characters.
# Copyright 2006, 2013 Adam Sampson <ats@offog.org>
#
# To use this, give the feed you want to truncate a "truncate" argument:
# feed 30m http://offog.org/books/feed.rss
# truncate 40
#
# To truncate all articles, make that a default option for all feeds:
# feeddefaults
# truncate 40
#
# You can also remove all HTML tags from the descriptions before truncating
# them, which'll make the formatting a bit nicer if you're aiming for very
# short descriptions:
# killtags true
import rawdoglib.plugins, re
def article_seen(rawdog, config, article, ignore):
fargs = rawdog.feeds[article.feed].args
n = int(fargs.get("truncate", "0"))
killtags = fargs.get("killtags", False) == "true"
def process(detail, n):
v = detail["value"]
if killtags:
v = re.sub(r'<[^>]*>', ' ', v)
if n != 0 and len(v) > n:
# Don't break a tag in half.
l = v.rfind("<", 0, n)
r = v.rfind(">", 0, n)
if l != -1 and r < l:
n = l
v = v[:n].rstrip() + "..."
detail["value"] = v.strip()
ei = article.entry_info
if "content" in ei:
for detail in ei["content"]:
process(detail, n)
if "summary_detail" in ei:
process(ei["summary_detail"], n)
return True
rawdoglib.plugins.attach_hook("article_seen", article_seen)