Making WordPress.org

Changeset 14611


Ignore:
Timestamp:
11/21/2025 07:39:32 AM (3 weeks ago)
Author:
dd32
Message:

Trac: Attempt to mark external links as rel="ugc nofollow". Take 3?.

Props dd32, sabernhardt.
See #4950

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/trac.wordpress.org/templates/site.html

    r14554 r14611  
    305305    <textarea class="${select('@class')} wp-exclude-emoji" id="${select('@id')}" name="${select('@name')}" rows="${select('@rows')}" cols="${select('@cols')}">${select('*|comment()|text()')}</textarea>
    306306</py:match>
     307
     308<!--! Mark user-generated-content links as nofollow ugc. -->
     309<div py:match="div[contains(@class,'description') or contains(@class,'comment') or contains(@class,'attachments')]" py:attrs="select('@*')">
     310    <?python
     311   
     312    import re
     313    def wporg_link_is_wporg_url(url):
     314        return None != re.match( r'([^>]*href=")?https?://([a-z0-9.-]+\.)?(wordpress\.(org|net)|buddypress\.org|bbpress\.org|wordcamp\.org)/', url, flags=re.IGNORECASE )
     315
     316    def wporg_replace_link_markup( m ):
     317        if 'ext-link' not in m.group(1):
     318            return '<' + 'a' + m.group(1) + m.group(2)
     319
     320        # Remove ext-link and set as ugc.
     321        if wporg_link_is_wporg_url( m.group(1) ):
     322            return '<' + 'a rel="ugc" ' + m.group(1).replace('class="ext-link"', '') + m.group(2)
     323
     324        # Keep ext-link, and set ugc nofollow.
     325        return '<' + 'a rel="ugc nofollow" ' + m.group(1) + m.group(2)
     326
     327    from genshi.core import TEXT, Markup
     328    def wporg_nofollow_ugc(stream):
     329        for kind, data, pos in stream:
     330            if kind is TEXT and isinstance( data, Markup ) and ( 'ext-link' in data ):
     331                data = re.sub( r'<a([^>]+)(>.+?</a>)', wporg_replace_link_markup, data, flags=re.IGNORECASE )
     332                data = Markup( data );
     333            yield kind, data, pos
     334    ?>
     335    ${wporg_nofollow_ugc(select('*|comment()|text()'))}
     336</div>
    307337
    308338<body py:match="body" id="wordpress-org" class="${project_slug} trac wporg-make make-${project_slug}" py:attrs="select('@*')">
Note: See TracChangeset for help on using the changeset viewer.