Modul:Text: Unterschied zwischen den Versionen

Aus WiiDatabase Wiki
Zur Navigation springenZur Suche springen
(Die Seite wurde neu angelegt: „local Text = { serial = "2017-11-01", suite = "Text" } --[=[ Text utilities ]=] -- local globals local PatternCJK = false local Patte…“)
 
(2019-11-12)
Zeile 1: Zeile 1:
local Text = { serial = "2017-11-01",
local Text = { serial = "2019-11-12",
               suite  = "Text" }
               suite  = "Text",
              item  = 29387871 }
--[=[
--[=[
Text utilities
Text utilities
]=]
]=]
 
local Failsafe  = Text
 
local GlobalMod = Text


-- local globals
-- local globals
Zeile 12: Zeile 13:
local PatternLatin      = false
local PatternLatin      = false
local PatternTerminated = false
local PatternTerminated = false
local QuoteLang        = false
local QuoteType        = false
local RangesLatin      = false
local RangesLatin      = false
local SeekQuote        = false
local SeekQuote        = false
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
    -- 2019-10-29
    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, 0 )
        end
    end
    return r
end -- foreignModule()




Zeile 21: Zeile 73:
local function factoryQuote()
local function factoryQuote()
     -- Create quote definitions
     -- Create quote definitions
     QuoteLang = { af        = "bd",
     if not Text.quoteLang then
                  ar        = "la",
        local quoting = foreignModule( "Text",
                  be        = "labd",
                                      false,
                  bg        = "bd",
                                      "quoting",
                  ca        = "la",
                                      Text.item )
                  cs        = "bd",
        if type( quoting ) == "table" then
                  da        = "bd",
            Text.quoteLang = quoting.langs
                  de        = "bd",
            Text.quoteType = quoting.types
                  dsb      = "bd",
        end
                  et        = "bd",
        if type( Text.quoteLang ) ~= "table" then
                  el        = "lald",
            Text.quoteLang = { }
                  en        = "ld",
        end
                  es        = "la",
        if type( Text.quoteType ) ~= "table" then
                  eu        = "la",
             Text.quoteType = { }
             --    fa        = "la",
        end
                  fi        = "rd",
        if type( Text.quoteLang.en ) ~= "string" then
                  fr        = "laSPC",
            Text.quoteLang.en = "ld"
                  ga        = "ld",
        end
                  he        = "ldla",
        if type( Text.quoteType[ Text.quoteLang.en ] ) ~= "table" then
                  hr        = "bd",
            Text.quoteType[ Text.quoteLang.en ] = { { 8220, 8221 },
                  hsb      = "bd",
                                                    { 8216, 8217 } }
                  hu        = "bd",
        end
                  hy        = "labd",
     end
                  id        = "rd",
                  is        = "bd",
                  it        = "ld",
                  ja        = "x300C",
                  ka        = "bd",
                  ko        = "ld",
                  lt        = "bd",
                  lv        = "bd",
                  nl        = "ld",
                  nn        = "la",
                  no        = "la",
                  pl        = "bdla",
                  pt        = "lald",
                  ro        = "bdla",
                  ru        = "labd",
                  sk        = "bd",
                  sl        = "bd",
                  sq        = "la",
                  sr        = "bx",
                  sv        = "rd",
                  th        = "ld",
                  tr        = "ld",
                  uk        = "la",
                  zh        = "ld",
                  ["de-ch"] = "la",
                  ["en-gb"] = "lsld",
                  ["en-us"] = "ld",
                  ["fr-ch"] = "la",
                  ["it-ch"] = "la",
                  ["pt-br"] = "ldla",
                  ["zh-tw"] = "x300C",
                  ["zh-cn"] = "ld" }
    QuoteType = { bd    = { { 8222, 8220 },  { 8218, 8217 } },
                  bdla  = { { 8222, 8220 },  {  171,  187 } },
                  bx    = { { 8222, 8221 },  { 8218, 8217 } },
                  la    = { {  171,  187 },  { 8249, 8250 } },
                  laSPC = { {  171,  187 },  { 8249, 8250 },  true },
                  labd  = { {  171,  187 },  { 8222, 8220 } },
                  lald  = { {  171,  187 },  { 8220, 8221 } },
                  ld    = { { 8220, 8221 },  { 8216, 8217 } },
                  ldla  = { { 8220, 8221 },  {  171,  187 } },
                  lsld  = { { 8216, 8217 },  { 8220, 8221 } },
                  rd    = { { 8221, 8221 },  { 8217, 8217 } },
                  x300C = { { 0x300C, 0x300D },
                            { 0x300E, 0x300F } } }
     return r
end -- factoryQuote()
end -- factoryQuote()


Zeile 102: Zeile 108:
     local r = apply
     local r = apply
     local suite
     local suite
     if not QuoteLang then
     factoryQuote()
        factoryQuote()
     suite = Text.quoteLang[ alien ]
    end
     suite = QuoteLang[ alien ]
     if not suite then
     if not suite then
         local slang = alien:match( "^(%l+)-" )
         local slang = alien:match( "^(%l+)-" )
         if slang then
         if slang then
             suite = QuoteLang[ slang ]
             suite = Text.quoteLang[ slang ]
         end
         end
         if not suite then
         if not suite then
             suite = QuoteLang[ "en" ]
             suite = Text.quoteLang.en
         end
         end
     end
     end
     if suite then
     if suite then
         local quotes = QuoteType[ suite ]
         local quotes = Text.quoteType[ suite ]
         if quotes then
         if quotes then
             local space
             local space
Zeile 232: Zeile 236:
     -- Returns: true, if CJK detected
     -- Returns: true, if CJK detected
     local r
     local r
     if not patternCJK then
     if not PatternCJK then
         patternCJK = mw.ustring.char( 91,
         PatternCJK = mw.ustring.char( 91,
                                       13312, 45,  40959,
                                       13312, 45,  40959,
                                       131072, 45, 178207,
                                       131072, 45, 178207,
                                       93 )
                                       93 )
     end
     end
     if mw.ustring.find( analyse, patternCJK ) then
     if mw.ustring.find( analyse, PatternCJK ) then
         r = true
         r = true
     else
     else
Zeile 269: Zeile 273:
         :gsub( "''(.+)''", "%1" )
         :gsub( "''(.+)''", "%1" )
         :gsub( " ", " " )
         :gsub( " ", " " )
     return r
     return mw.text.unstrip( r )
end -- Text.getPlain()
end -- Text.getPlain()


Zeile 442: Zeile 446:
     --    analyse  -- string
     --    analyse  -- string
     -- Returns: true, if sentence terminated
     -- Returns: true, if sentence terminated
     local r
     local r = mw.text.trim( analyse )
     if not PatternTerminated then
     if not PatternTerminated then
         PatternTerminated = mw.ustring.char( 91,
         PatternTerminated = mw.ustring.char( 91,
Zeile 451: Zeile 455:
                             .. "!%.%?…][\"'%]‹›«»‘’“”]*$"
                             .. "!%.%?…][\"'%]‹›«»‘’“”]*$"
     end
     end
     if mw.ustring.find( analyse, PatternTerminated ) then
     if mw.ustring.find( r, PatternTerminated ) then
         r = true
         r = true
     else
     else
Zeile 606: Zeile 610:
     return r
     return r
end -- Text.uprightNonlatin()
end -- Text.uprightNonlatin()
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()




Zeile 613: Zeile 662:
     if about == "quote" then
     if about == "quote" then
         factoryQuote()
         factoryQuote()
         r = { }
         r = { QuoteLang = Text.quoteLang,
        r.QuoteLang = QuoteLang
              QuoteType = Text.quoteType }
        r.QuoteType = QuoteType
     end
     end
     return r
     return r
Zeile 783: Zeile 831:
function p.ucfirstAll( frame )
function p.ucfirstAll( frame )
     return Text.ucfirstAll( frame.args[ 1 ] or "" )
     return Text.ucfirstAll( frame.args[ 1 ] or "" )
end
function p.unstrip( frame )
    return mw.text.trim( mw.text.unstrip( frame.args[ 1 ] or "" ) )
end
end


Zeile 833: Zeile 885:




function p.failsafe()
p.failsafe = function ( frame )
     return Text.serial
    -- Versioning interface
end
    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()