Modul:Sort

Aus skandinavien-wiki.net
Version vom 15. Oktober 2013, 08:15 Uhr von w>PerfektesChaos (Setup)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Die Dokumentation für dieses Modul kann unter Modul:Sort/doc erstellt werden

--[=[ 2013-10-13
Sort
]=]



local Sort = { }



Sort.num = function ( adjust, ad, at, align, absolute )
    -- Build sortkey for heading numerical value
    -- Precondition:
    --     adjust    -- string to be aligned; leading digits / minus
    --     ad        -- decimal separator; "." or ","; defaults to "."
    --     at        -- thousands group separator; defaults to none
    --                  ","  "."  "'"
    --     align     -- number of leading zeros / maximum length
    --                  defaults to 15
    --     absolute  -- negative figures by digits; default: by value
    -- Postcondition:
    --     Returns string with sortkey
    local max    = 15
    local mid    = 46    -- "."
    local min1   = -1    -- none
    local min2   = -2    -- none
    local low    = false
    local last   = false
    local lead   = true
    local source = tostring( adjust )
    local sub    = "."
    local suffix = false
    local n      = mw.ustring.len( source )
    local r      = ""
    local c
    if ad then
        mid = mw.ustring.codepoint( ad, 1 )
    end
    if at then
        min1, min2 = mw.ustring.codepoint( at, 1, 2 )
    end
    if align then
        max = align
    end
    for i = 1, n do
        c = mw.ustring.codepoint( source, i )
        if c > 32 then    -- not whitespace
            if c >= 48 and c <= 57 then    -- digits
                r   = string.format( "%s%c", r, c )
                max = max - 1
            elseif c == min1 or c == min2 then    -- group separator
            elseif c == mid then    -- decimal separator
                 for j = i + 1, n do
                     c = mw.ustring.codepoint( source, j )
                     if c >= 48 and c <= 57 then    -- digits
                         sub = string.format( "%s%c", sub, c )
                     elseif c == min1 or c == min2 then    -- grouping
                     else
                         suffix = mw.ustring.sub( source, j )
                         break    -- for j
                     end
                 end    -- for j
                 n    = 0
                 last = true
            elseif lead then
                if c == 45 or c == 8722 then    -- minus
                    low = true
                elseif c ~= 43 then    -- plus
                    last = true
                end
            else
                last = true
            end
            lead = false
        elseif not lead then    -- whitespace not leading
            last = true
        end
        if last then
            if i < n then
                suffix = mw.ustring.sub( source, i )
            end
            break    -- for i
       end
    end    -- for i
    if low then
        if not absolute then   -- complementary value
            local s    = "."
            local cmpl = function ( str, k )
                             return 57 - str:byte( k )
                         end
            for i = 2, #sub do
                s = string.format( "%s%d",  s,  cmpl( sub, i ) )
            end    -- for i
            for i = #r, 1, -1 do
                s = string.format( "%d%s",  cmpl( r, i ),  s )
            end    -- for i--
            r = s
            if max > 0 then
                r = string.rep( "9", max )  ..  r
            end
            sub = false
            max = 0
        end
    end
    if sub then
        r = r .. sub
    end
    if max > 0 then
        r = string.rep( "0", max )  ..  r
    end
    if low then
        r = "-" .. r
    end
    if suffix then
        r = string.format( "%s %s", r, suffix )
    end
    return r
end -- Sort.num()



-- Export
local p = { }

p.Ta = function ( frame )
    -- Template::alphanumerical
    --     {{{1}}}
    return "not yet ready"
end -- p.Ta



p.Td = function ( frame )
    -- Template::date
    --     {{{1}}}
    return "not yet ready"
end -- p.Td



p.Tn = function ( frame )
    -- Template::numerical
    --     {{{1}}}
    -- #invoke
    --     d  -- decimal separator; defaults to "."
    --     t  -- thousands group separator; defaults to none
    --     z  -- number of leading zeros / maximum length; defaults to 15
    --     m  -- negative figures by digits; default: by value
    local m = frame.args.m
    if m then
        m  =  mw.text.trim( m )
    end
    local lucky, r = pcall( Sort.num,
                            frame:getParent().args[ 1 ] or "",
                            frame.args.d,
                            frame.args.t,
                            tonumber( frame.args.z ),
                            m == "1" )
    -- DEBUG:
    if not lucky then
        r = "<span class='error'>" .. r .. "</span>"
    end
    return r;
end -- p.Tn



p.Sort = function ()
    return Sort
end -- p.Sort

return p