-
Notifications
You must be signed in to change notification settings - Fork 1
/
inline_link.py
64 lines (46 loc) · 1.93 KB
/
inline_link.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""inline-link ver 0.1
Copyright 2006 Brian Jaress
This plugin is for when you want to handle all your feeds in a
consistent way, but they all use the 'description' field differently.
Some of them use it to describe the article, some use it to repeat the
whole article, some make up it up as they go, etc.
This adds an '__inline_link__' for use in the item template. What you get
is the first N non-html characters of the description, turned into an
inline link of the whole description. See
http://en.wikipedia.org/wiki/Data:_URL for an explanation of that.
You can set 'inline_charset' and 'inline_length' as feed arguments. You
probably want to set the default inline_charset to match the encoding
you use for the rawdog template.
Brian Jaress
"""
import rawdoglib.plugins
from re import sub
def inline_link(rawdog, config, feed, article, itembits):
parts = {}
args = rawdog.feeds[article.feed].args
try: parts['title'] = article.entry_info["title"]
except: parts['title'] = '(no title)'
try: parts['desc'] = article.entry_info["description"]
except: parts['desc'] = ''
try: parts['link'] = article.entry_info["link"]
except: parts['link'] = ''
try: inline_length = int(args['inline_length'])
except: inline_length = 30
try: inline_charset = args['inline_charset']
except: inline_charset = 'utf-8'
parts['short'] = sub('<[^>]*>', '', parts['desc'])[:inline_length]
parts['html'] = """
<html><head><title>%(title)s</title></head>
<body>%(desc)s<br/>
<a href="%(link)s">[full]</a>
</body></html>
"""
parts['uri'] = 'data:text/html;base64,%(html)s'
parts['anchor'] = '<a href="%(uri)s">%(short)s</a>'
for k in parts: parts[k] = unicode(parts[k]).encode(inline_charset)
parts['html'] = (parts['html'] % parts).encode('base64')
for k in ('uri', 'anchor'):
parts[k] = (parts[k] % parts)
itembits['inline_link'] = parts['anchor']
return True
rawdoglib.plugins.attach_hook("output_item_bits", inline_link)