Jump to content

Module:Aside

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wnt (talk | contribs) at 19:34, 28 September 2016 (Starting with a description of what I want. I have some content pasted from Module:NewestAtTop that I still have to replace with the guts of this). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module is an experiment in an altered discussion dynamic for Wikipedia forums.
-- It presents a brief digest of the referenced discussion on a user's talk subpage.
-- The full conversation remains available via V-D-E buttons (V = view the talk subpage, 
-- D = file a comment to the user's MAIN talk page like if there's something you want him to 'admin' or to request an invite as applicable
-- E = edit the talk subpage
-- The digest should present usernames and the first words of each comment, for the past N comments
-- One intention is to allow off-topic conversations to be less obtrusive on the parent thread.
-- Another is that, potentially, some of these may be invite-only conversations in some forum where that is deemed appropriate.
-- Discussions of this type might be indexed in multiple specialized forums.
-- Part of the experiment is that if users feel *invited* to a discussion, it gives editors a chance to express mutual esteem,
-- as a counterbalance against the usual where they only really talk personally when in conflict.  Not sure this will really happen...

-- To try to keep things more accessible, I think the Lua module should only do the text processing to create the talk page extract
-- This then should get wrapped up in V-D-E boxes and cute formatting by an enveloping template.
-- There are some things I can't be perfect about - if you reply in the middle of someone's comment you'll get their beginning attributed to you.

local p = {}

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 - not sure if I should even access these but leaving them for now.
    user = frame.args.user or parent.args.user -- the location for discussion with whoever admins the aside, usually user talk:(x).  "D" defaults to (user)
    page = frame.args.page or parent.args.page -- the location of the page for the aside.  "V/E" defaults to (page), usually user talk:(x)/subpage
    comments = frame.args.comments or parent.args.comments -- the last N comments are returned
    length = frame.args.length or parent.args.length -- return at most N characters of text per comment
    -- "position" (left, right, plain) is a template parameter that can be handled in the template, so will not be used here.
    ---- args in the parent frame come next
    ---- default values if parameters aren't provided
    comments = comments or 6 -- Last 6 comments
    length = length or 80 -- In homage to TRS, but probably this will get longer
    -- get a pointer to the aside
    local pagepointer=mw.title.new(page)
    -- this needs to be revised to fail gracefully if the aside is deleted/not started
    assert(pagepointer,"failed to access mw.title.new("..tostring(currentpage)..")")
    ---- get the text of the aside
    local text=pagepointer.getContent(pagepointer)
    assert (text,"error: failed to get text from ".. currentpage)
    ------- below is pasted text from the template; I'll be back...
    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