Difference between revisions of "Module:Librivox book"

From blackwiki
Jump to navigation Jump to search
blackwiki>GreenC
m (56 revisions imported)
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
 
+
 
function p.book(frame)
 
function p.book(frame)
 
  
 
   local pframe = frame:getParent()
 
   local pframe = frame:getParent()
Line 9: Line 8:
 
   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 title  = nil -- display and search title
+
   local title  = nil -- display and search title (default: article name w/out dab)
 
   local dtitle  = nil -- display title (default: title)
 
   local dtitle  = nil -- display title (default: title)
 
   local stitle  = nil -- search title (default: title)
 
   local stitle  = nil -- search title (default: title)
Line 26: Line 25:
 
   title = trimArg(args.title)
 
   title = trimArg(args.title)
 
   if not title then
 
   if not title then
     title = mw.title.getCurrentTitle().text:gsub('%s+%([^%(]-%)$', '') -- Current page name without the final parentheses
+
     title = mw.title.getCurrentTitle().text
    dtitle = title
 
    stitle = dtitle
 
  else
 
    dtitle = title
 
    stitle = dtitle
 
 
   end
 
   end
 +
  dtitle = mw.ustring.gsub(title,'%s+%([^%(]-%)$', '')        -- Remove the final disambig paren
 +
  stitle = dtitle
  
 
   if trimArg(args.stitle) then
 
   if trimArg(args.stitle) then
 
     stitle = trimArg(args.stitle)
 
     stitle = trimArg(args.stitle)
     if not trimArg(args.title) then
+
     if not trimArg(args.title) then                           -- For when used outside main article space
 
       dtitle = stitle
 
       dtitle = stitle
 
     end
 
     end
Line 45: Line 41:
 
   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 stitle = mw.ustring.gsub(stitle," ", "+")            -- replace "<space>" with "+"
  
Line 59: Line 53:
 
   end
 
   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
+
   local url = "[[File:Speaker Icon.svg|15px|link=|alt=]] " .. "[" .. 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
 
   return url

Latest revision as of 06:12, 27 September 2020

Usage

There is currently 1 template that invokes this module, {{Librivox book}}.



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 (default: article name w/out dab)
  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
  end
  dtitle = mw.ustring.gsub(title,'%s+%([^%(]-%)$', '')        -- Remove the final disambig paren
  stitle = dtitle

  if trimArg(args.stitle) then
    stitle = trimArg(args.stitle)
    if not trimArg(args.title) then                           -- For when used outside main article space
      dtitle = stitle
    end
  end
  if trimArg(args.dtitle) then
    dtitle = trimArg(args.dtitle)
    italic  = ""
  end

  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 = "[[File:Speaker Icon.svg|15px|link=|alt=]] " .. "[" .. 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