jbe/bsw@0: --[[-- jbe@267: url_string = -- a string containing an URL jbe/bsw@0: encode.url{ jbe@267: external = external, -- external URL (instead of specifying base, module, etc. below) jbe@267: base = base, -- optional string containing a base URL of a WebMCP application jbe@267: static = static, -- an URL relative to the static file directory jbe@267: module = module, -- a module name of the WebMCP application jbe@267: view = view, -- a view name of the WebMCP application jbe@267: action = action, -- an action name of the WebMCP application jbe@267: id = id, -- optional id to be passed to the view or action to select a particular data record jbe@267: params = params, -- optional parameters to be passed to the view or action jbe@267: anchor = anchor -- anchor in URL jbe/bsw@0: } jbe/bsw@0: jbe/bsw@0: This function creates URLs to external locations, to static files within the WebMCP application or to a certain view or action inside a module. jbe/bsw@0: jbe/bsw@0: --]]-- jbe/bsw@0: jbe/bsw@0: function encode.url(args) jbe/bsw@0: local external = args.external jbe/bsw@0: local base = args.base or request.get_relative_baseurl() jbe/bsw@0: local static = args.static jbe/bsw@0: local module = args.module jbe/bsw@0: local view = args.view jbe/bsw@0: local action = args.action jbe/bsw@0: local id = args.id jbe/bsw@0: local params = args.params or {} bsw@80: local anchor = args.anchor jbe/bsw@0: local result = {} jbe/bsw@0: local id_as_param = false jbe/bsw@0: local function add(...) jbe/bsw@0: for i = 1, math.huge do jbe/bsw@0: local v = select(i, ...) jbe/bsw@0: if v == nil then break end jbe/bsw@0: result[#result+1] = v jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: if external then jbe/bsw@0: add(external) jbe/bsw@0: else jbe/bsw@0: add(base) jbe/bsw@0: if not string.find(base, "/$") then jbe/bsw@0: add("/") jbe/bsw@0: end jbe/bsw@0: if static then jbe/bsw@0: add("static/") jbe/bsw@0: add(static) jbe/bsw@0: elseif module or view or action or id then jbe/bsw@0: assert(module, "Module not specified.") jbe/bsw@0: add(encode.url_part(module), "/") jbe/bsw@0: if view and not action then jbe/bsw@0: local view_base, view_suffix = string.match( jbe/bsw@0: view, jbe/bsw@0: "^([^.]*)(.*)$" jbe/bsw@0: ) jbe/bsw@0: add(encode.url_part(view_base)) jbe/bsw@0: if args.id then jbe/bsw@0: add("/", encode.url_part(id)) jbe/bsw@0: end jbe/bsw@0: if view_suffix == "" then jbe/bsw@0: add(".html") jbe/bsw@0: else jbe/bsw@0: add(view_suffix) -- view_suffix includes dot as first character jbe/bsw@0: end jbe/bsw@0: elseif action and not view then jbe/bsw@0: add(encode.url_part(action)) jbe/bsw@0: id_as_param = true jbe/bsw@0: elseif view and action then jbe/bsw@0: error("Both a view and an action was specified.") jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: do jbe/bsw@0: local new_params = request.get_perm_params() jbe/bsw@0: for key, value in pairs(params) do jbe/bsw@0: new_params[key] = value jbe/bsw@0: end jbe/bsw@0: params = new_params jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: if next(params) ~= nil or (id and id_as_param) then jbe@452: if not (external and string.find(external, "%?")) then jbe@452: add("?") jbe@453: elseif external and not string.find(external, "&$") then jbe@452: add("&") jbe@452: end jbe/bsw@0: if id and id_as_param then jbe/bsw@0: add("_webmcp_id=", encode.url_part(id), "&") jbe/bsw@0: end jbe/bsw@0: for key, value in pairs(params) do jbe/bsw@11: -- TODO: better way to detect arrays? jbe/bsw@11: if string.match(key, "%[%]$") then jbe/bsw@11: for idx, entry in ipairs(value) do jbe/bsw@11: add(encode.url_part(key), "=", encode.url_part(entry), "&") jbe/bsw@11: end jbe/bsw@11: else jbe/bsw@11: add(encode.url_part(key), "=", encode.url_part(value), "&") jbe/bsw@11: end jbe/bsw@0: end jbe/bsw@0: result[#result] = nil -- remove last '&' or '?' jbe/bsw@0: end jbe@79: local string_result = table.concat(result) jbe@79: if anchor ~= nil then jbe@79: string_result = string.match(string_result, "^[^#]*") jbe@79: if anchor then bsw@80: string_result = string_result .. "#" .. encode.url_part(anchor) jbe@79: end jbe@79: end jbe@79: return string_result jbe/bsw@0: end