Add filename regular expression matching to Sauce. #1183
This commit is contained in:
parent
fa3f439e62
commit
b600f258d8
@ -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 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 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 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:
|
<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>%TURL</code>: Thumbnail URL.</li>
|
||||||
<li><code>%URL</code>: Full image 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>%hMD5</code>: MD5 hash in hexadecimal.</li>
|
||||||
<li><code>%name</code>: Original file name.</li>
|
<li><code>%name</code>: Original file name.</li>
|
||||||
<li><code>%board</code>: Current board.</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>
|
<li><code>%%</code>, <code>%semi</code>: Literal <code>%</code> and <code>;</code>.</li>
|
||||||
</ul>
|
</ul>
|
||||||
<textarea hidden name="sauces" class="field" spellcheck="false"></textarea>
|
<textarea hidden name="sauces" class="field" spellcheck="false"></textarea>
|
||||||
|
|||||||
@ -19,13 +19,28 @@ Sauce =
|
|||||||
parseLink: (link) ->
|
parseLink: (link) ->
|
||||||
return null if not (link = link.trim())
|
return null if not (link = link.trim())
|
||||||
parts = {}
|
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
|
if i is 0
|
||||||
parts['url'] = part
|
parts['url'] = part
|
||||||
else
|
else
|
||||||
m = part.match /^(\w*):?(.*)$/
|
m = part.match /^(\w*):?(.*)$/
|
||||||
parts[m[1]] = m[2]
|
parts[m[1]] = m[2]
|
||||||
parts['text'] or= parts['url'].match(/(\w+)\.\w+\//)?[1] or '?'
|
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
|
parts
|
||||||
|
|
||||||
createSauceLink: (link, post) ->
|
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['boards'] or post.board.ID in parts['boards'].split ','
|
||||||
return null unless !parts['types'] or ext in parts['types'].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
|
skip = false
|
||||||
for key in ['url', 'text']
|
for key in ['url', 'text']
|
||||||
parts[key] = parts[key].replace /%(T?URL|IMG|[sh]?MD5|board|name|%|semi)/g, (_, parameter) ->
|
parts[key] = parts[key].replace /%(T?URL|IMG|[sh]?MD5|board|name|%|semi|\$\d+)/g, (_, parameter) ->
|
||||||
type = Sauce.formatters[parameter] post, ext
|
if parameter[0] is '$'
|
||||||
if not type?
|
return parameter unless matches
|
||||||
skip = true
|
type = matches[parameter[1..]]
|
||||||
return ''
|
else
|
||||||
|
type = Sauce.formatters[parameter] post, ext
|
||||||
|
if not type?
|
||||||
|
skip = true
|
||||||
|
return ''
|
||||||
|
|
||||||
if key is 'url' and parameter not in ['%', 'semi']
|
if key is 'url' and parameter not in ['%', 'semi']
|
||||||
type = JSON.stringify type if /^javascript:/i.test parts['url']
|
type = JSON.stringify type if /^javascript:/i.test parts['url']
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user