Module:RedditUtil: Difference between revisions

From Rest of What I Know
Created page with "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,..."
 
No edit summary
Line 8: Line 8:
local function parse_from_url(url)
local function parse_from_url(url)
   if type(url) ~= 'string' then return {} end
   if type(url) ~= 'string' then return {} end
  local u = url
   local sub, id = url:match('^https?://[^/]*reddit%.com/r/([A-Za-z0-9_]+)/comments/([A-Za-z0-9]+)/')
  -- 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
   if sub and id then
     return {subreddit = sub, id = id}
     return {subreddit = sub, id = id}
   end
   end
  -- also handle .../comments/<id> (rare, without subreddit in path)
   id = url:match('^https?://[^/]*reddit%.com/comments/([A-Za-z0-9]+)/')
   id = u:match('^https?://[^/]*reddit%.com/comments/([A-Za-z0-9]+)/')
   if id then
   if id then
     return {id = id}
     return {id = id}
   end
   end
  -- shortlink: https://redd.it/<id>
   id = url:match('^https?://[^/]*redd%.it/([A-Za-z0-9]+)')
   id = u:match('^https?://[^/]*redd%.it/([A-Za-z0-9]+)')
   if id then
   if id then
     return {id = id}
     return {id = id}
   end
   end
   return {}
   return {}
end
local function anchor_text(subreddit, id)
  if subreddit and id then
    return string.format('Permalink: r/%s • %s', subreddit, id)
  elseif id then
    return 'Permalink: ' .. id
  else
    return 'Permalink'
  end
end
local function gen_refname(id, url)
  if id and id ~= '' then
    return 'reddit-' .. id
  end
  return 'reddit-' .. mw.hash.hashValue('md5', url or tostring(mw.title.getCurrentTitle()))
end
end


function p.render(frame)
function p.render(frame)
   local args = frame:getParent() and frame:getParent().args or frame.args
   local args = frame:getParent() and frame:getParent().args or frame.args
   local url       = trim(args.url)
   local url       = trim(args.url)
   local author   = trim(args.author)
   local author     = trim(args.author)
   local authorURL = trim(args.author_url)
   local authorURL = trim(args.author_url)
   local title     = trim(args.title)
   local title     = trim(args.title)
   local text     = trim(args.text)
   local text       = trim(args.text)
   local dateHuman = trim(args.date)
   local dateHuman = trim(args.date)
   local dateISO   = trim(args.datetime) or dateHuman
   local dateISO   = trim(args.datetime) or dateHuman
   local score     = trim(args.score)
   local score     = trim(args.score)
   local comments  = trim(args.comments)
   local comments   = trim(args.comments)
  local subreddit = trim(args.subreddit)
  local refname    = trim(args.refname)
  local noreference = (trim(args.noreference) == 'yes') or (trim(args.ref) == 'no')


   local parsed = parse_from_url(url or '')
   local parsed = parse_from_url(url or '')
   local subreddit = trim(args.subreddit) or parsed.subreddit
   if not subreddit or subreddit == '' then
    subreddit = parsed.subreddit
  end
   local id = parsed.id
   local id = parsed.id


Line 48: Line 65:
     :cssText('border:1px solid #e2e8f0;border-radius:8px;padding:12px 14px;margin:8px 0;background:#fff;')
     :cssText('border:1px solid #e2e8f0;border-radius:8px;padding:12px 14px;margin:8px 0;background:#fff;')


   -- Header line: “Reddit • r/subreddit”
   -- Header
   local header = h:tag('div'):addClass('mw-reddit-header')
   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;')
     :cssText('font-size:0.9em;color:#475569;margin-bottom:6px;display:flex;gap:8px;align-items:center;')
   header:tag('span'):wikitext('Reddit')
   header:tag('span'):wikitext('Reddit')
   if subreddit then
   if subreddit then
Line 58: Line 74:
   end
   end


   -- Title (if any)
   -- Title
   if title and title ~= '' then
   if title and title ~= '' then
     h:tag('div')
     h:tag('div')
Line 66: Line 82:
   end
   end


   -- Body text (optional)
   -- Body text
   if text and text ~= '' then
   if text and text ~= '' then
     h:tag('blockquote')
     h:tag('blockquote')
Line 74: Line 90:
   end
   end


   -- Meta line: author • date • score • comments
   -- Meta line
   local meta = h:tag('div'):addClass('mw-reddit-meta')
   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;')
     :cssText('font-size:0.85em;color:#475569;display:flex;flex-wrap:wrap;gap:8px;align-items:center;')
Line 92: Line 108:
   if dateHuman and dateHuman ~= '' then
   if dateHuman and dateHuman ~= '' then
     if author and author ~= '' then addBullet() end
     if author and author ~= '' then addBullet() end
     local t = meta:tag('time'):attr('datetime', dateISO or ''):wikitext(dateHuman)
     meta:tag('time'):attr('datetime', dateISO or ''):wikitext(dateHuman)
   end
   end


Line 105: Line 121:
   end
   end


   -- Permalink
   -- Footnote reference (permalink)
   if url and url ~= '' then
   if url and url ~= '' and not noreference then
     local linkLine = h:tag('div'):addClass('mw-reddit-link')
     local name = refname or gen_refname(id, url)
      :cssText('margin-top:6px;')
     local text_ref = string.format('[%s %s]', url, anchor_text(subreddit, id))
     local anchorText
     meta:wikitext(string.format(' <ref name="%s">%s</ref>', name, text_ref))
    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
   end



Revision as of 03:02, 15 September 2025

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 sub, id = url: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
  id = url:match('^https?://[^/]*reddit%.com/comments/([A-Za-z0-9]+)/')
  if id then
    return {id = id}
  end
  id = url:match('^https?://[^/]*redd%.it/([A-Za-z0-9]+)')
  if id then
    return {id = id}
  end
  return {}
end

local function anchor_text(subreddit, id)
  if subreddit and id then
    return string.format('Permalink: r/%s • %s', subreddit, id)
  elseif id then
    return 'Permalink: ' .. id
  else
    return 'Permalink'
  end
end

local function gen_refname(id, url)
  if id and id ~= '' then
    return 'reddit-' .. id
  end
  return 'reddit-' .. mw.hash.hashValue('md5', url or tostring(mw.title.getCurrentTitle()))
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 subreddit  = trim(args.subreddit)
  local refname    = trim(args.refname)
  local noreference = (trim(args.noreference) == 'yes') or (trim(args.ref) == 'no')

  local parsed = parse_from_url(url or '')
  if not subreddit or subreddit == '' then
    subreddit = parsed.subreddit
  end
  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
  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 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
  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
  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
    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

  -- Footnote reference (permalink)
  if url and url ~= '' and not noreference then
    local name = refname or gen_refname(id, url)
    local text_ref = string.format('[%s %s]', url, anchor_text(subreddit, id))
    meta:wikitext(string.format(' <ref name="%s">%s</ref>', name, text_ref))
  end

  return tostring(h)
end

return p