Mercurial > genshi > mirror
annotate genshi/util.py @ 1034:e02843c0fecc trunk
Add missing boolean attributes to XHTML and HTML serializers (fixes #570).
author | hodgestar |
---|---|
date | Wed, 19 Mar 2014 14:22:22 +0000 |
parents | 3f8fb83045b9 |
children |
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 # |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
3 # Copyright (C) 2006-2009 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 |
859 | 16 import htmlentitydefs as entities |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
17 import re |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
18 |
931
3f8fb83045b9
Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
859
diff
changeset
|
19 from compat import any, all, stringrepr |
3f8fb83045b9
Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
859
diff
changeset
|
20 |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
408
diff
changeset
|
21 __docformat__ = 'restructuredtext en' |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
408
diff
changeset
|
22 |
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
|
23 |
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
|
24 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
|
25 """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
|
26 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
|
27 |
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 >>> 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
|
29 >>> 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
|
30 >>> 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
|
31 >>> 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
|
32 >>> 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
|
33 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
|
34 |
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 >>> 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
|
36 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
|
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 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
|
39 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
|
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 >>> 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
|
42 >>> 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
|
43 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
|
44 >>> '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
|
45 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
|
46 |
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 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
|
48 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
|
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 >>> for key in cache: |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
51 ... print(key) |
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
|
52 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
|
53 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
|
54 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
|
55 |
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 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
|
57 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
|
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 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
|
60 """ |
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 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
|
63 def __init__(self, key, value): |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
64 self.prv = self.nxt = None |
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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 |
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 __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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 |
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 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
|
77 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
|
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 __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
|
80 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
|
81 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
|
82 yield cur.key |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
83 cur = cur.nxt |
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
|
84 |
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 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
|
86 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
|
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 __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
|
89 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
|
90 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
|
91 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
|
92 |
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 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 |
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 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
|
105 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
|
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 _insert_item(self, item): |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
108 item.prv = None |
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
109 item.nxt = self.head |
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
|
110 if self.head is not None: |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
111 self.head.prv = item |
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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 |
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 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
|
118 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
|
119 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
|
120 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
|
121 if self.tail != self.head: |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
122 self.tail = self.tail.prv |
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
123 self.tail.nxt = None |
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
|
124 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
|
125 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
|
126 |
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 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
|
128 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
|
129 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
|
130 |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
131 prv = item.prv |
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
132 prv.nxt = item.nxt |
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
133 if item.nxt is not None: |
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
134 item.nxt.prv = prv |
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
|
135 else: |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
136 self.tail = prv |
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
|
137 |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
138 item.prv = None |
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
139 item.nxt = self.head |
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
140 self.head.prv = 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
|
141 |
62de137b9322
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
142 |
62de137b9322
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
143 def flatten(items): |
433 | 144 """Flattens a potentially nested sequence into a flat list. |
145 | |
146 :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
|
147 |
62de137b9322
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
148 >>> 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
|
149 [1, 2] |
62de137b9322
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
150 >>> 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
|
151 [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
|
152 >>> 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
|
153 [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
|
154 """ |
62de137b9322
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
155 retval = [] |
62de137b9322
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
156 for item in items: |
580 | 157 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
|
158 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
|
159 else: |
62de137b9322
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
160 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
|
161 return retval |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
162 |
852
07f4339fecb0
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
163 |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
164 def plaintext(text, keeplinebreaks=True): |
856 | 165 """Return the text with all entities and tags removed. |
433 | 166 |
167 >>> plaintext('<b>1 < 2</b>') | |
168 u'1 < 2' | |
169 | |
170 The `keeplinebreaks` parameter can be set to ``False`` to replace any line | |
171 breaks by simple spaces: | |
172 | |
173 >>> plaintext('''<b>1 | |
174 ... < | |
175 ... 2</b>''', keeplinebreaks=False) | |
176 u'1 < 2' | |
177 | |
178 :param text: the text to convert to plain text | |
179 :param keeplinebreaks: whether line breaks in the text should be kept intact | |
180 :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
|
181 """ |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
182 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
|
183 if not keeplinebreaks: |
852
07f4339fecb0
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
184 text = text.replace('\n', ' ') |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
185 return text |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
186 |
852
07f4339fecb0
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
187 |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
188 _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
|
189 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
|
190 """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
|
191 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
|
192 |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
193 >>> stripentities('1 < 2') |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
194 u'1 < 2' |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
195 >>> stripentities('more …') |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
196 u'more \u2026' |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
197 >>> stripentities('…') |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
198 u'\u2026' |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
199 >>> stripentities('…') |
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 |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
202 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
|
203 core XML entities (&, ', >, < and ") are left intact. |
750 | 204 |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
205 >>> stripentities('1 < 2 …', keepxmlentities=True) |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
206 u'1 < 2 \u2026' |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
207 """ |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 else: |
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, 10) |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
215 return unichr(ref) |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
216 else: # character entity |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
217 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
|
218 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', 'quot'): |
852
07f4339fecb0
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
219 return '&%s;' % ref |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
220 try: |
856 | 221 return unichr(entities.name2codepoint[ref]) |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
222 except KeyError: |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
223 if keepxmlentities: |
852
07f4339fecb0
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
224 return '&%s;' % ref |
397
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
225 else: |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
226 return ref |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
227 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
|
228 |
852
07f4339fecb0
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
229 |
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
|
230 _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
|
231 def striptags(text): |
433 | 232 """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
|
233 |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
234 >>> 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
|
235 'Foo bar' |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
236 >>> 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
|
237 'Foo' |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
238 >>> striptags('Foo<br />') |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
239 'Foo' |
433 | 240 |
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
|
241 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
|
242 |
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 >>> 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
|
244 '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 |
433 | 246 :param text: the string to remove tags from |
247 :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
|
248 """ |
31742fe6d47e
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
249 return _STRIPTAGS_RE.sub('', text) |
856 | 250 |