Mercurial > genshi > genshi-test
annotate genshi/util.py @ 397:d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
* Minor performance tweaks for the serializers
author | cmlenz |
---|---|
date | Tue, 02 Jan 2007 17:48:06 +0000 |
parents | c5684b65c9b7 |
children | 49a3bae5a8bb 0742f421caba |
rev | line source |
---|---|
274
c5ec3146fcb6
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 -*- |
c5ec3146fcb6
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 # |
c5ec3146fcb6
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
|
3 # Copyright (C) 2006 Edgewall Software |
c5ec3146fcb6
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. |
c5ec3146fcb6
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 # |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 |
c5ec3146fcb6
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. |
c5ec3146fcb6
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 # |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 |
c5ec3146fcb6
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/. |
c5ec3146fcb6
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 |
c5ec3146fcb6
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.""" |
c5ec3146fcb6
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
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
16 import htmlentitydefs |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
17 import re |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
18 |
274
c5ec3146fcb6
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
|
19 |
c5ec3146fcb6
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
|
20 class LRUCache(dict): |
c5ec3146fcb6
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
|
21 """A dictionary-like object that stores only a certain number of items, and |
c5ec3146fcb6
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
|
22 discards its least recently used item when full. |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 >>> cache = LRUCache(3) |
c5ec3146fcb6
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 >>> cache['A'] = 0 |
c5ec3146fcb6
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 >>> cache['B'] = 1 |
c5ec3146fcb6
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 >>> cache['C'] = 2 |
c5ec3146fcb6
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 >>> len(cache) |
c5ec3146fcb6
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 3 |
c5ec3146fcb6
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 |
c5ec3146fcb6
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['A'] |
c5ec3146fcb6
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 0 |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 Adding new items to the cache does not increase its size. Instead, the least |
c5ec3146fcb6
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 recently used item is dropped: |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 >>> cache['D'] = 3 |
c5ec3146fcb6
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 >>> len(cache) |
c5ec3146fcb6
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 3 |
c5ec3146fcb6
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 >>> 'B' in cache |
c5ec3146fcb6
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 False |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 Iterating over the cache returns the keys, starting with the most recently |
c5ec3146fcb6
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 used: |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 >>> for key in cache: |
c5ec3146fcb6
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 ... print key |
c5ec3146fcb6
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 D |
c5ec3146fcb6
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 A |
c5ec3146fcb6
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 C |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 This code is based on the LRUCache class from ``myghtyutils.util``, written |
c5ec3146fcb6
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 by Mike Bayer and released under the MIT license. See: |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 http://svn.myghty.org/myghtyutils/trunk/lib/myghtyutils/util.py |
c5ec3146fcb6
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 """ |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 class _Item(object): |
c5ec3146fcb6
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 def __init__(self, key, value): |
c5ec3146fcb6
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 self.previous = self.next = None |
c5ec3146fcb6
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 self.key = key |
c5ec3146fcb6
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 self.value = value |
c5ec3146fcb6
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 __repr__(self): |
c5ec3146fcb6
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 return repr(self.value) |
c5ec3146fcb6
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 |
c5ec3146fcb6
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, capacity): |
c5ec3146fcb6
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._dict = dict() |
c5ec3146fcb6
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.capacity = capacity |
c5ec3146fcb6
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.head = None |
c5ec3146fcb6
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 self.tail = None |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def __contains__(self, key): |
c5ec3146fcb6
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 return key in self._dict |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def __iter__(self): |
c5ec3146fcb6
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 cur = self.head |
c5ec3146fcb6
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 while cur: |
c5ec3146fcb6
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 yield cur.key |
c5ec3146fcb6
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 cur = cur.next |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def __len__(self): |
c5ec3146fcb6
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 return len(self._dict) |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def __getitem__(self, key): |
c5ec3146fcb6
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 item = self._dict[key] |
c5ec3146fcb6
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 self._update_item(item) |
c5ec3146fcb6
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 return item.value |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def __setitem__(self, key, value): |
c5ec3146fcb6
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 item = self._dict.get(key) |
c5ec3146fcb6
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 if item is None: |
c5ec3146fcb6
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._Item(key, value) |
c5ec3146fcb6
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._dict[key] = item |
c5ec3146fcb6
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 self._insert_item(item) |
c5ec3146fcb6
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 else: |
c5ec3146fcb6
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.value = value |
c5ec3146fcb6
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._update_item(item) |
c5ec3146fcb6
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._manage_size() |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def __repr__(self): |
c5ec3146fcb6
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 return repr(self._dict) |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def _insert_item(self, item): |
c5ec3146fcb6
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 item.previous = None |
c5ec3146fcb6
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 item.next = self.head |
c5ec3146fcb6
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 if self.head is not None: |
c5ec3146fcb6
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 self.head.previous = item |
c5ec3146fcb6
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 else: |
c5ec3146fcb6
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 self.tail = item |
c5ec3146fcb6
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 self.head = item |
c5ec3146fcb6
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 self._manage_size() |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def _manage_size(self): |
c5ec3146fcb6
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 while len(self._dict) > self.capacity: |
c5ec3146fcb6
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 olditem = self._dict[self.tail.key] |
c5ec3146fcb6
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 del self._dict[self.tail.key] |
c5ec3146fcb6
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 if self.tail != self.head: |
c5ec3146fcb6
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.tail = self.tail.previous |
c5ec3146fcb6
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 self.tail.next = None |
c5ec3146fcb6
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 else: |
c5ec3146fcb6
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 self.head = self.tail = None |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 def _update_item(self, item): |
c5ec3146fcb6
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.head == item: |
c5ec3146fcb6
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 return |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 previous = item.previous |
c5ec3146fcb6
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 previous.next = item.next |
c5ec3146fcb6
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 if item.next is not None: |
c5ec3146fcb6
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 item.next.previous = previous |
c5ec3146fcb6
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 else: |
c5ec3146fcb6
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 self.tail = previous |
c5ec3146fcb6
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 |
c5ec3146fcb6
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 item.previous = None |
c5ec3146fcb6
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 item.next = self.head |
c5ec3146fcb6
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 self.head.previous = self.head = item |
357
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
137 |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
138 |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
139 def flatten(items): |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
140 """Flattens a potentially nested sequence into a flat list: |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
141 |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
142 >>> flatten((1, 2)) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
143 [1, 2] |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
144 >>> flatten([1, (2, 3), 4]) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
145 [1, 2, 3, 4] |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
146 >>> flatten([1, (2, [3, 4]), 5]) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
147 [1, 2, 3, 4, 5] |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
148 """ |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
149 retval = [] |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
150 for item in items: |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
151 if isinstance(item, (list, tuple)): |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
152 retval += flatten(item) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
153 else: |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
154 retval.append(item) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
155 return retval |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
156 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
157 def plaintext(text, keeplinebreaks=True): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
158 """Returns the text as a `unicode` string with all entities and tags |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
159 removed. |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
160 """ |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
161 text = stripentities(striptags(text)) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
162 if not keeplinebreaks: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
163 text = text.replace(u'\n', u' ') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
164 return text |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
165 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
166 _STRIPENTITIES_RE = re.compile(r'&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
167 def stripentities(text, keepxmlentities=False): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
168 """Return a copy of the given text with any character or numeric entities |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
169 replaced by the equivalent UTF-8 characters. |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
170 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
171 >>> stripentities('1 < 2') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
172 u'1 < 2' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
173 >>> stripentities('more …') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
174 u'more \u2026' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
175 >>> stripentities('…') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
176 u'\u2026' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
177 >>> stripentities('…') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
178 u'\u2026' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
179 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
180 If the `keepxmlentities` parameter is provided and is a truth value, the |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
181 core XML entities (&, ', >, < and ") are left intact. |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
182 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
183 >>> stripentities('1 < 2 …', keepxmlentities=True) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
184 u'1 < 2 \u2026' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
185 """ |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
186 def _replace_entity(match): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
187 if match.group(1): # numeric entity |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
188 ref = match.group(1) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
189 if ref.startswith('x'): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
190 ref = int(ref[1:], 16) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
191 else: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
192 ref = int(ref, 10) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
193 return unichr(ref) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
194 else: # character entity |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
195 ref = match.group(2) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
196 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', 'quot'): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
197 return u'&%s;' % ref |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
198 try: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
199 return unichr(htmlentitydefs.name2codepoint[ref]) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
200 except KeyError: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
201 if keepxmlentities: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
202 return u'&%s;' % ref |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
203 else: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
204 return ref |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
205 return _STRIPENTITIES_RE.sub(_replace_entity, text) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
206 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
207 _STRIPTAGS_RE = re.compile(r'<[^>]*?>') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
208 def striptags(text): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
209 """Return a copy of the text with all XML/HTML tags removed. |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
210 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
211 >>> striptags('<span>Foo</span> bar') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
212 'Foo bar' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
213 >>> striptags('<span class="bar">Foo</span>') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
214 'Foo' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
215 >>> striptags('Foo<br />') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
216 'Foo' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
217 """ |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
218 return _STRIPTAGS_RE.sub('', text) |