Difference between revisions of "Module:Citation"

From Betting Wiki
Jump to navigation Jump to search
Line 1: Line 1:
---------------------------------------------------------------------
+
require('Module:No globals')
-- 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 = {
+
local p = {}
    wikitext = require("Module:Wikitext"),
+
    extensiontags = {
+
-- articles in which traditional Chinese preceeds simplified Chinese
        nowiki = true,
+
local t1st = {
        ref = true,
+
["228 Incident"] = true,
        gallery = true,
+
["Chinese calendar"] = true,
        pre = true,
+
["Lippo Centre, Hong Kong"] = true,
        source = true,
+
["Republic of China"] = true,
        categorytree = true,
+
["Republic of China at the 1924 Summer Olympics"] = true,
        charinsert = true,
+
["Taiwan"] = true,
        hiero = true,
+
["Taiwan (island)"] = true,
        imagemap = true,
+
["Taiwan Province"] = true,
        inputbox = true,
+
["Wei Boyang"] = true,
        math = true,
 
        poem = true,
 
        ref = true,
 
        references = true,
 
        syntaxhighlight = true,
 
        timeline = true,
 
    }
 
 
}
 
}
  
function trim( str )
+
-- the labels for each part
    if str == nil then
+
local labels = {
        return nil;
+
["c"] = "Chinese",
    end
+
["s"] = "simplified Chinese",
    return str:match( "^%s*(.-)%s*$" );
+
["t"] = "traditional Chinese",
end
+
["p"] = "pinyin",
 +
["tp"] = "Tongyong Pinyin",
 +
["w"] = "Wade–Giles",
 +
["j"] = "Jyutping",
 +
["cy"] = "Cantonese Yale",
 +
["poj"] = "Pe̍h-ōe-jī",
 +
["zhu"] = "Zhuyin Fuhao",
 +
["l"] = "literally",
 +
}
  
function hideinprint(content)
+
-- article titles for wikilinks for each part
    return content
+
local wlinks = {
end
+
["c"] = "Chinese language",
 +
["s"] = "simplified Chinese characters",
 +
["t"] = "traditional Chinese characters",
 +
["p"] = "pinyin",
 +
["tp"] = "Tongyong Pinyin",
 +
["w"] = "Wade–Giles",
 +
["j"] = "Jyutping",
 +
["cy"] = "Yale romanization of Cantonese",
 +
["poj"] = "Pe̍h-ōe-jī",
 +
["zhu"] = "Bopomofo",
 +
}
  
function onlyinprint(content)
+
-- for those parts which are to be treated as languages their ISO code
    return ""
+
local ISOlang = {
end
+
["c"] = "zh",
 +
["t"] = "zh-Hant",
 +
["s"] = "zh-Hans",
 +
["p"] = "zh-Latn-pinyin",
 +
["tp"] = "zh-Latn",
 +
["w"] = "zh-Latn-wadegile",
 +
["j"] = "yue-jyutping",
 +
["cy"] = "yue",
 +
["poj"] = "nan",
 +
["zhu"] = "zh-Bopo",
 +
}
  
-- This returns a string with HTML character entities for wikitext markup characters.
+
local italic = {
function wikiescape(text)
+
["p"] = true,
    text = text:gsub( '[&\'%[%]{|}]', {   
+
["tp"] = true,
            ['&'] = '&',   
+
["w"] = true,
            ["'"] = ''',   
+
["j"] = true,
            ['['] = '[',   
+
["cy"] = true,
            [']'] = ']',
+
["poj"] = true,
            ['{'] = '{',
+
}
            ['|'] = '|',
+
-- Categories for different kinds of Chinese text
            ['}'] = '}' } );
+
local cats = {
    return text;
+
["c"] = "[[Category:Articles containing Chinese-language text]]",
end
+
["s"] = "[[Category:Articles containing simplified Chinese-language text]]",
 
+
["t"] = "[[Category:Articles containing traditional Chinese-language text]]",
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, '["&\'<=>%[%]{|}]', {
 
        ['"'] = '&#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.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="//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)
+
function p.Zh(frame)
    local P1 = trim(args[1]) or ""
+
-- load arguments module to simplify handling of args
    if ( args[5] ~= nil) then
+
local getArgs = require('Module:Arguments').getArgs
        return P1 .. " et al."
+
    else
+
local args = getArgs(frame)
        local P2 = trim(args[2]) or ""
+
return p._Zh(args)
        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
 
end
 +
 +
function p._Zh(args)
 +
local uselinks = not (args["links"] == "no") -- whether to add links
 +
local uselabels = not (args["labels"] == "no") -- whether to have labels
 +
local capfirst = args["scase"] ~= nil
 +
 +
local t1 = false -- whether traditional Chinese characters go first
 +
local j1 = false -- whether Cantonese Romanisations go first
 +
local testChar
 +
if (args["first"]) then
 +
for testChar in mw.ustring.gmatch(args["first"], "%a+") do
 +
if (testChar == "t") then
 +
t1 = true
 +
end
 +
if (testChar == "j") then
 +
j1 = true
 +
end
 +
end
 +
end
 +
if (t1 == false) then
 +
local title = mw.title.getCurrentTitle()
 +
t1 = t1st[title.text] == true
 +
end
  
function crossref(frame, label, args)
+
-- based on setting/preference specify order
    local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
+
local orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "poj", "zhu", "l"}
    local LB = config.BracketLeft or ""
+
if (t1) then
    local RB = config.BracketRight or ""
+
orderlist[2] = "t"
    local anchor = args.ref or args.Ref or anchorid( label, args)
+
orderlist[3] = "s"
    local text = name(args)
+
end
    local loc = args.loc
+
if (j1) then
    local page
+
orderlist[4] = "j"
    local pages = args.pp or args.pages
+
orderlist[5] = "cy"
    if pages == nil or pages == '' then
+
orderlist[6] = "p"
        page = args.p or args.page;
+
orderlist[7] = "tp"
    end
+
orderlist[8] = "w"
    if nil == loc then loc = "" else loc = " " .. loc end
+
end
    if ( page ~= nil ) then
+
        local pagesep = config.PageSep or ", p.&nbsp;"
+
-- rename rules. Rules to change parameters and labels based on other parameters
        loc = loc .. pagesep .. page
+
if args["hp"] then
    end
+
-- hp an alias for p ([hanyu] pinyin)
    if ( pages ~= nil ) then
+
args["p"] = args["hp"]
        local pagessep = config.PagesSep or ", pp.&nbsp;"
+
end
        loc = loc .. pagessep .. pages
+
if args["tp"] then
    end       
+
-- if also Tongyu pinyin use full name for Hanyu pinyin
    local pagename = args.pagename or ""
+
labels["p"] = "Hanyu Pinyin"
    local ps = args.Postscript or ""
+
end
    return LB .. "[[" .. pagename .. "#" .. anchor .. "|" .. text .. "]]" .. loc .. RB .. ps
+
end
+
if (args["s"] and args["s"] == args["t"]) then
 
+
-- Treat simplified + traditional as Chinese if they're the same
function r0(frame, name, group, page)
+
args["c"] = args["s"]
    if ( name == nil ) then return "" end
+
args["s"] = nil
    if ( group == nil ) then group = "" end
+
args["t"] = nil
    local p = ""
+
elseif (not (args["s"] and args["t"])) then
    if ( page ~= nil ) then
+
-- use short label if only one of simplified and traditional
        local contents = ":" .. page
+
labels["s"] = labels["c"]
        p = createTag({name="sup",contents=contents,params={class="reference",style="white-space:nowrap;"}})
+
labels["t"] = labels["c"]
    end
+
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]]&nbsp;[//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 .. "|&#91;" .. P2 .. "&#93;]]"
 
    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}}.
+
local body = "" -- the output string
function z.efn(frame)
+
local params -- for creating HTML spans
    local pframe = frame:getParent()
+
local label -- the label, i.e. the bit preceeding the supplied text
    local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
+
local val -- the supplied text
    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)
+
-- go through all possible fields in loop, adding them to the output
 +
for i, part in ipairs(orderlist) do
 +
if (args[part]) then
 +
-- build label
 +
label = ""
 +
if (uselabels) then
 +
label = labels[part]
 +
if (capfirst) then
 +
label = mw.language.getContentLanguage():ucfirst(label)
 +
capfirst = false
 +
end
 +
if (uselinks and part ~= "l") then
 +
label = "[[" .. wlinks[part] .. "|" .. label .. "]]"
 +
end
 +
label = label .. "&#58; "
 +
end
 +
-- build value
 +
val = args[part]
 +
if (cats[part]) then
 +
-- if has associated category add it
 +
val = cats[part] .. val
 +
end
 +
if (ISOlang[part]) then
 +
-- add span for language if needed
 +
params = {["lang"] = ISOlang[part], ["xml:lang"] = ISOlang[part]}
 +
val = mw.text.tag({name="span",attrs=params, content=val})
 +
elseif (part == "l") then
 +
-- put literals in quotes
 +
val = '"' .. val .. '"'
 +
end
 +
if (italic[part]) then
 +
-- italicise
 +
val = "''" .. val .. "''"
 +
end
 +
-- add both to body
 +
body = body .. label .. val .. "; "
 +
end
 +
end
 +
 +
if (body > "") then -- check for empty string
 +
return string.sub(body, 1, -3) -- chop off final semicolon and space
 +
else --no named parameters; see if there's a first parameter, ignoring its name
 +
if (args[1]) then
 +
-- if there is treat it as Chinese
 +
label = ""
 +
if (uselabels) then
 +
label = labels["c"]
 +
if (uselinks) then
 +
label = "[[" .. wlinks["c"] .. "|" .. label .. "]]"
 +
end
 +
label = label .. "&#58; "
 +
end
 +
-- default to show links and labels as no options given
 +
val = cats["c"] .. args[1]
 +
params = {["lang"] = ISOlang["c"], ["xml:lang"] = ISOlang["c"]}
 +
val = mw.text.tag({name="span",attrs=params, content=val})
 +
return label .. val
 +
end
 +
return ""
 +
end
 
end
 
end
  
return z
+
return p
---------------------------------------------------------------------
 
--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 &nbsp in "p.&nbsp;" 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.
 
--
 
--Endooooooooooo
 

Revision as of 22:24, 12 August 2017

Documentation for this module may be created at Module:Citation/doc

require('Module:No globals')

local p = {}
 
-- articles in which traditional Chinese preceeds simplified Chinese
local t1st = {
	["228 Incident"] = true,
	["Chinese calendar"] = true,
	["Lippo Centre, Hong Kong"] = true,
	["Republic of China"] = true,
	["Republic of China at the 1924 Summer Olympics"] = true,
	["Taiwan"] = true,
	["Taiwan (island)"] = true,
	["Taiwan Province"] = true,
	["Wei Boyang"] = true,
}

-- the labels for each part 
local labels = {
	["c"] = "Chinese",
	["s"] = "simplified Chinese",
	["t"] = "traditional Chinese",
	["p"] = "pinyin",
	["tp"] = "Tongyong Pinyin",
	["w"] = "Wade–Giles",
	["j"] = "Jyutping",
	["cy"] = "Cantonese Yale",
	["poj"] = "Pe̍h-ōe-jī",
	["zhu"] = "Zhuyin Fuhao",
	["l"] = "literally",
}

-- article titles for wikilinks for each part
local wlinks = {
	["c"] = "Chinese language",
	["s"] = "simplified Chinese characters",
	["t"] = "traditional Chinese characters",
	["p"] = "pinyin",
	["tp"] = "Tongyong Pinyin",
	["w"] = "Wade–Giles",
	["j"] = "Jyutping",
	["cy"] = "Yale romanization of Cantonese",
	["poj"] = "Pe̍h-ōe-jī",
	["zhu"] = "Bopomofo",
}

-- for those parts which are to be treated as languages their ISO code
local ISOlang = {
	["c"] = "zh",
	["t"] = "zh-Hant",
	["s"] = "zh-Hans",
	["p"] = "zh-Latn-pinyin",
	["tp"] = "zh-Latn",
	["w"] = "zh-Latn-wadegile",
	["j"] = "yue-jyutping",
	["cy"] = "yue",
	["poj"] = "nan",
	["zhu"] = "zh-Bopo",
}

local italic = {
	["p"] = true,
	["tp"] = true,
	["w"] = true,
	["j"] = true,
	["cy"] = true,
	["poj"] = true,
}
-- Categories for different kinds of Chinese text
local cats = {
	["c"] = "[[Category:Articles containing Chinese-language text]]",
	["s"] = "[[Category:Articles containing simplified Chinese-language text]]",
	["t"] = "[[Category:Articles containing traditional Chinese-language text]]",
}

function p.Zh(frame)
	-- load arguments module to simplify handling of args
	local getArgs = require('Module:Arguments').getArgs
	
	local args = getArgs(frame)
	return p._Zh(args)
end
	
function p._Zh(args)
	local uselinks = not (args["links"] == "no") -- whether to add links
	local uselabels = not (args["labels"] == "no") -- whether to have labels
	local capfirst = args["scase"] ~= nil
 
	local t1 = false -- whether traditional Chinese characters go first
	local j1 = false -- whether Cantonese Romanisations go first
	local testChar
	if (args["first"]) then
	 	 for testChar in mw.ustring.gmatch(args["first"], "%a+") do
			if (testChar == "t") then
				t1 = true
			 end
			if (testChar == "j") then
				j1 = true
			 end
		end
	end
	if (t1 == false) then
		local title = mw.title.getCurrentTitle()
		t1 = t1st[title.text] == true
	end

	-- based on setting/preference specify order
	local orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "poj", "zhu", "l"}
	if (t1) then
		orderlist[2] = "t"
		orderlist[3] = "s"
	end
	if (j1) then
		orderlist[4] = "j"
		orderlist[5] = "cy"
		orderlist[6] = "p"
		orderlist[7] = "tp"
		orderlist[8] = "w"
	end
	
	-- rename rules. Rules to change parameters and labels based on other parameters
	if args["hp"] then
		-- hp an alias for p ([hanyu] pinyin)
		args["p"] = args["hp"]
	end
	if args["tp"] then
		-- if also Tongyu pinyin use full name for Hanyu pinyin
		labels["p"] = "Hanyu Pinyin"
	end
	
	if (args["s"] and args["s"] == args["t"]) then
		-- Treat simplified + traditional as Chinese if they're the same
		args["c"] = args["s"]
		args["s"] = nil
		args["t"] = nil
	elseif (not (args["s"] and args["t"])) then
		-- use short label if only one of simplified and traditional
		labels["s"] = labels["c"]
		labels["t"] = labels["c"]
	end

	local body = "" -- the output string
	local params -- for creating HTML spans
	local label -- the label, i.e. the bit preceeding the supplied text
	local val -- the supplied text
	
	-- go through all possible fields in loop, adding them to the output
	for i, part in ipairs(orderlist) do
		if (args[part]) then
			-- build label
			label = ""
			if (uselabels) then
				label = labels[part]
				if (capfirst) then
					label = mw.language.getContentLanguage():ucfirst(label)
					capfirst = false
				end
				if (uselinks and part ~= "l") then
					label = "[[" .. wlinks[part] .. "|" .. label .. "]]"
				end
				label = label .. "&#58; "
			end
			-- build value
			val = args[part]
			if (cats[part]) then
				-- if has associated category add it
				val = cats[part] .. val
			end
			if (ISOlang[part]) then
				-- add span for language if needed
				params = {["lang"] = ISOlang[part], ["xml:lang"] = ISOlang[part]}
				val = mw.text.tag({name="span",attrs=params, content=val})
			elseif (part == "l") then
				-- put literals in quotes
				val = '"' .. val .. '"'
			end
			if (italic[part]) then
				-- italicise
				val = "''" .. val .. "''"
			end
			-- add both to body
			body = body .. label .. val .. "; "
		end
	end
	
	if (body > "") then -- check for empty string
		return string.sub(body, 1, -3) -- chop off final semicolon and space
	else --no named parameters; see if there's a first parameter, ignoring its name
		if (args[1]) then
			-- if there is treat it as Chinese
			label = ""
			if (uselabels) then
				label = labels["c"]
				if (uselinks) then
					label = "[[" .. wlinks["c"] .. "|" .. label .. "]]"
				end
				label = label .. "&#58; "
			end
			-- default to show links and labels as no options given
			val = cats["c"] .. args[1]
			params = {["lang"] = ISOlang["c"], ["xml:lang"] = ISOlang["c"]}
			val = mw.text.tag({name="span",attrs=params, content=val})
			return label .. val
		end
		return ""
	end
end

return p