PERF: Avoid lookbehinds when replacing links in imported emails (#11931)

Follow-up to 3c678df942
This commit is contained in:
Gerhard Schlager 2021-02-02 17:34:00 +01:00 committed by GitHub
parent 12a4fefef6
commit f88def5f5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 14 deletions

View File

@ -3,10 +3,6 @@
# A very simple formatter for imported emails
class EmailCook
def self.url_regexp
@url_regexp ||= /((?:https?:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.])(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»“”‘’\s]))/
end
def self.raw_regexp
@raw_regexp ||= /^\[plaintext\]$\n(.*)\n^\[\/plaintext\]$(?:\s^\[attachments\]$\n(.*)\n^\[\/attachments\]$)?(?:\s^\[elided\]$\n(.*)\n^\[\/elided\]$)?/m
end
@ -25,17 +21,21 @@ class EmailCook
def link_string!(line, unescaped_line)
unescaped_line = unescaped_line.strip
unescaped_line.scan(EmailCook.url_regexp).each do |m|
url = m[0]
line.gsub!(/\S+/) do |str|
if str.match?(/^(https?:\/\/)[\S]+$/i)
begin
url = URI.parse(str).to_s
if unescaped_line == url
# this could be oneboxed
val = %|<a href="#{url}" class="onebox" target="_blank">#{url}</a>|
str = %|<a href="#{url}" class="onebox" target="_blank">#{url}</a>|
else
val = %|<a href="#{url}">#{url}</a>|
str = %|<a href="#{url}">#{url}</a>|
end
line.gsub!(url, val)
rescue URI::Error
# don't fail if uri does not parse
end
end
str
end
end