Module:Citation/CS1: Difference between revisions

Content deleted Content added
sync to sandbox, deals with hidden comment issue
sync to sandbox
Line 1:
local z = {}
error_categories = {};
 
}
function hideinprint(content)
return content
end
 
function onlyinprint(content)
return ""
end
 
-- Formats a hidden comment for error trapping not intended to be visible to readers
function hiddencomment( content )
return '<span style="display: none;" class="citation-comment">' .. content .. '</span>';
end
 
Line 26 ⟶ 21:
end
 
-- Create an HTML tag
function createTag(t, frame)
local name = t.name or "!-- --"
Line 46 ⟶ 42:
--[[
This is a clone of mw.text.nowiki. When the mw.text library is installed,
this can be replaced by a call to that library.
]]
function nowiki( s )
-- string.gsub is safe here, because we're only caring about ASCII chars
Line 75 ⟶ 72:
end
 
-- Formats a wiki style external link
function externallinkid(args)
local sep = args.separator or "&nbsp;"
Line 83 ⟶ 81:
end
localreturn t0"[[" =.. args.link .. "|" .. onlyinprint(args.label .. "]]" .. sep .. "[" .. args.prefix .. url_string .. args.suffix .. " " .. nowiki(args.id) .. "]"
local t1 = hideinprint("[[" .. args.link .. "|" .. args.label .. "]]" .. sep .. "[" .. args.prefix .. url_string .. args.suffix .. " " .. nowiki(args.id) .. "]")
return t0 .. t1
end
 
-- Formats a wiki style internal link
function internallinkid(args)
local sep = args.separator or "&nbsp;"
args.suffix = args.suffix or ""
localreturn t0"[[" =.. args.link .. "|" .. onlyinprint(args.label .. "]]" .. sep .. "[[" .. args.prefix .. args.id .. args.suffix .. "|" .. nowiki(args.id) .. "]]"
local t1 = hideinprint("[[" .. args.link .. "|" .. args.label .. "]]" .. sep .. "[[" .. args.prefix .. args.id .. args.suffix .. "|" .. nowiki(args.id) .. "]]")
return t0 .. t1
end
 
-- Formats a link to Amazon
function amazon(id, ___domain)
if ( nil == ___domain ) then
Line 106 ⟶ 101:
end
 
-- Formats a DOI and checks for DOI errors.
function doi(id, inactive)
local cat = ""
local error_categories = {};
local text;
if ( inactive ~= nil ) then
text = "[[Digital object identifier|doi]]:" .. id;
table.insert( z.error_categories, "Pages with DOIs inactive since " .. selectyear(inactive) );
inactive = " (inactive " .. inactive .. ")"
else
Line 120 ⟶ 115:
end
if ( string.sub(id,1,3) ~= "10." ) then
table.insert( z.error_categories, "Pages with DOI errors" );
cat = ' <span class="error">Bad DOI (expected "10." prefix) in code number</span>'
end
return text .. inactive .. cat, error_categories
end
 
-- Escape sequences for content that will be used for URL descriptions
function url(id)
local t0 = onlyinprint(id)
local t1 = hideinprint("[" .. id .. " " .. nowiki(id) .. "]")
return t0 .. t1
end
 
function safeforurl( str )
return str:gsub( '[%[%]\n]', {
Line 139 ⟶ 129:
end
 
-- Converts a hyphen to a dash
function hyphentodash( str )
if str == nil then
Line 146 ⟶ 137:
end
 
-- Protects a string that will be wrapped in wiki italic markup '' ... ''
function safeforitalics( str )
--[[ Note: We can not use <i> for italics, as the expected behavior for
Line 161 ⟶ 153:
end
 
--[[
Joins a sequence of string together while checking for duplicate separation
characters.
]]
function safejoin( tbl, duplicate_char )
--[[
Line 213 ⟶ 209:
end
 
--[[
Return the year portion of a date string, if possible.
Returns empty string if the argument can not be interpreted
as a year.
]]
function selectyear( str )
local lang = mw.getContentLanguage();
Line 234 ⟶ 235:
end
 
-- Formats an OpenLibrary link, and checks for associated errors.
function openlibrary(id)
local error_categories = {};
local prefix = ""
local code = id:sub(-1,-1)
if ( code == "A" ) then
prefixreturn externallinkid({link="Open Library",label="OL",prefix="http://openlibrary.org/authors/OL",id=id})
elseif ( code == "M" ) then
prefixreturn externallinkid({link="Open Library",label="OL",prefix="http://openlibrary.org/books/OL",id=id})
elseif ( code == "W" ) then
prefixreturn externallinkid({link="Open Library",label="OL",prefix= "http://openlibrary.org/works/OL",id=id})
else
table.insert( z.error_categories, "Pages with OL errors" );
prefix = "http://openlibrary.org/OL"
return externallinkid({link="Open Library",label="OL",prefix= "http://openlibrary.org/OL",id=id}) ..
table.insert( error_categories, "Pages with OL errors" );
' <span class="error">Bad OL specified</span>';
end
local text = externallinkid({link="Open Library",label="OL",prefix=prefix,id=id})
if #error_categories ~= 0 then
text = text .. ' <span class="error">Bad OL specified</span>';
end
return text, error_categories
end
 
-- Attempts to convert names to initials.
function reducetoinitials(first)
local initials = {}
Line 263 ⟶ 260:
end
 
-- Formats a list of people (e.g. authors / editors)
function listpeople(control, people)
local sep = control.sep;
Line 309 ⟶ 307:
end
 
-- Generates a CITEREF anchor ID.
function anchorid(args)
local P1 = args[1] or ""
Line 318 ⟶ 317:
end
 
-- Gets author list from the input arguments
function extractauthors(args)
local authors = {};
Line 340:
end
 
-- Gets editor list from the input arguments
function extracteditors(args)
local editors = {};
Line 346 ⟶ 347:
while true do
local last = args["editor" .. i .. "-last"] or args["editor-last" .. i] or args["EditorSurname" .. i] or args["Editor" .. i] or args["editor" .. i]
if ( last and "" < last ) then -- just in case someone passed in an empty parameter
editors[i] = {
Line 362 ⟶ 363:
end
 
--[[
This is the main function foing the majority of the citation
formatting.
]]
function citation0( config, args)
-- Load Input Parameters
local error_categories = {};
 
--------------------------------------------------- Get parameters
local PPrefix = config.PPrefix or "p.&nbsp;"
local PPPrefix = config.PPPrefix or "pp.&nbsp;"
Line 457 ⟶ 461:
end
local PP = args.pp
local Edition = args.edition
local PublicationPlace = args["publication-place"] or args.publicationplace or args.place or args.___location
Line 467 ⟶ 470:
local ArchiveDate = args["archive-date"] or args.archivedate
local Agency = args.agency
local DeadURL = args.deadurl or "yes" -- Only used is ArchiveURL is present.
local Language = args.language or args["in"]
local Format = args.format
Line 498 ⟶ 501:
local Transcript = args.transcript
local TranscriptURL = args["transcript-url"] or args.transcripturl
local sepc = args.separator or "."
local no_tracking_cats = args["template doc demo"] or args.nocat or args.notracking or args["no-tracking"] or "";
 
local no_tracking_cats = args["template doc demo"] or args["nocat"] or args["notracking"] or args["no-tracking"];
 
if ( config.CitationClass == "journal" ) then
Line 513 ⟶ 515:
if ( Ref == nil ) then Ref = "harv" end
end
if ( sepc == nil ) then sepc = "." end
if ( DeadURL == nil ) then DeadURL = "yes" end
 
-- At this point fields may be nil if they weren't specified in the template use. We can use that fact.
Line 619:
OCinStitle = OCinStitle .. "&" .. name .. "=" .. mw.uri.encode(value)
end
local this_page = mw.title.getCurrentTitle();
OCinStitle = OCinStitle .. "&rfr_id=info:sid/en.wikipedia.org:"
.. configthis_page.fullpagenameprefixedText -- end COinS data by page's non-encoded pagename
 
-- Now perform various field substitutions.
Line 684 ⟶ 686:
-- Captures the value for Date prior to adding parens or other textual transformations
local DateIn = Date
-- Test is cite web is called without giving a URL
if ( config.CitationClass == "web" ) then
if ( URL == nil or URL == '' ) and
Line 690 ⟶ 694:
( ConferenceURL == nil or ConferenceURL == '' ) and
( TranscriptURL == nil or TranscriptURL == '' ) then
table.insert( z.error_categories, 'Pages using web citations with no URL' );
if Title == nil or Title == "" then
Authors = Authors .. hiddencomment("No URL on cite web here");
Line 699 ⟶ 703:
end
 
-- Test if citation has no title
if ( Chapter == nil or Chapter == "" ) and
( Title == nil or Title == "" ) and
( Periodical == nil or Periodical == "" ) and
( Conference == nil or Conference == "" ) then
table.insert( z.error_categories, 'Pages with citations lacking titles' );
Authors = Authors .. hiddencomment("No citation title here");
end
Line 867 ⟶ 872:
BIBCODE = sepc .. " " .. externallinkid({label="Bibcode",link="Bibcode",prefix="http://adsabs.harvard.edu/abs/",id=BIBCODE,separator=":"}) else BIBCODE = "" end
if ( DOI ~= nil and DOI ~= "" ) then
DOI = sepc .. " " .. doi(DOI, DoiBroken) else DOI = "" end
local DOI_string, DOI_error;
DOI_string, DOI_error = doi(DOI, DoiBroken);
DOI = sepc .. " " .. DOI_string
for _, v in ipairs( DOI_error ) do
table.insert( error_categories, v );
end
else
DOI = ""
end
if ( ID ~= nil and ID ~="") then ID = sepc .." ".. ID else ID="" end
if ( ISBN ~= nil and ISBN ~= "") then
Line 892 ⟶ 889:
OCLC = sepc .." " .. externallinkid({label="OCLC",link="OCLC",prefix="//www.worldcat.org/oclc/",id=OCLC}) else OCLC = "" end
if ( OL ~= nil and OL ~="") then
OL = sepc .. " " .. openlibrary(OL) else OL = "" end
local OL_string, OL_error;
OL_string, OL_error = openlibrary(OL);
OL = sepc .. " " .. OL_string
for _, v in ipairs( OL_error ) do
table.insert( error_categories, v );
end
else
OL = "";
end
if ( OSTI ~= nil and OSTI ~="") then
OSTI = sepc .." " .. externallinkid({label="OSTI",link="Office of Scientific and Technical Information",prefix="http://www.osti.gov/energycitations/product.biblio.jsp?osti_id=",id=OSTI}) else OSTI = "" end
Line 912 ⟶ 901:
SSRN = sepc .." " .. externallinkid({label="SSRN",link="Social Science Research Network",prefix="http://ssrn.com/abstract=",id=SSRN}) else SSRN = "" end
if ( URL ~= nil and URL ~="") then
URL = " " .. url"[" .. URL .. " " .. nowiki(URL) .. "]";
table.insert( z.error_categories, "Pages with citations having bare URLs" );
if config.CitationClass == "web" then
URL = URL .. " <span class='error'>No <code>title=</code> specified</span>"
Line 941 ⟶ 930:
else
ArchiveDate = " <span class='error'>If you specify <code>archiveurl=</code>, you must also specify <code>archivedate=</code></span> "
table.insert( z.error_categories, 'Pages with archiveurl citation errors' );
end
local arch_text = " archived"
Line 953 ⟶ 942:
Archived = sepc .. arch_text .. " from <span class='error'>If you specify <code>archiveurl=</code>" ..
", you must also specify <code>url=</code></span> on" .. ArchiveDate
table.insert( z.error_categories, 'Pages with archiveurl citation errors' );
end
end
Line 1,014 ⟶ 1,003:
 
local tcommon
if ( config.CitationClass == "journal" ) thenand
Periodical ~= "" ) then
if (Others ~= "") then Others = Others .. sepc .. " " end
tcommon = safejoin( {Others, Title, TitleNote, Format, TitleType, Conference, Periodical, Series, Language, Edition, Publisher, Agency, Volume, Issue, Position}, sepc );
elseif ( config.CitationClass == "citation" ) or (config.CitationClass == "encyclopaedia" ) then
Line 1,021 ⟶ 1,012:
tcommon = safejoin( {Title, TitleNote, Series, Format, TitleType, Conference, Periodical, Language, Volume, Issue, Others, Edition, Publisher, Agency, Position}, sepc );
end
-- DEBUG: tcommon = "/Title="..Title .. "/TitleType="..TitleType .. "/TitleNote="..TitleNote .. "/Format="..Format .. "/Edition="..Edition .. "/Language="..Language .. "/Conference="..Conference .. "/Periodical="..Periodical .. "/Series="..Series .. "/Volume="..Volume .. "/Issue="..Issue .. "/Position="..Position
 
local idcommon = safejoin( { ARXIV, ASIN, BIBCODE, DOI, ISBN, ISSN, JFM, JSTOR, LCCN, MR, OCLC, OL, OSTI, PMC, PMID, RFC, SSRN, URL, ZBL, ID, Archived, AccessDate, Via, SubscriptionRequired, Lay, Quote, PostScript }, sepc );
Line 1,037 ⟶ 1,027:
local pgtext = Page .. Pages .. At
if page_error then
table.insert( z.error_categories, 'Pages with citations using conflicting page specifications' );
pgtext = pgtext .. hiddencomment('Bad page specification here');
end
Line 1,136 ⟶ 1,126:
if string.len(text:gsub("<span[^>/]*>.-</span>", ""):gsub("%b<>","")) <= 2 then
table.insert( z.error_categories, 'Pages with empty citations' );
text = '<span class="error">Citation is empty</span>';
end
Line 1,148 ⟶ 1,138:
text = text .. OCinS;
if no_tracking_cats ~= nil and no_tracking_cats ~= '' then
for _, v in ipairs( z.error_categories =) {};do
text = text .. '[[Category:' .. v ..']]';
end
end
for _, v in pairs( error_categories ) do
text = text .. '[[Category:' .. v ..']]';
end
Line 1,276 ⟶ 1,265:
--20Mar2013 If CitationClass is journal, show "others=" before title.
--20Mar2013 If CitationClass is book, show "others=" before edition.
--20Mar2013 If CitationClass is journal, adjust "others=" to have sepc.
--20Mar2013 For class "journal", use book format unless Periodical set.
--
--End