Module:Featured: Difference between revisions
From PC Gaming Shelter
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function getArgs(frame) | |||
return frame:getParent().args or {} | |||
end | |||
local function value(args, names, default) | |||
for _, name in ipairs(names) do | |||
local candidate = args[name] | |||
if candidate ~= nil then | |||
candidate = mw.text.trim(candidate) | |||
if candidate ~= '' then | |||
return candidate | |||
end | |||
end | |||
end | |||
return default | |||
end | |||
local function setFeaturedProperties(data) | |||
local properties = {} | |||
for property, propertyValue in pairs(data) do | |||
if propertyValue ~= nil and propertyValue ~= '' then | |||
properties[property] = propertyValue | |||
end | |||
end | |||
mw.smw.set(properties) | |||
end | |||
local function youtubeId(content) | |||
if not content:find('://', 1, true) then | |||
return content | |||
end | |||
return content:match('[?&]v=([^&]+)') | |||
or content:match('youtu%.be/([^?&/]+)') | |||
or content:match('youtube%.com/embed/([^?&/]+)') | |||
or '' | |||
end | |||
function p.video(frame) | function p.video(frame) | ||
local args | local args = getArgs(frame) | ||
local header = value(args, { 2, 'header' }, 'Navigate') | |||
local featuredTitle = value(args, { 3, 'title' }, '') | |||
local featuredTagline = value(args, { 4, 'tagline' }, '') | |||
local title = featuredTitle ~= '' and featuredTitle or 'Title' | |||
local tagline = featuredTagline ~= '' and featuredTagline or 'Tagline' | |||
local content = value(args, { 5, 'id', 'url' }, '') | |||
local description = value(args, { 6, 'description', 'text' }, '') | |||
local contentUrl = content:find('://', 1, true) and content or '' | |||
local externalLink = value(args, { 'external link', 'url' }, contentUrl) | |||
local videoId = value(args, { 'video id', 'id' }, youtubeId(content)) | |||
local featuredMedia = value(args, { 'featured media', 'media' }, '') | |||
local category = value(args, { 'featured category', 'category' }, 'Video of the Month') | |||
local video = frame:callParserFunction('#tag', { | |||
'embedvideo', | |||
service = 'youtube', | |||
autoResize = '1', | |||
content | |||
}) | |||
setFeaturedProperties({ | |||
['Featured date'] = value(args, { 'featured date', 'date' }, ''), | |||
['Has featured media type'] = 'Video', | |||
['Has featured media'] = featuredMedia, | |||
['Has external link'] = externalLink, | |||
['Has video ID'] = videoId, | |||
['Has featured title'] = featuredTitle, | |||
['Has featured tagline'] = featuredTagline, | |||
['Has featured description'] = description, | |||
['Has featured category'] = category, | |||
['Has attribution'] = value(args, { 'attribution' }, ''), | |||
['Has alt text'] = value(args, { 'alt text', 'alt' }, '') | |||
}) | }) | ||
if | local displayDescription = description | ||
if displayDescription ~= '' then | |||
displayDescription = '\n' .. displayDescription | |||
end | end | ||
local html = mw.html.create() | local html = mw.html.create() | ||
html:tag('div'):addClass('card bg-dark thumbwall mb-3') | html:tag('div'):addClass('card bg-dark thumbwall mb-3') | ||
:tag('div'):addClass('card-header order-0'):wikitext(header):done() | |||
:tag('div'):addClass('card-img'):wikitext(video):done() | |||
:tag('div'):addClass('card-body bg-dark flex-column') | |||
:tag('div'):addClass('card-title mb-0'):wikitext(title):done() | |||
:tag('div'):addClass('card-tagline lead'):wikitext(tagline):done() | |||
:tag('div'):addClass('card-text'):wikitext(displayDescription):done() | |||
:done() | |||
:done() | :done() | ||
| Line 36: | Line 99: | ||
function p.image(frame) | function p.image(frame) | ||
local args | local args = getArgs(frame) | ||
local header | |||
local | local header = value(args, { 2, 'header' }, 'Navigate') | ||
local | local featuredTitle = value(args, { 3, 'title' }, '') | ||
local content | local featuredTagline = value(args, { 4, 'tagline' }, '') | ||
local | local title = featuredTitle ~= '' and featuredTitle or 'Title' | ||
local image | local content = value(args, { 5, 'image' }, 'Shelter.png') | ||
local description = value(args, { 6, 'description', 'text' }, '') | |||
if | local altText = value(args, { 'alt text', 'alt' }, 'Featured image') | ||
local featuredMedia = value(args, { 'featured media', 'media' }, 'File:' .. content) | |||
local category = value(args, { 'featured category', 'category' }, 'Picture of the Month') | |||
local image = frame:preprocess( | |||
'[[File:{{PAGENAME:' .. content .. '}}|' .. altText .. ']]' | |||
) | |||
setFeaturedProperties({ | |||
['Featured date'] = value(args, { 'featured date', 'date' }, ''), | |||
['Has featured media type'] = 'Image', | |||
['Has featured media'] = featuredMedia, | |||
['Has external link'] = value(args, { 'external link', 'url' }, ''), | |||
['Has video ID'] = value(args, { 'video id' }, ''), | |||
['Has featured title'] = featuredTitle, | |||
['Has featured tagline'] = featuredTagline, | |||
['Has featured description'] = description, | |||
['Has featured category'] = category, | |||
['Has attribution'] = value(args, { 'attribution' }, ''), | |||
['Has alt text'] = altText | |||
}) | |||
local displayDescription = description | |||
if displayDescription ~= '' then | |||
displayDescription = '\n' .. displayDescription | |||
end | end | ||
local html = mw.html.create() | local html = mw.html.create() | ||
html:tag('div'):addClass('card bg-dark thumbwall mb-3') | html:tag('div'):addClass('card bg-dark thumbwall mb-3') | ||
:tag('div'):addClass('card-header order-0'):wikitext(header):done() | |||
:tag('div'):addClass('card-img'):wikitext(image):done() | |||
:tag('div'):addClass('card-body bg-dark flex-column') | |||
:tag('div'):addClass('card-title lead'):wikitext(title):done() | |||
:tag('div'):addClass('card-text'):wikitext(displayDescription):done() | |||
:done() | |||
:done() | :done() | ||
Revision as of 18:22, 6 June 2026
Documentation for this module may be created at Module:Featured/doc
local p = {}
local function getArgs(frame)
return frame:getParent().args or {}
end
local function value(args, names, default)
for _, name in ipairs(names) do
local candidate = args[name]
if candidate ~= nil then
candidate = mw.text.trim(candidate)
if candidate ~= '' then
return candidate
end
end
end
return default
end
local function setFeaturedProperties(data)
local properties = {}
for property, propertyValue in pairs(data) do
if propertyValue ~= nil and propertyValue ~= '' then
properties[property] = propertyValue
end
end
mw.smw.set(properties)
end
local function youtubeId(content)
if not content:find('://', 1, true) then
return content
end
return content:match('[?&]v=([^&]+)')
or content:match('youtu%.be/([^?&/]+)')
or content:match('youtube%.com/embed/([^?&/]+)')
or ''
end
function p.video(frame)
local args = getArgs(frame)
local header = value(args, { 2, 'header' }, 'Navigate')
local featuredTitle = value(args, { 3, 'title' }, '')
local featuredTagline = value(args, { 4, 'tagline' }, '')
local title = featuredTitle ~= '' and featuredTitle or 'Title'
local tagline = featuredTagline ~= '' and featuredTagline or 'Tagline'
local content = value(args, { 5, 'id', 'url' }, '')
local description = value(args, { 6, 'description', 'text' }, '')
local contentUrl = content:find('://', 1, true) and content or ''
local externalLink = value(args, { 'external link', 'url' }, contentUrl)
local videoId = value(args, { 'video id', 'id' }, youtubeId(content))
local featuredMedia = value(args, { 'featured media', 'media' }, '')
local category = value(args, { 'featured category', 'category' }, 'Video of the Month')
local video = frame:callParserFunction('#tag', {
'embedvideo',
service = 'youtube',
autoResize = '1',
content
})
setFeaturedProperties({
['Featured date'] = value(args, { 'featured date', 'date' }, ''),
['Has featured media type'] = 'Video',
['Has featured media'] = featuredMedia,
['Has external link'] = externalLink,
['Has video ID'] = videoId,
['Has featured title'] = featuredTitle,
['Has featured tagline'] = featuredTagline,
['Has featured description'] = description,
['Has featured category'] = category,
['Has attribution'] = value(args, { 'attribution' }, ''),
['Has alt text'] = value(args, { 'alt text', 'alt' }, '')
})
local displayDescription = description
if displayDescription ~= '' then
displayDescription = '\n' .. displayDescription
end
local html = mw.html.create()
html:tag('div'):addClass('card bg-dark thumbwall mb-3')
:tag('div'):addClass('card-header order-0'):wikitext(header):done()
:tag('div'):addClass('card-img'):wikitext(video):done()
:tag('div'):addClass('card-body bg-dark flex-column')
:tag('div'):addClass('card-title mb-0'):wikitext(title):done()
:tag('div'):addClass('card-tagline lead'):wikitext(tagline):done()
:tag('div'):addClass('card-text'):wikitext(displayDescription):done()
:done()
:done()
return html
end
function p.image(frame)
local args = getArgs(frame)
local header = value(args, { 2, 'header' }, 'Navigate')
local featuredTitle = value(args, { 3, 'title' }, '')
local featuredTagline = value(args, { 4, 'tagline' }, '')
local title = featuredTitle ~= '' and featuredTitle or 'Title'
local content = value(args, { 5, 'image' }, 'Shelter.png')
local description = value(args, { 6, 'description', 'text' }, '')
local altText = value(args, { 'alt text', 'alt' }, 'Featured image')
local featuredMedia = value(args, { 'featured media', 'media' }, 'File:' .. content)
local category = value(args, { 'featured category', 'category' }, 'Picture of the Month')
local image = frame:preprocess(
'[[File:{{PAGENAME:' .. content .. '}}|' .. altText .. ']]'
)
setFeaturedProperties({
['Featured date'] = value(args, { 'featured date', 'date' }, ''),
['Has featured media type'] = 'Image',
['Has featured media'] = featuredMedia,
['Has external link'] = value(args, { 'external link', 'url' }, ''),
['Has video ID'] = value(args, { 'video id' }, ''),
['Has featured title'] = featuredTitle,
['Has featured tagline'] = featuredTagline,
['Has featured description'] = description,
['Has featured category'] = category,
['Has attribution'] = value(args, { 'attribution' }, ''),
['Has alt text'] = altText
})
local displayDescription = description
if displayDescription ~= '' then
displayDescription = '\n' .. displayDescription
end
local html = mw.html.create()
html:tag('div'):addClass('card bg-dark thumbwall mb-3')
:tag('div'):addClass('card-header order-0'):wikitext(header):done()
:tag('div'):addClass('card-img'):wikitext(image):done()
:tag('div'):addClass('card-body bg-dark flex-column')
:tag('div'):addClass('card-title lead'):wikitext(title):done()
:tag('div'):addClass('card-text'):wikitext(displayDescription):done()
:done()
:done()
return html
end
return p
