utils.py
コメントを解釈して、リンク等を追加するためのコード
サイズ 4.1 kB - File type text/python-sourceファイルのコンテンツ
from DocumentTemplate.ustr import ustr
from StructuredText.STletters import letters, dbl_quoted_punc
from cgi import escape
import re
# matches an "xxxx://yyyy" URL at the start of a line, or after a space.
# xxxx can only be alpha characters.
# yyyy is anything up to the first space, newline, comma, double quote or <
#re1 = re.compile("(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)")
query = "[\w\-_\.!~*'+%#/,]*(\?[\w\-_\.!~*';\\/:@&=+$,%#]*)?"
re1 = re.compile("(^|[^\"\w])((ftp|http)s?://[\w.-]+\.[a-zA-Z]{2,3}(:[\w]*)?" + query + ")")
# matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
# Must contain at least 2 dots. xxxx contains either alphanum, or "-"
# zzzz is optional.. will contain everything up to the first space, newline,
# comma, double quote or <.
#re2 = re.compile("(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)")
re2 = re.compile("(^|[^\w/])(www\.[\w.-]+\.[a-zA-Z]{2,3}(:[\w]*)?" + query + ")")
# matches an email@domain type address at the start of a line, or after a space.
# Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
#re3 = re.compile("(^|[\n ])([A-Za-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)")
re3 = re.compile("(^|[\W])(\w[\w&\-_.+]*)@(([\w]+[-\w]*\.)+[a-zA-Z]{2,9})")
# id:XXXXXX
re_id = re.compile("id:(\w+)")
# asin:XXXXXXXXXX
re_asin = re.compile("(isbn|asin):([\w-]+)")
# structure text, image link
re_link = re.compile(r'"([ %s0-9\n\r%s]+)"' % (letters,dbl_quoted_punc) + ":"
+ r'([%s0-9_\@\.\,\?\!\/\:\;\-\#\~\=\&\%%\+]+)' % letters)
re_image = re.compile(r'"([ %s0-9\n\r%s]+)"' % (letters,dbl_quoted_punc)
+ ':img:([a-zA-Z0-9%\_\-.:/\?=;,\n\~]+)')
def comment_parser(content):
try:
content = escape(ustr(content), 0)
end = 0
new_content = ''
for m in re_image.finditer(content):
new_content += content[end:m.start()]
grps = m.groups()
new_content += '<img src="%s" alt="%s" />'%(grps[1],grps[0])
end = m.end()
new_content += content[end:]
content = new_content
end = 0
new_content = ''
for m in re_link.finditer(content):
new_content += content[end:m.start()]
grps = m.groups()
new_content += '<a href="%s">%s</a>'%(grps[1],grps[0])
end = m.end()
new_content += content[end:]
content = new_content
end = 0
new_content = ''
for m in re1.finditer(content):
new_content += content[end:m.start()]
grps = m.groups()
new_content += '%s<a href="%s">%s</a>'%(grps[0],grps[1],grps[1])
end = m.end()
new_content += content[end:]
content = new_content
end = 0
new_content = ''
for m in re2.finditer(content):
new_content += content[end:m.start()]
grps = m.groups()
new_content += '%s<a href="http://%s">%s</a>'%(grps[0],grps[1],grps[1])
end = m.end()
new_content += content[end:]
content = new_content
end = 0
new_content = ''
for m in re3.finditer(content):
new_content += content[end:m.start()]
grps = m.groups()
new_content += '%s<a href="mailto:%s@%s">%s@%s</a>'%(grps[0],grps[1],grps[2],grps[1],grps[2])
end = m.end()
new_content += content[end:]
content = new_content
end = 0
new_content = ''
for m in re_id.finditer(content):
new_content += content[end:m.start()]
grps = m.groups()
new_content += '<a href="/Members/%s">id:%s</a>'%(grps[0],grps[0])
end = m.end()
new_content += content[end:]
content = new_content
end = 0
new_content = ''
for m in re_asin.finditer(content):
new_content += content[end:m.start()]
grps = m.groups()
new_content += '<a href="http://www.amazon.co.jp/exec/obidos/ASIN/%s">%s:%s</a>'%(grps[1].replace("-", "").upper(),grps[0],grps[1])
end = m.end()
new_content += content[end:]
content = new_content
except:
pass
content = content.replace('\n', '<br/>')
return content
Click here to get the file