Skip to content

Instantly share code, notes, and snippets.

@ctrlcctrlv
Created July 16, 2020 22:08
Show Gist options
  • Select an option

  • Save ctrlcctrlv/29a181cc03445e307b927bf8f35496e3 to your computer and use it in GitHub Desktop.

Select an option

Save ctrlcctrlv/29a181cc03445e307b927bf8f35496e3 to your computer and use it in GitHub Desktop.
Module:Ws
local mHatnote = require('Module:Hatnote')
local getArgs = require('Module:Arguments').getArgs
local p = {}
local function image()
return '[[Image:wikisource-logo.svg|12px]]'
end
local function ps(ps)
if ps == 'no' then
return ''
else
return '.'
end
end
local function makeErrorCat(name)
return string.format(
'[[%s:%s]]',
mw.site.namespaces[14].name,
name
)
end
local function makeError(err)
return string.format(
'<strong class="error">Error: %s.</strong>',
err
)
end
function p.main(frame)
local args = getArgs(frame)
local link = args[1] or ''
if (link == '') then
return makeError(string.format("You must give a link%s", link))
.. makeErrorCat("Ws templates without links")
end
local prePipe, display
local errorCat = ''
if link:match('|') and link:match('^%[%[') and link:match('%]%]$') then
prePipe, display = link:match('^%[%[(.-)|(.*)%]%]$')
elseif link:match('^%[%[') and link:match('%]%]$') then
prePipe, display = link:match('^%[%[(.*)%]%]$'), nil
end
local flink
if prePipe then
local fldisplay = prePipe:gsub('^s:', '')
if fldisplay == display then
errorCat = errorCat .. makeErrorCat("Needlessly verbose ws templates")
end
if display == '' or display == nil then
flink = mHatnote._formatLink({link = prePipe, display = fldisplay})
end
end
if not flink then flink = link end -- happens when contains a normal string
return image() .. frame:expandTemplate{title = 'nbsp'} .. flink
.. ps(args['ps']) .. errorCat
end
return p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment