Module:Gutenberg: Difference between revisions

Content deleted Content added
No edit summary
Add "Edit At Wikidata" link if using ID from Wikidata. Tested first with /sandbox and preview in article.
 
(20 intermediate revisions by 5 users not shown)
Line 6:
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. TheName later will go directgoes to the authorsearch page, thenumber formergoes direct to aauthor searchpage results page.
local name = nil -- display name on Wikipedia (default: article title)
local url = nil
local tagline = "at [[Project Gutenberg]]"
local urlheadname = "https://www.gutenberg.org/author/"
local urlheadnumb = "https://www.gutenberg.org/ebooks/author/"
local urlhead = nil
 
-- Argument |id=
if args.id == "" or args.id == nil then
id = trimArg(args[1]) or trimArg(args.id)
-- return "Error in {{" .. tname .. "}}, id missing. Please see documentation."
if not id then
return error("Parameter id is missing")
local bestP1938 = mw.wikibase.getBestStatements(mw.wikibase.getEntityIdForCurrentPage(), 'P1938')[1]
else
if bestP1938 and bestP1938.mainsnak.snaktype == 'value' then
id = mw.text.trim(args.id)
id = bestP1938.mainsnak.datavalue.value
if tonumber(id) ~= nil then -- it's a number
local entity = mw.wikibase.getEntityObject() or {}
urlhead = urlheadnumb
tagline = tagline .. " [[File:OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt=Edit this at Wikidata |link=https://www.wikidata.org/wiki/" .. entity.id .. "#P1938|Edit this at Wikidata]]"
else
error("Parameter id is missing. See [[Template:" .. tname .. "]] documentation")
urlhead = urlheadname
end
end
if tonumber(id) then -- it's a number
 
urlhead = urlheadnumb
if args.name == "" or args.name == nil then
name = mw.title.getCurrentTitle().basePageTitle
else
urlhead = urlheadname
name = mw.text.trim(args.name)
id = mw.ustring.gsub(id," ", "+")
end
 
-- Argument |name=
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
 
-- Argument |coda=
if trimArg(args.coda) then
tagline = tagline .. " " .. trimArg(args.coda)
end
 
Line 40 ⟶ 52:
end
 
function p.Australia(frame)
--[[
replace
local pframe = frame:getParent()
local args = pframe.args
 
local tname = "Gutenberg Australia" -- name of calling template. Change if template is renamed.
Source: https://en.wikipedia.org/wiki/Module:String#replace
local id = nil -- ID. eg. http://gutenberg.net.au/plusfifty-n-z.html#shanks .. the ID = plusfifty-n-z.html#shanks
-- ID is the same for linking an individual book title, or all books by the author.
local name = nil -- display name on Wikipedia (default: article title)
local author = nil -- flag if an author (default: no)
local url = nil
local urlhead = "http://gutenberg.net.au/"
local prefix = ""
local tagline = "at [[Project Gutenberg Australia]]"
local italic = "''"
 
-- Argument |id=
This function allows one to replace a target string or pattern within another string.
id = trimArg(args[1]) or trimArg(args.id)
if not id then
error("Parameter id is missing. See [[Template:" .. tname .. "]] documentation")
end
 
-- Argument |name=
Usage:
name = trimArg(args[2]) or trimArg(args.name)
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
if not name then
OR
name = mw.title.getCurrentTitle().text:gsub('%s+%([^%(]-%)$', '') -- Current page name without the final parentheses
{{#invoke:String|replace|source=source_string|pattern=pattern_string|replace=replace_string|
end
count=replacement_count|plain=plain_flag}}
 
-- Argument |author=
Parameters
author = trimArg(args.author)
source: The string to search
if author then
pattern: The string or pattern to find within source
if mw.ustring.lower(author) == "yes" then
replace: The replacement text
prefix = "Works by "
count: The number of occurences to replace, defaults to all.
italic = ""
plain: Boolean flag indicating that pattern should be understood as plain
end
text and not as a Lua style regular expression, defaults to true
end
]]
function replace( frame )
local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } );
local source_str = new_args['source'] or '';
local pattern = new_args['pattern'] or '';
local replace = new_args['replace'] or '';
local count = tonumber( new_args['count'] );
local plain = new_args['plain'] or true;
if source_str == '' or pattern == '' then
return source_str;
end
plain = str._getBoolean( plain );
 
-- Argument |coda=
if plain then
if trimArg(args.coda) then
pattern = str._escapePattern( pattern );
tagline = tagline .. " " .. trimArg(args.coda)
replace = mw.ustring.gsub( replace, "%%", "%%%%" ); --Only need to escape replacement sequences.
end
 
url = "[" .. urlhead .. id .. " " .. prefix .. italic .. name .. italic .. "] " .. tagline
 
return url
 
end
 
function p.Canada(frame)
local pframe = frame:getParent()
local args = pframe.args
 
local tname = "FadedPage" -- name of calling template. Change if template is renamed.
local id = nil -- ID for author, eg. http://fadedpage.com/csearch.php?author=Shortt%2C%20Adam .. the id = Shortt, Adam
-- ID for book titles, eg. http://fadedpage.com/showbook.php?pid=20160704 .. the id = 20160704
local name = nil -- display name on Wikipedia (default: article title)
local author = nil -- flag if an author (default: no)
local url = nil
local urlhead = "https://fadedpage.com/"
local urlbook = "showbook.php?pid="
local urlauth = "csearch.php?author="
local prefix = ""
local tagline = "at [[Distributed Proofreaders Canada|Faded Page]] (Canada)"
local italic = "''"
 
-- Argument |id=
id = trimArg(args[1]) or trimArg(args.id)
if not id then
error("Parameter id is missing. See [[Template:" .. tname .. "]] documentation")
end
 
-- Argument |name=
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
 
-- Argument |author=
author = trimArg(args.author)
if author then
if mw.ustring.lower(author) == "yes" then
id = mw.uri.encode( id, "PATH" ) -- handle spaces within id argument string
prefix = "Works by "
italic = ""
url = "[" .. urlhead .. urlauth .. id .. " " .. prefix .. italic .. name .. italic .. "] " .. tagline
return url
end
end
local result;
 
url = "[" .. urlhead .. urlbook .. id .. " " .. prefix .. italic .. name .. italic .. "] " .. tagline
if count ~= nil then
 
result = mw.ustring.gsub( source_str, pattern, replace, count );
return elseurl
result = mw.ustring.gsub( source_str, pattern, replace );
end
 
return result;
end
 
function trimArg(arg)
 
if arg == "" or arg == nil then
return nil
else
return mw.text.trim(arg)
end
 
end
 
return p