Module:Sandbox/Sebschlicht
Appearance
Documentation for this module may be created at Module:Sandbox/Sebschlicht/doc
local u = {}
local e = nil;
local function splitString(stringValue, pattern)
local splitted = {}
for s in stringValue:gmatch(pattern) do
table.insert(splitted, s);
end
return splitted;
end
local function splitLines(stringValue)
return splitString(stringValue, "(.-)\r?\n");
end
-- costs: 1
local function getDefaultIndex()
local crrTitle = mw.title.getCurrentTitle();
local rootTitle = crrTitle.rootPageTitle;
return rootTitle.fullText .. "/MoocIndex";
end
-- costs: 2 (1 intern)
local function readIndex(indexUrl)
local indexUrl = indexUrl or getDefaultIndex();
local indexPage = mw.title.new(indexUrl);
if indexPage then
return indexPage:getContent();
else
error('failed to read index from "' .. indexUrl .. '"');
end
end
local function extractFromIndex(indexPlain, itemType, itemName)
local itemIdentifier = itemType .. "|" .. itemName;
local textLines = splitLines(indexPlain);
return #textLines;
end
local function loadIndex(typeIdentifier, itemName, indexUrl)
local itemName = itemName;
if not itemName then
error('item name is missing');
end
local indexUrl = getDefaultIndex();
local itemType = typeIdentifier;
if not itemType then
error('item type is missing');
end
local indexPlain = readIndex(indexUrl);
if not indexPlain then
error("index is empty @ " .. indexUrl);
end
local indexRelevant = extractFromIndex(indexPlain, itemType, itemName);
return indexUrl;
end
function u.renderLesson( frame )
local itemName = frame.args["name"];
local itemType = frame.args["type"];
local indexUrl = frame.args["indexUrl"];
local isSuccess, value = pcall(loadIndex, itemType, itemName, indexUrl);
if isSuccess then
return value;
else
-- value is error message
return 'failed to load MOOC index: ' .. value;
end
end
return u