Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

科学道德与学风建设

  • Loading branch information...
commit aa6d16d4fd660674c26e861c2308c4ad14633e63 1 parent 0b1be76
@jondynet authored
Showing with 88 additions and 3 deletions.
  1. +25 −0 build_txt.py
  2. +5 −3 cgi-bin/index.py
  3. +58 −0 cgi-bin/txt.py
View
25 build_txt.py
@@ -0,0 +1,25 @@
+#coding:utf-8
+from whoosh.index import create_in
+from whoosh.fields import TEXT, Schema, ID
+from whoosh.analysis import RegexAnalyzer
+import os
+import cgi
+
+def clear_txt(html):
+ return cgi.escape(html)
+
+analyzer = RegexAnalyzer(ur"([\u4e00-\u9fa5])|(\w+(\.?\w+)*)")
+schema = Schema(title=TEXT(stored=True), path=ID(stored=True),
+ content=TEXT(stored=True, analyzer=analyzer))
+ix = create_in("indexdir", schema)
+writer = ix.writer()
+for root, dirs, files in os.walk('txt'):
+ for fn in files:
+ if fn.endswith('.txt'):
+ f = os.path.join(root, fn)
+ c = open(f).read()
+ c = clear_txt(c).decode('utf-8')
+ writer.add_document(title=unicode(f), path=unicode(f), content=c)
+
+writer.commit()
+
View
8 cgi-bin/index.py
@@ -19,7 +19,7 @@
</head>
<body>
<ul>
- <h3>这是分生课件搜索哦,亲!</h3>
+ <h3>这是科学道德与学风建设哦,亲!</h3>
<form action="">
<input type="text" name="keywords" style="width:200px;"/>
<input type="submit" value="搜索" />
@@ -33,6 +33,7 @@
if keywords:
from whoosh.index import open_dir
import time
+ import re
start = time.clock()
ix = open_dir("indexdir")
searcher = ix.searcher()
@@ -40,8 +41,9 @@
rs = [u'<ol>']
if results:
for result in results:
- rs.append(u'<li><a href="/%(path)s">%(name)s</a></li>' % {'path':result['path'],
- 'name':cgi.escape(result['path'])})
+ page = result['path'][4:].split('.').pop(0)
+ rs.append(u'<li><a href="/cgi-bin/txt.py?p=%(page)s">%(name)s</a></li>' % {'page':page,
+ 'name':u'%s' % page})
else:
rs.append(u'<span style="color:red">木有内容哦,请检查关键词!</span>')
rs.append(u'</ol>')
View
58 cgi-bin/txt.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+#coding:utf-8
+#
+# :copyright: 2011 by jondynet@gmail.com
+#
+import os
+import cgi, cgitb
+form = cgi.FieldStorage()
+print "Content-Type: text/html"
+print
+
+try:
+ page = int(form.getvalue('p'))
+except:
+ page = -7
+if page <-7 or page > 121:
+ page = -7
+path = os.path.dirname(__file__)
+filename = os.path.join(path, '../txt/%s.txt' % page)
+txt = open(filename).read()
+html = '''
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
+ <title>分生课件搜索</title>
+</head>
+<body>
+ <form action="">
+ <p>
+ <a href="/cgi-bin/index.py">搜索</a> |
+ <a href="txt.py?p=-7">首页</a> |
+ <a href="txt.py?p=-2">目录</a> |
+ <a href="txt.py?p=%(pf)s">上一页</a> |
+ <a href="txt.py?p=%(pn)s">下一页</a>
+
+ <input type="txt" name="p" size="2" />页
+ <input type="submit" value="GO" />
+ </p>
+ </form>
+ %(txt)s
+ <form action="">
+ <p>
+ <a href="/cgi-bin/index.py">搜索</a> |
+ <a href="txt.py?p=-7">首页</a> |
+ <a href="txt.py?p=-2">目录</a> |
+ <a href="txt.py?p=%(pf)s">上一页</a> |
+ <a href="txt.py?p=%(pn)s">下一页</a>
+
+ <input type="txt" name="p" size="2" />页
+ <input type="submit" value="GO" />
+ </p>
+ </form>
+</body>
+</html>
+'''
+
+print html % {'txt': txt, 'pf': page -1, 'pn': page +1 }
Please sign in to comment.
Something went wrong with that request. Please try again.