Przejdź do zawartości

Module:Nowy artykuł

Z Kolejopedia

local p = {}

function p.najnowszy(frame)

   local dpl = frame:callParserFunction('#dpl', {
       'namespace=0',
       'order=descending',
       'count=1',
       'mode=userformat',
       'listseparators=<startList>,<endList>,<item>',
       'allowcachedresults=true'
   })
   local pageName = mw.text.trim(dpl:match('<item>(.-)</item>') or "")
   if pageName == "" then
       return "Błąd: nie znaleziono nowego artykułu."
   end
   local page = mw.title.new(pageName)
   if not page or not page.exists then
       return "Błąd: artykuł „" .. pageName .. "” nie istnieje."
   end
   local content = page:getContent() or ""
   content = content:gsub("<[^>]+>", "")       -- usuń HTML
   content = content:gsub("Szablon:.-", "")        -- usuń szablony
   content = content:gsub("%[%[([^|%]]+)|([^%]]+)%]%]", "%2")
   content = content:gsub("%[%[([^%]]+)%]%]", "%1")
   content = content:gsub("+", "")           -- kursywa/pogrubienie
   local words = {}
   for word in content:gmatch("%S+") do
       table.insert(words, word)
       if #words >= 40 then break end
   end
   local zajawka = table.concat(words, " ") .. " [...]"
   return string.format("%s: %s", pageName, zajawka)

end

return p