"Fossies" - the Fresh Open Source Software Archive 
Member "Tahchee-1.0.0/Sources/tahchee/plugins/escape.py" (17 Feb 2009, 2975 Bytes) of package /linux/privat/old/tahchee-1.0.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "escape.py" see the
Fossies "Dox" file reference documentation.
1 # vim: ts=4
2 # -----------------------------------------------------------------------------
3 # Project : Tahchee <http://www.ivy.fr/tachee>
4 # -----------------------------------------------------------------------------
5 # Author : Sebastien Pierre <sebastien@ivy.fr>
6 # License : Revised BSD License
7 # -----------------------------------------------------------------------------
8 # Creation date : 27-Mar-2007
9 # Last mod. : 27-Mar-2007
10 # -----------------------------------------------------------------------------
11
12 import re, string, htmlentitydefs
13
14 NAME = "escape"
15 VERSION = None
16 SUMMARY = """Escapes a string so that it can be safely included into an HTML \
17 document."""
18
19 class EscapePlugin:
20
21 def __init__( self, site ):
22 self.site = site
23
24 def name( self ): return NAME
25 def summary( self ): return SUMMARY
26 def version( self ): return VERSION
27 def doc( self ): return __doc__
28
29 def install( self, localdict ):
30 localdict["escape"] = self
31
32 def toHTML( self, text ):
33 "Returns the given HTML with ampersands, quotes and carets encoded"
34 # NOTE: This is ripped from Django utils.html module
35 if not isinstance(text, basestring):
36 text = str(text)
37 return text.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')
38
39 def toText( self, text ):
40 """Expands the entities found in the given text and returns it as text."""
41 # NOTE: This is based on
42 # <http://www.shearersoftware.com/software/developers/htmlfilter/>
43 entityStart = text.find('&')
44 if entityStart != -1: # only run bulk of code if there are entities present
45 preferUnicodeToISO8859 = 1 #(outputEncoding is not 'iso-8859-1')
46 prevOffset = 0
47 textParts = []
48 while entityStart != -1:
49 textParts.append(text[prevOffset:entityStart])
50 entityEnd = text.find(';', entityStart+1)
51 if entityEnd == -1:
52 entityEnd = entityStart
53 entity = '&'
54 else:
55 entity = text[entityStart:entityEnd+1]
56 if len(entity) < 4 or entity[1] != '#':
57 entity = htmlentitydefs.entitydefs.get(entity[1:-1],entity)
58 if len(entity) == 1:
59 if preferUnicodeToISO8859 and ord(entity) > 127 and hasattr(entity, 'decode'):
60 entity = entity.decode('iso-8859-1')
61 else:
62 if len(entity) >= 4 and entity[1] == '#':
63 if entity[2] in ('X','x'):
64 entityCode = int(entity[3:-1], 16)
65 else:
66 entityCode = int(entity[2:-1])
67 if entityCode > 255:
68 entity = unichr(entityCode)
69 else:
70 entity = chr(entityCode)
71 if preferUnicodeToISO8859 and hasattr(entity, 'decode'):
72 entity = entity.decode('iso-8859-1')
73 textParts.append(entity)
74 prevOffset = entityEnd+1
75 entityStart = text.find('&', prevOffset)
76 textParts.append(text[prevOffset:])
77 text = ''.join(textParts)
78 return text
79
80 def __call__( self, text ):
81 return self.toHTML(text)
82
83 # EOF