Exemplo n.º 1
0
def postlistByTag(tagName, curNum):
    if not tagName.strip() or not curNum or curNum < 1:
        flash("参数错误!", "warning")
        return render_template('hintInfo.html')
    paginate = Post.query.filter(Post.tags.any(name=tagName)).order_by(
        Post.location).paginate(curNum, 15, False)
    cur_posts = paginate.items
    posts = list()
    for cur_post in cur_posts:
        abspath = util.getAbsPostPath(cur_post.location)
        if not os.path.exists(abspath):
            continue
        with open(abspath, 'r', encoding='utf-8') as f:
            content = f.read()
            conSplit = content.split('\n\n', 1)
            if len(conSplit) < 2:
                metaStr = ''
                postContent = conSplit[0]
            else:
                metaStr = conSplit[0]
                postContent = conSplit[1]
            meta = util.parsePostMeta(metaStr)
            summary = postContent[:200]
        posts.append({
            'location': cur_post.location,
            'title': meta['title'],
            'summary': summary,
            'meta': meta
        })
    return render_template('tagsPostList.html',
                           tagName=tagName,
                           paginate=paginate,
                           posts=posts)
Exemplo n.º 2
0
def post_edit(path):
    path = util.urlDirPathFormat(path)
    abspath = os.path.join(current_app.config['PAGE_DIR'],
                           path.replace('/', os.sep)) + ".md"
    if not os.path.exists(abspath):
        flash("文章不存在", 'warning')
        return render_template('hintInfo.html')
    isAdmin = util.checkAdmin()

    post = Post.query.filter_by(location=path).first()

    if post and post.userId != str(current_user.id) and (not isAdmin):
        flash("您没有权限编辑此文章!", 'warning')
        return render_template('hintInfo.html')

    if utilpost.isPostLocked(path):
        flash("文章已被锁定,您暂时无权编辑")
        return render_template('hintInfo.html')
    utilpost.createPostLock(path)

    with open(abspath, encoding='UTF-8') as f:
        content = f.read()
        # print('fcontent'+fcontent)
        # form.content.data=fcontent
    meta = content.split('\n\n', 1)[0]
    meta = util.parsePostMeta(meta)  # a dict
    content = content.split('\n\n', 1)[1]

    tags = ''
    location = path

    if post:
        tags = ','.join([t.name for t in post.tags])
        location = post.location

    fileModifyAt = datetime.fromtimestamp(
        os.stat(abspath).st_mtime).strftime('%Y-%m-%d %H:%M:%S')
    form = PostForm(title=meta.get('title', ''),
                    content=content,
                    location=location,
                    tags=tags,
                    createAt=meta.get('createAt', ''),
                    modifyAt=meta.get('modifyAt', ' '),
                    fileModifyAt=fileModifyAt)

    return render_template('editor.html',
                           isPost=True,
                           action=url_for('.post_save'),
                           form=form)
Exemplo n.º 3
0
def reBuildIndex():
    global ix
    ix.close()
    if os.path.exists(INDEX_DIR):
        shutil.rmtree(INDEX_DIR)
        checkIndexDir()
    ix = open_dir(INDEX_DIR)
    # todo
    pathlist = []
    util.walkDirGenDataUrl('pages', pathlist=pathlist)
    doclist = []
    for path in pathlist:
        with open(path, encoding='utf-8') as f:
            content = f.read()
            meta = util.parsePostMeta(content.split('\n\n', 1)[0])
            if not ('location' in meta and meta['location']):
                meta["location"] = path[len(config.PAGE_DIR):].rsplit('.',
                                                                      1)[0]
            content = content.split('\n\n', 1)[1]
            postvo = vo.SearchPostVo(content=content,
                                     summary=content[:100],
                                     **meta)
            doclist.append(postvo)
    updateDocument(doclist)