OSDN Git Service

static_url better
[pybbs/pybbs.git] / index.py
index 12a3b92..2643b83 100755 (executable)
--- a/index.py
+++ b/index.py
@@ -1,14 +1,18 @@
 
 import os.path
-import shutil,copy
-import tornado.auth
+import shutil,re
 import tornado.escape
 import tornado.web
-import tornado.wsgi
+import tornado.httpserver
+import tornado.ioloop
+import tornado.options
+from tornado.options import define,options
 from tinydb import TinyDB,Query,where
 from tinydb.operations import delete
 from datetime import datetime
 
+define('port',default=8000,help='run on the given port',type=int)
+
 class BaseHandler(tornado.web.RequestHandler):
     def get_current_user(self):
         user = self.get_secure_cookie('admin_user')
@@ -122,25 +126,37 @@ class RegistHandler(tornado.web.RequestHandler):
         rec = self.application.db.get(where('kinds') == 'conf')
         words = rec['bad_words']
         out = rec['out_words']
-        na = self.get_argument('name')
-        sub = self.get_argument('title')
+        na = self.get_argument('name',u'誰かさん')
+        sub = self.get_argument('title',u'タイトルなし')
         com = self.get_argument('comment')
         text = ''
         i = 0
+        url = []
         error = ''
         for word in out:
             if word in com:
                 error = error + u'禁止ワード.'
                 break
         for line in com.splitlines(True):
+            if error != '':
+                break
             for word in words:
                 if word in line.lower():
                     error = error + u'タグ違反.('+word+')'       
-            i += len(line)
+            i += len(line)   
+            obj = re.finditer('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line)
+            for x in obj:
+                if x.group() not in url:
+                    url.append(x.group())
+            if re.match(' ',line):
+                line = line.replace(' ',' ',1)
             text = text+'<p>'+self.link(line)+'<br></p>'
+        s = ''
+        for x in url:
+            s = s+'<tr><td><a class=livepreview target=_blank href={0}>{0}</a></td></tr>'.format(x)
+        if s:
+            text = text+'<table><tr><td>検出URL:</td></tr>'+s+'</table>'
         pw = self.get_argument('password')
-        if sub == '':
-            sub = u'タイトルなし.'
         if i == 0:
             error = error + u'本文がありません.'
         elif i > 1000:
@@ -322,7 +338,7 @@ class Application(tornado.web.Application):
                         'ui_modules':{'Footer':FooterModule},
                         'cookie_secret':'bZJc2sWbQLKos6GkHn/VB9oXwQt8SOROkRvJ5/xJ89E=',
                         'xsrf_cookies':True,
-                        #'debug':True,
+                        'debug':True,
                         'login_url':'/login'
                         }
         tornado.web.Application.__init__(self,handlers,**settings)
@@ -347,4 +363,8 @@ class static():
     bak = 'static/db/bak.json'
 
 st = static()
-app = tornado.wsgi.WSGIAdapter(Application())
+if __name__ == '__main__':
+    tornado.options.parse_command_line()
+    http_server = tornado.httpserver.HTTPServer(Application())
+    http_server.listen(options.port)
+    tornado.ioloop.IOLoop.instance().start()