- 论坛徽章:
- 0
|
看精华贴时可以把一些BBCode转成html
本人初学,时间仓促,请大家多指教
- #! C:\Python23\python.exe
- # -*- coding: cp936 -*-
- import BaseHTTPServer
- import urllib
- import webbrowser
- import re
- PORT = 10080
- CUDistillateRoot = "http://www.chinaunix.net/jh/";
- pageStyle = r"""
- <!--
- .code {
- font-family: 'song',Verdana; font-size: 12px; color: #006600;
- background-color: #FAFAFA; border: #D1D7DC; border-style: solid;
- border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px
- }
- .quote {
- font-family: 'song',Verdana; font-size: 12px; color: #444444; line-height: 125%;
- background-color: #FAFAFA; border: #D1D7DC; border-style: solid;
- border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px
- }
- -->
- </style>
- """
- def main():
- httpserver = BaseHTTPServer.HTTPServer(("", PORT), CUDistillateHandler)
- webbrowser.open("http://localhost:" + `PORT`);
- httpserver.serve_forever()
- def replace_TAG(htmlcode):
- htmlcode = re.compile(r'(src="?)(/images/\w*"?)').sub(r"\1http://www.chinaunix.net\2", htmlcode)
- htmlcode = re.compile(r'(<a href="?)http://www.chinaunix.net/jh(/\d{1,3}/\d*\.html?"?)').sub(r"\1\2", htmlcode)
- return htmlcode
- def replace_BBCode(htmlcode):
- htmlcode = re.compile(r'</style>',re.VERBOSE).sub(pageStyle, htmlcode)
-
- htmlcode = re.compile(r'\[b:[^\]]*\]').sub(r'<span style="font-weight: bold">', htmlcode)
- htmlcode = re.compile(r'\[/b:[^\]]*\]').sub(r'</span>', htmlcode)
-
- htmlcode = re.compile(r'\[color=(\w*):[^\]]*\]').sub(r'<span style="color: \1">', htmlcode)
- htmlcode = re.compile(r'\[/color:[^\]]*\]').sub(r'</span>', htmlcode)
-
- htmlcode = re.compile(r'\[url=([^\]]*)\]([^\[]*)\[/url\]').sub(r'<a href=\1 target=_blank>\2</a>', htmlcode)
- htmlcode = re.compile(r'\[url\]([^\[]*)\[/url\]').sub(r'<a href=\1 target=_blank>\1</a>', htmlcode)
-
- htmlcode = re.compile(r'\[code:\w{1,}:[^\]]*\]').sub(r"""
- <table width=90% border=0 align=center cellspacing=1 cellpadding=3><tr><td class=code>""", htmlcode)
- htmlcode = re.compile(r'\[/code:\w{1,}:[^\]]*\]').sub(r'</td></tr></table>', htmlcode)
- htmlcode = re.compile(r'\[quote:\w{1,}="([^"]*)"\]').sub(r"""
- <table width=90% border=0 align=center cellspacing=1 cellpadding=3>
- <tr><td><br/><b>\1写到:</b></td></tr>
- <tr><td class=quote>""", htmlcode)
- htmlcode = re.compile(r'\[/quote:[^\]]*\]').sub(r'</td></tr></table>', htmlcode)
- htmlcode = re.compile(r'\[list:[^\]]*\]').sub(r'<ol>', htmlcode)
- htmlcode = re.compile(r'\[list=(\w{1}):[^\]]*\]').sub(r'<ol type=\1>', htmlcode)
- htmlcode = re.compile(r'\[/list[^\]]*\]').sub(r'</ol>', htmlcode)
- htmlcode = re.compile(r'\[\*:\w*\]').sub(r'<li>', htmlcode)
-
- return htmlcode
- class CUDistillateHandler(BaseHTTPServer.BaseHTTPRequestHandler):
- def do_GET(self):
- if re.match("^/\d{1,3}", self.path):
- srcurl = CUDistillateRoot + self.path
- else:
- srcurl = CUDistillateRoot
- try:
- htmlpage = urllib.urlopen(srcurl).read()
- htmlpage = replace_TAG(htmlpage)
- if re.match("^/\d{1,3}/\d{1,}\.html$", self.path):
- htmlpage = replace_BBCode(htmlpage)
- except:
- htmlpage = """
- <html>
- <body><h1>不能访问www.chinaunix.net,请检查网络连接</h1>
- </body>
- </html>
- """
- self.send_response(200)
- self.send_header("Content-type", "text/html")
- self.end_headers()
- self.wfile.write(htmlpage)
- if __name__ == '__main__':
- main()
复制代码 |
|