---
-- Module:Citation - Lua module for Citation auxiliary templates
---
-- For the 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( '[&\'%[%]{|}]', {
['&'] = '&#38;',
["'"] = '&#39;',
['['] = '&#91;',
[']'] = '&#93;',
['{'] = '&#123;',
['|'] = '&#124;',
['}'] = '&#125;' } );
return text;
end  
function createTag(t, )
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 ) then error ("Please supply an extra argument to the createTag function.") end
local params = {}
for n,v in pairs(t.params) do
table.insert(params, "|" .. n .. "=" .. v)
end
return :preprocess("")
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, '["&\'<=>%[%]{|}]', {
['"'] = '&#34;',
['&'] = '&#38;',
["'"] = '&#39;',
['<'] = '&#60;',
['='] = '&#61;',
['>'] = '&#62;',
['['] = '&#91;',
[']'] = '&#93;',
['{'] = '&#123;',
['|'] = '&#124;',
['}'] = '&#125;',
} )
s = string.sub( string.gsub( '\n' .. s, '\n[#*:;]', {
["\n#"] = "\n&#35;",
["\n*"] = "\n&#42;",
["\n:"] = "\n&#58;",
["\n;"] = "\n&#59;",
} ), 2 )
s = string.gsub( s, '://', '&#58;//' )
s = string.gsub( s, 'ISBN ', 'ISBN&#32;' )
s = string.gsub( s, 'RFC ', 'RFC&#32;' )  
return s
end  
function externallinkid(args)
local sep = args.separator or "&nbsp;"
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.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 = "doi:" .. id;
cat = cat .. ""
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 .. "" .. '<span> 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 = " &amp; " .. P3
P2 = ", " .. P2
elseif ( args[3] ~= nil ) then
P3 = " " .. P3
P2 = " &amp; " .. P2
elseif ( args[2] ~= nil ) then
P2 = " " .. P2
end
return P1 .. P2 .. P3 .. P4
end
end  
function crossref(, label, args)
local config = .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.&nbsp;"
loc = loc .. pagesep .. page
end
if ( pages ~= nil ) then
local pagessep = config.PagesSep or ", pp.&nbsp;"
loc = loc .. pagessep .. pages
end
local pagename = args.pagename or ""
local ps = args.Postscript or ""
return LB .. "" .. text .. "" .. loc .. RB .. ps
end  
function r0(, 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={,}})
end
return createTag({name="ref",contents="",params={name=name,group=group}}, ) .. p
end  
function reflist0(, 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.
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}}, )
return createTag({name="div",contents=references,params=params})
end  
function refbegin0(, 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.
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