Modul:TemplUtl/01

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

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


local TemplUtl = { suite   = "TemplUtl",
                   serial  = "2019-12-03",
                   sub     = "01",
                   commons = "i18n/01",
                   main    = 52364930 }
--[=[
Auxilary table generation,
showing strings for boolean values in all known languages.
]=]
local Failsafe = TemplUtl



local fetch = function ()
    -- Retrieve specifications from commons:Data:****.tab
    -- Postcondition:
    --     Returns
    --        1. table, sequence, with data
    --        2. table, with maximum colspan, as number
    local storage = string.format( "%s.tab", TemplUtl.commons )
    local lucky, data = pcall( mw.ext.data.get, storage, "_" )
    local r1 = { }
    local r2 = { [false] = 0,
                 [true]  = 0 }
    if type( data ) == "table"  and
       type( data.data ) == "table" then
        local o = { }
        local t = { }
        local e, l, s, w
        data = data.data
        for i = 1, #data do
            e = data[ i ]
            if type( e ) == "table"  and
               type( e[ 1 ] ) == "boolean"  and
               type( e[ 2 ] ) == "table" then
                l = e[ 1 ]
                e = e[ 2 ]
                for k, v in pairs( e ) do
                    if type( k ) == "string"  and
                       type( v ) == "string" then
                        w = mw.text.split( v, "|" )
                        if #w > r2[ l ] then
                            r2[ l ] = #w
                        end
                        table.insert( o, k )
                        if not t[ k ] then
                            t[ k ] = { }
                        end
                        t[ k ][ l ] = w
                    end
                end -- for k, v
            else
                break   -- for i
            end
        end   -- for i
        table.sort( o )
        for i = 1, #o do
            s = o[ i ]
            e = t[ s ]
            if e then
                w = { s = s }
                if type( e[ false ] ) == "table" then
                    w[ false ] = e[ false ]
                end
                if type( e[ true ] ) == "table" then
                    w[ true ] = e[ true ]
                end
                table.insert( r1, w )
                t[ s ] = false
            end
        end   -- for i
    end
    return r1, r2
end -- fetch()



local fiat = function ( apply, align, at, around )
    -- Create table row
    -- Precondition:
    --     apply   -- table, with data
    --     align   -- table, with maximum colspan
    --     at      -- string, with project language code
    --     around  -- string, with wrapping scripting direction
    -- Postcondition:
    --     Returns  mw.html.TR object
    local r = mw.html.create( "tr" )
    local e = mw.html.create( "code" )
                     :attr( "lang", "zxx" )
                     :wikitext( apply.s )
    local scripting
    local fk = function ( a, attr )
                   local td = mw.html.create( "td" )
                                     :attr( "lang", a )
                   if attr  and  attr ~= around then
                       td:attr( "dir", attr )
                   end
                   td:wikitext( mw.language.fetchLanguageName( apply.s,
                                                               a ) )
                   return td
               end -- fk()
    local fv = function ( a )
                   local v = apply[ a ]
                   r:newline()
                   if v then
                       local k = align[ a ] - #v
                       for i = 1, #v do
                           e = mw.html.create( "td" )
                                      :attr( "lang", apply.s )
                                      :wikitext( v[ i ] )
                           if scripting ~= around then
                               e:attr( "dir", scripting )
                           end
                           r:node( e )
                       end   -- for i
                       if k > 0 then
                           e = mw.html.create( "td" )
                           if k > 1 then
                               e:attr( "colspan",  tostring( k ) )
                           end
                           r:node( e )
                       end
                   else
                       r:node( mw.html.create( "td" )
                                      :attr( "colspan",
                                             tostring( align[ a ] ) ) )
                   end
               end -- fv()
    if mw.language.new( apply.s ):isRTL() then
        scripting = "rtl"
    else
        scripting = "ltr"
    end
    if around == "rtl" then
        e:attr( "dir", "ltr" )
    end
    r:newline()
     :node( mw.html.create( "td" )
                   :node( e ) )
     :newline()
     :node( fk( "en", "ltr" ) )
     :newline()
     :node( fk( apply.s, scripting ) )
    if at ~= "en" then
        r:newline()
         :node( fk( at ) )
    end
    fv( false )
    fv( true )
    return r
end -- fiat()



TemplUtl.f = function ( alien )
    -- Create table
    -- Precondition:
    --     alien  -- string, with language code, or not
    -- Postcondition:
    --     Returns  string with entire HTML table
    local tbl = mw.html.create( "table" )
                  :addClass( "wikitable sortable" )
                  :addClass( "TemplUtl-table-01" )
    local tr  = mw.html.create( "tr" )
    local rows, cols = fetch()
    local scripting, slang, speak
    if type( alien ) == "string"  and
       mw.language.isSupportedLanguage( alien ) then
        slang = mw.text.trim( alien )
    else
        local sub = mw.title.getCurrentTitle().subpageText
        if sub:find( "%l%l%l?%-?%a?%a?%a?%a?$" ) and
           mw.language.isSupportedLanguage( sub ) then
            slang = sub
        end
        if not slang then
            slang = mw.language.getContentLanguage():getCode()
        end
    end
    slang = slang:lower()
    speak = mw.language.fetchLanguageName( slang )
    tbl:attr( "lang", slang )
    if mw.language.new( slang ):isRTL() then
        scripting = "rtl"
    else
        scripting = "ltr"
    end
    tbl:attr( "dir", scripting )
    tr:newline()
      :node( mw.html.create( "th" )
                    :wikitext( "ISO 639 / BCP47" ) )
      :newline()
      :node( mw.html.create( "th" )
                    :wikitext( "English" ) )
      :newline()
      :node( mw.html.create( "th" )
                    :wikitext( "*" ) )
      :newline()
    if slang ~="en" then
        tr:node( mw.html.create( "th" )
                        :attr( "lang", slang )
                        :wikitext( speak ) )
          :newline()
    end
    tr:node( mw.html.create( "th" )
                    :attr( "colspan",  tostring( cols[ false ] ) )
                    :wikitext( "0" ) )
      :newline()
      :node( mw.html.create( "th" )
                    :attr( "colspan",  tostring( cols[ true ] ) )
                    :wikitext( "1" ) )
    tbl:newline()
--     :node( mw.html.create( "thead" )
                     :node( tr )
--          )
    for i = 1, #rows do
        tbl:newline()
           :node( fiat( rows[ i ], cols, slang, scripting ) )
    end -- for i
    return  tostring( tbl:newline() )
end -- TemplUtl.f()



-- Export
local p = { }

function p.f( frame )
    return TemplUtl.f( frame.args.lang )
end -- p.f

return p