Module:RedditUtil
From Rest of What I Know
Documentation for this module may be created at Module:RedditUtil/doc
local p = {}
local function trim(s)
if type(s) ~= 'string' then return nil end
return (s:gsub('^%s+', ''):gsub('%s+$', ''))
end
local function parse_from_url(url)
if type(url) ~= 'string' then return {} end
local u = url
-- normalize common host variants
-- supports old.reddit.com / www.reddit.com / reddit.com
local sub, id = u:match('^https?://[^/]*reddit%.com/r/([A-Za-z0-9_]+)/comments/([A-Za-z0-9]+)/')
if sub and id then
return {subreddit = sub, id = id}
end
-- also handle .../comments/<id> (rare, without subreddit in path)
id = u:match('^https?://[^/]*reddit%.com/comments/([A-Za-z0-9]+)/')
if id then
return {id = id}
end
-- shortlink: https://redd.it/<id>
id = u:match('^https?://[^/]*redd%.it/([A-Za-z0-9]+)')
if id then
return {id = id}
end
return {}
end
function p.render(frame)
local args = frame:getParent() and frame:getParent().args or frame.args
local url = trim(args.url)
local author = trim(args.author)
local authorURL = trim(args.author_url)
local title = trim(args.title)
local text = trim(args.text)
local dateHuman = trim(args.date)
local dateISO = trim(args.datetime) or dateHuman
local score = trim(args.score)
local comments = trim(args.comments)
local parsed = parse_from_url(url or '')
local subreddit = trim(args.subreddit) or parsed.subreddit
local id = parsed.id
local h = mw.html.create('div')
:addClass('mw-reddit')
:cssText('border:1px solid #e2e8f0;border-radius:8px;padding:12px 14px;margin:8px 0;background:#fff;')
-- Header line: “Reddit • r/subreddit”
local header = h:tag('div'):addClass('mw-reddit-header')
:cssText('font-size:0.9em;color:#475569;margin-bottom:6px;display:flex;gap:8px;align-items:center;')
header:tag('span'):wikitext('Reddit')
if subreddit then
header:tag('span'):wikitext('•'):css('opacity','0.6')
header:tag('span'):wikitext('r/' .. subreddit)
end
-- Title (if any)
if title and title ~= '' then
h:tag('div')
:addClass('mw-reddit-title')
:cssText('font-weight:600;margin:2px 0 6px 0;')
:wikitext(title)
end
-- Body text (optional)
if text and text ~= '' then
h:tag('blockquote')
:addClass('mw-reddit-text')
:cssText('margin:6px 0 10px 0;padding:0 0 0 12px;border-left:3px solid #e2e8f0;color:#0f172a;')
:wikitext(text)
end
-- Meta line: author • date • score • comments
local meta = h:tag('div'):addClass('mw-reddit-meta')
:cssText('font-size:0.85em;color:#475569;display:flex;flex-wrap:wrap;gap:8px;align-items:center;')
local function addBullet()
meta:tag('span'):wikitext('•'):css('opacity','0.6')
end
if author and author ~= '' then
if authorURL and authorURL ~= '' then
meta:wikitext(string.format('[[%s|%s]]', authorURL, author))
else
meta:wikitext(author)
end
end
if dateHuman and dateHuman ~= '' then
if author and author ~= '' then addBullet() end
local t = meta:tag('time'):attr('datetime', dateISO or ''):wikitext(dateHuman)
end
if score and score ~= '' then
if (author and author ~= '') or (dateHuman and dateHuman ~= '') then addBullet() end
meta:wikitext(tostring(score) .. ' points')
end
if comments and comments ~= '' then
if (author and author ~= '') or (dateHuman and dateHuman ~= '') or (score and score ~= '') then addBullet() end
meta:wikitext(tostring(comments) .. ' comments')
end
-- Permalink
if url and url ~= '' then
local linkLine = h:tag('div'):addClass('mw-reddit-link')
:cssText('margin-top:6px;')
local anchorText
if subreddit and id then
anchorText = string.format('Permalink: r/%s • %s', subreddit, id)
elseif id then
anchorText = 'Permalink: ' .. id
else
anchorText = 'Permalink'
end
linkLine:wikitext(string.format('[%s %s]', url, anchorText))
end
return tostring(h)
end
return p
