bugdown: Make the youtube URL regex slightly easier to read.

This commit is contained in:
Puneeth Chaganti 2019-04-12 09:28:57 +05:30 committed by Tim Abbott
parent 8afda1c1bb
commit ca8e9fb800
1 changed files with 6 additions and 3 deletions

View File

@ -679,9 +679,12 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
# Youtube video id extraction regular expression from http://pastebin.com/KyKAFv1s # Youtube video id extraction regular expression from http://pastebin.com/KyKAFv1s
# Slightly modified to support URLs of the form youtu.be/<id> # Slightly modified to support URLs of the form youtu.be/<id>
# If it matches, match.group(2) is the video id. # If it matches, match.group(2) is the video id.
youtube_re = r'^((?:https?://)?(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/)' + \ schema_re = r'(?:https?://)'
r'(?:(?:(?:v|embed)/)|(?:(?:watch(?:_popup)?(?:\.php)?)?(?:\?|#!?)(?:.+&)?v=))?)' + \ host_re = r'(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/)'
r'?([0-9A-Za-z_-]+)(?(1).+)?$' param_re = r'(?:(?:(?:v|embed)/)|(?:(?:watch(?:_popup)?(?:\.php)?)?(?:\?|#!?)(?:.+&)?v=))'
id_re = r'([0-9A-Za-z_-]+)'
youtube_re = r'^({schema_re}?{host_re}{param_re}?)?{id_re}(?(1).+)?$'
youtube_re = youtube_re.format(schema_re=schema_re, host_re=host_re, id_re=id_re, param_re=param_re)
match = re.match(youtube_re, url) match = re.match(youtube_re, url)
if match is None: if match is None:
return None return None