~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to gtdata/modules/external/cgilua/dispatcher.lua

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-- CGILua dispatcher module
 
2
-- @release $Id: dispatcher.lua,v 1.8 2007/12/07 18:49:49 carregal Exp $
 
3
 
 
4
module(..., package.seeall)
 
5
 
 
6
-- Checks if an URL matches a route pattern
 
7
local function route_match(url, pattern) 
 
8
    local params = {}
 
9
    local captures = string.gsub(pattern, "(/$[%w_-]+)", "/([^/]*)")
 
10
    local url_parts = {string.match(url, captures)}
 
11
    local i = 1
 
12
    for name in string.gmatch(pattern, "/$([%w_-]+)") do
 
13
        params[name] = url_parts[i]
 
14
        i = i + 1
 
15
    end
 
16
    return next(params) and params
 
17
end
 
18
 
 
19
local route_URLs = {}
 
20
 
 
21
-- Maps the correct function for an URL
 
22
local function route_map(url) 
 
23
    for i, v in ipairs(route_URLs) do
 
24
        local pattern, f, name = unpack(v)
 
25
        local params = route_match(url, pattern)
 
26
        if params then 
 
27
            return f, params 
 
28
        end
 
29
    end
 
30
end
 
31
 
 
32
-- Returns an URL for a named route
 
33
-- @param map_name Name associated with the map in the routed URL table.
 
34
-- @param params Table of named parameters used in the URL map
 
35
-- @param queryargs Optional table of named parameters used for the QUERY part of the URL
 
36
function route_url(map_name, params, queryargs)
 
37
        local queryparams = ""
 
38
        if queryargs then
 
39
                queryparams = "?"..cgilua.urlcode.encodetable(queryargs)
 
40
        end
 
41
        for i, v in ipairs(route_URLs) do
 
42
        local pattern, f, name = unpack(v)
 
43
        if name == map_name then
 
44
            local url = string.gsub(pattern, "$([%w_-]+)", params)
 
45
            url = cgilua.urlpath.."/"..cgilua.app_name..url..queryparams
 
46
            return url
 
47
        end
 
48
    end
 
49
end
 
50
 
 
51
-- Defines the routing using a table of URLs maps or a single map
 
52
-- a map defines a URL mask using $name to extract parameters,
 
53
-- a function to be called with the extracted parameters and
 
54
-- a name for the map when used with route_url
 
55
-- @param table of maps or a single map
 
56
function route(URLs)
 
57
        URLs = URLs or {}
 
58
        if type(URLs[1]) == "string" then
 
59
                -- accepts a single map as the only entry in a map table
 
60
                URLs = {URLs}
 
61
        end
 
62
    route_URLs = URLs
 
63
    f, args = route_map(cgilua.script_vpath)
 
64
 
 
65
    if f then
 
66
        return f(args)
 
67
    else
 
68
        error("Missing page parameters")
 
69
    end
 
70
end
 
 
b'\\ No newline at end of file'