Exemplo n.º 1
0
    def pageListWithContext(self, request, formatter, info=1, context=180,
                            maxlines=1, paging=True, hitsFrom=0, hitsInfo=0,
                            highlight_titles=True, highlight_pages=True):
        """ Format a list of found pages with context

        @param request: current request
        @param formatter: formatter to use
        @param info: show match info near the page link
        @param context: how many characters to show around each match.
        @param maxlines: how many contexts lines to show.
        @param paging: toggle paging
        @param hitsFrom: current position in the hits
        @param hitsInfo: toggle hits info line
        @param highlight_titles: perform highlighting in page list
        @param highlight_pages: add highlight parameter to page URLs
        @rtype: unicode
        @return formatted page list with context
        """
        ngowikiutil = NgoWikiUtil(request)
        ngowikiutil.open_database()

        self._reset(request, formatter)
        f = formatter
        write = self.buffer.write
        _ = request.getText

        if paging and len(self.hits) <= request.cfg.search_results_per_page:
            paging = False

        # Add pages formatted as definition list
        hitsTo = hitsFrom + request.cfg.search_results_per_page + 1
        hitDiff = 0
        if self.hits:
            write(f.definition_list(1))

            displayHits = []
            hitIdx = 0
            for hit in self.hits:
                result = ngowikiutil.select_page_by_path(hit.page_name)
                if result == None:
                    hitDiff = hitDiff + 1
                    continue
                if len(ngowikiutil.select_page_tags_by_id(result["id"])) == 0:
                    hitDiff = hitDiff + 1
                    continue
                hitIdx = hitIdx + 1
                if paging:
                    if hitsTo <= hitIdx - 1:
                        break
                    if hitsFrom <= hitIdx - 1:
                        displayHits.append(hit)
                else:
                    displayHits.append(hit)
            if len(displayHits) <= request.cfg.search_results_per_page and hitsFrom == 0:
                paging = False
            if len(displayHits) > request.cfg.search_results_per_page:
                displayHits = displayHits[0:request.cfg.search_results_per_page]

            if len(displayHits) == 0:
                write(u'没有找到相关内容,请调整搜索条件重新搜索')

            template = '''
                <table class="listitem_with_logosummary">
                    <tr>
                        <!--
                        <td class="logo">
                            %(logo)s
                        </td>
                        -->
                        <td>
                           <div class="title">
                              <a href="%(link)s">%(title)s</a>
                           </div>
                           <div class="meta">
                               <span>%(lastmodified)s</span>
                               <span>%(tags)s</span>
                               <span><span class="metaitem">%(likecount)s<span></span>
                               <span><span class="metaitem">%(commentcount)s<span></span>
                               <span><span class="metaitem">%(hitcount)s<span></span>
                           </div>
                           <div class="summary">%(summary)s</div>
                        </td>
                     </tr>
                 </table>
                '''
            for page in displayHits:
                result = ngowikiutil.select_page_by_path(page.page_name)
                if result != None:
                    if len(ngowikiutil.select_page_tags_by_id(result["id"])) > 0:
                        page = Page(request, result["path"]) 
                        logo = '<div class="logo defaultLogo">&nbsp;</div>'
                        if len(result["logo"]) > 0 and exists(request, result["path"], result["logo"]):
                            logo = '<img class="logo" src="' + getAttachUrl(result["path"], result["logo"], request) + '">'
                        link = page.url(request)
                        title = result["title"]
                        lastmodified = page.mtime_printable(request)
                        summary = result["summary"].replace(u"'''", "").replace(u"【请在此插入图片】", "").replace(u"【请在此插入图片,最多可插入9张】", "")

                        tags = (", ".join(
                                  map(lambda x: '<a href=\'javascript:add_filter_by_tag(' + json.dumps(x["tag"]) + ')\' >' + x["tag"] + '</a>', 
                                      filter(lambda x: x["type"] == 1, ngowikiutil.select_page_tags_by_id(result["id"]))
                                  )))

                        if len(tags) > 0:
                            tags = '<span class="metaitem">' + tags + '</span>'

                        write(template % {"logo":logo, "title": title, "link": link, "lastmodified": lastmodified, "tags": tags, "summary": summary, "likecount": u'\u559c\u6b22\uff1a' + str(result["likecount"]), "commentcount": u'\u8bc4\u8bba\u6570\uff1a' + str(result["commentcount"]), "hitcount": u'\u8bbf\u95ee\u91cf\uff1a' + str(result["hitcount"])})
            write(f.definition_list(0))
            if paging:
                write(self.formatPageLinks(hitsFrom=hitsFrom,
                    hitsPerPage=request.cfg.search_results_per_page,
                    hitsNum=len(self.hits) - hitDiff))

        return self.getvalue()
Exemplo n.º 2
0
    def execute(self):
        ngowikiutil = NgoWikiUtil(self.request)
        ngowikiutil.open_database()
        try:
            offset = 0
            length = 10
            sortby = "lastmodified"
            order = "DESC"
            filterByTags = self.filterByTags
            favorite = None

            form = self.request.values
            if 'from' in form:
                offset = int(form['from'])
            if 'length' in form:
                length = int(form['length'])
            if 'sortby' in form:
                sortby = form['sortby']
                if sortby == 'title':
                    order = "ASC"
            if 'order' in form:
                order = form['order']
            if 'favorite' in form and form[
                    'favorite'] != "false" and self.user != None and self.user.valid:
                favorite = self.user.id
            if 'filterByTags' in form:
                filterByTags = form['filterByTags'].split(",")

            for tag in filterByTags:
                ngowikiutil.update_tag_hitcount(tag)

            results = ngowikiutil.select_pages_by_tag(filterByTags, favorite,
                                                      sortby, order, offset,
                                                      length)
            total = ngowikiutil.count_pages_by_tag(filterByTags, favorite)

            buffer = []
            buffer.append(
                '''
                <script language="javascript">window.__ListPagesByTag_filterByTag = %(filterByTags)s;window.__ListPagesByTag_filterByTag_default = %(filterByTagsDefault)s;</script>
                <div id="listpagesbytag_sorter"></div>
				<div id="listpagesbytag_favorite"></div>
                <div id="listpagesbytag_filter"></div>
            ''' % {
                    "filterByTagsDefault": json.dumps(self.filterByTags),
                    "filterByTags": json.dumps(",".join(filterByTags))
                })

            template = '''
                <table class="listitem_with_logosummary">
                    <tr>
                        <!--
                        <td class="logo">
                            %(logo)s
                        </td>
                        -->
                        <td>
                           <div class="title">
                              <a href="%(link)s">%(title)s</a>
                           </div>
                           <div class="meta">
                               <span>%(lastmodified)s</span>
                               <span>%(tags)s</span>
                               <span><span class="metaitem">%(likecount)s<span></span>
                               <span><span class="metaitem">%(commentcount)s<span></span>
                               <span><span class="metaitem">%(hitcount)s<span></span>
                           </div>
                           <div class="summary">%(summary)s</div>
                        </td>
                     </tr>
                 </table>
                '''
            for result in results:
                page = Page(self.request, result["path"])
                logo = '<div class="logo defaultLogo">&nbsp;</div>'
                if len(result["logo"]) > 0 and exists(
                        self.request, result["path"], result["logo"]):
                    logo = '<img class="logo" src="' + getAttachUrl(
                        result["path"], result["logo"], self.request) + '">'
                link = page.url(self.request)
                title = result["title"]
                lastmodified = page.mtime_printable(self.request)
                summary = result["summary"].replace("'''", "").replace(
                    u"【请在此插入图片】", "").replace(u"【请在此插入图片,最多可插入9张】", "")

                tags = (", ".join(
                    map(
                        lambda x: '<a href=\'javascript:add_filter_by_tag(' +
                        json.dumps(x["tag"]) + ')\' >' + x["tag"] + '</a>',
                        filter(
                            lambda x: x["type"] == 1 or x["type"] == 2,
                            ngowikiutil.select_page_tags_by_id(
                                result["id"])))))

                if len(tags) > 0:
                    tags = '<span class="metaitem">' + tags + '</span>'

                buffer.append(
                    template % {
                        "logo":
                        logo,
                        "title":
                        title,
                        "link":
                        link,
                        "lastmodified":
                        lastmodified,
                        "tags":
                        tags,
                        "summary":
                        summary,
                        "likecount":
                        u'\u559c\u6b22\uff1a' + str(result["likecount"]),
                        "commentcount":
                        u'\u8bc4\u8bba\u6570\uff1a' +
                        str(result["commentcount"]),
                        "hitcount":
                        u'\u8bbf\u95ee\u91cf\uff1a' + str(result["hitcount"])
                    })

            buffer.append("<script language='javascript'>render_pagingbar(" +
                          str(total) + ', ' + str(length) + ');</script>')
            ret = ''.join(buffer)
            return ret
        finally:
            ngowikiutil.close_database(True)