Exemple #1
0
    def _filterCore(self,  chunk, **kwargs):
        pc = self.context.portal_catalog
        brains = pc(Title=chunk)

        # lets only handle unique matches
        if brains and len(brains) == 1:
            url = brains[0].getURL()
            # build a context and render a macro for the link
            # (again, to keep things flexible)
            data = {
                'url' : url,
                'anchor' : chunk,
                }

            econtext = createContext(self.context,
                                    **data)

            # reuse some of the macro code to render the "wikilink" macro
            # and insert a stylized link into the output
            template = self.context.restrictedTraverse(path=kwargs.get('template'))
            if template:
                macro = template.macros['wikilink']
                return macro_render(macro, self.context, econtext)

        return chunk
Exemple #2
0
    def _macro_renderer(self,  macro, template=None, **kw):
        """render approved macros"""
        try:
            if not template:
                view = _getViewFor(self.context)
                macro = view.macros[macro]
            else:
                template = self.context.restrictedTraverse(path=template)
                macro = template.macros[macro]
        except ConflictError:
            raise
        except:
            import traceback
            traceback.print_exc()
            return ''

        econtext = createContext(self.context, **kw)
        return macro_render(macro, self.context, econtext, **kw)
Exemple #3
0
    def filter(self,  text, **kwargs):
        page = kwargs.get('page')
        if not page:
            page = self.context.REQUEST.get('page', 1)
        page = int(page)
        if page == 0: page = 1 # non-geek counting

        pages = self.chunkpage(text, limit=int(kwargs.get('limit', self.SIZE_LIMIT)))
        # if we couldn't parse it or they indicated they want everything
        if not pages or page == -1:
            # we couldn't do anything?
            return text

        page -= 1
        if page > len(pages) or page < 0:
            page = 0
        p = pages[page]

        # we should now have the relevant page text, but we want to
        # include some additional information in the output that can
        # be used to page among these things
        if kwargs.get('template'):
            data = {
                'pages'   : len(pages),
                'current' : page + 1,
                'prev'    : max(page, 1),
                'next'    : min(page + 2, len(pages)),
                }
            econtext = createContext(self.context,
                                    **data)

            # reuse some of the macro code to render the "pages" macro
            # and insert a pager into the resultant text
            template = self.context.restrictedTraverse(path=kwargs.get('template'))
            if template:
                macro = template.macros['pages']
                text = macro_render(macro, self.context, econtext)
                p = p + text
        return p
Exemple #4
0
    def _filterCore(self,  chunk, **kwargs):
        # Obtain the id of the reference from the expression
        parts =  chunk.split('/', 1)
        targetId = parts[:1]
        expr = parts[1:]
        if targetId:
            targetId = targetId[0]
            # resolve references for this id and relationship
            reference_tool = getToolByName(self.context,
                                           REFERENCE_CATALOG)
            # We employ two strategies here.
            # look for the targetId as a UID (this is the most
            # flexible form as it allow even the object to be renamed)
            brains = reference_tool._queryFor(sid=self.context.UID(),
                                              tid=targetId,
                                              relationship=self.relationship)
            if not brains:
                # look for targetId as an Id ( this is more common on
                # smaller sites with hand coded HTML)
                brains = reference_tool._queryFor(sid=self.context.UID(),
                                                  targetId=targetId,
                                                  relationship=self.relationship)

            if not brains:
                # if there were no results we can't do anything
                atlog('''Link Resolution Problem: %s references
                             missing object with id (%s)''' % (
                    self.context.getId(), targetId))
                return chunk
            elif len(brains) > 1:
                # there should only be one, however, its possible that
                # referenced objects could share an id (not a UUID). In
                # this unlikely event we issue a warning and use the first
                atlog('''Link Resolution Problem: %s references
                             more than one object with the same id (%s)''' % (
                    self.context.getId(), targetId))
                brains = (brains[0],)


        # Generate a TALES Expression from chunk
        if expr:
            expr = expr[0]
        else:
            expr = "reference/getTargetObject"

        expression = PathExpr('reference', expr, TALESEngine)

        # brains is still in context, we can use it to generate the
        # default context, remember, this points to the "reference
        # object", not the targetObject itself.
        brain  = brains[0]
        refobj = brain.getObject()

        # some of this information is not in the default referernce
        # object. To get this to appear and be used here we need to
        # update the ref catalog and use a new relationship that
        # inlcudes this information as the referenceClass.
        # Extensions/Install/configureReferenceCatalog shows this
        econtext = createContext(self.context,
                                reference=refobj,
                                Title=brain.targetTitle,
                                URL=brain.targetURL,
                                )
        # and evaluate the expression
        result = expression(econtext)
        if callable(result):
            result = result()
        return result