NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

urls.py (2779B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\chatutil\urls.py
      2 import re
      3 import eveformat
      4 from chatutil.filter import CleanAndHighlightText
      5 seemsURL = re.compile('\\b\n        (\n            (\n                (https?://|www\\d{0,3}[.])           # Starts with http(s) or www with optional number\n                [a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,63}     # Followed by *. and .* with 2 to 63 characters\n            )\n            |                                       # or\n            (\n                https?://                           # Starts with http(s)\n                \\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}  # Followed by ip number\n            )\n        )\n        (:\\d{1,6})?                                 # Optional port number\n        (\n            (/([^\\s]+)?)                            # Must continue with / before we eat up optional non-space\n            |\n            /?                                      # or may end with single /\n        )\n        ', re.X)
      6 alreadyURLOrTag = re.compile('(<a .*?/a>|<url.*?/url>|<.*?>)')
      7 
      8 def LinkURLs(text):
      9     idx = 0
     10     parseParts = []
     11     match = alreadyURLOrTag.search(text)
     12     while match:
     13         start, end = match.span()
     14         parse = text[idx:start]
     15         if parse:
     16             parseParts.append((1, parse))
     17         notParse = text[start:end]
     18         parseParts.append((0, notParse))
     19         match = alreadyURLOrTag.search(text, end)
     20         idx = end
     21 
     22     leftOver = text[idx:]
     23     if leftOver:
     24         parseParts.append((1, leftOver))
     25     retText = ''
     26     for parseFlag, parseText in parseParts:
     27         if parseFlag == 0:
     28             cleanText = CleanTextInsideTags(parseText)
     29             retText += cleanText
     30             continue
     31         normalizedText = parseText
     32         match = seemsURL.search(normalizedText)
     33         idx = 0
     34         while match:
     35             start, end = match.span()
     36             url = normalizedText[start:end]
     37             while url[-1] in ',.':
     38                 url = url[:-1]
     39                 end -= 1
     40 
     41             if not url.startswith('http'):
     42                 url = 'http://' + url
     43             urlText = parseText[start:end]
     44             urlText = CleanAndHighlightText(urlText)
     45             retText += parseText[idx:start] + '<url=' + url + '>' + urlText + '</url>'
     46             match = seemsURL.search(normalizedText, end)
     47             idx = end
     48 
     49         cleanText = CleanAndHighlightText(parseText[idx:])
     50         retText += cleanText
     51 
     52     return retText
     53 
     54 
     55 def CleanTextInsideTags(parseText):
     56     withoutLinks = eveformat.strip_tags(parseText, tags=['url', 'a'])
     57     cleanTextWithoutLinks = CleanAndHighlightText(withoutLinks)
     58     return eveformat.replace_text_ignoring_tags(parseText, withoutLinks, cleanTextWithoutLinks)