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', '&nbsp;'*tabstop)
  17            t = t.replace(' ', '&nbsp;')
  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

<[email protected]>

我原先做个一个,不过不是for django的,你看看吧:

   1 def htmlEncode(strings):
   2     strings = strings.replace("\'", "&#39;")
   3     strings = strings.replace("\\", "&#92;")
   4     strings = strings.replace("\.", "&#46;")
   5     strings = strings.replace("\|", "&#124;")
   6 
   7     #strings = strings.replace('<br>', '')
   8     strings = strings.replace("  ", " ")
   9     strings = strings.replace("<", "&#60;")
  10     #大写的html标签全部被替换,小写的<strong>、<center>、<font>、<img>标签不会被替换
  11     strings = re.sub(re.compile('&#60;(?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 [2008-07-11 14:11:59]

Name Password4deL ;) :( X-( B-)

PageCommentData