1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
'''
markup / media -- conversion / coercion
'''
from peak.rules import abstract, when
class htmlstr(str):
pass
class uhtmlstr(unicode):
pass
@abstract()
def plain_text(s):
''' Returns the text passed as a markup free / plain text string. '''
@abstract()
def html_text(s):
''' Returns the text passed as an html marked-up text string. '''
@when(html_text, (str, unicode))
def txt2html(s):
return ''.join(['<p>', s, '</p>' ])
@when(html_text, (htmlstr, uhtmlstr))
def html2html(s):
return s
def txt2txt(s):
return s
when(plain_text, (str,))(txt2txt)
when(plain_text, (unicode,))(txt2txt)
@when(plain_text, (htmlstr, uhtmlstr))
def html2txt(s):
return re.sub(r'<[^>]*?>','',s)
|