Jump to content

Module:Gutenberg: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
example of supporting args[1] and args[2]
Line 16: Line 16:
local urlhead = nil
local urlhead = nil


args.id = trimArg(args.id)
id = trimArg(args[1]) or trimArg(args.id)
if not args.id then
if not id then
return error("Parameter id is missing. See [[Template:" .. tname .. "]] documentation")
error("Parameter id is missing. See [[Template:" .. tname .. "]] documentation")
else
else
if tonumber(id) then -- it's a number
id = args.id
if tonumber(id) ~= nil then -- it's a number
urlhead = urlheadnumb
urlhead = urlheadnumb
else
else
Line 28: Line 27:
end
end


args.name = trimArg(args.name)
name = trimArg(args[2]) or trimArg(args.name)
if not args.name then
if not name then
name = mw.title.getCurrentTitle().basePageTitle.text
name = mw.title.getCurrentTitle().basePageTitle.text
else
name = args.name
end
end



Revision as of 01:24, 5 October 2015

local p = {}
 
function p.author(frame)
  
  local pframe = frame:getParent()
  local args = pframe.args
 
  local tname = "Gutenberg author" -- name of calling template. Change if template is renamed.
 
  local id       = nil -- author name, or author number. The later will go direct to the author page, the former to a search results page.
  local name     = nil -- display name on Wikipedia (default: article title)
  local url      = nil
  local tagline  = "at [[Project Gutenberg]]"
  local urlheadname  = "//www.gutenberg.org/author/"
  local urlheadnumb  = "//www.gutenberg.org/ebooks/author/"
  local urlhead  = nil

  id = trimArg(args[1]) or trimArg(args.id)
  if not id then
    error("Parameter id is missing. See [[Template:" .. tname .. "]] documentation")
  else
    if tonumber(id) then -- it's a number
      urlhead = urlheadnumb
    else
      urlhead = urlheadname
    end
  end 

  name = trimArg(args[2]) or trimArg(args.name)
  if not name then
    name = mw.title.getCurrentTitle().basePageTitle.text
  end

  url = "[" .. urlhead .. id .. " Works by " .. name .. "] " .. tagline

  return url

end

function trimArg(arg)

  if arg == "" or arg == nil then
    return nil
  else
    return mw.text.trim(arg)
  end

end

return p