Module:Statement
Documentation for this module may be created at Module:Statement/doc
local get_label = require('Module:Wikidata label')._getLabel
local getLemmaById = require('Module:Lexeme').getLemmaById
local function format_id(id)
return get_label(id, nil, 'wikidata', nil, 1)
end
local p = {}
local function format_thing (thing)
if thing then
if thing:match('^[QP]?%d+$') then
return format_id(thing)
elseif thing:match('^L?%d+$') then
return '[[Lexeme:' .. thing .. '|' .. getLemmaById(thing) .. ' <small>(' .. thing .. ')</small>]]'
end
end
return thing
end
local function format_predicate(pred)
if pred:match('^[P]%d+$') then
return format_id(pred)
elseif pred:match('^%d+$') then
return format_id('P' .. pred)
end
return pred
end
local function missing(i)
return '{{{' .. i .. '}}}'
end
-- debug with e.g. =p.main(nil, {'you', 'P2283', 'Q207316'})
function p.main (frame, debugArgs)
local args = debugArgs or frame:getParent().args
-- 1. parse arguments
-- (We're not using table.remove because that doesn't work with MediaWiki's frame object)
local subject = args[1]
local predicate = args[2] or missing(2)
local object = args[3] or missing(3)
local qualifiers = {}
local i = 4
-- (We're not using a for loop because #args doesn't work with MediaWiki's frame object)
while args[i] do
table.insert(qualifiers, {args[i], args[i+1]})
i = i + 2
end
-- 2. build HTML
local stmt = mw.html.create('span'):attr('class', 'statement')
if subject ~= '' then
stmt:tag('b'):attr('class', 'subject'):wikitext(format_thing(subject))
end
stmt:tag('span'):attr('class', 'predicate'):wikitext(format_predicate(predicate))
stmt:tag('span'):attr('class', 'object'):wikitext(format_thing(object))
for i, qual in pairs(qualifiers) do
stmt:wikitext('<wbr>')
local qual_span = stmt:tag('span'):attr('class', 'qualifier')
qual_span:tag('span'):attr('class', 'predicate'):wikitext(format_predicate(qual[1]))
qual_span:tag('span'):attr('class', 'object'):wikitext(format_thing(qual[2]) or missing(3 + i*2))
end
return tostring(stmt)
end
return p