Módulo:Changelog

De Wiki Gla
Revisão de 20h19min de 2 de setembro de 2025 por Gurren1 (discussão | contribs)
Ir para navegação Ir para pesquisar

A documentação para este módulo pode ser criada em Módulo:Changelog/doc

local p = {}

local function N(s) return s and mw.text.trim(tostring(s)) or '' end
local function has(s) return N(s) ~= '' end

function p.item(frame)
  local parent = frame:getParent()
  local a = parent and parent.args or frame.args

  local title = N(a.title)
  local link  = N(a.link)
  local date  = N(a.date)
  local desc  = N(a.description)
  local icon  = N(a.icon)
  local color = N(a.color)

  local item = mw.html.create('div'):addClass('notice-item')
  -- layout and visuals are handled by stylesheet in MainPageChangelog.html

  if has(color) then item:css('--notice-color', color) end

  -- handle icon first so it appears before the content (left side in flex layout)
  if has(icon) then
    -- Accept either a filename like "Bigexppot.png" (MediaWiki File:) or a full URL.
    local icon_wikitext = ''
    local lowered = string.lower(icon)
    if not string.find(lowered, '://', 1, true) and not string.find(icon, 'File:', 1, true) and not string.find(icon, 'Image:', 1, true) then
      -- treat as a filename -> prepend File:
      icon_wikitext = string.format('[[File:%s|alt=%s|class=notice-icon]]', mw.text.trim(icon), mw.text.trim(title))
    else
      -- raw URL or explicit File: -> use as-is (for URLs we'll build an <img> tag so external URLs still work)
      if string.find(lowered, '://', 1, true) then
        -- external URL -> plain img tag via wikitext
        icon_wikitext = string.format('<img src="%s" alt="%s" class="notice-icon" />', mw.text.trim(icon), mw.text.trim(title))
      else
        -- already has File: or Image: prefix -> wrap as wikitext (in case File:Name is provided)
        icon_wikitext = string.format('[[%s|alt=%s|class=notice-icon]]', mw.text.trim(icon), mw.text.trim(title))
      end
    end
    -- add the icon as wikitext so MediaWiki resolves local files correctly
    item:wikitext(icon_wikitext)
  end

  local content = item:tag('div'):addClass('notice-content'):css('flex','1')

  -- Header row: title (left) and date (right)
  if has(title) or has(date) then
    local header = content:tag('div'):addClass('notice-header')
    local left = header:tag('div'):addClass('notice-header-left')
    local right = header:tag('div'):addClass('notice-header-right')

    if has(title) then
      local holder = left:tag('span'):addClass('notice-title')
      if has(link) then
        holder:wikitext('[' .. link .. ' ' .. title .. ']')
      else
        holder:wikitext(title)
      end
    end

    if has(date) then
      right:tag('span'):addClass('notice-date'):wikitext(date)
    end
  end

  if has(desc) then
    content:tag('p'):addClass('notice-desc')
      :wikitext(desc)
  end

  -- icon already handled above (so it appears before content)

  return tostring(item)  -- já sai como HTML + wikitexto parseável
end

return p