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