Moduuli:Suositeltu sivu

Jedipediasta, vapaasta Tähtien sota-tietosanakirjasta tänään, 10. maaliskuuta 2025
Siirry navigaatioonSiirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Suositeltu sivu/ohje

local p = {}

local function getWeek()
	local lang = mw.getContentLanguage()
	local week = tonumber(lang:formatDate('W'))
	if week == 53 then
		if lang:formatDate('N') < 4 then
			week = 52
		else
			week = 1
		end
	end
	return week
end

-- Remove any links from the text, leaving
-- the displayed portion of the link only.
local function strip_links(text)
    -- First, convert links like this: [[foo|bar]] to this: bar
    text = text:gsub("%[%[[^%]\n]+%|([^%]\n]+)%]%]", "%1")
    -- Next, convert links like this: [[foo]] to this: foo
    return text:gsub("%[%[([^%]\n]+)%]%]", "%1")
end

local function formatPrevLink(list, week)
	local title = list.articleData[week].title
	local displayTitle = list.articleData[week].displayTitle
	local italics = list.articleData[week].italics
	
	return string.format(
		'%s[[%s]]%s',
		italics and "''" or '',
		displayTitle and title .. '|' .. displayTitle or title,
		italics and "''" or ''
	)
end

local function pick_article()
	local list = require('Moduuli:Suositeltu sivu/data')
	local week = getWeek()
	local pagename = list.articleData[week].title
	local file_string = ""
	local main_link = ""
	local pagetext = mw.title.new(pagename):getContent()
	local x = pagetext:gsub("%=%=(.*)", "")
	local prev1, prev2, prev3
	if week == 1 then
		prev1 = week + 51
	else
		prev1 = week - 1
	end
	if week <= 2 then
		prev2 = week + 50
	else
		prev2 = week - 2
	end
	if week <= 3 then
		prev3 = week + 49
	else
		prev3 = week - 3
	end
 
-- Generate image
	local filename = x:match(".*%[%[Tiedosto:(.*)%]%].*")
	if filename then
		filename = filename:gsub("%|.*", "")
		filename = filename:gsub("%]%].*", "")
		file_string = string.format("[[Tiedosto:%s|left|200px]]\n", filename)
	end

-- Extract the bolded segment
	local bolded = ""
	local front_italics = false
	local end_italics = false
	local intro = x:gsub(".*()%}%}\n(.*)", "%2")
	
	local s1, e1 = intro:find("'''")
	if intro:sub(e1 + 1, e1 + 2) == "''" then
		e1 = e1 + 2
		front_italics = true
	end
	local s2, e2 = intro:find("'''", e1 + 1)
	if intro:sub(e2 + 1, e2 + 2) == "''" then
		s2 = s2 + 2
		end_italics = true
	end
	
	if front_italics and end_italics then
		bolded = strip_links(intro:sub(e1 + 1, s2 - 3))
		main_link = string.format("[[%s|%s]]", pagename, bolded)
		s2 = s2 - 2
	elseif front_italics then
		bolded = strip_links(intro:sub(e1 - 1, s2 - 1))
		main_link = string.format("[[%s|%s]]", pagename, bolded)
		e1 = e1 - 2
	elseif end_italics then
		bolded = strip_links(intro:sub(e1 + 1, s2 - 1))
		main_link = string.format("[[%s|%s]]", pagename, bolded)
	else
		bolded = strip_links(intro:sub(e1 + 1, s2 - 1))
		main_link = string.format("[[%s|%s]]", pagename, bolded)
	end

	intro = intro:sub(0, e1) .. main_link .. intro:sub(s2)
	
-- Substitute templates, add {{Rm}}, and remove any images
	intro = intro:gsub("\{\{'s\}\}", "<span style=\"padding-left: 0.1em;\">'</span>s")
	intro = intro:gsub("\n\n$", string.format(
		"\n\n\nÄskettäin etusivulla olleet sivut: %s &mdash; %s &mdash; %s",
		formatPrevLink(list, prev1),
		formatPrevLink(list, prev2),
		formatPrevLink(list, prev3)
		))
	if x:find("|image=") then
		intro = intro:gsub("%[%[Tiedosto:(.*)%]%]", "")
	end

	return file_string .. intro
end

function p.getContinuity(frame)
	local list = require('Moduuli:Suositeltu sivu/data')
	local week = getWeek()
	
	local continuity = list.articleData[week].continuity
	if continuity == 'canon' then
		return '[[Tiedosto:Eras-canon-edit.png|100px|link=Kaanon]]'
	elseif continuity == 'legends' then
		return '[[Tiedosto:LegendsBanner.png|100px|link=Star Wars Legends]]'
	end
end

function p.printList(frame)
	local list = require('Moduuli:Suositeltu sivu/data')
	local result = ''
	local w = 1
	while w <= 52 do
		result = result .. string.format('*Viikko %s: [[%s]]\n', w, list.articleData[w].title)
		w = w + 1
	end
	return result
end

function p._main(args)
	return pick_article()
end

function p.main(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		v = v:match('^%s*(.-)%s*$') -- trim whitespace
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end
 
return p