(self, root)
return root
Shorten popular git repository links.
Shorten popular git repository links.
Shorten popular git repository links .
def run(self, root):
        """Shorten popular git repository links."""

        self.hide_protocol = self.config['hide_protocol']

        links = root.iter('a')
        for link in links:
            has_child = len(list(link))
            is_magic = link.attrib.get('magiclink')
            href = link.attrib.get('href', '')
            text = link.text

            if is_magic:
                del link.attrib['magiclink']

            # We want a normal link.  No subelements embedded in it, just a normal string.
            if has_child or not text:  # pragma: no cover
                continue

            # Make sure the text matches the href.  If needed, add back protocol to be sure.
            # Not all links will pass through MagicLink, so we try both with and without protocol.
            if (text == href or (is_magic and self.hide_protocol and ('https://' + text) == href)):
                m = RE_REPO_LINK.match(href)
                if m:
                    provider = self.get_provider(m)
                    self.my_repo = self.is_my_repo(provider, m)
                    self.my_user = self.my_repo or self.is_my_user(provider, m)
                    value, link_type = self.get_type(provider, m)

                    # All right, everything set, let's shorten.
                    self.shorten(
                        link,
                        provider,
                        link_type,
                        m.group(provider + '_user_repo'),
                        value,
                        href,
                        PROVIDER_INFO[provider]['hash_size']
                    )
        return root
def run ( self , root ) : self . hide_protocol = self . config [ 'hide_protocol' ] links = root . iter ( 'a' ) for link in links : has_child = len ( list ( link ) ) is_magic = link . attrib . get ( 'magiclink' ) href = link . attrib . get ( 'href' , '' ) text = link . text if is_magic : del link . attrib [ 'magiclink' ] # We want a normal link.  No subelements embedded in it, just a normal string. if has_child or not text : # pragma: no cover continue # Make sure the text matches the href.  If needed, add back protocol to be sure. # Not all links will pass through MagicLink, so we try both with and without protocol. if ( text == href or ( is_magic and self . hide_protocol and ( 'https://' + text ) == href ) ) : m = RE_REPO_LINK . match ( href ) if m : provider = self . get_provider ( m ) self . my_repo = self . is_my_repo ( provider , m ) self . my_user = self . my_repo or self . is_my_user ( provider , m ) value , link_type = self . get_type ( provider , m ) # All right, everything set, let's shorten. self . shorten ( link , provider , link_type , m . group ( provider + '_user_repo' ) , value , href , PROVIDER_INFO [ provider ] [ 'hash_size' ] ) return root