Jump to content

Module:Librivox book/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 9: Line 9:
local tname = "Librivox book" -- name of calling template. Change if template rename.
local tname = "Librivox book" -- name of calling template. Change if template rename.


local dtitle = nil -- display title (default: title field)
local title = nil -- display and search title
local stitle = nil -- search title (default: title field)
local dtitle = nil -- display title (default: title)
local stitle = nil -- search title (default: title)
local lname = nil -- last name
local id = nil -- unsupported argument
local author = nil -- author
local tagline = "public domain audiobook at [[LibriVox]]"
local tagline = "public domain audiobook at [[LibriVox]]"
local urlhead = "https://librivox.org/search?"
local urlhead = "https://librivox.org/search?"
local italic = "''"
local italic = "''"


if args.id ~= "" and args.id ~= nil then
id = trimArg(args.id)
if id then
return "Error in {{" .. tname .. "}}, id not supported. Please use {{Librivox author}}"
error("Error in Template:" .. tname .. " - id argument not supported - please see documentation at [[Template:Librivox author]]")
end
end


if args.title == "" or args.title == nil then
title = trimArg(args.title)
title = mw.title.getCurrentTitle().text
if not title then
title = mw.title.getCurrentTitle().text:gsub('%s+%([^%(]-%)$', '') -- Current page name without the final parentheses
dtitle = title
dtitle = title
stitle = dtitle
stitle = dtitle
else
else
title = mw.text.trim(args.title)
dtitle = title
dtitle = title
stitle = dtitle
stitle = dtitle
end
end


if args.stitle ~= nil and args.stitle ~= "" then
if trimArg(args.stitle) then
stitle = mw.text.trim(args.stitle)
stitle = trimArg(args.stitle)
if not trimArg(args.title) then
dtitle = stitle
end
end
end
if args.dtitle ~= nil and args.dtitle ~= "" then
if trimArg(args.dtitle) then
dtitle = mw.text.trim(args.dtitle)
dtitle = trimArg(args.dtitle)
italic = ""
italic = ""
end
end
local dtitle = mw.ustring.gsub(dtitle,"%s%(.*%)", "") -- remove disambiguation ()
local stitle = mw.ustring.gsub(stitle,"%s%(.*%)", "")
local stitle = mw.ustring.gsub(stitle," ", "+") -- replace "<space>" with "+"


local dtitle = mw.ustring.gsub(dtitle,"%s+%([^%(]-%)$", "") -- remove disambiguation ()
if args.author == "" or args.author == nil then
local stitle = mw.ustring.gsub(stitle,"%s+%([^%(]-%)$", "")
local stitle = mw.ustring.gsub(stitle," ", "+") -- replace "<space>" with "+"

author = trimArg(args.author)
if not author then
lname = ""
lname = ""
else
else
--- Split name into words, count words, set name to last word
--- Split name into words, count words, set name to last word
local N = mw.text.split(mw.text.trim(args.author), " ")
local N = mw.text.split(author, " ")
local l, count = mw.ustring.gsub(mw.text.trim(args.author), "%S+", "")
local l, count = mw.ustring.gsub(author, "%S+", "")
lname = N[count]
lname = N[count]
end
end
Line 53: Line 63:
return url
return url


end

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



Revision as of 00:55, 6 October 2015

local p = {}

function p.book(frame)


  local pframe = frame:getParent()
  local args = pframe.args

  local tname = "Librivox book" -- name of calling template. Change if template rename.

  local title   = nil -- display and search title
  local dtitle  = nil -- display title (default: title)
  local stitle  = nil -- search title (default: title)
  local lname   = nil -- last name
  local id      = nil -- unsupported argument
  local author  = nil -- author
  local tagline = "public domain audiobook at [[LibriVox]]"
  local urlhead = "https://librivox.org/search?"
  local italic   = "''"

  id = trimArg(args.id)
  if id then
    error("Error in Template:" .. tname .. " - id argument not supported - please see documentation at [[Template:Librivox author]]")
  end

  title = trimArg(args.title)
  if not title then
    title = mw.title.getCurrentTitle().text:gsub('%s+%([^%(]-%)$', '') -- Current page name without the final parentheses
    dtitle = title
    stitle = dtitle
  else
    dtitle = title
    stitle = dtitle
  end

  if trimArg(args.stitle) then
    stitle = trimArg(args.stitle)
    if not trimArg(args.title) then
      dtitle = stitle
    end
  end
  if trimArg(args.dtitle) then
    dtitle = trimArg(args.dtitle)
    italic  = ""
  end

  local dtitle = mw.ustring.gsub(dtitle,"%s+%([^%(]-%)$", "") -- remove disambiguation () 
  local stitle = mw.ustring.gsub(stitle,"%s+%([^%(]-%)$", "")
  local stitle = mw.ustring.gsub(stitle," ", "+")             -- replace "<space>" with "+"

  author = trimArg(args.author)
  if not author then
    lname = ""
  else
    --- Split name into words, count words, set name to last word
    local N = mw.text.split(author, " ")
    local l, count = mw.ustring.gsub(author, "%S+", "")
    lname = N[count]
  end

  local url = "[[Image:Speaker Icon.svg|20px]] " .. "[" .. urlhead .. "title=" .. stitle .. "&author=" .. lname .. "&reader=&keywords=&genre_id=0&status=all&project_type=either&recorded_language=&sort_order=catalog_date&search_page=1&search_form=advanced" .. " " .. italic .. dtitle .. italic .. "]" .. " " .. 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