Module:Cite Q: Difference between revisions

Content deleted Content added
multiple minor fixes from sandbox
make function exportable; improve title linking; minor bugfix
Tag: Reverted
Line 1:
-- Version: 20202021-1201-2609
 
local citeqp = {}
 
require('Module:No globals')
Line 25:
-- we need three exceptions in English for 1st, 2nd, 3rd, 21st, .. 31st, etc.
-------------------------------------------------------------------------------
local p.makeOrdinal = function(cardinal)
local card = tonumber(cardinal)
if not card then return cardinal end
Line 224:
end
return maxpos
end
end -- of local function makelink
 
--[=[-------------------------< G E T _ N A M E _ L I S T >----------------------------------------------------
Line 339:
end
 
function citeqp.cite_q_cite_q (frameciteq_args)
local citeq_argsframe = {};mw.getCurrentFrame()
local expand = '' -- when set to anything, causes {{cite q}} to render <code><nowiki>{{citation|...}}</nowiki></code>
 
for k, v in pairs(frame:getParent().args) do
if in_array (k, {'expand', '_debug'}) then
if is_set(v) then
expand = v -- record setting but don't pass |expand= to {{citation}}
end
else
if v ~= "" then citeq_args[k] = v end
end
end
 
for k, v in pairs(frame.args) do
if v ~= "" then citeq_args[k] = v end
end
 
-- parameters that don't get passed to Citation
local expand = citeq_args.expand -- when set to anything, causes {{cite q}} to render <code><nowiki>{{citation|...}}</nowiki></code>
local qid = citeq_args.qid
local qid = citeq_args[1] or citeq_args.qid
local wdl = citeq_args.wdl
local template = citeq_args.template
citeq_args.expand = nil
citeq_args[1] = nil
citeq_args.qid = nil
citeq_args.wdl = nil
citeq_args.template = nil
 
-- if title supplied, flag to not read html title
local titleforced = (citeq_args.title ~= nil)
 
Line 382 ⟶ 371:
citeq_args[name] = getValue( {data.id, fwd = "ALL", osd = "no", noicon = "true", qid = qid, maxvals = data.maxvals, linked = data.linked, rank = data.rank or "best", citeq_args[name] } )
if data.populate_from_journal then
citeq_args[name]local publishedin = getValue( {"P1433", ps = 1, qid = qid, maxvals = 0, citeq_args[name], qual = data.id, qualsonly = 'yes'} )
citeq_args[name] = citeq_args[name]publishedin or getPropOfProp({qid = qid, prop1 = "P1433", prop2 = data.id, maxvals = data.maxvals, ps = 1})
end
if citeq_args[name] and citeq_args[name]:find('[[Category:Articles with missing Wikidata information]]', 1, true) then
Line 423 ⟶ 412:
citeq_args.url = getValue( {"P2699", ps = 1, qid = qid, maxvals = 0, citeq_args.url } ) -- try url
 
if citeq_args.publisher == "Unknown" then -- look for "stated as" (P1932)
local slink = mw.wikibase.getSitelink(qid)
local stated_as = getValue( {"P123", ps = 1, qid = qid, maxvals = 1, qual="P1932", qo="y"} )
local label = mw.wikibase.getLabel(qid) or citeq_args.language and mw.wikibase.getLabelByLang(qid, citeq_args.language)
if stated_as then citeq_args.publisher = stated_as end
local slink_flag = false
end
local wrap_title = ''
 
if citeq_args.title then
if slinknot titleforced then
local htmltitle = getValue( {"P1476", qual = "P6833", ps = 1, qid = qid, maxvals = 1, qo = "y"} )
citeq_args.url = nil
if htmltitle then
wrap_title = wrap_nowiki(citeq_args.title, titleforced)
citeq_args.title = htmltitle:gsub("</?i>", "''")
slink_flag = true
else
citeq_args.titlelocal title_display = wrap_nowiki(citeq_args.title, titleforced)
or mw.wikibase.getLabel(qid)
end
or (citeq_args.language and mw.wikibase.getLabelByLang(qid, citeq_args.language))
else
or ("No label or title -- debug: " .. qid)
if slink then
if citeq_args.url = nilthen
citeq_args.title = wrap_nowiki(title_display)
if slink:lower() == label:lower() then
citeq_args.title = '[[' .. slink .. ']]'
else
local slink = mw.wikibase.getSitelink(qid)
wrap_title = wrap_nowiki(slink:gsub("%s%(.+%)$", ""):gsub(",.+$", ""), titleforced)
local slink_flag = truefalse
local wrap_title = ''
if citeq_args.title then
if slink then
wrap_title = wrap_nowiki(citeq_args.title)
slink_flag = true
else
citeq_args.title = wrap_nowiki(citeq_args.title)
end
else
if slink then
if slink:lower() == title_display:lower() then
citeq_args.title = '[[' .. slink .. ']]'
else
wrap_title = wrap_nowiki(slink:gsub("%s%(.+%)$", ""):gsub(",.+$", ""))
slink_flag = true
end
else
citeq_args.title = wrap_nowiki(title_display)
end
end
if slink_flag then
if slink == wrap_title then -- direct link
citeq_args.title = '[[' .. slink .. ']]'
else -- piped link
citeq_args.title = '[[' .. slink .. '|' .. wrap_title .. ']]'
end
end
end
else
citeq_args.title = wrap_nowiki(label, titleforced)
end
end
if slink_flag then
if slink == wrap_title then -- direct link
citeq_args.title = '[[' .. slink .. ']]'
else -- piped link
citeq_args.title = '[[' .. slink .. '|' .. wrap_title .. ']]'
end
end
Line 523 ⟶ 529:
 
-- change edition to ordinal if it's set and numeric
citeq_args.edition = citeq_args.edition and p.makeOrdinal(citeq_args.edition)
 
-- code to make a guess what template to use from the supplied parameters
-- (first draft for proof-of-concept)
if citeq_args.isbn then
template = template or "book"
elseif citeq_args.journal then
template = template or "journal"
elseif citeq_args.website then
template = template or "web"
end
 
Line 558 ⟶ 564:
else
citeq_args.id = id;
end
 
-- clean up any blank parameters
for k, v in pairs(citeq_args) do
if v == "" then citeq_args[k] = nil end
end
 
-- if |expand=<anything>, write a nowiki'd version to see what the {{citation}} template call looks like
if is_set(expand) then
local expand_args = { '"{{'" .. template }; -- init with citation template
if expand == "self" then
citeq_args.id = old_id; -- restore original |id= parameter
expand_args = {' "{{cite Q|'" .. qid }; -- expand to itself
end
for p, v in pairs (citeq_args) do -- spin through citeq_args and
table.insert (expand_args, p .. '=' .. v); -- add parameter name = value
end
-- make the nowiki'd string and done
return table.concat ({'"<code>'," frame:callParserFunction ('#tag:nowiki',.. table.concat (expand_args, ' |') .. '"}}'), '</code>'})"
end
 
Line 584 ⟶ 595:
end
 
function p.cite_q (frame)
return citeq
local args = {}
for k, v in pairs(frame:getParent().args) do
if v ~= "" then args[k] = v end
end
for k, v in pairs(frame.args) do
if v ~= "" then args[k] = v end
end
return p._cite_q(args)
end
 
return p