local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local mArguments -- initialize lazily
local data = mw.loadJsonData('Module:Cite web/data.json')
local p = {}
--- Get the formatted wikitext for the website part
---
--- @param uri table - URI object
--- @param args table - Template arguments
--- @return string
local function getWebsite(uri, args)
-- Check if there are any matches in Module:Cite web/data.json
for label, v in pairs(data.websites) do
for _, pattern in ipairs(v.pattern) do
if string.find( args['articleurl'], pattern, 1, true ) then
if v.page then
return string.format('[[%s|%s]]', v.page, label)
else
return string.format('[%s://%s %s]', uri.protocol, uri.host, label)
end
end
end
end
-- Fallback to template arguments if there are no match
if args['website'] then
return args['website']
end
-- Fallback to host name if none of the above
return string.format('[%s://%s %s]', uri.protocol, uri.host, uri.host)
end
--- Build a table of citation info from the template arguments
---
--- @param args table - Template arguments
--- @return table|nil
function p.buildCiteData(args)
local uri = mw.uri.new(args['articleurl'])
local cite = {
['order'] = {}
}
if args['author'] then
if args['dated'] then
cite['author'] = string.format('%s (%s)', args['author'], args['dated'])
else
cite['author'] = args['author']
end
table.insert(cite['order'], 'author')
end
-- Logic for Archive URL handling
local urlToLink = args['articleurl']
if args['archiveurl'] then
urlToLink = args['archiveurl']
end
-- Logic for Name/Label handling (archivename > articlename > url)
local label = args['archivename'] or args['articlename']
if label then
cite['article'] = string.format('[%s "%s"]', urlToLink, label)
else
cite['article'] = string.format('[%s %s]', urlToLink, urlToLink)
end
-- Append Archive Suffix if applicable
if args['archiveurl'] then
local originalLink = string.format('[%s original]', args['articleurl'])
local archiveSuffix = string.format(', archived from the %s', originalLink)
if args['archivedate'] then
archiveSuffix = archiveSuffix .. string.format(' on %s', args['archivedate'])
end
cite['article'] = cite['article'] .. archiveSuffix
end
table.insert(cite['order'], 'article')
cite['website'] = getWebsite(uri, args)
table.insert(cite['order'], 'website')
if args['accessed'] then
cite['accessed'] = string.format('Retrieved %s', args['accessed'])
table.insert(cite['order'], 'accessed')
end
mw.logObject(cite, string.format('[Cite web] Citation data of [%s]', args['articleurl']))
return cite
end
--Implements {{Cite web}} from the frame
function p.citeWeb(frame)
mArguments = require('Module:Arguments')
return p._citeWeb(mArguments.getArgs(frame), frame)
end
function p._citeWeb(args, frame)
checkType('_citeWeb', 1, args, 'table')
checkType('_citeWeb', 2, frame, 'table')
checkTypeForNamedArg('_citeWeb', 'articleurl', args.articleurl, 'string', false)
-- Type checks for archive parameters
checkTypeForNamedArg('_citeWeb', 'archiveurl', args.archiveurl, 'string', true)
checkTypeForNamedArg('_citeWeb', 'archivename', args.archivename, 'string', true)
checkTypeForNamedArg('_citeWeb', 'archivedate', args.archivedate, 'string', true)
local cite = {}
local citeData = p.buildCiteData(args)
for _, key in ipairs(citeData['order']) do
local spanHtml = mw.html.create('span')
spanHtml:addClass('cite-web-' .. key)
--spanHtml:attr('title', key)
spanHtml:wikitext(citeData[key])
table.insert(cite, tostring(spanHtml))
end
local wikitext = table.concat(cite, '. ')
local html = mw.html.create('cite'):addClass('citation cite-web')
html:wikitext(wikitext)
return frame:extensionTag {
name = 'templatestyles', args = { src = 'Module:Cite web/styles.css' }
} .. tostring(html)
end
return p