Module:Citation/CS1: Difference between revisions
once more. Locally copying mw.text.nowiki
m>Dragons flight (self rv, the mw.text library isn't installed? well joy) |
m>Dragons flight (once more. Locally copying mw.text.nowiki) |
||
Line 7: | Line 7: | ||
local z = { | local z = { | ||
wikitext = require("Module:Wikitext"), | 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, | |||
} | |||
} | } | ||
Line 18: | Line 35: | ||
end | end | ||
function | -- This returns a string with HTML character entities for wikitext markup characters. | ||
function wikiescape(text) | |||
text = text:gsub( '[&\'%[%]{|}]', { | |||
['&'] = '&', | |||
["'"] = ''', | |||
['['] = '[', | |||
[']'] = ']', | |||
['{'] = '{', | |||
['|'] = '|', | |||
['}'] = '}' } ); | |||
return text; | |||
end | end | ||
function | 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 " " | local sep = args.separator or " " | ||
args.suffix = args.suffix or "" | args.suffix = args.suffix or "" | ||
Line 31: | Line 116: | ||
local t0 = onlyinprint(args.label .. sep .. args.id) | local t0 = onlyinprint(args.label .. sep .. args.id) | ||
local t1 = hideinprint("[[" .. args.link .. "|" .. args.label .. "]]" .. sep .. "[" .. args.prefix .. url_string .. args.suffix .. " " .. nowiki( | local t1 = hideinprint("[[" .. args.link .. "|" .. args.label .. "]]" .. sep .. "[" .. args.prefix .. url_string .. args.suffix .. " " .. nowiki(args.id) .. "]") | ||
return t0 .. t1 | return t0 .. t1 | ||
end | end | ||
function internallinkid( | function internallinkid(args) | ||
local sep = args.separator or " " | local sep = args.separator or " " | ||
args.suffix = args.suffix or "" | args.suffix = args.suffix or "" | ||
local t0 = onlyinprint(args.label .. sep .. args.id) | local t0 = onlyinprint(args.label .. sep .. args.id) | ||
local t1 = hideinprint("[[" .. args.link .. "|" .. args.label .. "]]" .. sep .. "[[" .. args.prefix .. args.id .. args.suffix .. "|" .. nowiki( | local t1 = hideinprint("[[" .. args.link .. "|" .. args.label .. "]]" .. sep .. "[[" .. args.prefix .. args.id .. args.suffix .. "|" .. nowiki(args.id) .. "]]") | ||
return t0 .. t1 | return t0 .. t1 | ||
end | end | ||
function amazon( | function amazon(id, domain) | ||
if ( nil == domain ) then | if ( nil == domain ) then | ||
domain = "com" | domain = "com" | ||
Line 50: | Line 135: | ||
domain = "co." .. domain | domain = "co." .. domain | ||
end | end | ||
return externallinkid( | return externallinkid({link="Amazon Standard Identification Number",label="ASIN",prefix="//www.amazon."..domain.."/dp/",id=id,encode=false}) | ||
end | end | ||
function doi( | function doi(id, inactive, nocat) | ||
local cat = "" | local cat = "" | ||
local text; | local text; | ||
Line 61: | Line 146: | ||
inactive = " (inactive " .. inactive .. ")" | inactive = " (inactive " .. inactive .. ")" | ||
else | else | ||
text = externallinkid( | text = externallinkid({link="Digital object identifier",label="doi",prefix="http://dx.doi.org/",id=id,separator=":"}) | ||
inactive = "" | inactive = "" | ||
end | end | ||
Line 71: | Line 156: | ||
end | end | ||
function url( | function url(id) | ||
local t0 = onlyinprint(id) | local t0 = onlyinprint(id) | ||
local t1 = hideinprint("[" .. id .. " " .. nowiki( | local t1 = hideinprint("[" .. id .. " " .. nowiki(id) .. "]") | ||
return t0 .. t1 | return t0 .. t1 | ||
end | end | ||
Line 166: | Line 251: | ||
end | end | ||
function openlibrary( | function openlibrary(id) | ||
local cat = "" | local cat = "" | ||
local prefix = "" | local prefix = "" | ||
Line 180: | Line 265: | ||
cat = cat .. "[[Category:Pages with OL errors]]" | cat = cat .. "[[Category:Pages with OL errors]]" | ||
end | end | ||
local text = externallinkid( | local text = externallinkid({link="Open Library",label="OL",prefix=prefix,id=id}) | ||
return text .. cat | return text .. cat | ||
end | end | ||
Line 234: | Line 319: | ||
end | end | ||
if ( "scap" == format ) then result= | if ( "scap" == format ) then result= createTag({name="span", contents=result, params={class="smallcaps", style="font-variant:small-caps;"}}) end -- if necessary wrap result in <span> tag to format in Small Caps | ||
return result, count | return result, count | ||
end | end | ||
Line 346: | Line 431: | ||
end | end | ||
function citation0( | function citation0( config, args) | ||
--------------------------------------------------- Get parameters | --------------------------------------------------- Get parameters | ||
local PPrefix = config.PPrefix or "p. " | local PPrefix = config.PPrefix or "p. " | ||
Line 807: | Line 888: | ||
if ( SubscriptionRequired ~= nil and | if ( SubscriptionRequired ~= nil and | ||
SubscriptionRequired ~= "" ) then | SubscriptionRequired ~= "" ) then | ||
SubscriptionRequired = sepc .. " " .. | SubscriptionRequired = sepc .. " " .. createTag({name="span", contents="(subscription required)", params={style="font-size:0.95em; font-size: 90%; color: #555"}}) | ||
else | else | ||
SubscriptionRequired = "" | SubscriptionRequired = "" | ||
end | end | ||
if ( ARXIV ~= nil and ARXIV ~= "" ) then | if ( ARXIV ~= nil and ARXIV ~= "" ) then | ||
ARXIV = sepc .. " " .. externallinkid( | ARXIV = sepc .. " " .. externallinkid({label="arXiv",link="arXiv",prefix="http://arxiv.org/abs/",id=ARXIV,separator=":",encode=false}) else ARXIV = "" end | ||
if ( ASIN ~= nil and ASIN ~= "" ) then | if ( ASIN ~= nil and ASIN ~= "" ) then | ||
ASIN = sepc .. " " .. amazon( | ASIN = sepc .. " " .. amazon(ASIN, ASINTLD) else ASIN = "" end | ||
if ( BIBCODE ~= nil and BIBCODE ~= "" ) then | if ( BIBCODE ~= nil and BIBCODE ~= "" ) then | ||
BIBCODE = sepc .. " " .. externallinkid( | 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 = sepc .. " " .. doi(DOI, DoiBroken) else DOI = "" end | ||
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 | ||
ISBN = sepc .. " " .. internallinkid( | ISBN = sepc .. " " .. internallinkid({label="ISBN",link="International Standard Book Number",prefix="Special:BookSources/",id=ISBN}) else ISBN = "" end | ||
if ( ISSN ~= nil and ISSN ~="" ) then | if ( ISSN ~= nil and ISSN ~="" ) then | ||
ISSN = sepc .. " " .. externallinkid( | ISSN = sepc .. " " .. externallinkid({label="ISSN",link="International Standard Serial Number",prefix="//www.worldcat.org/issn/",id=ISSN,encode=false}) else ISSN = "" end | ||
if ( JFM ~= nil and JFM ~="" ) then | if ( JFM ~= nil and JFM ~="" ) then | ||
JFM = sepc .." " .. externallinkid( | JFM = sepc .." " .. externallinkid({label="JFM",link="Jahrbuch über die Fortschritte der Mathematik",prefix="http://www.zentralblatt-math.org/zmath/en/search/?format=complete&q=an:",id=JFM}) else JFM = "" end | ||
if ( JSTOR ~= nil and JSTOR ~="") then | if ( JSTOR ~= nil and JSTOR ~="") then | ||
JSTOR = sepc .." " .. externallinkid( | JSTOR = sepc .." " .. externallinkid({label="JSTOR",link="JSTOR",prefix="http://www.jstor.org/stable/",id=JSTOR}) else JSTOR = "" end | ||
if ( LCCN ~= nil and LCCN ~="" ) then | if ( LCCN ~= nil and LCCN ~="" ) then | ||
LCCN = sepc .." " .. externallinkid( | LCCN = sepc .." " .. externallinkid({label="LCCN",link="Library of Congress Control Number",prefix="http://lccn.loc.gov/",id=LCCN,encode=false}) else LCCN = "" end | ||
if ( MR ~= nil and MR ~="" ) then | if ( MR ~= nil and MR ~="" ) then | ||
MR = sepc .." " .. externallinkid( | MR = sepc .." " .. externallinkid({label="MR",link="Mathematical Reviews",prefix="http://www.ams.org/mathscinet-getitem?mr=",id=MR}) else MR = "" end | ||
if ( OCLC ~= nil and OCLC ~="") then | if ( OCLC ~= nil and OCLC ~="") then | ||
OCLC = sepc .." " .. externallinkid( | 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 = sepc .." " .. openlibrary(OL) else OL = "" end | ||
if ( OSTI ~= nil and OSTI ~="") then | if ( OSTI ~= nil and OSTI ~="") then | ||
OSTI = sepc .." " .. externallinkid( | 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 | ||
if ( PMC ~= nil and PMC ~="") then | if ( PMC ~= nil and PMC ~="") then | ||
PMC = sepc .." " .. externallinkid( | PMC = sepc .." " .. externallinkid({label="PMC",link="PubMed Central",prefix="//www.ncbi.nlm.nih.gov/pmc/articles/PMC",suffix=" ",id=PMC}) else PMC = "" end | ||
if ( PMID ~= nil and PMID ~="") then | if ( PMID ~= nil and PMID ~="") then | ||
PMID = sepc .." " .. externallinkid( | PMID = sepc .." " .. externallinkid({label="PMID",link="PubMed Identifier",prefix="//www.ncbi.nlm.nih.gov/pubmed/",id=PMID,encode=false}) else PMID = "" end | ||
if ( RFC ~= nil and RFC ~="") then | if ( RFC ~= nil and RFC ~="") then | ||
RFC = sepc .." " .. externallinkid( | RFC = sepc .." " .. externallinkid({label="RFC",link="Request for Comments",prefix="//tools.ietf.org/html/rfc",id=RFC,encode=false}) else RFC = "" end | ||
if ( SSRN ~= nil and SSRN ~="") then | if ( SSRN ~= nil and SSRN ~="") then | ||
SSRN = sepc .." " .. externallinkid( | 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 = " " .. url(URL) | ||
else | else | ||
URL = "" | URL = "" | ||
end | end | ||
if ( ZBL ~= nil and ZBL ~="") then | if ( ZBL ~= nil and ZBL ~="") then | ||
ZBL = sepc .." " .. externallinkid( | ZBL = sepc .." " .. externallinkid({label="Zbl",link="Zentralblatt MATH",prefix="http://www.zentralblatt-math.org/zmath/en/search/?format=complete&q=an:",id=ZBL}) else ZBL = "" end | ||
if ( Quote and Quote ~="" ) then | if ( Quote and Quote ~="" ) then | ||
if Quote:sub(1,1) == '"' and Quote:sub(-1,-1) == '"' then | if Quote:sub(1,1) == '"' and Quote:sub(-1,-1) == '"' then | ||
Line 1,050: | Line 1,131: | ||
end | end | ||
text = | text = createTag({name="span", contents=text, params=args}) | ||
local empty_span = | local empty_span = createTag( {name="span", contents=" ", params={style="display: none;"}} ); | ||
-- Note: Using display: none on then COinS span breaks some clients. | -- Note: Using display: none on then COinS span breaks some clients. | ||
local OCinS = | local OCinS = createTag({name="span", contents=empty_span, params={class="Z3988",title=OCinStitle }}) | ||
return text .. OCinS | return text .. OCinS | ||
Line 1,066: | Line 1,147: | ||
if ( page ~= nil ) then | if ( page ~= nil ) then | ||
local contents = ":" .. page | local contents = ":" .. page | ||
p = | p = createTag({name="sup",contents=contents,params={class="reference",style="white-space:nowrap;"}}) | ||
end | end | ||
return | return createTag({name="ref",contents="",params={name=name,group=group}}, frame) .. p | ||
end | end | ||
Line 1,099: | Line 1,180: | ||
params.style = params.style .. " " .. z.wikitext.columnwidthstyle(width) | params.style = params.style .. " " .. z.wikitext.columnwidthstyle(width) | ||
end | end | ||
local references = | local references = createTag({name="references",contents=contents,params={group=group}}, frame) | ||
return | return createTag({name="div",contents=references,params=params}) | ||
end | end | ||
Line 1,155: | Line 1,236: | ||
local pframe = frame:getParent() | local pframe = frame:getParent() | ||
local id = pframe.args.id or pframe.args[1] or "" | local id = pframe.args.id or pframe.args[1] or "" | ||
return doi( | return doi(id) | ||
end | end | ||
Line 1,162: | Line 1,243: | ||
local pframe = frame:getParent() | local pframe = frame:getParent() | ||
local Name = pframe.args[1] or "" | local Name = pframe.args[1] or "" | ||
return hideinprint( | return hideinprint("[[International Standard Serial Number|ISSN]] [http://www.worldcat.org/search?fq=x0:jrnl&q=n2:" .. Name .. " " .. Name .. "]") | ||
end | end | ||
Line 1,185: | Line 1,266: | ||
args[k] = v; | args[k] = v; | ||
end | end | ||
return citation0( | |||
local config = {}; | |||
for k, v in pairs( frame.args ) do | |||
config[k] = v; | |||
end | |||
return citation0( config, args) | |||
end | end | ||
Line 1,193: | Line 1,280: | ||
local content = crossref(frame, pframe.args) | local content = crossref(frame, pframe.args) | ||
local args = { name = refid(pframe.args) } | local args = { name = refid(pframe.args) } | ||
return | return createTag({name = "ref", contents = content, params = args}, frame) | ||
end | end | ||
Line 1,227: | Line 1,314: | ||
params.class="reference" | params.class="reference" | ||
if ( args.noid == nil or args.noid == "" ) then params.id = "ref_" .. P1 .. P3 end | if ( args.noid == nil or args.noid == "" ) then params.id = "ref_" .. P1 .. P3 end | ||
return | return createTag({name="sup",contents=contents,params=params}) | ||
end | end | ||
Line 1,240: | Line 1,327: | ||
local contents | local contents | ||
if arrow ~= "" then | if arrow ~= "" then | ||
local sup_arrow = | local sup_arrow = createTag({name="sup",contents=arrow,params={}}) | ||
contents = "[[#ref_" .. id .. arrow .. "|<b>" .. sup_arrow .. "</b>]]" .. postscript | 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. | if "none" == arrow then arrow = "^" end -- Change this AFTER using it in the ID parameter and the contents. | ||
Line 1,250: | Line 1,337: | ||
params.id = mw.uri.anchorEncode("endnote_" .. id .. arrow) | params.id = mw.uri.anchorEncode("endnote_" .. id .. arrow) | ||
end | end | ||
return | return createTag({name="span",contents=contents,params=params}) | ||
end | end | ||
Line 1,282: | Line 1,369: | ||
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself | 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 args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template | ||
return | return createTag({name="ref",contents=(args[1] or ""),params={name=args.name,group=config.default_group}}, frame) | ||
end | end | ||