Exemplo n.º 1
0
def search():
    """
    Search posts
    """
    conf = current_app.config
    per_page = int(conf.get('PER_PAGE'))
    query = request.args.get('query', '')

    posts = []
    ix = whoosh.index.open_dir(current_app.fm.index_dir)
    with ix.searcher() as searcher:
        q = QueryParser('content', ix.schema).parse(query)
        results = searcher.search(q, limit=None)
        results.fragmenter.charlimit = None
        results.fragmenter.surround = 100
        results.formatter = whoosh.highlight.HtmlFormatter(tagname='span', classname='search-match')
        for r in results:
            post = Post.single(r['category'], r['slug'])
            post.search_highlight = r.highlights('content', text=post.plain)

            # If the result is only in the title, the highlight will be empty
            if not post.search_highlight:
                post.search_highlight = post.plain[:200]

            posts.append(post)

    posts, page, last_page = _pagination(posts, per_page)

    return render_template('search.html',
                           posts=posts, page=page+1,
                           last_page=last_page,
                           site_data=Meta(request))
Exemplo n.º 2
0
def search():
    """
    Search posts
    """
    conf = current_app.config
    per_page = int(conf.get('PER_PAGE'))
    query = request.args.get('query', '')

    posts = []
    ix = whoosh.index.open_dir(current_app.fm.index_dir)
    with ix.searcher() as searcher:
        q = QueryParser('content', ix.schema).parse(query)
        results = searcher.search(q, limit=None)
        results.fragmenter.charlimit = None
        results.fragmenter.surround = 100
        results.formatter = whoosh.highlight.HtmlFormatter(
            tagname='span', classname='search-match')
        for r in results:
            post = Post.single(r['category'], r['slug'])
            post.search_highlight = r.highlights('content', text=post.plain)

            # If the result is only in the title, the highlight will be empty
            if not post.search_highlight:
                post.search_highlight = post.plain[:200]

            posts.append(post)

    posts, page, last_page = _pagination(posts, per_page)

    return render_template('search.html',
                           posts=posts,
                           page=page + 1,
                           last_page=last_page,
                           site_data=Meta(request))
Exemplo n.º 3
0
def post(category, slug):
    """
    Show a single post;
    The slug is the filename of the original markdown file
    """
    post = Post.single(category, slug)
    if post is not None:
        return render_template('single.html',
                               post=post,
                               site_data=Meta(request))

    abort(404)
Exemplo n.º 4
0
def post(category, slug):
    """
    Show a single post;
    The slug is the filename of the original markdown file
    """
    post = Post.single(category, slug)
    if post is not None:
        return render_template('single.html',
                               post=post,
                               site_data=Meta(request))

    abort(404)