Add filename regular expression matching to Sauce. #1183

This commit is contained in:
ccd0 2017-01-21 15:23:41 -08:00
parent fa3f439e62
commit b600f258d8
2 changed files with 29 additions and 6 deletions

View File

@ -3,6 +3,7 @@
<div>You can specify a display text by appending <code>;text:[text]</code> to the URL.</div>
<div>You can specify the applicable boards by appending <code>;boards:[board1],[board2]</code>.</div>
<div>You can specify the applicable file types by appending <code>;types:[extension1],[extension2]</code>.</div>
<div>You can specify a regular expression the filename must match by appending <code>;regexp:[regular expression]</code>.</div>
<ul>These parameters will be replaced by their corresponding values in the URL and displayed text:
<li><code>%TURL</code>: Thumbnail URL.</li>
<li><code>%URL</code>: Full image URL.</li>
@ -12,6 +13,8 @@
<li><code>%hMD5</code>: MD5 hash in hexadecimal.</li>
<li><code>%name</code>: Original file name.</li>
<li><code>%board</code>: Current board.</li>
<li><code>%$0</code>: The matched regular expression.</li>
<li><code>%$1</code>, <code>%$2</code>, and so on: Subexpressions within the matched regular expression.</li>
<li><code>%%</code>, <code>%semi</code>: Literal <code>%</code> and <code>;</code>.</li>
</ul>
<textarea hidden name="sauces" class="field" spellcheck="false"></textarea>

View File

@ -19,13 +19,28 @@ Sauce =
parseLink: (link) ->
return null if not (link = link.trim())
parts = {}
for part, i in link.split /;(?=(?:text|boards|types|sandbox):?)/
for part, i in link.split /;(?=(?:text|boards|types|regexp|sandbox):?)/
if i is 0
parts['url'] = part
else
m = part.match /^(\w*):?(.*)$/
parts[m[1]] = m[2]
parts['text'] or= parts['url'].match(/(\w+)\.\w+\//)?[1] or '?'
if 'regexp' of parts
try
if (regexp = parts['regexp'].match /^\/(.*)\/(\w*)$/)
parts['regexp'] = RegExp regexp[1], regexp[2]
else
parts['regexp'] = RegExp parts['regexp']
catch err
new Notice 'warning', [
$.tn "Invalid regexp for Sauce link:"
$.el 'br'
$.tn link
$.el 'br'
$.tn err.message
], 60
return null
parts
createSauceLink: (link, post) ->
@ -35,14 +50,19 @@ Sauce =
return null unless !parts['boards'] or post.board.ID in parts['boards'].split ','
return null unless !parts['types'] or ext in parts['types'].split ','
return null unless !parts['regexp'] or (matches = post.file.name.match parts['regexp'])
skip = false
for key in ['url', 'text']
parts[key] = parts[key].replace /%(T?URL|IMG|[sh]?MD5|board|name|%|semi)/g, (_, parameter) ->
type = Sauce.formatters[parameter] post, ext
if not type?
skip = true
return ''
parts[key] = parts[key].replace /%(T?URL|IMG|[sh]?MD5|board|name|%|semi|\$\d+)/g, (_, parameter) ->
if parameter[0] is '$'
return parameter unless matches
type = matches[parameter[1..]]
else
type = Sauce.formatters[parameter] post, ext
if not type?
skip = true
return ''
if key is 'url' and parameter not in ['%', 'semi']
type = JSON.stringify type if /^javascript:/i.test parts['url']