diff --git a/src/General/Settings/Sauce.html b/src/General/Settings/Sauce.html index 1a5d86cc4..017084ae3 100644 --- a/src/General/Settings/Sauce.html +++ b/src/General/Settings/Sauce.html @@ -3,6 +3,7 @@
You can specify a display text by appending ;text:[text] to the URL.
You can specify the applicable boards by appending ;boards:[board1],[board2].
You can specify the applicable file types by appending ;types:[extension1],[extension2].
+
You can specify a regular expression the filename must match by appending ;regexp:[regular expression].
diff --git a/src/Images/Sauce.coffee b/src/Images/Sauce.coffee index 5f2bff1e6..0281b6b46 100644 --- a/src/Images/Sauce.coffee +++ b/src/Images/Sauce.coffee @@ -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']