PC Gaming Shelter
An archive dedicated to preserving PC Gaming history and more

Module:Resource: Difference between revisions

From PC Gaming Shelter
No edit summary
No edit summary
 
(79 intermediate revisions by the same user not shown)
Line 3: Line 3:
local p = {}
local p = {}


local function clean(value)
local function splitBy(str, sep)
if value == nil then
local t = {}
for part in str:gmatch("[^" .. sep .. "]+") do
table.insert(t, mw.text.trim(part))
end
return t
end
 
local function isValidUrl(url)
return utils.clean(url) and mw.ustring.match(url, '^https?://[^%s]+$') ~= nil
end
 
local function externalHost(url)
url = utils.clean(url)
if not url then
return nil
return nil
end
end


value = mw.text.trim(tostring(value))
local host = mw.ustring.match(url, '^https?://([^/%?#]+)')
return value ~= '' and value or nil
if not host then
return nil
end
local cleanedHost = mw.ustring.gsub(host, '^www%.', '')
return cleanedHost
end
end


local function isTruthy(value)
local function urlDecode(value)
value = clean(value)
value = utils.clean(value)
if not value then
if not value then
return false
return nil
end
end


value = mw.ustring.lower(value)
value = string.gsub(value, '+', ' ')
return value == '1' or value == 'true' or value == 'yes' or value == 'y' or value == 'on'
value = string.gsub(value, '%%(%x%x)', function(hex)
return string.char(tonumber(hex, 16))
end)
 
return value
end
end


local function splitValues(value)
local function queryParam(url, name)
value = clean(value)
url = utils.clean(url)
if not value then
if not url then
return {}
return nil
end
end


local values = {}
local query = string.match(url, '%?([^#]+)')
for part in string.gmatch(value, '([^,]+)') do
if not query then
local item = clean(part)
return nil
if item then
table.insert(values, item)
end
end
end


if #values == 0 then
for key, value in string.gmatch(query, '([^&=]+)=([^&]*)') do
table.insert(values, value)
if urlDecode(key) == name then
return urlDecode(value)
end
end
end


return values
return nil
end
end


local function firstValue(value)
local function pathMatch(url, pattern)
local values = splitValues(value)
url = utils.clean(url)
return values[1]
if not url then
return nil
end
 
local path = string.match(url, '^https?://[^/]+([^%?#]*)') or ''
local value = string.match(path, pattern)
return urlDecode(value)
end
end


local function scrapeVideoId(url)
local function scrapeVideoId(url)
url = clean(url)
url = utils.clean(url)
if not isValidUrl(url) then
if not isValidUrl(url) then
return nil
return nil
Line 148: Line 175:


local function propertiesPlan()
local function propertiesPlan()
-- ['Form field name'] = { 'Semantic property', 'delimiter' }
-- ['Form field name'] = { 'Semantic property', 'delimiter' }
local propMap = {
local propMap = {
['Image']                  = {'Image', 'file'},
['Has URL']                = {'Has URL' },
['Has URL']                = {'Has URL' },
['Has short description']  = {'Has short description' },
['Has short description']  = {'Has short description' },
['Related game']          = {'Related game', ',' },
['Related game']          = {'Related game' },
['Has resource type']      = {'Has resource type' },
['Has resource type']      = {'Has resource type' },
['Has content type']      = {'Has content type', ',' },
['Has content type']      = {'Has content type', ',' },
Line 165: Line 193:
end
end


local function isValidUrl(url)
local function infoboxSectionPlan()
return clean(url) and mw.ustring.match(url, '^https?://[^%s]+$') ~= nil
local sectionMap = {
end
['Cover image'] = {
 
heading = '',
local function externalHost(url)
template = 'Infobox/Resource Cover',
url = clean(url)
order = '1',
if not url then
rows = {
return nil
'Image',
end
'Related game',
 
'Has resource type',
local host = mw.ustring.match(url, '^https?://([^/%?#]+)')
'Has short description'
if not host then
}
return nil
},
end
['Resource Meta'] = {
 
heading = 'Resource Info',
return mw.ustring.gsub(host, '^www%.', '')
template = 'Infobox/Resource Info',
order = '2',
rows = {
'Has short description',
'Related game',
'Has resource type',
'Has content type',
'Has spoilers',
'Has date',
'Created by',
'Has language',
'Has subtitles',
'Has subtitles language',
}
}
}
return sectionMap
end
end


local function embed(scraped)
function p.infobox(frame)
if not scraped then
local template    = frame:getParent().args
return
local url          = template['Has URL']
end
local title        = mw.title.getCurrentTitle().text
local resourceName = title or ''


local service = clean(scraped.service)
local scraped = url
local id = clean(scraped.id)
local service, id = '', ''
if not service or not id then
if url and template['Has resource type'] == 'Video' then
return
scraped = scrapeVideoId(url)
service = utils.clean(scraped.service)
id = utils.clean(scraped.id)
end
end
 
mw.smw.set({
mw.smw.set({
['Has video provider'] = service,
['Has video provider'] = service,
['Has video id'] = id
['Has video id']       = id,
['Has resource name']  = resourceName
})
})
end


local function cssList(values, linked)
local category  = 'Resources'
if not values or #values == 0 then
local properties = propertiesPlan()
return nil
local sections  = infoboxSectionPlan()
end
local infobox    = utils.createInfobox(frame, category, properties, sections)
 
return infobox
local items = {}
for _, value in ipairs(values) do
if linked then
table.insert(items, string.format('[[%s]]', value))
else
table.insert(items, value)
end
end
 
return table.concat(items, ', ')
end
end


local function addProperty(props, name, value, delimiter)
local function makeBadge(val, style)
value = clean(value)
if not val and val ~= '' then
if not value then
return
return
end
end
local style = style or 'secondary'


if delimiter then
local html = mw.html.create()
props[name] = splitValues(value)
html:tag('span'):addClass('mr-2 badge badge-' .. style):wikitext(val)
else
return tostring(html)
props[name] = value
end
end
end


local function setProperties(args)
local function pageExists(page)
local props = {}
 
local page = mw.smw.ask {
addProperty(props, 'Has URL', args['Has URL'])
'[[' .. page .. ']][[Modification date::+]]',
addProperty(props, 'Has short description', args['Has short description'])
} or {}
addProperty(props, 'Related game', args['Related game'], ',')
local check = #page > 0
addProperty(props, 'Has resource type', args['Has resource type'], ',')
addProperty(props, 'Has content type', args['Has content type'], ',')
addProperty(props, 'Has date', args['Has date'])
addProperty(props, 'Created by', args['Created by'])
addProperty(props, 'Has language', args['Has language'], ',')
addProperty(props, 'Has subtitles language', args['Has subtitles language'], ',')
 
if isTruthy(args['Has spoilers']) then
props['Has spoilers'] = 'Yes'
end
 
if isTruthy(args['Has subtitles']) then
props['Has subtitles'] = 'Yes'
end


if next(props) then
return check
mw.smw.set(props)
end
end
end


local function makeBadge(parent, text, badgeClass)
function p.inline(frame)
if not clean(text) then
local args        = frame:getParent().args
local url        = args['Has URL']
local content    = args['Has content type'] or ''
local subtitles  = args['Has subtitles'] or ''
local sublang    = args['Has subtitles language'] or ''
local spoilers    = args['Has spoilers'] or ''
local restype    = args['Has resource type'] or ''
local lang        = args['Has language'] or ''
local game        = args['Related game'] or ''
    local description = args['Has short description'] or nil
   
if not isValidUrl(url) then
return
return
end
end


parent
if content ~= '' then
:tag('span')
content = splitBy(content, ',')
:addClass('badge')
for i, ct in ipairs(content) do
:addClass(badgeClass or 'badge-secondary')
content[i] = makeBadge(ct)
:addClass('mr-1 mb-1')
end
:wikitext(text)
end
end
 
local subBadges = ''
local function addRow(parent, label, value)
if subtitles == "Yes" and sublang ~= '' then
value = clean(value)
sublang = splitBy(sublang, ',')
if not value then
for i, st in ipairs(sublang) do
return
sublang[i] = makeBadge(st)
end
subBadges = "<span class='nowrap'>" .. makeBadge('CC', ' border border-secondary') .. table.concat(sublang, '') .. "</span>"
end
end
 
local row = parent:tag('div'):addClass('infobox-section d-flex flex-column flex-sm-row border-0 rounded-0 px-3 py-1')
local spoilerBadge = ''
row:tag('div'):addClass('infobox-label'):wikitext(label)
if spoilers == "Yes" then
row:tag('div'):addClass('infobox-value'):wikitext(value)
spoilerBadge = makeBadge('SPOILERS', 'danger')
end
 
local function renderUrl(args)
local url = clean(args['Has URL'])
if not isValidUrl(url) then
return nil
end
end
 
local label = externalHost(url) or url
local resBadge = ''
return string.format('[%s %s]', url, label)
if restype ~= '' then
end
resBadge = makeBadge(restype)
 
local function renderResource(frame, args)
local url = clean(args['Has URL'])
local title = mw.title.getCurrentTitle().text
local host = externalHost(url)
local resourceType = firstValue(args['Has resource type'])
 
local root = mw.html.create()
local card = root
:tag('div')
:addClass('infobox d-flex flex-column card shadow-none rounded-0 bg-transparent mb-4 float-sm-right')
 
local header = card
:tag('div')
:addClass('infobox-block border-bottom pb-2 mb-2')
 
local titleLine = header
:tag('div')
:addClass('d-flex flex-column flex-sm-row justify-content-between align-items-sm-start px-3 pt-3')
 
local titleBox = titleLine:tag('div')
if host then
titleBox:tag('div'):addClass('text-muted small mb-2'):wikitext(host)
end
end
 
local badges = titleLine:tag('div'):addClass('text-sm-right mt-2 mt-sm-0')
local contentBadge = ''
makeBadge(badges, resourceType, 'badge-info')
if content ~= '' and type(content) == 'table' then
makeBadge(badges, contentType, 'badge-secondary')
contentBadge = table.concat(content, '')
 
if isTruthy(args['Has spoilers']) then
makeBadge(badges, 'Spoilers', 'badge-warning')
end
end
 
if isTruthy(args['Has subtitles']) then
local langBadge = ''
makeBadge(badges, 'Subtitles', 'badge-success')
if lang ~= '' then
langBadge = makeBadge(lang, 'light')
end
end
 
if isValidUrl(url) then
local gameBadge = ''
header
if game and pageExists(game) == true then
:tag('div')
gameBadge = string.format('[[%s|%s]]', game, makeBadge('Game', ' border border-secondary'))
:addClass('px-3 pb-3')
:tag('span')
:addClass('btn btn-success font-weight-bold')
:wikitext(renderUrl(args))
else
header
:tag('div')
:addClass('alert alert-warning mx-3 mb-3')
:wikitext('No valid URL has been provided.')
end
end
 
local details = card:tag('div'):addClass('infobox-block')
local html = mw.html.create()
 
local resource = html:tag('div'):addClass('resource-item')
addRow(details, 'Game', cssList(splitValues(args['Related game']), true))
resource:tag('div')
addRow(details, 'Media type', cssList(splitValues(args['Has resource type']), true))
:addClass('badges d-flex mb-2 plainlinks')
addRow(details, 'Content type', cssList(splitValues(args['Has content type']), true))
:wikitext(
addRow(details, 'Date', clean(args['Has date']))
string.format('[%s %s]', url, makeBadge('► Open', 'primary')) ..
addRow(details, 'Created by', clean(args['Created by']))
string.format('<span>%s%s%s%s</span>'
addRow(details, 'Language', cssList(splitValues(args['Has language']), true))
, resBadge
 
, contentBadge
if isTruthy(args['Has subtitles']) then
, spoilerBadge
addRow(details, 'Subtitles', cssList(splitValues(args['Has subtitles language']), true) or 'Yes')
, gameBadge
) ..
string.format('<span class="ml-sm-auto">%s%s</span>'
, langBadge
, subBadges
)
)
if description then
resource:tag('div'):wikitext(description)
end
end
 
resource:tag('div')
return root
:tag('small'):addClass('text-muted'):wikitext(externalHost(url))
:wikitext('[[Category:Resources]]')
end
return html
 
function p.main(frame)
local parent = frame:getParent()
local args = parent and parent.args or frame.args
 
setProperties(args)
return tostring(renderResource(frame, args))
end
end


return p
return p

Latest revision as of 08:59, 10 July 2026

Documentation for this module may be created at Module:Resource/doc

local utils = require( 'Module:Utils' )

local p = {}

local function splitBy(str, sep)
	local t = {}
	for part in str:gmatch("[^" .. sep .. "]+") do
		table.insert(t, mw.text.trim(part))
	end
	return t
end

local function isValidUrl(url)
	return utils.clean(url) and mw.ustring.match(url, '^https?://[^%s]+$') ~= nil
end

local function externalHost(url)
	url = utils.clean(url)
	if not url then
		return nil
	end

	local host = mw.ustring.match(url, '^https?://([^/%?#]+)')
	if not host then
		return nil
	end
	
	local cleanedHost = mw.ustring.gsub(host, '^www%.', '')
	return cleanedHost
end

local function urlDecode(value)
	value = utils.clean(value)
	if not value then
		return nil
	end

	value = string.gsub(value, '+', ' ')
	value = string.gsub(value, '%%(%x%x)', function(hex)
		return string.char(tonumber(hex, 16))
	end)

	return value
end

local function queryParam(url, name)
	url = utils.clean(url)
	if not url then
		return nil
	end

	local query = string.match(url, '%?([^#]+)')
	if not query then
		return nil
	end

	for key, value in string.gmatch(query, '([^&=]+)=([^&]*)') do
		if urlDecode(key) == name then
			return urlDecode(value)
		end
	end

	return nil
end

local function pathMatch(url, pattern)
	url = utils.clean(url)
	if not url then
		return nil
	end

	local path = string.match(url, '^https?://[^/]+([^%?#]*)') or ''
	local value = string.match(path, pattern)
	return urlDecode(value)
end

local function scrapeVideoId(url)
	url = utils.clean(url)
	if not isValidUrl(url) then
		return nil
	end

	local host = mw.ustring.lower(externalHost(url) or '')
	local id

	if host == 'archive.org' then
		id = pathMatch(url, '^/details/([^/]+)') or pathMatch(url, '^/embed/([^/]+)')
		return id and { service = 'archiveorg', id = id } or nil
	end

	if host == 'player.bilibili.com' then
		id = queryParam(url, 'bvid') or queryParam(url, 'aid')
		return id and { service = 'bilibili', id = id } or nil
	end

	if host == 'bilibili.com' or host == 'm.bilibili.com' then
		id = pathMatch(url, '^/video/([^/]+)')
		return id and { service = 'bilibili', id = id } or nil
	end

	if host == 'play-tv.kakao.com' then
		id = pathMatch(url, '^/embed/player/cliplink/([^/]+)')
		return id and { service = 'kakaotv', id = id } or nil
	end

	if host == 'tv.kakao.com' then
		id = pathMatch(url, '^/channel/[^/]+/cliplink/([^/]+)') or pathMatch(url, '^/v/([^/]+)')
		return id and { service = 'kakaotv', id = id } or nil
	end

	if host == 'tv.naver.com' then
		id = pathMatch(url, '^/embed/([^/]+)') or pathMatch(url, '^/v/([^/]+)')
		return id and { service = 'navertv', id = id } or nil
	end

	if host == 'embed.nicovideo.jp' or host == 'nicovideo.jp' or host == 'www.nicovideo.jp' then
		id = pathMatch(url, '^/watch/([^/]+)')
		return id and { service = 'niconico', id = id } or nil
	end

	if host == 'soundcloud.com' then
		return { service = 'soundcloud', id = url }
	end

	if host == 'open.spotify.com' then
		local spotifyType = pathMatch(url, '^/(album)/[^/]+') or pathMatch(url, '^/(artist)/[^/]+') or pathMatch(url, '^/(track)/[^/]+')
		id = pathMatch(url, '^/[^/]+/([^/]+)')
		if spotifyType and id then
			return { service = 'spotify' .. spotifyType, id = id }
		end
	end

	if host == 'twitch.tv' or host == 'www.twitch.tv' then
		id = pathMatch(url, '^/videos/([^/]+)')
		if id then
			return { service = 'twitchvod', id = id }
		end

		id = pathMatch(url, '^/[^/]+/clip/([^/]+)')
		if id then
			return { service = 'twitchclip', id = id }
		end

		id = pathMatch(url, '^/([^/]+)')
		return id and { service = 'twitch', id = id } or nil
	end

	if host == 'clips.twitch.tv' then
		id = pathMatch(url, '^/([^/]+)')
		return id and { service = 'twitchclip', id = id } or nil
	end

	if host == 'vimeo.com' or host == 'www.vimeo.com' or host == 'player.vimeo.com' then
		id = pathMatch(url, '^/video/([^/]+)') or pathMatch(url, '^/([^/]+)')
		return id and { service = 'vimeo', id = id } or nil
	end

	if host == 'youtube.com' or host == 'www.youtube.com' or host == 'm.youtube.com' or host == 'music.youtube.com' then
		id = queryParam(url, 'v') or pathMatch(url, '^/embed/([^/]+)') or pathMatch(url, '^/shorts/([^/]+)') or pathMatch(url, '^/live/([^/]+)')
		if id then
			return { service = 'youtube', id = id }
		end

		id = queryParam(url, 'list')
		return id and { service = 'youtubeplaylist', id = id } or nil
	end

	if host == 'youtu.be' then
		id = pathMatch(url, '^/([^/]+)')
		return id and { service = 'youtube', id = id } or nil
	end

	return nil
end

local function propertiesPlan()
-- ['Form field name'] = { 'Semantic property', 'delimiter' }
	local propMap = {
		['Image']                  = {'Image', 'file'},
		['Has URL']                = {'Has URL' },
		['Has short description']  = {'Has short description' },
		['Related game']           = {'Related game' },
		['Has resource type']      = {'Has resource type' },
		['Has content type']       = {'Has content type', ',' },
		['Has date']               = {'Has date' },
		['Created by']             = {'Created by' },
		['Has language']           = {'Has language', ',' },
		['Has spoilers']           = {'Has spoilers', },
		['Has subtitles']          = {'Has subtitles' },
		['Has subtitles language'] = {'Has subtitles language', ',' }
	}
	return propMap
end

local function infoboxSectionPlan()
	local sectionMap = {
		['Cover image'] = {
			heading = '',
			template = 'Infobox/Resource Cover',
			order = '1',
			rows = {
				'Image',
				'Related game',
				'Has resource type',
				'Has short description'
			}
		},
		['Resource Meta'] = {
			heading = 'Resource Info',
			template = 'Infobox/Resource Info',
			order = '2',
			rows = {
				'Has short description',
				'Related game',
				'Has resource type',
				'Has content type',
				'Has spoilers',
				'Has date',
				'Created by',
				'Has language',
				'Has subtitles',
				'Has subtitles language',
			}
		}
	}
	return sectionMap
end

function p.infobox(frame)
	local template     = frame:getParent().args
	local url          = template['Has URL']
	local title        = mw.title.getCurrentTitle().text
	local resourceName = title or ''

	local scraped = url
	local service, id = '', ''
	if url and template['Has resource type'] == 'Video' then
		scraped = scrapeVideoId(url)
		service = utils.clean(scraped.service)
		id = utils.clean(scraped.id)
	end
	
	mw.smw.set({
		['Has video provider'] = service,
		['Has video id']       = id,
		['Has resource name']  = resourceName
	})

	local category   = 'Resources'
	local properties = propertiesPlan()
	local sections   = infoboxSectionPlan()
	local infobox    = utils.createInfobox(frame, category, properties, sections)
	return infobox
end

local function makeBadge(val, style)
	if not val and val ~= '' then
		return
	end
	local style = style or 'secondary'

	local html = mw.html.create()
	html:tag('span'):addClass('mr-2 badge badge-' .. style):wikitext(val)
	return tostring(html)
end

local function pageExists(page)
	
	local page = mw.smw.ask {
		'[[' .. page .. ']][[Modification date::+]]',
	} or {}
	local check = #page > 0

	return check
end

function p.inline(frame)
	local args        = frame:getParent().args
	local url         = args['Has URL']
	local content     = args['Has content type'] or ''
	local subtitles   = args['Has subtitles'] or ''
	local sublang     = args['Has subtitles language'] or ''
	local spoilers    = args['Has spoilers'] or ''
	local restype     = args['Has resource type'] or ''
	local lang        = args['Has language'] or ''
	local game        = args['Related game'] or ''
    local description = args['Has short description'] or nil
    
	if not isValidUrl(url) then
		return
	end

	if content ~= '' then
		content = splitBy(content, ',')
		for i, ct in ipairs(content) do
			content[i] = makeBadge(ct)
		end
	end
	
	local subBadges = ''
	if subtitles == "Yes" and sublang ~= '' then
		sublang = splitBy(sublang, ',')
		for i, st in ipairs(sublang) do
			sublang[i] = makeBadge(st)
		end
		subBadges = "<span class='nowrap'>" .. makeBadge('CC', ' border border-secondary') .. table.concat(sublang, '') .. "</span>"
	end
	
	local spoilerBadge = ''
	if spoilers == "Yes" then
		spoilerBadge = makeBadge('SPOILERS', 'danger')
	end
	
	local resBadge = ''
	if restype ~= '' then
		resBadge = makeBadge(restype)
	end
		
	local contentBadge = ''
	if content ~= '' and type(content) == 'table' then
		contentBadge = table.concat(content, '')
	end
	
	local langBadge = ''
	if lang ~= '' then
		langBadge = makeBadge(lang, 'light')
	end
	
	local gameBadge = ''
	if game and pageExists(game) == true then
		gameBadge = string.format('[[%s|%s]]', game, makeBadge('Game', ' border border-secondary'))
	end
	
	local html = mw.html.create()
	local resource = html:tag('div'):addClass('resource-item')
	resource:tag('div')
	:addClass('badges d-flex mb-2 plainlinks')
	:wikitext(
		string.format('[%s %s]', url, makeBadge('► Open', 'primary')) ..
		string.format('<span>%s%s%s%s</span>'
			, resBadge
			, contentBadge
			, spoilerBadge
			, gameBadge
		) ..
		string.format('<span class="ml-sm-auto">%s%s</span>'
			, langBadge
			, subBadges
		)
	)
	if description then
		resource:tag('div'):wikitext(description)
	end
	resource:tag('div')
	:tag('small'):addClass('text-muted'):wikitext(externalHost(url))
	
	return html
end

return p