Module:Citation: Difference between revisions
Jump to navigation
Jump to search
(Created page with "------------------------") |
mNo edit summary |
||
Line 1: | Line 1: | ||
------------------------ | --------------------------------------------------------------------- | ||
-- Module:Citation - Lua module for Citation auxiliary templates | |||
--------------------------------------------------------------------- | |||
-- For the {{citation}} formatting functions, see: Module:Citation/CS1 | |||
-- (see NOTES at bottom) | |||
--require "mw.text" | |||
local z = { | |||
wikitext = require("Module:Wikitext"), | |||
extensiontags = { | |||
nowiki = true, | |||
ref = true, | |||
gallery = true, | |||
pre = true, | |||
source = true, | |||
categorytree = true, | |||
charinsert = true, | |||
hiero = true, | |||
imagemap = true, | |||
inputbox = true, | |||
math = true, | |||
poem = true, | |||
ref = true, | |||
references = true, | |||
syntaxhighlight = true, | |||
timeline = true, | |||
} | |||
} | |||
function trim( str ) | |||
if str == nil then | |||
return nil; | |||
end | |||
return str:match( "^%s*(.-)%s*$" ); | |||
end | |||
function hideinprint(content) | |||
return content | |||
end | |||
function onlyinprint(content) | |||
return "" | |||
end | |||
-- This returns a string with HTML character entities for wikitext markup characters. | |||
function wikiescape(text) | |||
text = text:gsub( '[&\'%[%]{|}]', { | |||
['&'] = '&', | |||
["'"] = ''', | |||
['['] = '[', | |||
[']'] = ']', | |||
['{'] = '{', | |||
['|'] = '|', | |||
['}'] = '}' } ); | |||
return text; | |||
end | |||
function createTag(t, frame) | |||
local name = t.name or "!-- --" | |||
local content = t.contents or "" | |||
local attrs = {} | |||
if ( z.extensiontags[name] ) then | |||
-- We have to preprocess these, so that they are properly turned into so-called "strip markers" in the generated wikitext. | |||
if ( not frame ) then error ("Please supply an extra frame argument to the createTag() function.") end | |||
local params = {} | |||
for n,v in pairs(t.params) do | |||
table.insert(params, "|" .. n .. "=" .. v) | |||
end | |||
return frame:preprocess("{{#tag:" .. name .. "|" .. content .. table.concat(params) .. "}}") | |||
else | |||
for n,v in pairs(t.params) do | |||
if (v) then | |||
table.insert(attrs, n .. "=\"" .. wikiescape(v) .. "\"") | |||
else | |||
table.insert(attrs, n) | |||
end | |||
end | |||
if ("" == content) then | |||
return "<" .. name .. " " .. table.concat(attrs, " ") .. "/>" | |||
else | |||
return "<" .. name .. " " .. table.concat(attrs, " ") .. ">" .. content .. "</" .. name .. ">" | |||
end | |||
end | |||
end | |||
--[[ | |||
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 | |||
s = string.gsub( s, '["&\'<=>%[%]{|}]', { | |||
['"'] = '"', | |||
['&'] = '&', | |||
["'"] = ''', | |||
['<'] = '<', | |||
['='] = '=', | |||
['>'] = '>', | |||
['['] = '[', | |||
[']'] = ']', | |||
['{'] = '{', | |||
['|'] = '|', | |||
['}'] = '}', | |||
} ) | |||
s = string.sub( string.gsub( '\n' .. s, '\n[#*:;]', { | |||
["\n#"] = "\n#", | |||
["\n*"] = "\n*", | |||
["\n:"] = "\n:", | |||
["\n;"] = "\n;", | |||
} ), 2 ) | |||
s = string.gsub( s, '://', '://' ) | |||
s = string.gsub( s, 'ISBN ', 'ISBN ' ) | |||
s = string.gsub( s, 'RFC ', 'RFC ' ) | |||
return s | |||
end | |||
function externallinkid(args) | |||
local sep = args.separator or " " | |||
args.suffix = args.suffix or "" | |||
local url_string = args.id | |||
if args.encode == true or args.encode == nil then | |||
url_string = mw.uri.encode( url_string ); | |||
end | |||
local t0 = onlyinprint(args.label .. sep .. args.id) | |||
local t1 = hideinprint("[[" .. args.link .. "|" .. args.label .. "]]" .. sep .. "[" .. args.prefix .. url_string .. args.suffix .. " " .. nowiki(args.id) .. "]") | |||
return t0 .. t1 | |||
end | |||
function doi(id, inactive, nocat) | |||
local cat = "" | |||
local text; | |||
if ( inactive ~= nil ) then | |||
text = "[[Digital object identifier|doi]]:" .. id; | |||
cat = cat .. "[[Category:Pages with DOIs inactive since " .. selectyear(inactive) .. "]]" | |||
inactive = " (inactive " .. inactive .. ")" | |||
else | |||
text = externallinkid({link="Digital object identifier",label="doi",prefix="http://dx.doi.org/",id=id,separator=":"}) | |||
inactive = "" | |||
end | |||
if ( string.sub(id,1,3) ~= "10." ) then | |||
cat = cat .. "[[Category:Pages with DOI errors]]" .. '<span class="error"> Bad DOI (expected "10." prefix) in code number</span>' | |||
end | |||
if ( nocat and nocat ~= "" ) then cat = "" end | |||
return text .. inactive .. cat | |||
end | |||
function selectyear( str ) | |||
local lang = mw.getContentLanguage(); | |||
local good, result; | |||
good, result = pcall( lang.formatDate, lang, 'Y', str ) | |||
if good then | |||
return result; | |||
else | |||
return ''; | |||
end | |||
end | |||
function anchorid(label, args) | |||
local P1 = trim(args[1]) or "" | |||
local P2 = trim(args[2]) or "" | |||
local P3 = trim(args[3]) or "" | |||
local P4 = trim(args[4]) or "" | |||
local P5 = trim(args[5]) or "" | |||
local anchor = P1 .. P2 .. P3 .. P4 .. P5; | |||
if anchor ~= '' then -- See bug description in Citation/CS1 | |||
anchor = mw.uri.anchorEncode( anchor ); | |||
end | |||
return label .. anchor | |||
end | |||
function refid(label, args) | |||
local p = args.p or "" | |||
local pp = args.pp or "" | |||
local loc = args.loc or "" | |||
return anchorid(label, args) .. p .. pp .. loc | |||
end | |||
function name(args) | |||
local P1 = trim(args[1]) or "" | |||
if ( args[5] ~= nil) then | |||
return P1 .. " et al." | |||
else | |||
local P2 = trim(args[2]) or "" | |||
local P3 = trim(args[3]) or "" | |||
local P4 = trim(args[4]) or "" | |||
if ( args[4] ~= nil ) then | |||
P4 = " " .. P4 | |||
P3 = " & " .. P3 | |||
P2 = ", " .. P2 | |||
elseif ( args[3] ~= nil ) then | |||
P3 = " " .. P3 | |||
P2 = " & " .. P2 | |||
elseif ( args[2] ~= nil ) then | |||
P2 = " " .. P2 | |||
end | |||
return P1 .. P2 .. P3 .. P4 | |||
end | |||
end | |||
function crossref(frame, label, args) | |||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | |||
local LB = config.BracketLeft or "" | |||
local RB = config.BracketRight or "" | |||
local anchor = args.ref or args.Ref or anchorid( label, args) | |||
local text = name(args) | |||
local loc = args.loc | |||
local page | |||
local pages = args.pp or args.pages | |||
if pages == nil or pages == '' then | |||
page = args.p or args.page; | |||
end | |||
if nil == loc then loc = "" else loc = " " .. loc end | |||
if ( page ~= nil ) then | |||
local pagesep = config.PageSep or ", p. " | |||
loc = loc .. pagesep .. page | |||
end | |||
if ( pages ~= nil ) then | |||
local pagessep = config.PagesSep or ", pp. " | |||
loc = loc .. pagessep .. pages | |||
end | |||
local pagename = args.pagename or "" | |||
local ps = args.Postscript or "" | |||
return LB .. "[[" .. pagename .. "#" .. anchor .. "|" .. text .. "]]" .. loc .. RB .. ps | |||
end | |||
function r0(frame, name, group, page) | |||
if ( name == nil ) then return "" end | |||
if ( group == nil ) then group = "" end | |||
local p = "" | |||
if ( page ~= nil ) then | |||
local contents = ":" .. page | |||
p = createTag({name="sup",contents=contents,params={class="reference",style="white-space:nowrap;"}}) | |||
end | |||
return createTag({name="ref",contents="",params={name=name,group=group}}, frame) .. p | |||
end | |||
function reflist0(frame, config, args) | |||
local contents = args.refs or "" | |||
local liststyle = args.liststyle | |||
local count = args[1] | |||
local width = args.colwidth | |||
local group = args.group or config.default_group | |||
if ( nil == tonumber(count) and nil == width ) then | |||
width = count | |||
count = nil | |||
end | |||
if ( nil == liststyle ) then | |||
if ( "upper-alpha" == group or "lower-alpha" == group or "upper-roman" == group or "lower-roman" == group or "upper-greek" == group or "lower-greek" == group ) then | |||
liststyle = group | |||
else | |||
liststyle = config.default_liststyle | |||
end | |||
end | |||
local params = {} | |||
params.class = "reflist" | |||
params.style = z.wikitext.liststyle(liststyle) | |||
if ( nil ~= count ) then | |||
params.class = params.class .. " references-column-count references-column-count-" .. count | |||
params.style = params.style .. " " .. z.wikitext.columncountstyle(count) | |||
end | |||
if ( nil ~= width ) then | |||
params.class = params.class .. " references-column-width" | |||
params.style = params.style .. " " .. z.wikitext.columnwidthstyle(width) | |||
end | |||
local references = createTag({name="references",contents=contents,params={group=group}}, frame) | |||
return createTag({name="div",contents=references,params=params}) | |||
end | |||
function refbegin0(frame, config, args) | |||
local liststyle = args.liststyle | |||
local indent = args.indent | |||
local indentsize = args.indentsize | |||
local count = args[1] | |||
local width = args.colwidth | |||
if ( nil == tonumber(count) and nil == width ) then | |||
width = count | |||
count = nil | |||
end | |||
if ( nil == liststyle ) then | |||
if ( "upper-alpha" == group or "lower-alpha" == group or "upper-roman" == group or "lower-roman" == group or "upper-greek" == group or "lower-greek" == group ) then | |||
liststyle = group | |||
else | |||
liststyle = config.default_liststyle | |||
end | |||
end | |||
local params = {} | |||
params.class = "refbegin" | |||
params.style = z.wikitext.liststyle(liststyle) | |||
if ( nil ~= count ) then | |||
params.class = params.class .. " references-column-count references-column-count-" .. count | |||
params.style = params.style .. " " .. z.wikitext.columncountstyle(count) | |||
end | |||
if ( nil ~= width ) then | |||
params.class = params.class .. " references-column-width" | |||
params.style = params.style .. " " .. z.wikitext.columnwidthstyle(width) | |||
end | |||
local dlopen | |||
if ( nil ~= indent ) then | |||
dlopen = z.wikitext.OpenHTMLTag({name="dl",params={style="text-indent: -" .. (indentsize or "3.2") .. "em;"}}) | |||
else | |||
dlopen = "" | |||
end | |||
return z.wikitext.OpenHTMLTag({name="div",params=params}) .. dlopen | |||
end | |||
function refend0(frame, config, args) | |||
local indent = args.indent | |||
local dlclose | |||
if ( nil ~= indent ) then | |||
dlclose = "</dl>" | |||
else | |||
dlclose = "" | |||
end | |||
return dlclose .. "</div>" | |||
end | |||
-- This is used by {{doi}} to create DOI links in the style used in citations. | |||
function z.doi(frame) | |||
local pframe = frame:getParent() | |||
local id = pframe.args.id or pframe.args[1] or "" | |||
return doi(id) | |||
end | |||
-- This is used by {{ISSN}} to create ISSN links in the style used in citations. | |||
function z.ISSN(frame) | |||
local pframe = frame:getParent() | |||
local Name = pframe.args[1] or "" | |||
return hideinprint("[[International Standard Serial Number|ISSN]] [http://www.worldcat.org/search?fq=x0:jrnl&q=n2:" .. Name .. " " .. Name .. "]") | |||
end | |||
-- This is used by templates such as {{SfnRef}} to create the (encoded) anchor name for a Harvard cross-reference hyperlink. | |||
function z.SFNID(frame) | |||
local pframe = frame:getParent() | |||
return anchorid('FOOTNOTE', pframe.args) | |||
end | |||
-- This is used by templates such as {{Harvard citation}} to create the Harvard cross-reference text. | |||
function z.Harvard(frame) | |||
local pframe = frame:getParent() | |||
return crossref(frame, pframe.args) | |||
end | |||
-- This is used by templates such as {{sfn}} to create the entire cross-reference. | |||
function z.sfn(frame) | |||
local pframe = frame:getParent() | |||
pframe.args.Postscript = pframe.args.postscript or pframe.args.ps or "."; | |||
local content = crossref(frame, 'CITEREF', pframe.args) | |||
local args = { name = refid( 'FOOTNOTE', pframe.args) } | |||
return createTag({name = "ref", contents = content, params = args}, frame) | |||
end | |||
-- This is used by template {{r}}. | |||
function z.r(frame) | |||
local pframe = frame:getParent() | |||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | |||
local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template | |||
args.page1 = args.page1 or args.page | |||
local text = "" | |||
-- This would be shorter using ipairs(), but that doesn't work on an arguments table supplied to a template. | |||
local index = 1 | |||
while args[index] ~= nil do | |||
local arg = args[index] | |||
local t = r0(frame, arg, args.group, args["page" .. index]) | |||
text = text .. t | |||
index = index + 1 | |||
end | |||
return text | |||
end | |||
-- This is used by template {{ref label}}. | |||
function z.reflabel(frame) | |||
local pframe = frame:getParent() | |||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | |||
local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template | |||
local P1 = args[1] or "" | |||
local P2 = args[2] or "" | |||
local P3 = args[3] or "" | |||
local id = nil | |||
local contents = "[[#endnote_" .. P1 .. P3 .. "|[" .. P2 .. "]]]" | |||
local params = {} | |||
params.class="reference" | |||
if ( args.noid == nil or args.noid == "" ) then params.id = "ref_" .. P1 .. P3 end | |||
return createTag({name="sup",contents=contents,params=params}) | |||
end | |||
-- This is used by template {{note label}}. | |||
function z.notelabel(frame) | |||
local pframe = frame:getParent() | |||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | |||
local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template | |||
local id = args[1] or "" | |||
local arrow = args[3] or "" | |||
local postscript = args[4] or "" | |||
local contents | |||
if arrow ~= "" then | |||
local sup_arrow = createTag({name="sup",contents=arrow,params={}}) | |||
contents = "[[#ref_" .. id .. arrow .. "|<b>" .. sup_arrow .. "</b>]]" .. postscript | |||
if "none" == arrow then arrow = "^" end -- Change this AFTER using it in the ID parameter and the contents. | |||
else | |||
contents = (args[2] or "") .. postscript | |||
end | |||
local params = { class="citation wikicite" } | |||
if id ~= "" and ( args.noid == nil or args.noid == "" ) then | |||
params.id = mw.uri.anchorEncode("endnote_" .. id .. arrow) | |||
end | |||
return createTag({name="span",contents=contents,params=params}) | |||
end | |||
-- This is used by templates {{reflist}} and {{notelist}}. | |||
function z.reflist(frame) | |||
local pframe = frame:getParent() | |||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | |||
local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template | |||
return reflist0(frame, config, args) | |||
end | |||
-- This is used by template {{refbegin}}. | |||
function z.refbegin(frame) | |||
local pframe = frame:getParent() | |||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | |||
local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template | |||
return refbegin0(frame, config, args) | |||
end | |||
-- This is used by template {{refend}}. | |||
function z.refend(frame) | |||
local pframe = frame:getParent() | |||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | |||
local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template | |||
return refend0(frame, config, args) | |||
end | |||
-- This is used by template {{efn}}. | |||
function z.efn(frame) | |||
local pframe = frame:getParent() | |||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | |||
local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template | |||
return createTag({name="ref",contents=(args[1] or ""),params={name=args.name,group=config.default_group}}, frame) | |||
end | |||
return z | |||
--------------------------------------------------------------------- | |||
--NOTES | |||
-- | |||
-- NOTE A1: This Lua module was originally designed to handle a mix | |||
-- of citation styles, crossing Vancouver style with Wikipedia's | |||
-- local Citation Style 1 (CS1) from {Template:Citation/core}. | |||
-- However, the conflicting positions of parameters, scattered | |||
-- in twisted locations across this module, led to a separate | |||
-- variation just to untangle the CS1 format of citations. | |||
-- | |||
-- NOTE D2: The placement of dots and other separators between the | |||
-- displayed parameters has been a continual headache, to keep | |||
-- coordinated with the data in parentheses "(data)". There | |||
-- has been a need to pre-check for the existence of related | |||
-- options, to keep from putting double-dots ".." in some cases. | |||
-- In particular, the omission of the "title=" parameter has led | |||
-- to several cases of a spurious dot ". ." because the original | |||
-- design had treated the title as a mandatory parameter. | |||
-- | |||
------------------------------------------------------------------------ | |||
--HISTORY: | |||
--18Oct2012 Fixed lead-space in Chapter by omitting " ". | |||
--18Oct2012 Fixed lead-space in Chapter/Title as end " " of Authors/Date/... | |||
--19Oct2012 Put HISTORY comments to log major changes (not typos). | |||
--19Oct2012 Fixed extra dot ".." in Title by omitting at end of "tcommon=...". | |||
--19Oct2012 For pages, put   in "p. " etc. | |||
--19Oct2012 Enhanced "pages=" to detect lone page as "p." else "pp." prefix. | |||
--19Oct2012 Fixed to show "." after Periodical name (work, newspaper...). | |||
--19Oct2012 Fixed web-link to have spaces "[... Archived] from the original". | |||
--19Oct2012 Fixed to show ";" between authors & coauthors. | |||
--19Oct2012 Fixed to omit extra "." after coauthors. | |||
--20Oct2012 Fixed COinS data to not urlencode all, as "ctx_ver=Z39.88-2004" | |||
--20Oct2012 Fixed COinS to not end as "&" but use lead "&rft...=" form. | |||
--20Oct2012 Fixed COinS to not url.encode page's "rfr_id=..." pagename. | |||
--20Oct2012 Fixed COinS data when "web" to default to rft.genre "book". | |||
--05Nov2012 Add a span wrapper even when there is no Ref parameter | |||
--15Feb2013 Added Agency for "agency=xx". | |||
--19Feb2013 Put NOTES comments to explain module operation. | |||
--19Feb2013 Copied as Module:Citation/CS1 to alter to match wp:CS1 form. | |||
--19Feb2013 Changed OrigYear to use [__] for CS1 style. | |||
--19Feb2013 Fixed to not show duplicate Publisher/Agency. | |||
--19Feb2013 Moved page-number parameters to after final date. | |||
--19Feb2013 Fixed to not put double-dots after title again. | |||
--20Feb2013 Changed to omit dot "." if already ends with dot. | |||
--20Feb2013 If class "journal" shows Publisher after Periodical/Series. | |||
--20Feb2013 Shifted Format to after Language, and Others after Volume. | |||
--20Feb2013 Set AccessDate + <span class="reference-accessdate"> | |||
--20Feb2013 Fixed url when deadurl=no. | |||
--20Feb2013 Added sepc for separator character between parameters. | |||
--20Feb2013 Put "OCLC" for "Online Computer Library Center". | |||
--20Feb2013 Fix empty "authorlink=" as person.link ~= "". | |||
--20Feb2013 Added space after AuthorSep & AuthorNameSep. | |||
--21Feb2013 Added args.contributor (was missing parameter). | |||
--21Feb2013 Fixed EditorSep (was misspelled "EdithorSep"). | |||
--21Feb2013 Set OCinSdata.rft_val_fmt = "info:ofi/fmt:kev:mtx:book" | |||
--21Feb2013 Checked to omit blank codes (asin= | doi= etc.). | |||
--21Feb2013 Set enddot to end line if not config.CitationClass "citation". | |||
--21Feb2013 Fixed to show "issn=x" as the ISSN code. | |||
--21Feb2013 Fixed to show "id=x" after Zbl code. | |||
--21Feb2013 Changed to omit double-dot before date when already dot. | |||
--21Feb2013 Order config.CitationClass "citation": Volume, Issue, Publisher. | |||
--21Feb2013 Put warning "Bad DOI (expected "10."..)" in DOI result. | |||
--21Feb2013 Automatically unbolded volume+comma when > 4 long. | |||
--21Feb2013 Changed to allow lowercase "asin-tld". | |||
--22Feb2013 Fixed ref=harv to extract Year from Date. | |||
--22Feb2013 Set Harvard refer. span id if config.CitationClass "citation". | |||
--22Feb2013 Fixed config.CitationClass "citation" as span class="citation". | |||
--22Feb2013 Capitalized "Archived/Retrieved" only when sepc is dot ".". | |||
--23Feb2013 Fixed author editor for "in" or "In" and put space after sepc. | |||
--23Feb2013 Changed to omit dot in "et al." when sepc is "." separator. | |||
--23Feb2013 Fixed "author1-first" to also get args.given or args.given1. | |||
--23Feb2013 Fixed args.article to set Title, after Periodical is Title. | |||
--23Feb2013 Fixed to allow blank Title (such as "contribution=mytitle"). | |||
--23Feb2013 Fixed double-dot ".." at end of Editors list | |||
--26Feb2013 Moved "issue=" data to show before "page=". | |||
--26Feb2013 Moved "type=" data to show after "format=". | |||
--26Feb2013 For "pmc=" link, omitted suffix "/?tool=pmcentrez". | |||
--27Feb2013 For coauthors, omitted extra separator after authors. | |||
--27Feb2013 For date, allowed empty date to use month/day/year. | |||
--27Feb2013 Fixed double-dot ".." at end of authors/coauthors list. | |||
--27Feb2013 Reset editor suffix as ", ed." when date exists. | |||
--27Feb2013 Removed duplicate display of "others=" data. | |||
--27Feb2013 Removed parentheses "( )" around "department" TitleNote. | |||
--05Mar2013 Moved Language to follow Periodical or Series. | |||
--05Mar2013 Fixed Edition to follow Series or Volume. | |||
--05Mar2013 Fixed class encyclopaedia to show article as quoted Chapter. | |||
--05Mar2013 Fixed class encyclopaedia to show page as "pp." or "p.". | |||
--07Mar2013 Changed class encyclopaedia to omit "( )" around publisher. | |||
--07Mar2013 Fixed end double-dot by string.sub(idcommon,-1,-1) was "-1,1". | |||
--13Mar2013 Removed enddot "." after "quote=" parameter. | |||
--13Mar2013 Changed config.CitationClass "news" to use "p." page format. | |||
--13Mar2013 Fixed missing "location=" when "web" or "encyclopaedia". | |||
--14Mar2013 Fixed end double-dot after book/work title. | |||
--14Mar2013 Fixed double-dot before "p." or "pp." page number. | |||
--14Mar2013 Fixed config.CitationClass "book" to use p./pp. page. | |||
-- | |||
--End |
Latest revision as of 19:16, 8 March 2016
Documentation for this module may be created at Module:Citation/doc
1 ---------------------------------------------------------------------
2 -- Module:Citation - Lua module for Citation auxiliary templates
3 ---------------------------------------------------------------------
4 -- For the {{citation}} formatting functions, see: Module:Citation/CS1
5 -- (see NOTES at bottom)
6 --require "mw.text"
7
8 local z = {
9 wikitext = require("Module:Wikitext"),
10 extensiontags = {
11 nowiki = true,
12 ref = true,
13 gallery = true,
14 pre = true,
15 source = true,
16 categorytree = true,
17 charinsert = true,
18 hiero = true,
19 imagemap = true,
20 inputbox = true,
21 math = true,
22 poem = true,
23 ref = true,
24 references = true,
25 syntaxhighlight = true,
26 timeline = true,
27 }
28 }
29
30 function trim( str )
31 if str == nil then
32 return nil;
33 end
34 return str:match( "^%s*(.-)%s*$" );
35 end
36
37 function hideinprint(content)
38 return content
39 end
40
41 function onlyinprint(content)
42 return ""
43 end
44
45 -- This returns a string with HTML character entities for wikitext markup characters.
46 function wikiescape(text)
47 text = text:gsub( '[&\'%[%]{|}]', {
48 ['&'] = '&',
49 ["'"] = ''',
50 ['['] = '[',
51 [']'] = ']',
52 ['{'] = '{',
53 ['|'] = '|',
54 ['}'] = '}' } );
55 return text;
56 end
57
58 function createTag(t, frame)
59 local name = t.name or "!-- --"
60 local content = t.contents or ""
61 local attrs = {}
62 if ( z.extensiontags[name] ) then
63 -- We have to preprocess these, so that they are properly turned into so-called "strip markers" in the generated wikitext.
64 if ( not frame ) then error ("Please supply an extra frame argument to the createTag() function.") end
65 local params = {}
66 for n,v in pairs(t.params) do
67 table.insert(params, "|" .. n .. "=" .. v)
68 end
69 return frame:preprocess("{{#tag:" .. name .. "|" .. content .. table.concat(params) .. "}}")
70 else
71 for n,v in pairs(t.params) do
72 if (v) then
73 table.insert(attrs, n .. "=\"" .. wikiescape(v) .. "\"")
74 else
75 table.insert(attrs, n)
76 end
77 end
78 if ("" == content) then
79 return "<" .. name .. " " .. table.concat(attrs, " ") .. "/>"
80 else
81 return "<" .. name .. " " .. table.concat(attrs, " ") .. ">" .. content .. "</" .. name .. ">"
82 end
83 end
84 end
85
86 --[[
87 This is a clone of mw.text.nowiki. When the mw.text library is installed,
88 this can be replaced by a call to that library. ]]
89 function nowiki( s )
90 -- string.gsub is safe here, because we're only caring about ASCII chars
91 s = string.gsub( s, '["&\'<=>%[%]{|}]', {
92 ['"'] = '"',
93 ['&'] = '&',
94 ["'"] = ''',
95 ['<'] = '<',
96 ['='] = '=',
97 ['>'] = '>',
98 ['['] = '[',
99 [']'] = ']',
100 ['{'] = '{',
101 ['|'] = '|',
102 ['}'] = '}',
103 } )
104 s = string.sub( string.gsub( '\n' .. s, '\n[#*:;]', {
105 ["\n#"] = "\n#",
106 ["\n*"] = "\n*",
107 ["\n:"] = "\n:",
108 ["\n;"] = "\n;",
109 } ), 2 )
110 s = string.gsub( s, '://', '://' )
111 s = string.gsub( s, 'ISBN ', 'ISBN ' )
112 s = string.gsub( s, 'RFC ', 'RFC ' )
113
114 return s
115 end
116
117 function externallinkid(args)
118 local sep = args.separator or " "
119 args.suffix = args.suffix or ""
120 local url_string = args.id
121 if args.encode == true or args.encode == nil then
122 url_string = mw.uri.encode( url_string );
123 end
124
125 local t0 = onlyinprint(args.label .. sep .. args.id)
126 local t1 = hideinprint("[[" .. args.link .. "|" .. args.label .. "]]" .. sep .. "[" .. args.prefix .. url_string .. args.suffix .. " " .. nowiki(args.id) .. "]")
127
128 return t0 .. t1
129 end
130
131 function doi(id, inactive, nocat)
132 local cat = ""
133 local text;
134 if ( inactive ~= nil ) then
135 text = "[[Digital object identifier|doi]]:" .. id;
136 cat = cat .. "[[Category:Pages with DOIs inactive since " .. selectyear(inactive) .. "]]"
137 inactive = " (inactive " .. inactive .. ")"
138 else
139 text = externallinkid({link="Digital object identifier",label="doi",prefix="http://dx.doi.org/",id=id,separator=":"})
140 inactive = ""
141 end
142 if ( string.sub(id,1,3) ~= "10." ) then
143 cat = cat .. "[[Category:Pages with DOI errors]]" .. '<span class="error"> Bad DOI (expected "10." prefix) in code number</span>'
144 end
145 if ( nocat and nocat ~= "" ) then cat = "" end
146 return text .. inactive .. cat
147 end
148
149 function selectyear( str )
150 local lang = mw.getContentLanguage();
151 local good, result;
152 good, result = pcall( lang.formatDate, lang, 'Y', str )
153 if good then
154 return result;
155 else
156 return '';
157 end
158 end
159
160 function anchorid(label, args)
161 local P1 = trim(args[1]) or ""
162 local P2 = trim(args[2]) or ""
163 local P3 = trim(args[3]) or ""
164 local P4 = trim(args[4]) or ""
165 local P5 = trim(args[5]) or ""
166 local anchor = P1 .. P2 .. P3 .. P4 .. P5;
167 if anchor ~= '' then -- See bug description in Citation/CS1
168 anchor = mw.uri.anchorEncode( anchor );
169 end
170
171 return label .. anchor
172 end
173
174 function refid(label, args)
175 local p = args.p or ""
176 local pp = args.pp or ""
177 local loc = args.loc or ""
178 return anchorid(label, args) .. p .. pp .. loc
179 end
180
181 function name(args)
182 local P1 = trim(args[1]) or ""
183 if ( args[5] ~= nil) then
184 return P1 .. " et al."
185 else
186 local P2 = trim(args[2]) or ""
187 local P3 = trim(args[3]) or ""
188 local P4 = trim(args[4]) or ""
189 if ( args[4] ~= nil ) then
190 P4 = " " .. P4
191 P3 = " & " .. P3
192 P2 = ", " .. P2
193 elseif ( args[3] ~= nil ) then
194 P3 = " " .. P3
195 P2 = " & " .. P2
196 elseif ( args[2] ~= nil ) then
197 P2 = " " .. P2
198 end
199 return P1 .. P2 .. P3 .. P4
200 end
201 end
202
203 function crossref(frame, label, args)
204 local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
205 local LB = config.BracketLeft or ""
206 local RB = config.BracketRight or ""
207 local anchor = args.ref or args.Ref or anchorid( label, args)
208 local text = name(args)
209 local loc = args.loc
210 local page
211 local pages = args.pp or args.pages
212 if pages == nil or pages == '' then
213 page = args.p or args.page;
214 end
215 if nil == loc then loc = "" else loc = " " .. loc end
216 if ( page ~= nil ) then
217 local pagesep = config.PageSep or ", p. "
218 loc = loc .. pagesep .. page
219 end
220 if ( pages ~= nil ) then
221 local pagessep = config.PagesSep or ", pp. "
222 loc = loc .. pagessep .. pages
223 end
224 local pagename = args.pagename or ""
225 local ps = args.Postscript or ""
226 return LB .. "[[" .. pagename .. "#" .. anchor .. "|" .. text .. "]]" .. loc .. RB .. ps
227 end
228
229 function r0(frame, name, group, page)
230 if ( name == nil ) then return "" end
231 if ( group == nil ) then group = "" end
232 local p = ""
233 if ( page ~= nil ) then
234 local contents = ":" .. page
235 p = createTag({name="sup",contents=contents,params={class="reference",style="white-space:nowrap;"}})
236 end
237 return createTag({name="ref",contents="",params={name=name,group=group}}, frame) .. p
238 end
239
240 function reflist0(frame, config, args)
241 local contents = args.refs or ""
242 local liststyle = args.liststyle
243 local count = args[1]
244 local width = args.colwidth
245 local group = args.group or config.default_group
246 if ( nil == tonumber(count) and nil == width ) then
247 width = count
248 count = nil
249 end
250 if ( nil == liststyle ) then
251 if ( "upper-alpha" == group or "lower-alpha" == group or "upper-roman" == group or "lower-roman" == group or "upper-greek" == group or "lower-greek" == group ) then
252 liststyle = group
253 else
254 liststyle = config.default_liststyle
255 end
256 end
257 local params = {}
258 params.class = "reflist"
259 params.style = z.wikitext.liststyle(liststyle)
260 if ( nil ~= count ) then
261 params.class = params.class .. " references-column-count references-column-count-" .. count
262 params.style = params.style .. " " .. z.wikitext.columncountstyle(count)
263 end
264 if ( nil ~= width ) then
265 params.class = params.class .. " references-column-width"
266 params.style = params.style .. " " .. z.wikitext.columnwidthstyle(width)
267 end
268 local references = createTag({name="references",contents=contents,params={group=group}}, frame)
269 return createTag({name="div",contents=references,params=params})
270 end
271
272 function refbegin0(frame, config, args)
273 local liststyle = args.liststyle
274 local indent = args.indent
275 local indentsize = args.indentsize
276 local count = args[1]
277 local width = args.colwidth
278 if ( nil == tonumber(count) and nil == width ) then
279 width = count
280 count = nil
281 end
282 if ( nil == liststyle ) then
283 if ( "upper-alpha" == group or "lower-alpha" == group or "upper-roman" == group or "lower-roman" == group or "upper-greek" == group or "lower-greek" == group ) then
284 liststyle = group
285 else
286 liststyle = config.default_liststyle
287 end
288 end
289 local params = {}
290 params.class = "refbegin"
291 params.style = z.wikitext.liststyle(liststyle)
292 if ( nil ~= count ) then
293 params.class = params.class .. " references-column-count references-column-count-" .. count
294 params.style = params.style .. " " .. z.wikitext.columncountstyle(count)
295 end
296 if ( nil ~= width ) then
297 params.class = params.class .. " references-column-width"
298 params.style = params.style .. " " .. z.wikitext.columnwidthstyle(width)
299 end
300 local dlopen
301 if ( nil ~= indent ) then
302 dlopen = z.wikitext.OpenHTMLTag({name="dl",params={style="text-indent: -" .. (indentsize or "3.2") .. "em;"}})
303 else
304 dlopen = ""
305 end
306 return z.wikitext.OpenHTMLTag({name="div",params=params}) .. dlopen
307 end
308
309 function refend0(frame, config, args)
310 local indent = args.indent
311 local dlclose
312 if ( nil ~= indent ) then
313 dlclose = "</dl>"
314 else
315 dlclose = ""
316 end
317 return dlclose .. "</div>"
318 end
319
320 -- This is used by {{doi}} to create DOI links in the style used in citations.
321 function z.doi(frame)
322 local pframe = frame:getParent()
323 local id = pframe.args.id or pframe.args[1] or ""
324 return doi(id)
325 end
326
327 -- This is used by {{ISSN}} to create ISSN links in the style used in citations.
328 function z.ISSN(frame)
329 local pframe = frame:getParent()
330 local Name = pframe.args[1] or ""
331 return hideinprint("[[International Standard Serial Number|ISSN]] [http://www.worldcat.org/search?fq=x0:jrnl&q=n2:" .. Name .. " " .. Name .. "]")
332 end
333
334 -- This is used by templates such as {{SfnRef}} to create the (encoded) anchor name for a Harvard cross-reference hyperlink.
335 function z.SFNID(frame)
336 local pframe = frame:getParent()
337 return anchorid('FOOTNOTE', pframe.args)
338 end
339
340 -- This is used by templates such as {{Harvard citation}} to create the Harvard cross-reference text.
341 function z.Harvard(frame)
342 local pframe = frame:getParent()
343 return crossref(frame, pframe.args)
344 end
345
346 -- This is used by templates such as {{sfn}} to create the entire cross-reference.
347 function z.sfn(frame)
348 local pframe = frame:getParent()
349 pframe.args.Postscript = pframe.args.postscript or pframe.args.ps or ".";
350
351 local content = crossref(frame, 'CITEREF', pframe.args)
352 local args = { name = refid( 'FOOTNOTE', pframe.args) }
353 return createTag({name = "ref", contents = content, params = args}, frame)
354 end
355
356 -- This is used by template {{r}}.
357 function z.r(frame)
358 local pframe = frame:getParent()
359 local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
360 local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
361 args.page1 = args.page1 or args.page
362 local text = ""
363 -- This would be shorter using ipairs(), but that doesn't work on an arguments table supplied to a template.
364 local index = 1
365 while args[index] ~= nil do
366 local arg = args[index]
367 local t = r0(frame, arg, args.group, args["page" .. index])
368 text = text .. t
369 index = index + 1
370 end
371 return text
372 end
373
374 -- This is used by template {{ref label}}.
375 function z.reflabel(frame)
376 local pframe = frame:getParent()
377 local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
378 local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
379 local P1 = args[1] or ""
380 local P2 = args[2] or ""
381 local P3 = args[3] or ""
382 local id = nil
383 local contents = "[[#endnote_" .. P1 .. P3 .. "|[" .. P2 .. "]]]"
384 local params = {}
385 params.class="reference"
386 if ( args.noid == nil or args.noid == "" ) then params.id = "ref_" .. P1 .. P3 end
387 return createTag({name="sup",contents=contents,params=params})
388 end
389
390 -- This is used by template {{note label}}.
391 function z.notelabel(frame)
392 local pframe = frame:getParent()
393 local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
394 local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
395 local id = args[1] or ""
396 local arrow = args[3] or ""
397 local postscript = args[4] or ""
398 local contents
399 if arrow ~= "" then
400 local sup_arrow = createTag({name="sup",contents=arrow,params={}})
401 contents = "[[#ref_" .. id .. arrow .. "|<b>" .. sup_arrow .. "</b>]]" .. postscript
402 if "none" == arrow then arrow = "^" end -- Change this AFTER using it in the ID parameter and the contents.
403 else
404 contents = (args[2] or "") .. postscript
405 end
406 local params = { class="citation wikicite" }
407 if id ~= "" and ( args.noid == nil or args.noid == "" ) then
408 params.id = mw.uri.anchorEncode("endnote_" .. id .. arrow)
409 end
410 return createTag({name="span",contents=contents,params=params})
411 end
412
413 -- This is used by templates {{reflist}} and {{notelist}}.
414 function z.reflist(frame)
415 local pframe = frame:getParent()
416 local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
417 local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
418 return reflist0(frame, config, args)
419 end
420
421 -- This is used by template {{refbegin}}.
422 function z.refbegin(frame)
423 local pframe = frame:getParent()
424 local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
425 local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
426 return refbegin0(frame, config, args)
427 end
428
429 -- This is used by template {{refend}}.
430 function z.refend(frame)
431 local pframe = frame:getParent()
432 local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
433 local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
434 return refend0(frame, config, args)
435 end
436
437 -- This is used by template {{efn}}.
438 function z.efn(frame)
439 local pframe = frame:getParent()
440 local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
441 local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
442 return createTag({name="ref",contents=(args[1] or ""),params={name=args.name,group=config.default_group}}, frame)
443 end
444
445 return z
446 ---------------------------------------------------------------------
447 --NOTES
448 --
449 -- NOTE A1: This Lua module was originally designed to handle a mix
450 -- of citation styles, crossing Vancouver style with Wikipedia's
451 -- local Citation Style 1 (CS1) from {Template:Citation/core}.
452 -- However, the conflicting positions of parameters, scattered
453 -- in twisted locations across this module, led to a separate
454 -- variation just to untangle the CS1 format of citations.
455 --
456 -- NOTE D2: The placement of dots and other separators between the
457 -- displayed parameters has been a continual headache, to keep
458 -- coordinated with the data in parentheses "(data)". There
459 -- has been a need to pre-check for the existence of related
460 -- options, to keep from putting double-dots ".." in some cases.
461 -- In particular, the omission of the "title=" parameter has led
462 -- to several cases of a spurious dot ". ." because the original
463 -- design had treated the title as a mandatory parameter.
464 --
465 ------------------------------------------------------------------------
466 --HISTORY:
467 --18Oct2012 Fixed lead-space in Chapter by omitting " ".
468 --18Oct2012 Fixed lead-space in Chapter/Title as end " " of Authors/Date/...
469 --19Oct2012 Put HISTORY comments to log major changes (not typos).
470 --19Oct2012 Fixed extra dot ".." in Title by omitting at end of "tcommon=...".
471 --19Oct2012 For pages, put   in "p. " etc.
472 --19Oct2012 Enhanced "pages=" to detect lone page as "p." else "pp." prefix.
473 --19Oct2012 Fixed to show "." after Periodical name (work, newspaper...).
474 --19Oct2012 Fixed web-link to have spaces "[... Archived] from the original".
475 --19Oct2012 Fixed to show ";" between authors & coauthors.
476 --19Oct2012 Fixed to omit extra "." after coauthors.
477 --20Oct2012 Fixed COinS data to not urlencode all, as "ctx_ver=Z39.88-2004"
478 --20Oct2012 Fixed COinS to not end as "&" but use lead "&rft...=" form.
479 --20Oct2012 Fixed COinS to not url.encode page's "rfr_id=..." pagename.
480 --20Oct2012 Fixed COinS data when "web" to default to rft.genre "book".
481 --05Nov2012 Add a span wrapper even when there is no Ref parameter
482 --15Feb2013 Added Agency for "agency=xx".
483 --19Feb2013 Put NOTES comments to explain module operation.
484 --19Feb2013 Copied as Module:Citation/CS1 to alter to match wp:CS1 form.
485 --19Feb2013 Changed OrigYear to use [__] for CS1 style.
486 --19Feb2013 Fixed to not show duplicate Publisher/Agency.
487 --19Feb2013 Moved page-number parameters to after final date.
488 --19Feb2013 Fixed to not put double-dots after title again.
489 --20Feb2013 Changed to omit dot "." if already ends with dot.
490 --20Feb2013 If class "journal" shows Publisher after Periodical/Series.
491 --20Feb2013 Shifted Format to after Language, and Others after Volume.
492 --20Feb2013 Set AccessDate + <span class="reference-accessdate">
493 --20Feb2013 Fixed url when deadurl=no.
494 --20Feb2013 Added sepc for separator character between parameters.
495 --20Feb2013 Put "OCLC" for "Online Computer Library Center".
496 --20Feb2013 Fix empty "authorlink=" as person.link ~= "".
497 --20Feb2013 Added space after AuthorSep & AuthorNameSep.
498 --21Feb2013 Added args.contributor (was missing parameter).
499 --21Feb2013 Fixed EditorSep (was misspelled "EdithorSep").
500 --21Feb2013 Set OCinSdata.rft_val_fmt = "info:ofi/fmt:kev:mtx:book"
501 --21Feb2013 Checked to omit blank codes (asin= | doi= etc.).
502 --21Feb2013 Set enddot to end line if not config.CitationClass "citation".
503 --21Feb2013 Fixed to show "issn=x" as the ISSN code.
504 --21Feb2013 Fixed to show "id=x" after Zbl code.
505 --21Feb2013 Changed to omit double-dot before date when already dot.
506 --21Feb2013 Order config.CitationClass "citation": Volume, Issue, Publisher.
507 --21Feb2013 Put warning "Bad DOI (expected "10."..)" in DOI result.
508 --21Feb2013 Automatically unbolded volume+comma when > 4 long.
509 --21Feb2013 Changed to allow lowercase "asin-tld".
510 --22Feb2013 Fixed ref=harv to extract Year from Date.
511 --22Feb2013 Set Harvard refer. span id if config.CitationClass "citation".
512 --22Feb2013 Fixed config.CitationClass "citation" as span class="citation".
513 --22Feb2013 Capitalized "Archived/Retrieved" only when sepc is dot ".".
514 --23Feb2013 Fixed author editor for "in" or "In" and put space after sepc.
515 --23Feb2013 Changed to omit dot in "et al." when sepc is "." separator.
516 --23Feb2013 Fixed "author1-first" to also get args.given or args.given1.
517 --23Feb2013 Fixed args.article to set Title, after Periodical is Title.
518 --23Feb2013 Fixed to allow blank Title (such as "contribution=mytitle").
519 --23Feb2013 Fixed double-dot ".." at end of Editors list
520 --26Feb2013 Moved "issue=" data to show before "page=".
521 --26Feb2013 Moved "type=" data to show after "format=".
522 --26Feb2013 For "pmc=" link, omitted suffix "/?tool=pmcentrez".
523 --27Feb2013 For coauthors, omitted extra separator after authors.
524 --27Feb2013 For date, allowed empty date to use month/day/year.
525 --27Feb2013 Fixed double-dot ".." at end of authors/coauthors list.
526 --27Feb2013 Reset editor suffix as ", ed." when date exists.
527 --27Feb2013 Removed duplicate display of "others=" data.
528 --27Feb2013 Removed parentheses "( )" around "department" TitleNote.
529 --05Mar2013 Moved Language to follow Periodical or Series.
530 --05Mar2013 Fixed Edition to follow Series or Volume.
531 --05Mar2013 Fixed class encyclopaedia to show article as quoted Chapter.
532 --05Mar2013 Fixed class encyclopaedia to show page as "pp." or "p.".
533 --07Mar2013 Changed class encyclopaedia to omit "( )" around publisher.
534 --07Mar2013 Fixed end double-dot by string.sub(idcommon,-1,-1) was "-1,1".
535 --13Mar2013 Removed enddot "." after "quote=" parameter.
536 --13Mar2013 Changed config.CitationClass "news" to use "p." page format.
537 --13Mar2013 Fixed missing "location=" when "web" or "encyclopaedia".
538 --14Mar2013 Fixed end double-dot after book/work title.
539 --14Mar2013 Fixed double-dot before "p." or "pp." page number.
540 --14Mar2013 Fixed config.CitationClass "book" to use p./pp. page.
541 --
542 --End