Modul:Text: Unterschied zwischen den Versionen
2019-11-12
ICON (Diskussion | Beiträge) (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 = " | 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 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 | ||
if not Text.quoteLang then | |||
local quoting = foreignModule( "Text", | |||
false, | |||
"quoting", | |||
Text.item ) | |||
if type( quoting ) == "table" then | |||
Text.quoteLang = quoting.langs | |||
Text.quoteType = quoting.types | |||
end | |||
if type( Text.quoteLang ) ~= "table" then | |||
Text.quoteLang = { } | |||
end | |||
if type( Text.quoteType ) ~= "table" then | |||
Text.quoteType = { } | |||
end | |||
if type( Text.quoteLang.en ) ~= "string" then | |||
Text.quoteLang.en = "ld" | |||
end | |||
if type( Text.quoteType[ Text.quoteLang.en ] ) ~= "table" then | |||
Text.quoteType[ Text.quoteLang.en ] = { { 8220, 8221 }, | |||
{ 8216, 8217 } } | |||
end | |||
end | |||
end -- factoryQuote() | end -- factoryQuote() | ||
Zeile 102: | Zeile 108: | ||
local r = apply | local r = apply | ||
local suite | local suite | ||
factoryQuote() | |||
suite = Text.quoteLang[ alien ] | |||
suite = | |||
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 = | suite = Text.quoteLang[ slang ] | ||
end | end | ||
if not suite then | if not suite then | ||
suite = | suite = Text.quoteLang.en | ||
end | end | ||
end | end | ||
if suite then | if suite then | ||
local quotes = | 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 | if not PatternCJK then | ||
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, | 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( | 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, | ||
QuoteType = Text.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: | ||
p.failsafe = function ( frame ) | |||
return | -- 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() | |||