Module:RandomPortlet
ትምሳል
Documentation for this module may be created at Module:RandomPortlet/doc
local p = {}
function p.displayRandomArticles(frame)
-- List of categories with articles and unique background colors
local categories = {
["አልአሚን ዐብደለጢፍ"] = {
articles = {
"ሕላይ/አልአሚን ዐብደለጢፍ/አልአሚን፡ ሰር-ዘመ ንእት ፈን/11ይ ክፈል",
"ሕላይ/አልአሚን ዐብደለጢፍ/አልአሚን፡ ሰር-ዘመ ን እት ፈን/2ይ ክፈል",
"ሕላይ/አልአሚን ዐብደለጢፍ/አልአሚን፡ ሰር-ዘመ ን እት ፈን/3ይ ክፈል"
},
color = "#f4a261"
},
["ዓፍየት"] = {
articles = {
"ሐሺሽ እት ሸባብ ወደውለት ለልአጀርየ መደረት",
"መንፈዐት ሐበት-አሰውዳ",
"ወለፍ ስጃር ወትንባክ"
},
color = "#2a9d8f"
},
["ስያሰት"] = {
articles = {
"መጅልስ አምን ወለዛልም ቀራራቱ ድድ ኤረትርየ/1ይ ክፈል",
"መጅልስ አምን ወለዛልም ቀራራቱ ድድ ኤረትርየ/2ይ ክፈል",
"መጅልስ አምን ወለዛልም ቀራራቱ ድድ ኤረትርየ/3ይ ክፈል"
},
color = "#2a9d8f"
}
,
["ርያደት"] = {
articles = {
"ትልህያታት ኦለምፒክ"
},
color = "#2a9d8f"
}
}
local timestamp = frame:preprocess("{{#time: U}}")
math.randomseed(tonumber(timestamp) + os.time())
local result = "<div style='display: flex; flex-wrap: wrap; gap: 20px;'>"
for categoryName, data in pairs(categories) do
local articles = data.articles
local bgColor = data.color
if #articles > 0 then
local randomArticlePath = articles[math.random(#articles)]
local articleTitleObj = mw.title.new(randomArticlePath)
if articleTitleObj and articleTitleObj.exists then
local content = articleTitleObj:getContent() or "Content unavailable"
local displayTitle = mw.text.encode(articleTitleObj.subpageText)
local articleUrl = articleTitleObj:fullUrl()
-- Check and log the URL for debugging
if articleUrl and articleUrl ~= "" then
mw.log("Generated URL: " .. articleUrl)
else
mw.log("URL not generated for article: " .. (displayTitle or "Unknown"))
end
-- Extract the first 25 lines for preview
local previewContent = ""
local lineCount = 0
for line in content:gmatch("[^\r\n]+") do
previewContent = previewContent .. line .. "<br>"
lineCount = lineCount + 1
if lineCount >= 25 then
break
end
end
-- Append the HTML for the portlet content
result = result .. string.format(
"<div style='flex: 0 0 40%%; box-sizing: border-box; padding: 10px; background-color: #ffffff; border-radius: 5px; max-height: 50vh; overflow: hidden;'>"
.. "<div style='background-color: %s; width: 100%%; display: block; padding: 5px 0;'><h2 style='margin: 0; color: white; text-align: center;'>%s</h2></div>"
.. "<h3 style='color: %s; font-weight: bold;'>%s</h3><div>%s</div>"
.. "<a href='%s' style='display: block; margin-top: 10px; color: %s; text-align: right;'>Read More</a></div>",
bgColor, categoryName, bgColor, displayTitle, previewContent, mw.text.encode(articleUrl), bgColor
)
else
result = result .. string.format(
"<div style='flex: 0 0 40%%; box-sizing: border-box; padding: 10px; background-color: %s; border-radius: 5px;'>"
.. "<h2 style='margin: 0; color: white;'>%s</h2>"
.. "<p>Article not found.</p></div>",
bgColor, categoryName
)
end
else
result = result .. string.format(
"<div style='flex: 0 0 40%%; box-sizing: border-box; padding: 10px; background-color: %s; border-radius: 5px;'>"
.. "<h2 style='margin: 0; color: white;'>%s</h2>"
.. "<p>No articles available.</p></div>",
bgColor, categoryName
)
end
end
result = result .. "</div>"
return result
end
return p