Modul:SimpleStruct
Modul:SimpleStruct
| Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | |||
| Modul | Deutsch | English
|
Modul: | Dokumentation | ||
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
--[=[ SimpleStruct 2021-12-21
Module for generating tables and strukts from simple strings
Author: Vollbracht
Version: 2022-07-03 no change to previous; added altParse
* SimpleStruct.parse(string)
* SimpleStruct.altParse(string) variant interpretation of more limited string
]=]
-- Module globals
local SimpleStruct = {}
--[[
altParse
alternative Parser for strictly limited string parameters
StringPar string in Format
<Label1>{<value1>}
<Label2>{<value2>}
<Label3>{<value3>}
returns table ordered by input positions
{{<Label1>, <value1>}, {<Label2>, <value2>}, {<Label3>, <value3>}}
where Parser.parse would return table without order
{<Label1>{<value1>}, <Label2>{<value2>}, <Label3>{<value3>}}
]]
SimpleStruct.altParse = function(StringPar)
local result = {}
local tabR = mw.text.split(StringPar, '}')
if string.gsub(tabR[#tabR], '^%s*(.-)%s*$', '%1') == '' then
tabR[#tabR] = nil
end
for i, elmR in ipairs(tabR) do
local tabL = mw.text.split(elmR, '{')
if #tabL ~= 2 then
table.insert(result, {"Fehler", "Bitte hier nur Elemente im Format <label>{<Wert>} eintragen! [[Kategorie:Wikipedia:Qualitätssicherung Vorlageneinbindung fehlerhaft]] "})
else
table.insert(result,
{string.gsub(tabL[1], '^%s*(.-)%s*$', '%1'),
string.gsub(tabL[2], '^%s*(.-)%s*$', '%1')})
end
end
return result
end
local ElmList
local currentIndex
local StopIndex
-- Iterator, omitting all ipairs
local namePairs = function(namedTable)
local nmKeys = {}
for elm in pairs(namedTable) do
if type(elm) == 'string' then table.insert(nmKeys, elm) end
end
local i = 0
local namePairs_iterator = function()
i = i + 1
return nmKeys[i], namedTable[nmKeys[i]]
end
return namePairs_iterator
end
local function generateStruct()
local result = {}
while currentIndex <= StopIndex do
if type(ElmList[currentIndex]) == 'string' then
table.insert(result, ElmList[currentIndex])
currentIndex = currentIndex + 1
elseif ElmList[currentIndex] == -1 then
currentIndex = currentIndex + 1
return result
else
-- assert no changes to ElmList structure: only string, table, or -1
assert(type(ElmList[currentIndex]) == 'table')
local name = ElmList[currentIndex][1]
currentIndex = currentIndex + 1
local childValue = generateStruct()
if name == '' then
-- containing unnamed structure
-- copy name-value pairs from child
for pname, value in namePairs(childValue) do result[pname] = value end
-- copy unnamed values from child
for _, value in ipairs(childValue) do table.insert(result, value) end
else
-- assert store
if result[name] == nil then result[name] = {} end
-- copy name-value pairs from child
for pname, value in namePairs(childValue) do
if type(value) == 'table' and #value == 1 and type(value[1]) == 'string'
then result[name][pname] = value[1]
else result[name][pname] = value
end
end
-- copy unnamed values from child
for i, value in ipairs(childValue) do
table.insert(result[name], value)
end
end
end
end
return result
end
SimpleStruct.parse = function (StringPar)
if StringPar == '' then
ElmList = {}
currentIndex = 0
StopIndex = 0
return
end
local tabR = mw.text.split(StringPar, '}')
-- trim: prevent tailing empty portion from being processed
if string.gsub(tabR[#tabR], '^%s*(.-)%s*$', '%1') == '' then tabR[#tabR] = nil end
-- process all portions
ElmList = {}
for i, elmR in ipairs(tabR) do
-- trim:
elmR = string.gsub(elmR, '^%s*(.-)%s*$', '%1')
if elmR == '' then -- nothing but closing bracket
table.insert(ElmList, -1)
else
local tabL = mw.text.split(elmR, '{')
if #tabL == 1 then -- plain value before closing bracket
-- process plain value
table.insert(ElmList, elmR)
-- process bracket
table.insert(ElmList, -1)
else
local j = 1
while j < #tabL do
-- process opening brackets
table.insert(ElmList, {string.gsub(tabL[j], '^%s*(.-)%s*$', '%1')})
j = j + 1
end
-- process plain value
local v = string.gsub(tabL[j], '^%s*(.-)%s*$', '%1')
table.insert(ElmList, v)
-- process bracket
table.insert(ElmList, -1)
end
end
end
currentIndex = 1
StopIndex = #ElmList
local result = generateStruct()
return result
end
return SimpleStruct
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.