Module:Sandbox/Rogerhc/NewestAtTop

-- Roger is disecting this code to try to learn some Lua. -- So it is likely entirely broken. --This module aims to reorder the sections on a page so that, for example, you can reorder a talk page from newest sections at bottom to newest sections at top.

local p = {}

assert (p,"HERE IS THE PAGE:/n/n/n/n") --Roger: See what p contains.

-- Return the reordered page, used like this: function p.main(frame,header) local parent=frame.getParent(frame) or {} local currentpage,top args in the #invoke itself trump args in the parent frame currentpage = frame.args.page header = header or frame.args.header or parent.args.header -- can get from function name top = frame.args.top or parent.args.top args in the parent frame come next if parent then currentpage=currentpage or parent.args.page header=header or parent.args.header top = top or parent.args.top end default values if parameters aren't provided header=header or 2 -- Reorder even = = sections. Note this isn't tested and may not work... local pagepointer if top then top=1 else top=0 end if not(currentpage) then pagepointer=mw.title.getCurrentTitle assert(pagepointer,"failed to access getCurrentTitle") currentpage=pagepointer.fullText else pagepointer=mw.title.new(currentpage) assert(pagepointer,"failed to access mw.title.new("..tostring(currentpage)..")") end get the text of the currentpage local text=pagepointer.getContent(pagepointer) assert (text,"error: failed to get text from ".. currentpage) local headerdef=mw.ustring.rep("=",header) local headermatch="%s*\n%s*"..headerdef.."(=*)(.-)(=*)"..headerdef.."%s*\n%s*" local prowl=mw.ustring.gmatch(text,"(.-)"..headermatch) local archive={} local chunk,h1,title,h2=prowl local level=math.min(mw.ustring.len(h1),mw.ustring.len(h2)) -- if unmatched, higher level trumps local lastchunk="";local lasth1="==";local lasth2="==" -- keeps track of the title from the previous iteration to recover at end local tinsertpoint={};local insertpoint=1;local lastlevel=level table.insert(archive,insertpoint,chunk or "") --- top chunk is the first thing in. Top means display it at bottom table.insert(archive,insertpoint+top,"\n"..headerdef..title..headerdef.."\n") repeat chunk, h1,title,h2=prowl h1=h1 or lasth1;h2=h2 or lasth2 -- default to normal header and to avoid script error if not(chunk) then break end lastchunk=chunk; -- to find next section. You can't find a title in the string it came from. table.insert(archive,1+insertpoint+top,chunk..tostring(2+top)..tostring(level)) --- insert the OLDER chunk under LAST iteration's title and level local lastlevel=level level=math.min(mw.ustring.len(h1),mw.ustring.len(h2)) -- if unmatched, higher level trumps if level>lastlevel then tinsertpoint[lastlevel]=insertpoint;insertpoint=insertpoint+2 else if level < lastlevel then insertpoint=tinsertpoint[level] end end if title then lasth1=h1;lasth2=h2 table.insert(archive,insertpoint+top,"\n"..string.rep("=",header+level)..title..string.rep("=",header+level).."\n") end --- insert the new title at the beginning until false h1,title,h2,chunk=mw.ustring.match(text,lastchunk..headermatch.."(.*)$")-- everything from the last section header to the end of string. Fails if two identical section headers. table.insert(archive,insertpoint+1+top,chunk) local output="" for i = 1, table.maxn(archive) do       output=output..(archive[i] or "") end return frame.preprocess(frame,output) end

return p