Jump to content

Module:User:Mr. Stradivarius/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 05:32, 8 October 2015 (save half-finished Module:Gutenberg code). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local function makeGutenbergLink(options)
  
  local pframe = options.frame:getParent()
  local args = pframe.args
 
  local tname = options.tname or "Gutenberg author"                -- name of calling template. Change if template is renamed.
 
  local id       = nil                                             -- author name, or number. Name goes to search page, number goes direct to author page 
  local name     = nil                                             -- display name on Wikipedia (default: article title)
  local url      = nil
  local tagline  = options.tagline or "at [[Project Gutenberg]]"
  local urlheadname  = options.urlheadname or "http://www.gutenberg.org/author/" -- SSL problems with certain browsers. See [[Template_talk:Gutenberg_author#https_problem]]
  local urlheadnumb  = options.urlheadnumb or "http://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().text:gsub('%s+%([^%(]-%)$', '') -- Current page name without the final parentheses
  end

  local stitle = mw.ustring.gsub(name," ", "+")                       -- replace "<space>" with "+"

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

  return url

end

function p.author(frame)
  return makeGutenbergLink{
    frame = frame,
    tname = "Gutenberg author",
    tagline = "at [[Project Gutenberg]]",
    urlheadname = "http://www.gutenberg.org/author/",
    urlheadnumb = "http://www.gutenberg.org/ebooks/author/"
  }
end

function p.Australia(frame)
  return makeGutenbergLink{
    frame = frame,
    tname = "Gutenberg Australia",
    tagline = "at [[Project Gutenberg]]",
    urlheadname = "http://www.gutenberg.org/author/",
    urlheadnumb = "http://www.gutenberg.org/ebooks/author/"
  }
end

function trimArg(arg)

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

end

return p