パーソナルツール
現在の場所: ホーム plone SNS風味 コメント utils.py
書いた本
Plone 完全活用ガイド の Chapter 1, 2, 3, 11 を執筆しました。
plone のインストール、使い方から、機能・デザインのカスタマイズ、プロダクトの作り方まで、 plone のすべてがぎゅっと詰まっている書籍になっていると思います。
plone に興味がある人から、すでに使いこなしている方まで、ぜひ読んでみてください。
Plone 完全活用ガイドのサポートページ
ナビゲーション

 
文書操作

utils.py

作成者 takanori 最終変更日時 2008年06月04日 12時37分

コメントを解釈して、リンク等を追加するためのコード

Click here to get the file

サイズ 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

Powered by vine linux, python, zope, plone, coreblog