Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Sandbox/Wnt/NewestAtTop
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
--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 = {} -- Return the reordered page, used like this: {{#invoke:NewestAtTop|main|page=Module talk:NewestAtTop/Test|top=yes}} 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) --- 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
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Sandbox/Wnt/NewestAtTop/doc
(
edit
)