Jump to content

Module:Transclude DYK

From Wikipedia, the free encyclopedia

localp={}-- Transclude randomly selected "Did you know?" entriesfunctionp.main(frame)-- args = { 1,2,... = page names, paragraphs = list e.g. "1,3-5", files = list, more = text}localargs={}-- args[k] = frame.args[k] or frame:getParent().args[k] for all k in either (numeric or not)fork,vinpairs(frame:getParent().args)doargs[k]=vendfork,vinpairs(frame.args)doargs[k]=vend-- args from a Lua call have priority over parent args from template-- Read the input pagelocalpage=args[1]orerror("No page name given")localtitle=mw.title.new(page)orerror("Missing input page "..page)localtext=title:getContent()orerror("No content for page "..page)-- Limit to the DYK section if presentlocalsectionstart=mw.ustring.find(text,"\n==''Did you know?'' articles==",1,true)ifsectionstartthenlocalsectionend=mw.ustring.find(text,"\n==",sectionstart+1,true)or-1text=mw.ustring.sub(text,sectionstart,sectionend)end-- Parse the entriesentries={}forentryinmw.ustring.gmatch(text,"\n%*[.…%s]*([^\n]+)")doifnotmw.ustring.find(entry,"article's talk page missing blurb",1,true)thentable.insert(entries,entry)endend-- Swap some random entries into the first n positionslocaln=math.min(#entries,args.countor10)-- the number of entries to producemath.randomseed(os.time())fori=1,ndoj=math.random(i,#entries)entries[i],entries[j]="*... "..entries[j],entries[i]end-- Return the first n entriestext=table.concat(entries,"\n",1,n)returnframe:preprocess(text)endreturnp
close