-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.py
124 lines (108 loc) · 3.92 KB
/
blog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright belong to kyc
from flask import Flask, render_template,g, request, url_for
from flask_flatpages import FlatPages
from flask_frozen import Freezer
from flask.ext.paginate import Pagination
import os
from search import build_index
from slider import slide
CSRF_ENABLED = True
SECRET_KEY = 'you-will-never-guess'
##################################
import sys
####################################
from flask.ext.wtf import Form
from wtforms import TextField, BooleanField, TextAreaField
from wtforms.validators import Required, Length
class SearchForm(Form):
search = TextField('search', validators = [Required()])
##################################
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_ROOT = 'content'
POST_DIR = 'posts'
FLATPAGES_EXTENSION = '.md'
SITE_NAME = u"aceking静态博客"
SERVER_PORT = 5000
PER_PAGE=4
WHOOSH_BASE = './'+FLATPAGES_ROOT+'/'+POST_DIR+'/'
app = Flask(__name__)
app.config.from_object(__name__)
##注册蓝图
app.register_blueprint(slide)
flatpages = FlatPages(app)
freezer = Freezer(app)
def get_all():
if not hasattr(g, 'all'):
app.logger.debug('has start ')
g.all = [p for p in flatpages if p.path.startswith(POST_DIR)]
def get_entry_for_page(page,length,entries):
entry_page=[]
for i in range(10):
index= page*10-10+i
if index < len(entries):
entry_page.append(entries[index])
return entry_page
@app.route('/')
@app.route('/<int:page>')
def index(page = 1):
"""On the index page of our blog, show a list of blog posts"""
#if has_attribute
get_all()
posts = g.all[PER_PAGE*(page - 1):page*PER_PAGE]
form = SearchForm()
pagination = Pagination(page=page, bs_version=3,total=len(g.all), per_page=PER_PAGE, record_name='posts')
return render_template('index.html', form = form, posts=posts, pagination=pagination)
@app.route('/build_index')
def index_build():
build_index(WHOOSH_BASE)
return "Build sucess !"
@app.route('/search', methods=['GET', 'POST'])
def search():
page=1
if request.method=='POST':
entries=[]
keywords = request.form['search'].split(' ')
# 搜索
entries = whoosh_search(keywords)
else:
try:
page=int(request.args.get('page',1))
except ValueError:
page=1
page_entries=get_entry_for_page(page,PER_PAGE,entries)
get_all()
posts = [p for p in g.all if p.path in [ POST_DIR+'/'+k[0][:-3] for k in page_entries]]
pagination=Pagination(page=page, total=len(entries), search=False, record_name='result')
return render_template('index.html', form = SearchForm(), posts=posts, pagination=pagination)
@app.route('/posts/<name>')
def post(name):
"""For blogpost type pages"""
path = u'{}/{}'.format(POST_DIR, name)
post = flatpages.get_or_404(path)
return render_template('post.html', post=post)
def whoosh_search(keywords):
from whoosh.index import create_in,open_dir
from whoosh.qparser import QueryParser
indexFile = open_dir('./tmp')
resultsList = []
for key in keywords:
searchQuery = QueryParser("content", indexFile.schema).parse(key)
with indexFile.searcher() as searcher:
# resultsList.append(searcher.search(searchQuery, limit=1000, terms=True))
searchResult = searcher.search(searchQuery, limit=1000, terms=True)
for result in searchResult:
# resultsList.append(list(result["title"], result["path"]))
resultsList.append((result["title"], result["path"]))
return resultsList
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == "build":
#freezer.freeze()
build_index(WHOOSH_BASE)
elif len(sys.argv) > 1 and sys.argv[1] == "search":
#freezer.freeze()
print whoosh_search([u'执行'])
else:
app.run(host='0.0.0.0', port = 8080, debug=True)