annotate genshi/util.py @ 655:14aa86c3e514 trunk

The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
author cmlenz
date Thu, 22 Nov 2007 19:43:05 +0000
parents 678278fa92e7
children 52219748e5c1
rev   line source
274
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
2 #
408
4675d5cf6c67 Update copyright year for files modified this year.
cmlenz
parents: 397
diff changeset
3 # Copyright (C) 2006-2007 Edgewall Software
274
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
4 # All rights reserved.
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
5 #
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
9 #
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
13
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
14 """Various utility classes and functions."""
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
15
397
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
16 import htmlentitydefs
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
17 import re
580
678278fa92e7 Also handle sets in flatten utility function.
cmlenz
parents: 433
diff changeset
18 try:
678278fa92e7 Also handle sets in flatten utility function.
cmlenz
parents: 433
diff changeset
19 set
678278fa92e7 Also handle sets in flatten utility function.
cmlenz
parents: 433
diff changeset
20 except NameError:
678278fa92e7 Also handle sets in flatten utility function.
cmlenz
parents: 433
diff changeset
21 from sets import ImmutableSet as frozenset
678278fa92e7 Also handle sets in flatten utility function.
cmlenz
parents: 433
diff changeset
22 from sets import Set as set
397
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
23
425
073640758a42 Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents: 408
diff changeset
24 __docformat__ = 'restructuredtext en'
073640758a42 Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents: 408
diff changeset
25
274
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
26
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
27 class LRUCache(dict):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
28 """A dictionary-like object that stores only a certain number of items, and
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
29 discards its least recently used item when full.
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
30
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
31 >>> cache = LRUCache(3)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
32 >>> cache['A'] = 0
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
33 >>> cache['B'] = 1
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
34 >>> cache['C'] = 2
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
35 >>> len(cache)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
36 3
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
37
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
38 >>> cache['A']
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
39 0
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
40
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
41 Adding new items to the cache does not increase its size. Instead, the least
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
42 recently used item is dropped:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
43
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
44 >>> cache['D'] = 3
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
45 >>> len(cache)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
46 3
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
47 >>> 'B' in cache
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
48 False
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
49
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
50 Iterating over the cache returns the keys, starting with the most recently
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
51 used:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
52
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
53 >>> for key in cache:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
54 ... print key
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
55 D
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
56 A
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
57 C
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
58
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
59 This code is based on the LRUCache class from ``myghtyutils.util``, written
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
60 by Mike Bayer and released under the MIT license. See:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
61
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
62 http://svn.myghty.org/myghtyutils/trunk/lib/myghtyutils/util.py
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
63 """
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
64
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
65 class _Item(object):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
66 def __init__(self, key, value):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
67 self.previous = self.next = None
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
68 self.key = key
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
69 self.value = value
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
70 def __repr__(self):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
71 return repr(self.value)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
72
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
73 def __init__(self, capacity):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
74 self._dict = dict()
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
75 self.capacity = capacity
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
76 self.head = None
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
77 self.tail = None
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
78
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
79 def __contains__(self, key):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
80 return key in self._dict
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
81
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
82 def __iter__(self):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
83 cur = self.head
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
84 while cur:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
85 yield cur.key
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
86 cur = cur.next
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
87
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
88 def __len__(self):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
89 return len(self._dict)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
90
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
91 def __getitem__(self, key):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
92 item = self._dict[key]
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
93 self._update_item(item)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
94 return item.value
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
95
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
96 def __setitem__(self, key, value):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
97 item = self._dict.get(key)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
98 if item is None:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
99 item = self._Item(key, value)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
100 self._dict[key] = item
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
101 self._insert_item(item)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
102 else:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
103 item.value = value
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
104 self._update_item(item)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
105 self._manage_size()
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
106
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
107 def __repr__(self):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
108 return repr(self._dict)
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
109
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
110 def _insert_item(self, item):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
111 item.previous = None
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
112 item.next = self.head
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
113 if self.head is not None:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
114 self.head.previous = item
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
115 else:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
116 self.tail = item
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
117 self.head = item
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
118 self._manage_size()
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
119
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
120 def _manage_size(self):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
121 while len(self._dict) > self.capacity:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
122 olditem = self._dict[self.tail.key]
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
123 del self._dict[self.tail.key]
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
124 if self.tail != self.head:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
125 self.tail = self.tail.previous
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
126 self.tail.next = None
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
127 else:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
128 self.head = self.tail = None
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
129
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
130 def _update_item(self, item):
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
131 if self.head == item:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
132 return
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
133
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
134 previous = item.previous
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
135 previous.next = item.next
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
136 if item.next is not None:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
137 item.next.previous = previous
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
138 else:
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
139 self.tail = previous
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
140
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
141 item.previous = None
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
142 item.next = self.head
f2b8932a610e Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff changeset
143 self.head.previous = self.head = item
357
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
144
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
145
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
146 def flatten(items):
433
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
147 """Flattens a potentially nested sequence into a flat list.
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
148
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
149 :param items: the sequence to flatten
357
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
150
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
151 >>> flatten((1, 2))
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
152 [1, 2]
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
153 >>> flatten([1, (2, 3), 4])
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
154 [1, 2, 3, 4]
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
155 >>> flatten([1, (2, [3, 4]), 5])
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
156 [1, 2, 3, 4, 5]
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
157 """
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
158 retval = []
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
159 for item in items:
580
678278fa92e7 Also handle sets in flatten utility function.
cmlenz
parents: 433
diff changeset
160 if isinstance(item, (frozenset, list, set, tuple)):
357
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
161 retval += flatten(item)
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
162 else:
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
163 retval.append(item)
62de137b9322 Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents: 274
diff changeset
164 return retval
397
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
165
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
166 def plaintext(text, keeplinebreaks=True):
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
167 """Returns the text as a `unicode` string with all entities and tags
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
168 removed.
433
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
169
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
170 >>> plaintext('<b>1 &lt; 2</b>')
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
171 u'1 < 2'
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
172
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
173 The `keeplinebreaks` parameter can be set to ``False`` to replace any line
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
174 breaks by simple spaces:
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
175
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
176 >>> plaintext('''<b>1
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
177 ... &lt;
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
178 ... 2</b>''', keeplinebreaks=False)
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
179 u'1 < 2'
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
180
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
181 :param text: the text to convert to plain text
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
182 :param keeplinebreaks: whether line breaks in the text should be kept intact
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
183 :return: the text with tags and entities removed
397
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
184 """
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
185 text = stripentities(striptags(text))
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
186 if not keeplinebreaks:
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
187 text = text.replace(u'\n', u' ')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
188 return text
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
189
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
190 _STRIPENTITIES_RE = re.compile(r'&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
191 def stripentities(text, keepxmlentities=False):
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
192 """Return a copy of the given text with any character or numeric entities
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
193 replaced by the equivalent UTF-8 characters.
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
194
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
195 >>> stripentities('1 &lt; 2')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
196 u'1 < 2'
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
197 >>> stripentities('more &hellip;')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
198 u'more \u2026'
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
199 >>> stripentities('&#8230;')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
200 u'\u2026'
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
201 >>> stripentities('&#x2026;')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
202 u'\u2026'
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
203
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
204 If the `keepxmlentities` parameter is provided and is a truth value, the
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
205 core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;) are left intact.
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
206
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
207 >>> stripentities('1 &lt; 2 &hellip;', keepxmlentities=True)
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
208 u'1 &lt; 2 \u2026'
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
209 """
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
210 def _replace_entity(match):
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
211 if match.group(1): # numeric entity
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
212 ref = match.group(1)
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
213 if ref.startswith('x'):
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
214 ref = int(ref[1:], 16)
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
215 else:
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
216 ref = int(ref, 10)
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
217 return unichr(ref)
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
218 else: # character entity
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
219 ref = match.group(2)
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
220 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', 'quot'):
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
221 return u'&%s;' % ref
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
222 try:
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
223 return unichr(htmlentitydefs.name2codepoint[ref])
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
224 except KeyError:
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
225 if keepxmlentities:
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
226 return u'&amp;%s;' % ref
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
227 else:
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
228 return ref
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
229 return _STRIPENTITIES_RE.sub(_replace_entity, text)
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
230
655
14aa86c3e514 The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents: 580
diff changeset
231 _STRIPTAGS_RE = re.compile(r'(<!--.*?-->|<[^>]*>)')
397
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
232 def striptags(text):
433
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
233 """Return a copy of the text with any XML/HTML tags removed.
397
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
234
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
235 >>> striptags('<span>Foo</span> bar')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
236 'Foo bar'
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
237 >>> striptags('<span class="bar">Foo</span>')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
238 'Foo'
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
239 >>> striptags('Foo<br />')
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
240 'Foo'
433
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
241
655
14aa86c3e514 The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents: 580
diff changeset
242 HTML/XML comments are stripped, too:
14aa86c3e514 The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents: 580
diff changeset
243
14aa86c3e514 The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents: 580
diff changeset
244 >>> striptags('<!-- <blub>hehe</blah> -->test')
14aa86c3e514 The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents: 580
diff changeset
245 'test'
14aa86c3e514 The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents: 580
diff changeset
246
433
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
247 :param text: the string to remove tags from
bc430fd7c54d More API docs.
cmlenz
parents: 425
diff changeset
248 :return: the text with tags removed
397
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
249 """
31742fe6d47e * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 357
diff changeset
250 return _STRIPTAGS_RE.sub('', text)
Copyright (C) 2012-2017 Edgewall Software