Modul Diskussion:Benutzer:Kpfiwa/calc
Letzter Kommentar: vor 3 Jahren von Lómelinde in Abschnitt Linterfehler
Nur Zwischensicherung
Bearbeiten--[=[ calc Version 1b BETA, 2020-007-06
project management: [[:de:User:Gadacz]]
Should still be thoroughly "cleaned" and optimized.
Due the different language versions only after consultation.
Various extensions planned (number formatting, attributes, alignment, more
calculation options, e.g.: item no/rank {# {1}} and much more
© „CC-by-sa 4.0“
--]=]
----
--- https://en.wikipedia.org/wiki/Module:Math
--[[ https://phabricator.wikimedia.org/T68051 BEACHTE PREPROCESS!! Hilfe:Lua/Modul_im_Wiki
https://en.wikipedia.org/wiki/Wikipedia:Lua#Unit_testing
mw.getContentLanguage().code -- Returns the content language.
mw.getCurrentFrame():preprocess("{{PAGELANGUAGE}}") -- Returns the page language.
mw.getCurrentFrame():preprocess( "{{int:Lang}}" ) -- Returns the user language
(if the wiki has created the messages needed to support this feature).
--]]
local p = {} -- transfer array
text, result1, result2 , txt= '',{}, {}, '' -- for interim results
local regexg = "%{([^{]-)%{.*" -- extract group sign
local regexv = "%{[^{]-%{([%d-+*/^|]*)}}" -- AKTUELL NUR ZAHLENWERTE! extract value (number or 'sum', 'avg', 'min', 'max', numbers in combinations)
local l10na = {}
l10na["de"] = {
frm = "for", -- Format
max = "max", -- Maximum
min = "min", -- Minimum
ave = "dur", -- Durchschnitt
sum = "sum", -- Summe
tot = "tot", -- Total
sub = "zwi", -- Zwichensumme
emp = "dek", -- "Dekoration" (Schriftbild, Ausrichtung)
}
l10na["en"] = {
frm = "for", -- format
max = "max", -- maximum
min = "min", -- minimum
ave = "ave", -- average
sum = "sum", -- sum
tot = "tot", -- total
sub = "sub", -- subtotal
emp = "emp", -- emphasis, typeface
}
l10na["lv"] = {
frm = "for", -- formāts
max = "max", -- maksimums
min = "min", -- minimāls
ave = "vid", -- vidējais
sum = "kop", -- kopā
tot = "vis", -- viss
sub = "sta", -- starpsumma
emp = "rot", -- rotājums
}
l10na["ru"] = {
frm = "фор", -- формат
max = "мак", -- максимальная
min = "мин", -- минимальный
ave = "дис", -- дисплей
sum = "всь", -- всього
tot = "все", -- Все
sub = "про", -- Промежуточный итог
emp = "укр", -- украшение
}
l10na["uk"] = {
frm = "фор", -- формат
max = "мак", -- максимум
min = "мін", -- мінімум
ave = "сре", -- средний
sum = "всь", -- всього
tot = "все", -- Все
sub = "під", -- Підточний
emp = "при", -- прикраса
}
local l10n = l10na[mw.language.getContentLanguage():getCode()]
if not l10n then
l10n = l10na.en
end
-- say = l10n.msgA
local function tu(a,z) --[[ converts a to uppercase and extracts the first character
or z number of characters utf8 --]]
-- return string.sub(string.upper(a), 1, z or 1)
local a = mw.ustring.sub(a, 1, z or 1)
a = mw.ustring.upper(a)
return a
end
local function tonum(ein) -- converts numbers entered differently
local erg, stat = '',0
erg, stat = string.gsub(ein, "(%D*)([%s%-%d,%.%']+)(%D*)", "%1|%2|%3")
if stat > 0 then
nix, erg ,nix = string.match(erg, "(.*)|(.*)|(.*)")
erg=string.gsub(string.reverse(erg), "[%.,]+", "QIX",1) -- QIX = temp dummy wo any risk of confusion
erg=string.gsub(erg, "[%.,%s%']+", "")
erg=string.reverse(string.gsub(erg, "QIX", "."))*1
end -- if stat
return erg, ein
end
local function emph(was) -- emphasis, typeface for the text acc. template parameters 'd = ...' (decoration)
local css = ''
-- if not was or #was < 1 then return 'NIX' end - undecorated return
if #was <= 6 then -- what is larger will be a directly entered css statement
was = tu(was,6)
for e in string.gmatch(was, ".") do -- separate if several parameters
if e == 'F' or e == 'B' then css = css .. " font-weight:bold;"
elseif e == 'K' or e == 'I' then css = css .. " font-style:italic;" -- geman kursiv
elseif e == 'U' then css = css .. " text-decoration:underline;"
elseif e == 'V' or e == 'C' then css = css .. " text-transform:uppercase;" -- german versal
elseif e == 'S' then css = css .. " font-variant:small-caps;"
elseif e == 'L' then css = css .. " letter-spacing:0.3em;"
end -- if e
end -- for e
else -- if #was
was = string.gsub(was,"['%\"]+",'') -- possibly remove quotation marks
css = string.gsub(was,"[%w]+%s*=%s*",'') -- if accidentally started with 'string ='
end -- if #was
return css
end
--[[ NICHT FÜR de-WP!!! Da gab es Löschung von Vorlage:Coltab, aus der die Farbgebung stammt
function hsv2rgb(h,s,v)
function val2hsv(val, max, min, gb)
--]]
--[=[ siehe auch
https://de.wikipedia.org/wiki/Hilfe:Lua/Modul_im_Wiki#frame:expandTemplate()
https://de.wikipedia.org/wiki/Hilfe:Lua/Modul_im_Wiki#frame:preprocess()
in [[:de:Modul:Flagge]] : tt=trailer; trailer = frame:preprocess{text = tt}
--]=]
function prep(a) -- extract group
ret = string.gsub(a,'([^{^}]+)',"%1") --regex for group-name [A-Z0-9#]
return tu(ret)
end
function templatevalues(t) -- processes all contained templates into values
--[[
frame:expandTemplate{ title = title, args = table } ?????
https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual/de#frame:expandTemplate
z, zr= string.gsub(t,reg,prep("%1") or nil);zz=zz+1
if zr > 0 then
return z, zr
end
while zzr do -- derzeit MIST
txt,zzr=filter(txt)
if zzr then print(txt,zzr) end
end --]]
return t
end
-- https://stackoverflow.com/questions/20598937/handling-cr-line-endings-in-lua >>
local function cr_lines(s)
--return s:gsub('\r\n?', '\n'):gmatch('(.-)\n')
return s:gsub('\n', '\n\nX')
end
function nowiex(txt,frame) --- unstrip nowiki & process all including templates
return frame:preprocess(mw.text.unstripNoWiki(txt))
end
--[=[ function p.f(frame)
* Evaluation
** Calculation of the color value via val2hsv and hsv2rgb - NICHT FÜR de-WP
** Composition of the field content in the table including
formatting and coloring instructions. - NICHT FÜR de:WP
* Return of the result as an array (Lua table)
!! Note: The identifiers for the parameters are reduced to the first
letter and changed to upper case
Lower case is meaningless, the identifier can be chosen as long as the
first letter is unique
Parameter:
@param 1 or named-key = 'V'alie oder de:'W'ert
transfer of the text to be evaluated.
If the text contains pipes, especially tables,
the transferred text must be included in <nowiki> ... </nowiki>
@param 2 oder 'F'ormat
Specification of formatting numerical values to be used:
thousands separator, decimal separator, preceding minus, identifier (€ % $ km² ...)
@param 'O'rientation if different from general specification of table
O=L -> left-justified
O=R -> right-justified
O=Z oder O=C -> centered :de:zentriert
O=D Alignment with decimal point/-comma (# of dec places if necessary)
@param 'A'ttribute: addition like 'style = ', colspan, rowspan, class ...
@param 'C'ontrast, <100 fades -- NICHT FÜR de-WP!!!
--]=]
function p.f(frame)
local numval, maximum, kontrast, hell , minimum = 0, 10, 25, 100, 0
local stringval, orient, anhang, sort, erg , class, css, style = '', '', '', '', '', '','', ''
local ft
frame = frame:getParent() -- first call up the transferred parameters
for key, val in pairs(frame.args) do -- assign individual parameters
key = tu(key) -- convert/shorten all parameters to one capital letter
if key == "1" or key == 'V' or key == 'W' then text = nowiex(val,frame) -- frame:preprocess(mw.text.unstripNoWiki(val))-- ; txt='</nowiki>' .. text-- processes all contained templates into values
--if key == "1" or key == 'V' or key == 'W' then text = templatevalues(val) -- processes all contained templates into values
elseif key == "2" or key == 'F' then format = val --[[ General formatting
of the numbers (# of digits, decimal places, signs, characters,
such as $ € km³ % ... --]]
end -- if key
end -- for key
--[[ The now translated text (template evaluation) is searched for "{? [...}}""
and the instructions specified there are executed.
Then the processed text with the calculation results is transferred
to the function call. --]]
--local mm = mw.getCurrentFrame():preprocess( "{{int:Lang}} {{Flagge|DEU}}", text )
local mmm = frame:preprocess("\n"..text) -- \n wichtig für Tabelle
txt=string.gsub(txt,"%{%|",'-CR-{|\n')
--return text, '<br>PREPROCESS= '..mm.."<br>TEXT="..txt1
--ret= 'TEXT=\n'.. text .. "<br>PREPROCESSED=<br>\n".. mmm
--ret= string.gsub(ret,"%{%|","TBL\n{|")
return text
end -- p.f function
return p
Linterfehler
BearbeitenDa diese Seite Linterfehler auslöst setze ich den Code mal in syntaxhighlight, ich hoffe das ist ok. --Liebe Grüße, Lómelinde Diskussion 17:51, 11. Feb. 2021 (CET)