Module:HackerNewsUtil

From Rest of What I Know
Revision as of 23:28, 20 October 2025 by Roshan (talk | contribs)

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

local p = {}

local HN_SVG = '<svg xmlns="http://www.w3.org/2000/svg" height="18" viewBox="4 4 188 188" width="18"><path d="m4 4h188v188h-188z" fill="#f60"/><path d="m73.2521756 45.01 22.7478244 47.39130083 22.7478244-47.39130083h19.56569631l-34.32352071 64.48661468v41.49338532h-15.98v-41.49338532l-34.32352071-64.48661468z" fill="#fff"/></svg>'

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 host, id = url:match('^https?://([^/]+)/item%?id=(%d+)')
  if id then
    return { id = id, site = host }
  end
  return {}
end

-- Render plain text with paragraphs preserved.
-- Two or more newlines => new <p>. Single newline => <br>.
local function render_paragraph_text(parent, s)
  if type(s) ~= 'string' or s == '' then return end
  s = s:gsub('\r\n', '\n')
  local paras, i = {}, 1
  while true do
    local a, b = s:find('\n\n+', i)
    if not a then table.insert(paras, s:sub(i)); break end
    table.insert(paras, s:sub(i, a - 1))
    i = b + 1
  end
  for _, para in ipairs(paras) do
    local pnode = parent:tag('p')
    local first = true
    for line in (para .. '\n'):gmatch('(.-)\n') do
      if not first then pnode:tag('br') end
      pnode:wikitext(mw.text.nowiki(line))
      first = false
    end
  end
end

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

local function gen_refname(id, url)
  if id and id ~= '' then
    return 'hn-' .. id
  end
  return 'hn-' .. mw.hash.hashValue('md5', url or tostring(mw.title.getCurrentTitle()))
end

-- Render one or more HN author links. Supports "a|b" delimited authors.
local function render_authors(parent, author, authorURL)
  if not author or author == '' then return end
  local parts = mw.text.split(author, '%s*|%s*')
  for i, name in ipairs(parts) do
    local url = authorURL
    if not url or url == '' or #parts > 1 then
      local enc = mw.uri.encode(name, 'QUERY')
      url = 'https://news.ycombinator.com/user?id=' .. enc
    end
    -- Use MediaWiki external link syntax instead of raw HTML
    parent:wikitext(string.format('[%s %s]', url, mw.text.nowiki(name)))
    if i < #parts then parent:wikitext(', ') end
  end
end

function p.render(frame)
  local args = frame:getParent() and frame:getParent().args or frame.args
  local url        = trim(args.url)
  local siteArg    = trim(args.site)
  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 refname    = trim(args.refname)
  local noreference = (trim(args.noreference) == 'yes') or (trim(args.ref) == 'no')

  local parsed = parse_from_url(url or '')
  local id   = parsed.id
  local site = siteArg or parsed.site or 'news.ycombinator.com'

  -- Fallback permalink if url omitted but id present
  if (not url or url == '') and id then
    url = 'https://news.ycombinator.com/item?id=' .. id
  end

  -- Auto author URL if not provided and single author
  if author and author ~= '' and (not authorURL or authorURL == '') and not author:find('|') then
    local enc = mw.uri.encode(author, 'QUERY')
    authorURL = 'https://news.ycombinator.com/user?id=' .. enc
  end

  local h = mw.html.create('div')
    :addClass('mw-hn')
    :cssText('border:1px solid #e2e8f0;border-radius:8px;padding:12px 14px;margin:8px 0;background:#fff;')

  -- Header with icon via CSS data-URI
  local header = h:tag('div'):addClass('mw-hn-header')
    :cssText('font-size:0.9em;color:#475569;margin-bottom:6px;display:flex;gap:8px;align-items:center;')

  header:tag('span')
    :addClass('mw-hn-logo')
    :attr('aria-hidden', 'true')

  header:tag('span'):wikitext('Hacker News')

  -- Title
  if title and title ~= '' then
    h:tag('div')
      :addClass('mw-hn-title')
      :cssText('font-weight:600;margin:2px 0 6px 0;')
      :wikitext(title)
  end

  -- Body text (optional)
  if text and text ~= '' then
    local bq = h:tag('blockquote')
      :addClass('mw-hn-text')
      :cssText('margin:6px 0 10px 0;padding:0 0 0 12px;border-left:3px solid #e2e8f0;color:#0f172a;')
    render_paragraph_text(bq, text)
  end

  -- Meta line
  local meta = h:tag('div'):addClass('mw-hn-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
    render_authors(meta, author, authorURL)
  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(id, site))
    local ref_markup = frame:extensionTag('ref', text_ref, { name = name })
    meta:wikitext(' ' .. ref_markup)
  end

  return tostring(h)
end

return p