Modul:JSONutil: Unterschied zwischen den Versionen

(2019-05-29)
(2019-05-29)
Zeile 9: Zeile 9:


JSONutil.more = 50    -- length of trailing context
JSONutil.more = 50    -- length of trailing context
local Fallback = function ()
    -- Retrieve current default language code
    --    Returns  string
    return mw.language.getContentLanguage():getCode()
                                          :lower()
end -- Fallback()




Zeile 54: Zeile 63:
     --    apply  -- string, with enhanced JSON
     --    apply  -- string, with enhanced JSON
     -- Returns:
     -- Returns:
     --    1    -- string, or nil or false, with error keyword
     --    1    -- string|nil|false, with error keyword
     --    2    -- string, with JSON or context
     --    2    -- string, with JSON or context
     local m  = 0
     local m  = 0
Zeile 196: Zeile 205:




JSONutil.fault = function ( alert, add, alien )
JSONutil.fault = function ( alert, add, adapt )
     -- Retrieve formatted message
     -- Retrieve formatted message
     -- Parameter:
     -- Parameter:
     --    alert  -- string, with error keyword, or other text
     --    alert  -- string, with error keyword, or other text
     --    add    -- string, or nil or false, with context
     --    add    -- string|nil|false, with context
     --    alien -- string, or nil or false, with language codes
     --    adapt -- function|string|table|nil|false, for I18N
     -- Returns string, with HTML span
     -- Returns string, with HTML span
     local e = mw.html.create( "span" )
     local e = mw.html.create( "span" )
                     :addClass( "error" )
                     :addClass( "error" )
    local f = function ( all, at )
                  local slang
                  if at == "*" then
                      slang = mw.language.getContentLanguage():getCode()
                  elseif at then
                      slang = at
                  end
                  return all[ slang ]
              end -- f()
     local s = alert
     local s = alert
     if type( s ) == "string" then
     if type( s ) == "string" then
Zeile 235: Zeile 235:
                                 e = e[ 2 ]
                                 e = e[ 2 ]
                                 if type( e ) == "table" then
                                 if type( e ) == "table" then
                                     local q
                                     local q = type( adapt )
                                     if type( alien ) == "string" then
                                     if q == "function" then
                                      t = mw.text.split( alien, "%s+" )
                                        s = adapt( e, s )
                                        t = false
                                    elseif q == "string" then
                                        t = mw.text.split( adapt, "%s+" )
                                    elseif q == "table" then
                                        t = adapt
                                     else
                                     else
                                      t = { }
                                        t = { }
                                    end
                                    if t then
                                        table.insert( t, Fallback() )
                                        table.insert( t, "en" )
                                        for k = 1, #t do
                                            q = e[ t[ k ] ]
                                            if type( q ) == "string" then
                                                s = q
                                                break  -- for k
                                            end
                                        end  -- for k
                                     end
                                     end
                                    table.insert( t, "*" )
                                    table.insert( t, "en" )
                                    for k = 1, #t do
                                        q = f( e, t[ k ] )
                                        if type( q ) == "string" then
                                            s = q
                                            break  -- for k
                                        end
                                    end  -- for k
                                 else
                                 else
                                     s = "JSONutil.fault I18N bad #" ..
                                     s = "JSONutil.fault I18N bad #" ..
Zeile 279: Zeile 286:




JSONutil.fetch = function ( apply, always, alien )
JSONutil.fetch = function ( apply, always, adapt )
     -- Retrieve JSON data, or error message
     -- Retrieve JSON data, or error message
     -- Parameter:
     -- Parameter:
     --    apply  -- string, with presumable JSON text
     --    apply  -- string, with presumable JSON text
     --    always  -- true, if apply is expected to need preprocessing
     --    always  -- true, if apply is expected to need preprocessing
     --    alien  -- string, or nil or false, with language codes
     --    adapt  -- function|string|table|nil|false, for I18N
     -- Returns table, with data, or string, with error as HTML span
     -- Returns table, with data, or string, with error as HTML span
     local lucky, r
     local lucky, r
Zeile 293: Zeile 300:
         lucky, r = JSONutil.fair( apply )
         lucky, r = JSONutil.fair( apply )
         if lucky then
         if lucky then
             r = JSONutil.fault( lucky, r, alien )
             r = JSONutil.fault( lucky, r, adapt )
         else
         else
             lucky, r = pcall( mw.text.jsonDecode, r )
             lucky, r = pcall( mw.text.jsonDecode, r )
             if not lucky then
             if not lucky then
                 r = JSONutil.fault( r, false, alien )
                 r = JSONutil.fault( r, false, adapt )
             end
             end
         end
         end