Module:FeaturedProject

From WikiEducator
Jump to: navigation, search
local p = {} --- djb2 hash of a string -> positive integer local function hashString(s) local h = 5381 for i = 1, #s do h = ((h * 33) + string.byte(s, i)) % 2147483647 end return h end local function todaysEntry(entries) local date = os.date("*t") local seed = string.format("%04d%02d%02d", date.year, date.month, date.day) return entries[(hashString(seed) % #entries) + 1] end local function parseDataPage(data_page) local title = mw.title.new(data_page) if not title or not title.exists then return nil, "Data page not found: " .. data_page end local entries = {} local content = title:getContent() for section in (content .. "\n----\n"):gmatch("(.-)\n%-%-%-%-%s*\n") do section = section:match("^%s*(.-)%s*$") if section ~= "" then local firstline, body = section:match("^([^\n]+)\n+(.-)%s*$") if firstline then -- Strip optional heading markers firstline = firstline:match("^=+%s*(.-)%s*=+$") or firstline local link, display = firstline:match("%[%[([^|%]]+)|([^%]]+)%]%]") if not link then display = firstline:match("%[%[([^%]]+)%]%]") link = display end if display then table.insert(entries, { title = display, link = link, teaser = body }) end end end end return entries, nil end function p.show(frame) local data_page = frame.args.list or frame.args[1] or "WikiEducator:FeaturedProject/Data" local entries, err = parseDataPage(data_page) if err then return '<div class="error">' .. err .. '</div>' end if #entries == 0 then return '<div class="error">FeaturedProject: no entries found in data page.</div>' end local entry = todaysEntry(entries) local root = mw.html.create("div"):addClass("featured-project") root:tag("h3") :wikitext("[[" .. entry.link .. "|" .. entry.title .. "]]") root:tag("p") :addClass("featured-project-teaser") :wikitext(entry.teaser or "") return tostring(root) end return p -- [[Module:FeaturedProject/doc]]