Module:GetData: Difference between revisions

From Bodhicitta
((by SublimeText.Mediawiker))
((by SublimeText.Mediawiker))
Line 40: Line 40:
     local finalString = ""
     local finalString = ""


     if parentArgs.intro ~= "" and parentArgs.intro ~= nil then
     if parentArgs.intro then
         for k,v in ipairs(propTable) do
         for k,v in ipairs(propTable) do
             if pData[v] ~= "" then
             if pData[v] ~= "" then
Line 49: Line 49:


     for i,prop in pairs(propTable) do
     for i,prop in pairs(propTable) do
         if pData[prop] ~= "" and pData[prop] ~= nil then
         if pData[prop] then


             local pdString = pData[prop]
             local pdString = pData[prop]


             local pdTable = {}
             local pdTable = {}
             if parentArgs.sep ~= "" and parentArgs.sep ~= nil then
             if parentArgs.sep then
                 local sep = parentArgs.sep
                 local sep = parentArgs.sep
                 for value in string.gmatch(pdString, "([^" .. sep .. "]+)") do
                 for value in string.gmatch(pdString, "([^" .. sep .. "]+)") do
Line 63: Line 63:
             end
             end


             if parentArgs.map ~= "" and parentArgs.map ~= nil then
             if parentArgs.map then
                 local map = parentArgs.map
                 local map = parentArgs.map
                 pdString = ""
                 pdString = ""
Line 72: Line 72:
             end
             end


             if parentArgs.printsep ~= "" and parentArgs.printsep ~= nil then
             if parentArgs.printsep then
             finalString = finalString .. parentArgs.printsep .. frame:preprocess( pdString )
             finalString = finalString .. parentArgs.printsep .. frame:preprocess( pdString )
             if mw.ustring.sub(finalString, 1, 1) == parentArgs.printsep then
             if mw.ustring.sub(finalString, 1, 1) == parentArgs.printsep then
Line 84: Line 84:
     end
     end


     if parentArgs.outro ~= "" and parentArgs.outro ~= nil then
     if parentArgs.outro then
         for k,v in ipairs(propTable) do
         for k,v in ipairs(propTable) do
             if pData[v] ~= "" then
             if pData[v] ~= "" then
Line 93: Line 93:


     if parentArgs.silent ~= "yes" then
     if parentArgs.silent ~= "yes" then
     if finalString ~= "" and finalString ~= nil then
     if finalString then
        return finalString
        return finalString
    else
    else
    if parentArgs.default ~= "" and parentArgs.default ~= nil then
    if parentArgs.default then
    return parentArgs.default
    return parentArgs.default
    end
    end

Revision as of 18:12, 25 September 2023

Documentation for this module may be created at Module:GetData/doc

local p = {}

function p.set(frame)

	local pData = mw.loadData('Module:GetData/'..frame.args['data'])
	local parentArgs = frame:getParent().args
    local props = parentArgs.prop
    local dataStore = {}

    for propValue in string.gmatch(props, "([^;]+)") do
        table.insert(dataStore, propValue .. '=' .. pData[propValue])
        if parentArgs.sep then
        	table.insert(dataStore, '+sep=' .. parentArgs.sep)
        end
    end

	local dataStored = mw.smw.set( dataStore )

    if dataStored == true then
        return '✅'
    else
        return 'An error occurred during the storage process. Message reads ' .. dataStored.error
    end


end

function p.main(frame)
	local pData = mw.loadData('Module:GetData/'..frame.args['data'])

	local parentArgs = frame:getParent().args

    local props = parentArgs.prop

    local propTable = {}
    for propValue in string.gmatch(props, "([^;]+)") do
        table.insert(propTable, propValue)
    end

    local finalString = ""

    if parentArgs.intro then
        for k,v in ipairs(propTable) do
            if pData[v] ~= "" then
                finalString = parentArgs.intro
            end
        end
    end

    for i,prop in pairs(propTable) do
        if pData[prop] then

            local pdString = pData[prop]

            local pdTable = {}
            if parentArgs.sep then
                local sep = parentArgs.sep
                for value in string.gmatch(pdString, "([^" .. sep .. "]+)") do
                    table.insert(pdTable, value)
                end
            else
                table.insert(pdTable, pdString)
            end

            if parentArgs.map then
                local map = parentArgs.map
                pdString = ""
                for k,v in pairs(pdTable) do
                    mapdVal = string.gsub(map, "@@@", v)
                    pdString = pdString .. mapdVal
                end
            end

            if parentArgs.printsep then
            	finalString = finalString .. parentArgs.printsep .. frame:preprocess( pdString )
            	if mw.ustring.sub(finalString, 1, 1) == parentArgs.printsep then
					finalString = mw.ustring.sub(finalString, 2)
				end
            else
            	finalString = finalString .. frame:preprocess( pdString )
            end

        end
    end

    if parentArgs.outro then
        for k,v in ipairs(propTable) do
            if pData[v] ~= "" then
                finalString = finalString .. parentArgs.outro
            end
        end
    end

    if parentArgs.silent ~= "yes" then
    	if finalString then
	        return finalString
	    else
	    	if parentArgs.default then
	    		return parentArgs.default
	    	end
	    end
	end
end

return p