webmcp

diff framework/env/encode/url.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children d76a8857ba62
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/encode/url.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +--[[--
     1.5 +url_string =              -- a string containing an URL
     1.6 +encode.url{
     1.7 +  external  = external,   -- external URL (instead of specifying base, module, etc. below)
     1.8 +  base      = base,       -- optional string containing a base URL of a WebMCP application
     1.9 +  static    = static,     -- an URL relative to the static file directory
    1.10 +  module    = module,     -- a module name of the WebMCP application
    1.11 +  view      = view,       -- a view name of the WebMCP application
    1.12 +  action    = action,     -- an action name of the WebMCP application
    1.13 +  id        = id,         -- optional id to be passed to the view or action to select a particular data record
    1.14 +  params    = params      -- optional parameters to be passed to the view or action
    1.15 +}
    1.16 +
    1.17 +This function creates URLs to external locations, to static files within the WebMCP application or to a certain view or action inside a module.
    1.18 +
    1.19 +--]]--
    1.20 +
    1.21 +function encode.url(args)
    1.22 +  local external  = args.external
    1.23 +  local base      = args.base or request.get_relative_baseurl()
    1.24 +  local static    = args.static
    1.25 +  local module    = args.module
    1.26 +  local view      = args.view
    1.27 +  local action    = args.action
    1.28 +  local id        = args.id
    1.29 +  local params    = args.params or {}
    1.30 +  local result    = {}
    1.31 +  local id_as_param = false
    1.32 +  local function add(...)
    1.33 +    for i = 1, math.huge do
    1.34 +      local v = select(i, ...)
    1.35 +      if v == nil then break end
    1.36 +      result[#result+1] = v
    1.37 +    end
    1.38 +  end
    1.39 +  if external then
    1.40 +    add(external)
    1.41 +  else
    1.42 +    add(base)
    1.43 +    if not string.find(base, "/$") then
    1.44 +      add("/")
    1.45 +    end
    1.46 +    if static then
    1.47 +      add("static/")
    1.48 +      add(static)
    1.49 +    elseif module or view or action or id then
    1.50 +      assert(module, "Module not specified.")
    1.51 +      add(encode.url_part(module), "/")
    1.52 +      if view and not action then
    1.53 +        local view_base, view_suffix = string.match(
    1.54 +          view,
    1.55 +          "^([^.]*)(.*)$"
    1.56 +        )
    1.57 +        add(encode.url_part(view_base))
    1.58 +        if args.id then
    1.59 +          add("/", encode.url_part(id))
    1.60 +        end
    1.61 +        if view_suffix == "" then
    1.62 +          add(".html")
    1.63 +        else
    1.64 +          add(view_suffix)  -- view_suffix includes dot as first character
    1.65 +        end
    1.66 +      elseif action and not view then
    1.67 +        add(encode.url_part(action))
    1.68 +        id_as_param = true
    1.69 +      elseif view and action then
    1.70 +        error("Both a view and an action was specified.")
    1.71 +      end
    1.72 +    end
    1.73 +    do
    1.74 +      local new_params = request.get_perm_params()
    1.75 +      for key, value in pairs(params) do
    1.76 +        new_params[key] = value
    1.77 +      end
    1.78 +      params = new_params
    1.79 +    end
    1.80 +  end
    1.81 +  if next(params) ~= nil or (id and id_as_param) then
    1.82 +    add("?")
    1.83 +    if id and id_as_param then
    1.84 +      add("_webmcp_id=", encode.url_part(id), "&")
    1.85 +    end
    1.86 +    for key, value in pairs(params) do
    1.87 +      add(encode.url_part(key), "=", encode.url_part(value), "&")
    1.88 +    end
    1.89 +    result[#result] = nil  -- remove last '&' or '?'
    1.90 +  end
    1.91 +  return table.concat(result)
    1.92 +end

Impressum / About Us