source: LinkExchange/trunk/linkexchange/formatters.py @ 116

Revision 116, 6.3 KB checked in by lostclus, 21 months ago (diff)

Feature to use particular formatters only for particular clients. The client parameter was added to the BaseFormatter? class.

Line 
1# LinkExchange - Universal link exchange service client
2# Copyright (C) 2009 Konstantin Korikov
3#
4# This library is free software; you can redistribute it and/or
5# modify it under the terms of the GNU Lesser General Public
6# License as published by the Free Software Foundation; either
7# version 2.1 of the License, or (at your option) any later version.
8#
9# This library is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public
15# License along with this library; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17#
18# NOTE: In the context of the Python environment, I interpret "dynamic
19# linking" as importing -- thus the LGPL applies to the contents of
20# the modules, but make no requirements on code importing these
21# modules.
22
23from xml.sax import saxutils
24
25class BaseFormatter(object):
26    def __init__(self, count, client=None):
27        """
28        @param count: links count in the block (0 or None - catch all)
29        @keyword client: catch links only from client
30        """
31        self.count = count
32        self.client = client
33
34    def format(self, tags, links):
35        """
36        Perform links formatting. tags is a list of unicode strings that don't
37        contains '<a '. links is a list of unicode strings that contains links
38        HTML code.
39
40        @param tags: list of special tags
41        @param links: list of links
42        @return: HTML code
43        """
44        pass
45
46    def _add_prefix(self, link, prefix):
47        i = 0
48        while not (prefix[:i] + link).startswith(prefix):
49            i += 1
50        link = prefix[:i] + link
51        return link
52
53    def _add_suffix(self, link, suffix):
54        i = len(suffix)
55        while not (link + suffix[i:]).endswith(suffix):
56            i -= 1
57        link += suffix[i:]
58        return link
59
60    def _format_container(self, tag, id, class_, content):
61        html = u'<' + tag
62        if id:
63            html += u' id="%s"' % saxutils.escape(id)
64        if class_:
65            html += u' class="%s"' % saxutils.escape(class_)
66        html += u'>'
67        html += content
68        html += u'</%s>' % tag
69        return html
70
71class InlineFormatter(BaseFormatter):
72    def __init__(self, count,
73            client=None,
74            delimiter='',
75            prefix='',
76            suffix='',
77            prolog='',
78            epilog='',
79            id=None,
80            class_=None,
81            class_for_empty=None,
82            strip=False):
83        """
84        @param count: links count in the block (None - catch all)
85        @keyword client: catch links only from client
86        @keyword delimiter: links delimiter
87        @keyword prefix: links prefix
88        @keyword suffix: links suffix
89        @keyword prolog: text before links
90        @keyword epilog: text after links
91        @keyword id: value for id attribute
92        @keyword class_: CSS class for nonempty block
93        @keyword class_for_empty: CSS class for empty block
94        @keyword strip: skip DIV tag
95        """
96        super(InlineFormatter, self).__init__(count, client=client)
97        self.delimiter = delimiter
98        self.prefix = prefix
99        self.suffix = suffix
100        self.prolog = prolog
101        self.epilog = epilog
102        self.id = id
103        self.class_ = class_
104        self.class_for_empty = class_for_empty
105        self.strip = strip
106
107    def format(self, tags, links):
108        def format_link(link):
109            if self.prefix:
110                link = self._add_prefix(link, self.prefix)
111            if self.suffix:
112                link = self._add_suffix(link, self.suffix)
113            return link
114        if links:
115            css_class = self.class_
116            content = self.prolog + self.delimiter.join(
117                    map(format_link, links)) + self.epilog
118        else:
119            css_class = self.class_for_empty
120            content = u''
121        if self.strip:
122            html = content
123        else:
124            html = self._format_container('div', self.id, css_class, content)
125        html += u''.join(tags)
126        return html
127
128class ListFormatter(BaseFormatter):
129    def __init__(self, count,
130            client=None,
131            prefix='',
132            suffix='',
133            id=None,
134            class_=None,
135            class_for_empty=None,
136            li_class=None,
137            tag_for_empty='span',
138            strip=False):
139        """
140        @param count: links count in the block (None - catch all)
141        @keyword client: catch links only from client
142        @keyword prefix: links prefix
143        @keyword suffix: links suffix
144        @keyword id: value for id attribute
145        @keyword class_: CSS class for nonempty block
146        @keyword class_for_empty: CSS class for empty block
147        @keyword li_class: CSS class for LI elements
148        @keyword tag_for_empty: HTML tag for empty block
149        @keyword strip: skip UL tag or empty tag
150        """
151        super(ListFormatter, self).__init__(count, client=client)
152        self.prefix = prefix
153        self.suffix = suffix
154        self.id = id
155        self.class_ = class_
156        self.class_for_empty = class_for_empty
157        self.li_class = li_class
158        self.tag_for_empty = tag_for_empty
159        self.strip = strip
160
161    def format(self, tags, links):
162        def format_link(link):
163            if self.prefix:
164                link = self._add_prefix(link, self.prefix)
165            if self.suffix:
166                link = self._add_suffix(link, self.suffix)
167            if self.li_class:
168                attrs = u' class="%s"' % saxutils.escape(self.li_class)
169            else:
170                attrs = u''
171            link = u'<li%s>%s</li>' % (attrs, link)
172            return link
173        content = u''.join(map(format_link, links))
174        if self.strip:
175            html = content
176        else:
177            if links:
178                css_class = self.class_
179            else:
180                css_class = self.class_for_empty
181            html_tag = links and u'ul' or self.tag_for_empty
182            html = self._format_container(html_tag, self.id,
183                    css_class, content)
184        html += u''.join(tags)
185        return html
Note: See TracBrowser for help on using the repository browser.