Modul:Sort/cellText

Aus Wikipedia
Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus

Weiterleitung der Diskussionsseite fehlt

local Sort = { suite   = "Sort",
               sub     = "cellText",
               serial  = "2020-04-10",
               item    = 90147626,
               globals = { Sorter = 24205172,
                           Cell   = 90144855,
                           WLink  = 19363224 } }
--[=[
Sort/cellText -- support table cells with lexically sortable wikitext
]=]
local Failsafe  = Sort
local GlobalMod = Sort



local foreignModule = function ( access, advanced, append, alt, alert )
    -- Fetch global module
    -- Precondition:
    --     access    -- string, with name of base module
    --     advanced  -- true, for require(); else mw.loadData()
    --     append    -- string, with subpage part, if any; or false
    --     alt       -- number, of wikidata item of root; or false
    --     alert     -- true, for throwing error on data problem
    -- Postcondition:
    --     Returns whatever, probably table
    -- 2020-01-01
    local storage = access
    local finer = function ()
                      if append then
                          storage = string.format( "%s/%s",
                                                   storage,
                                                   append )
                      end
                  end
    local fun, lucky, r, suited
    if advanced then
        fun = require
    else
        fun = mw.loadData
    end
    GlobalMod.globalModules = GlobalMod.globalModules or { }
    suited = GlobalMod.globalModules[ access ]
    if not suited then
        finer()
        lucky, r = pcall( fun,  "Module:" .. storage )
    end
    if not lucky then
        if not suited  and
           type( alt ) == "number"  and
           alt > 0 then
            suited = string.format( "Q%d", alt )
            suited = mw.wikibase.getSitelink( suited )
            GlobalMod.globalModules[ access ] = suited or true
        end
        if type( suited ) == "string" then
            storage = suited
            finer()
            lucky, r = pcall( fun, storage )
        end
        if not lucky and alert then
            error( "Missing or invalid page: " .. storage )
        end
    end
    return r
end -- foreignModule()



local fetch = function ( access, append )
    -- Fetch global library
    -- Precondition:
    --     access    -- string|false, with name of base module
    --     append    -- string, with subpage part, if any; or false
    local store, sub, suite
    if access then
        suite = access
        store = access
    else
        suite = Sort.suite
        if append then
            sub   = append:lower()
            store = append
        else
            store = "Sorter"
        end
    end
    if type( Sort[ store ] ) == "nil" then
        local bib = foreignModule( suite,
                                   true,
                                   sub,
                                   Sort.globals[ store ],
                                   true )
        if bib  and  type( bib[ suite ] ) == "function" then
            Sort[ store ] = bib[ suite ]()
        else
            error( tostring( bib ) )
        end
    end
end -- fetch()



local fiat = function ( args )
    -- Execute task
    -- Parameter:
    --     args    -- table, parameters
    -- Postcondition:
    --     Returns string, or expands .cell
    --     Throws error on failure
    local r
    fetch( false, "Cell" )
    if type( args ) == "table" then
        local present = Sort.Cell.first( args, true )
        Sort.Cell.fair( args, "source", present )
        if present.source then
            local s = mw.text.unstrip( present.source )
            if s:find( "<", 1, true ) then
                s = s:gsub( "(</?%l[^>]*>)", "" )
            end
            if s:find( "'", 1, true ) then
                s = s:gsub( "'''(.+)'''", "%1" )
                     :gsub( "''(.+)''", "%1" )
            end
            if s:find( "[", 1, true ) then
                fetch( "WLink" )
                s = Sort.WLink.getPlain( s )
            end
            s = mw.text.decode( s )
            s = mw.ustring.gsub( s, mw.ustring.char( 160 ), " " )
            s = mw.text.trim( s )
            s = mw.ustring.gsub( s, "%s+", " " )
            if s ~= "" then
                r = present.source
                fetch()
                s = Sort.Sorter.lex( s, "latin" )
                if s ~= r then
                    Sort.Cell.faced( present, s )
                end
                r = Sort.Cell.finalize( present, r )
            end
        end
        if not r then
            r = Sort.Cell.fault( "?????", present )
        end
    else
        error( "'args' is not a table" )
    end
    return r
end -- fiat()



Sort.f = function ( args )
    -- Create table cell start or equip table cell object
    -- Parameter:
    -- Parameter:
    --     args    -- table, parameters
    --                .source     -- string, with wikitext
    --                .cell       -- table|nil, sort environment
    --                .rowspan    -- number|string, for cell attribute
    --                .colspan    -- number|string, for cell attribute
    --                .class      -- string, for cell attribute
    --                .style      -- string|table, for cell attribute
    --                .id         -- string, for cell attribute
    --                .lang       -- string, for cell attribute
    --                .dir        -- string, for cell attribute
    --                .cat        -- string|nil, for error category
    -- Postcondition:
    --     Returns string, or expands .cell
    local lucky, r = pcall( fiat, args )
    if not lucky then
        local e = mw.html.create( "span" )
                         :addClass( "error" )
                         :wikitext( "Module:Sort/cell * " .. r )
        if type( args.cell ) == "table"  and
           type( args.cell.wikitext ) == "function" then
            args.elem:node( e )
        else
            r = tostring( e )
        end
    end
    return r
end -- Sort.f()



Failsafe.failsafe = function ( atleast )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --     atleast  -- string, with required version or "wikidata" or "~"
    --                 or false
    -- Postcondition:
    --     Returns  string  -- with queried version, also if problem
    --              false   -- if appropriate
    -- 2019-10-15
    local last  = ( atleast == "~" )
    local since = atleast
    local r
    if last  or  since == "wikidata" then
        local item = Failsafe.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local entity = mw.wikibase.getEntity( string.format( "Q%d",
                                                                 item ) )
            if type( entity ) == "table" then
                local seek = Failsafe.serialProperty or "P348"
                local vsn  = entity:formatPropertyValues( seek )
                if type( vsn ) == "table"  and
                   type( vsn.value ) == "string"  and
                   vsn.value ~= "" then
                    if last  and  vsn.value == Failsafe.serial then
                        r = false
                    else
                        r = vsn.value
                    end
                end
            end
        end
    end
    if type( r ) == "nil" then
        if not since  or  since <= Failsafe.serial then
            r = Failsafe.serial
        else
            r = false
        end
    end
    return r
end -- Failsafe.failsafe()



-- Export
local p = { }

p.f = function ( frame )
    -- Template call
    Sort.frame = frame
    return Sort.f( frame.args )  or  ""
end -- p.f

p.failsafe = function ( frame )
    -- Versioning interface
    local s = type( frame )
    local since
    if s == "table" then
        since = frame.args[ 1 ]
    elseif s == "string" then
        since = frame
    end
    if since then
        since = mw.text.trim( since )
        if since == "" then
            since = false
        end
    end
    return Failsafe.failsafe( since )  or  ""
end -- p.failsafe

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

return p