Changeset 17832


Ignore:
Timestamp:
Aug 10, 2020, 6:46:57 PM (5 years ago)
Author:
Ryan J Ollos
Message:

TracMediaWikiMacro 1.1dev: Fix multiple issues

  • Fixed issue where _specialUrlPat regex in replaceFreeExternalLinks was failing to render external links in paragraphs containing newlines because the re.DOTALL regex flag was not being used to allow .* to match newlines.
  • Fixed bug in replaceFreeExternalLinks where the loop variable i was being stepped on for another purpose by code inside the loop.
  • Fixed regex issue where control-char ranges for ascii were being specified to match against unicode strings.
  • Fixed issue where nextItem was called with only one rather than the required two parameters.
  • Support rendering of anchor links such as: [[#heading 4|heading four]]

Patch by James Yonan.

Untested by committer.

Refs #3382.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mediawikipluginmacro/mediawiki/parser.py

    r17387 r17832  
    1515
    1616
    17 class MediaWikiRenderer(Component):
    18     """
    19     Renders plain text in MediaWiki format as HTML
    20     """
    21     implements(IWikiMacroProvider)
    22 
    23     def get_macros(self):
    24         """Return a list of provided macros"""
    25         yield 'mediawiki'
    26 
    27     def get_macro_description(self, name):
    28         return '''desc'''
    29 
    30     def expand_macro(self, formatter, name, content):
    31         if name == 'mediawiki':
    32             return parse(content)
    33 
    34 
    35     if __name__ == "__main__":
    36         import sys
    37         try:
    38             file = open(sys.argv[1])
    39             print parse(file.read())
    40         except:
    41             pass
     17if __name__ == "__main__":
     18    import sys
     19    try:
     20        file = open(sys.argv[1])
     21        print parse(file.read())
     22    except:
     23        pass
    4224
    4325mTagHooks = {}
     
    928910    return u'/'.join(slugifyBit(t) for t in text.split(u'/'))
    929911
    930 _linkPat = re.compile(ur'^([A-Za-z0-9\s]+:)?([A-Za-z0-9_\.\-\s\/]+)(?:\|([^\n]+?))?\]\](.*)$', re.UNICODE | re.DOTALL)
     912_linkPat = re.compile(ur'^([A-Za-z0-9\s]+:)?(#)?([A-Za-z0-9_\.\-\s\/\(\)]+)(?:\|([^\n]+?))?\]\](.*)$', re.UNICODE | re.DOTALL)
    931913def replaceInternalLinks(text):
    932914    arr = text.split('[[')
     
    934916    sb.append(arr.pop(0))
    935917    for bit in arr:
    936         namespace, link, alt, rest = None, None, None, None
     918        namespace, anchorchar, link, alt, rest = None, None, None, None, None
    937919        match = _linkPat.match(bit)
    938920        if match:
    939             namespace, link, alt, rest = match.groups()
     921            namespace, anchorchar, link, alt, rest = match.groups()
    940922        if link:
    941923            if not namespace:
     
    956938                #sb.append(namespace)
    957939                #sb.append(u'/')
    958                 sb.append(slugify(link))
     940                if anchorchar:
     941                    sb.append(u'#')
     942                    sb.append(slugifyBit(link))
     943                    sb.append(u'">')
     944                else:
     945                    sb.append(slugify(link))
     946                    sb.append(u'/">')
    959947                if alt:
    960948                    link = alt
    961                 sb.append(u'/">')
    962949                sb.append(link)
    963950                sb.append(u'</a>')
     
    979966    return text, showToc
    980967
    981 _bracketedLinkPat = re.compile(ur'(?:\[((?:https?://|ftp://|/)[^<>\]\[' + u"\x00-\x20\x7f" + ur']+)\s*(.*?)\])', re.UNICODE)
     968_bracketedLinkPat = re.compile(ur'(?:\[((?:https?://|ftp://|/)[^<>\]\[' + ur'\u0000-\u0020\u007f-\u009f' + ur']+)\s*(.*?)\])', re.UNICODE)
    982969def replaceExternalLinks(text):
    983970    sb = []
     
    1006993
    1007994_protocolPat = re.compile(ur'(\b(?:https?://|ftp://))', re.UNICODE)
    1008 _specialUrlPat = re.compile(ur'^([^<>\]\[' + u"\x00-\x20\x7f" + ur']+)(.*)$', re.UNICODE)
     995_specialUrlPat = re.compile(ur'^([^<>\]\[' + ur'\u0000-\u0020\u007f-\u009f' + ur']+)(.*)$', re.UNICODE | re.DOTALL)
    1009996_protocolsPat = re.compile(ur'^(https?://|ftp://)$', re.UNICODE)
    1010997
     
    10451032                sep += ')'
    10461033
    1047             i = len(url)-1
    1048             while i >= 0:
    1049                 char = url[i]
     1034            j = len(url)-1
     1035            while j >= 0:
     1036                char = url[j]
    10501037                if char not in sep:
    10511038                    break
    1052                 i -= 1
    1053             i += 1
    1054 
    1055             if i != len(url):
    1056                 trail = url[i:] + trail
    1057                 url = url[0:i]
     1039                j -= 1
     1040            j += 1
     1041
     1042            if j != len(url):
     1043                trail = url[j:] + trail
     1044                url = url[0:j]
    10581045
    10591046            url = cleanURL(url)
     
    10641051            sb.append(url)
    10651052            sb.append(u'</a>')
    1066             sb.append(text)
    10671053            sb.append(trail)
    10681054        else:
     
    10771063    return "%%%02x" % num
    10781064
    1079 _controlCharsPat = re.compile(ur'[\]\[<>"' + u"\\x00-\\x20\\x7F" + ur']]', re.UNICODE)
     1065_controlCharsPat = re.compile(ur'[\]\[<>"' + ur'\u0000-\u0020\u007f-\u009f' + ur']]', re.UNICODE)
    10801066_hostnamePat = re.compile(ur'^([^:]+:)(//[^/]+)?(.*)$', re.UNICODE)
    10811067_stripPat = re.compile(u'\\s|\u00ad|\u1806|\u200b|\u2060|\ufeff|\u03f4|\u034f|\u180b|\u180c|\u180d|\u200c|\u200d|[\ufe00-\ufe0f]', re.UNICODE)
     
    23432329                lastPrefixLength -= 1
    23442330            if prefixLength <= commonPrefixLength and commonPrefixLength > 0:
    2345                 tmpOutput, tmpMDTopen = nextItem(pref[commonPrefixLength-1])
     2331                tmpOutput, tmpMDTopen = nextItem(pref[commonPrefixLength-1], mDTopen)
    23462332                output += tmpOutput
    23472333                if tmpMDTopen is not None:
Note: See TracChangeset for help on using the changeset viewer.