~ubuntu-branches/ubuntu/lucid/canto/lucid

« back to all changes in this revision

Viewing changes to canto/handlers.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-04-17 21:11:00 UTC
  • mfrom: (2.1.5 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090417211100-011toye0gt8dk3pz
Tags: 0.6.10-1
MergingĀ upstreamĀ versionĀ 0.6.10.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#Canto - ncurses RSS reader
 
2
#   Copyright (C) 2008 Jack Miller <jack@codezen.org>
 
3
#
 
4
#   This program is free software; you can redistribute it and/or modify
 
5
#   it under the terms of the GNU General Public License version 2 as 
 
6
#   published by the Free Software Foundation.
 
7
 
 
8
class Handler():
 
9
    def __init__(self):
 
10
        self.reset()
 
11
 
 
12
    def get_attr(self, attrs, attr):
 
13
        l = [v for k,v in attrs if k == attr]
 
14
        if l :
 
15
            return l[0]
 
16
 
 
17
class LinkHandler(Handler):
 
18
    def reset(self):
 
19
        self.active = 0
 
20
        self.link = ""
 
21
        self.content = ""
 
22
        self.handler = "link"
 
23
 
 
24
    def match(self, tag, attrs, open, ll):
 
25
        if tag == "a":
 
26
            if open:
 
27
                href = self.get_attr(attrs, "href")
 
28
                if href:
 
29
                    self.link = href
 
30
                    self.active = 1
 
31
                    return "%4"
 
32
                else:
 
33
                    self.reset()
 
34
            else:
 
35
                ll.append((self.content, self.link, self.handler))
 
36
                self.reset()
 
37
                return u"[" + unicode(len(ll)) + u"]%0"
 
38
 
 
39
class ImageHandler(Handler):
 
40
    def reset(self):
 
41
        self.active = 0
 
42
        self.handler = "image"
 
43
 
 
44
    def match(self, tag, attrs, open, ll):
 
45
        if tag == "img":
 
46
            if open:
 
47
                src = self.get_attr(attrs, "src")
 
48
                alt = self.get_attr(attrs, "alt")
 
49
                if not alt:
 
50
                    alt = "image"
 
51
                if src:
 
52
                    extension = src.rsplit('.',1)[-1]
 
53
                    ll.append((alt, src, self.handler))
 
54
                self.reset()
 
55
                return u"%7["+ alt + u"][" + unicode(len(ll)) + u"]%0"