2 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Check whether a media script accepts the URL
28 domains .. table of domain names
29 paths .. table of URL path patterns to match
30 queries .. table of URL query patterns to match
32 true if media scripts accepts the URL, otheriwise false
34 function M
.accepts(url
, domains
, paths
, queries
)
36 if not url
or not domains
then
37 return {accepts
=false, domains
={}}
40 local U
= require
'quvi/util'
41 local R
= require
'quvi/url'
43 local t
= R
.parse(url
)
44 local a
= U
.match_any(domains
, t
.host
)
45 -- for k,v in pairs(t) do print(k,v) end
50 a
= U
.match_any(paths
, t
.path
)
56 a
= U
.match_any(queries
, t
.query
)
65 for _
,v
in pairs(domains
) do
66 v
= v
:gsub('%%w%+', 'com') -- Naive replacement.
67 v
= v
:gsub('%%', '') -- Strip any remaining Lua escape characters.
68 v
= v
:gsub('%s', '') -- Strip any whitespace.
72 return {accepts
=a
, domains
=table.concat(t
,',')}
76 package.path = package.path .. ';../?.lua'
78 local function dump(r)
80 for k,v in pairs(r) do print(k,v) end
85 dump(M.accepts('http://example.com', {'example%.%w+'}))
86 -- should return false
87 dump(M.accepts('http://example.com', {'examle%.%w+'}))
91 local d = {'example.%w+', 'foo.bar'}
93 dump(M.accepts('http://example.com/foo/bar', d, {'/foo/'}))
95 dump(M.accepts('http://foo.bar/baz/1234_fubar', d, {'/baz/'}))
96 -- should return false
97 dump(M.accepts('http://foobar/baz/1234_fubar', d, {'/baz/'}))
106 -- vim: set ts=2 sw=2 tw=72 expandtab: