Size: 0
Comment: error idx
|
Size: 3305
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 [[TableOfContents]] ## 默许导航,请保留 [[Include(ZPyUGnav)]] = txt 2 html = ##startInc == 题面 == {{{ free won <[email protected]> reply-to [email protected] to [email protected] date Fri, Jul 11, 2008 at 19:12 subject [CPyUG:58809] 文本转换html问题 }}} 最近在完成一个 类似论坛的站点。by django. 出现了一个问题就是在回帖的时候,把回帖的内容转换成html格式发布出来。 目前尝试了些正则方式,都不太理想。 希望有经验的朋友能给个方向。 == limodou == {{{#!python #coding=utf-8 import re import cgi re_string = re.compile(r'(?P<htmlchars>[<&>])|(?P<space>^[ \t]+)|(?P<lineend>\r\n|\r|\n)|(?P<protocal>(^|\s*)((http|ftp)://.*?))(\s|$)', re.S|re.M|re.I) def text2html(text, tabstop=4): def do_sub(m): c = m.groupdict() if c['htmlchars']: return cgi.escape(c['htmlchars']) if c['lineend']: return '<br>' elif c['space']: t = m.group().replace('\t', ' '*tabstop) t = t.replace(' ', ' ') return t elif c['space'] == '\t': return ' '*tabstop; else: url = m.group('protocal') if url.startswith(' '): prefix = ' ' url = url[1:] else: prefix = '' last = m.groups()[-1] if last in ['\n', '\r', '\r\n']: last = '<br>' return '%s<a href="%s">%s</a>%s' % (prefix, url, url, last) return re.sub(re_string, do_sub, text) if __name__ == '__main__': text=""" http://groups.google.com/group/python-cn/pending """ print text2html(text) }}} == yrh == `<[email protected]>` 我原先做个一个,不过不是for django的,你看看吧: {{{#!python def htmlEncode(strings): strings = strings.replace("\'", "'") strings = strings.replace("\\", "\") strings = strings.replace("\.", ".") strings = strings.replace("\|", "|") #strings = strings.replace('<br>', '') strings = strings.replace(" ", " ") strings = strings.replace("<", "<") #大写的html标签全部被替换,小写的<strong>、<center>、<font>、<img>标签不会被替换 strings = re.sub(re.compile('<(?P<xg>\/?)(?P<bq>strong|center|font|img |pre)'), '<\g<xg>\g<bq>', strings) strings = re.sub('\n', '\n<br>', strings) return strings }}} 这个函数是我自己写的一个论坛的函数,允许 小写的<strong>、<center>、<font>、<img>标签,其他html标签一律转换为文本。 替换斜杠,英文单引号、句号,竖线的目的是因为某些数据库存储这些字符的时候会出错,具体哪些字符要替换,你参考数据库手册好了。 如果你要更多功能的,建议你参考论坛处理ubb标签的方式,你google一下好了(比如动网论坛,不过大部分是asp的,python的很少) ##endInc ---- '''反馈''' 创建 by -- ZoomQuiet [[[DateTime(2008-07-11T14:11:59Z)]]] ||<^>[[PageComment2]]||<^>[:/PageCommentData:PageCommentData]''|| |
txt 2 html
题面
free won <[email protected]> reply-to [email protected] to [email protected] date Fri, Jul 11, 2008 at 19:12 subject [CPyUG:58809] 文本转换html问题
最近在完成一个 类似论坛的站点。by django.
出现了一个问题就是在回帖的时候,把回帖的内容转换成html格式发布出来。
目前尝试了些正则方式,都不太理想。
希望有经验的朋友能给个方向。
limodou
1 #coding=utf-8
2 import re
3 import cgi
4
5 re_string = re.compile(r'(?P<htmlchars>[<&>])|(?P<space>^[
6 \t]+)|(?P<lineend>\r\n|\r|\n)|(?P<protocal>(^|\s*)((http|ftp)://.*?))(\s|$)',
7 re.S|re.M|re.I)
8 def text2html(text, tabstop=4):
9 def do_sub(m):
10 c = m.groupdict()
11 if c['htmlchars']:
12 return cgi.escape(c['htmlchars'])
13 if c['lineend']:
14 return '<br>'
15 elif c['space']:
16 t = m.group().replace('\t', ' '*tabstop)
17 t = t.replace(' ', ' ')
18 return t
19 elif c['space'] == '\t':
20 return ' '*tabstop;
21 else:
22 url = m.group('protocal')
23 if url.startswith(' '):
24 prefix = ' '
25 url = url[1:]
26 else:
27 prefix = ''
28 last = m.groups()[-1]
29 if last in ['\n', '\r', '\r\n']:
30 last = '<br>'
31 return '%s<a href="%s">%s</a>%s' % (prefix, url, url, last)
32 return re.sub(re_string, do_sub, text)
33
34 if __name__ == '__main__':
35 text="""
36 http://groups.google.com/group/python-cn/pending
37 """
38 print text2html(text)
yrh
我原先做个一个,不过不是for django的,你看看吧:
1 def htmlEncode(strings):
2 strings = strings.replace("\'", "'")
3 strings = strings.replace("\\", "\")
4 strings = strings.replace("\.", ".")
5 strings = strings.replace("\|", "|")
6
7 #strings = strings.replace('<br>', '')
8 strings = strings.replace(" ", " ")
9 strings = strings.replace("<", "<")
10 #大写的html标签全部被替换,小写的<strong>、<center>、<font>、<img>标签不会被替换
11 strings = re.sub(re.compile('<(?P<xg>\/?)(?P<bq>strong|center|font|img |pre)'), '<\g<xg>\g<bq>', strings)
12 strings = re.sub('\n', '\n<br>', strings)
13 return strings
这个函数是我自己写的一个论坛的函数,允许 小写的<strong>、<center>、<font>、<img>标签,其他html标签一律转换为文本。 替换斜杠,英文单引号、句号,竖线的目的是因为某些数据库存储这些字符的时候会出错,具体哪些字符要替换,你参考数据库手册好了。
如果你要更多功能的,建议你参考论坛处理ubb标签的方式,你google一下好了(比如动网论坛,不过大部分是asp的,python的很少)
反馈
创建 by -- ZoomQuiet [DateTime(2008-07-11T14:11:59Z)]