~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to test/util/build_page/libs/pywebsite/escape.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# coding: utf-8
 
2
 
 
3
################################################################################
 
4
 
 
5
import re
 
6
import htmlentitydefs
 
7
 
 
8
_escape_re = re.compile(eval(r'u"[&<>\"]|[\u0080-\uffff]+"'))
 
9
 
 
10
################################################################################
 
11
 
 
12
def _escape_sub(match):
 
13
    try:
 
14
        entity_code = ord(match.group(0))
 
15
    
 
16
    except Exception, e:
 
17
        print match.group(0)
 
18
        raise
 
19
 
 
20
    named_entitiy = htmlentitydefs.codepoint2name.get(entity_code)
 
21
    if named_entitiy: return '&%s;' % named_entitiy
 
22
    else: return '&#%d;' % entity_code
 
23
 
 
24
################################################################################
 
25
 
 
26
def escape(uni, codec=None):
 
27
    if codec:  uni = uni.decode(codec)
 
28
    return _escape_re.sub(_escape_sub, uni)
 
29
 
 
30
ehtml = escape
 
31
 
 
32
__all__ = ['escape', 'ehtml']
 
33
 
 
34
################################################################################
 
35
 
 
36
if __name__ == '__main__':
 
37
    print ehtml(
 
38
        '“Gross national happiness is more important”', 'utf-8'
 
39
    )
 
40
    
 
41
    print ehtml('&&', 'utf-8')
 
42
    
 
43
################################################################################
 
 
b'\\ No newline at end of file'