コード例 #1
0
ファイル: oshaview.py プロジェクト: EU-OSHA/osha.theme
    def makeAbsoluteUrls(self, text):
        """ turn relative URLs into absolute URLs
            based on the context's URL """
        links = []

        for link in retrieveSTX(text):
            links.append(link)

        for link in retrieveHTML(text):
            links.append(link)

        au = self.context.absolute_url()
        for link in links:
            if not link.startswith("mailto"):
                # text = text.replace(link, urljoin(au, link))
                joinchar = link.find("?") > 0 and "&" or "?"
                newlink = (
                    "%(link)s%(joinchar)sutm_source=shortmessage&"
                    "utm_medium=email&utm_campaign=%(campaign)s"
                    % dict(link=urljoin(au, link), joinchar=joinchar, campaign="shortmessage")
                )
                # below is a fix for the problem that you have two links in
                # text, one being a prefix of the
                # other and appearing below it. In this case, the smaller
                # one will be replaced with GA postfix in the longer one
                # as fix, only replace links which terminate with a ".
                # Of course this requires that all links are closed properly
                # if the text already is a URL however, we don't want this hack
                text = text.replace(link + '"', newlink + '"')
        return text
コード例 #2
0
ファイル: oshaview.py プロジェクト: EU-OSHA/osha.theme
    def handleOSHMailUrls(self, text, id=""):
        """ turn relative URLs into absolute URLs based on the context's URL;
            append google analytics code
        """
        encoding = getSiteEncoding(self)
        if type(text) == unicode:
            text = text.encode(encoding)
        links = []
        isURL = validation.validatorFor("isURL")
        if isURL(text) == 1:
            links.append(text)
        else:
            for link in retrieveSTX(text):
                links.append(link)

            for link in self.retrieveLinksFromHTML(text):
                links.append(link)

        au = self.context.absolute_url()
        for link in links:
            if not link.startswith("mailto"):
                joinchar = link.find("?") > 0 and "&" or "?"
                newlink = "%(link)s%(joinchar)sutm_source=oshmail&" "utm_medium=email&utm_campaign=%(campaign)s" % dict(
                    link=urljoin(au, link), joinchar=joinchar, campaign=id != "" and id or "oshmail"
                )
                # below is a fix for the problem that you have two links in
                # text, one being a prefix of the
                # other and appearing below it. In this case, the smaller
                # one will be replaced with GA postfix in the longer one
                # as fix, only replace links which terminate with a ".
                # Of course this requires that all links are closed properly
                # if the text already is a URL however, we don't want this hack
                if isURL(text) == 1:
                    text = text.replace(link, newlink)
                else:
                    text = text.replace(link + '"', newlink + '"')
        text = text.decode(encoding)
        return text