Module:MM Schedule

local p = {} local h = {} local Data = require("Module:MM Schedule/Data")

local DataTable = require("Module:Data Table") local Term = require("Module:Term") local utilsArg = require("Module:UtilsArg") local utilsLayout = require("Module:UtilsLayout") local utilsMarkup = require("Module:UtilsMarkup") local utilsTable = require("Module:UtilsTable")

local CATEGORY_INVALID_ARGS = require("Module:Constants/category/invalidArgs")

function h.warn(msg, ...) local utilsError = require("Module:UtilsError") msg = string.format(msg, ...) utilsError.warn(msg) end

-- Template:MM Schedule/Header function p.Header(frame) local header = " " return footer end

-- Template:MM Schedule 2 (WIP) function p.Main2(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["MM Schedule"]) local categories = err and err.categoryText or "" local schedule, categories2 = h.parseSchedule(args.schedule) categories = categories..categories2 end

function h.parseSchedule(input) local rows = DataTable.parseRows(input) local entriesInput = utilsTable.map(rows, "cells") local entries, events, categories = h.parseEntries(entriesInput) local initialEntries = utilsTable.takeWhile(entries, function(entry)		return entry.eventConditions == nil	end) local conditionalEntries = utilsTable.dropWhile(entries, function(entry)		return entry.eventConditions == nil	end) local scheduleTree = h.buildTree(events, conditionalEntries) mw.logObject(events) mw.logObject(initialEntries) mw.logObject(scheduleTree) local schedule = h.printSchedule(initialEntries, scheduleTree) return schedule, categories end

function h.parseEntries(entriesInput) local categories = "" local entries = {} local events = {} local latestTimecard for i, entryInput in ipairs(entriesInput) do		local entry, errCategories = h.parseEntry(entryInput) categories = categories..errCategories if errCategories == "" then events = utilsTable.concat(events, entry.events) table.insert(entries, entry) end end events = utilsTable.uniqueBy(events, "name") return entries, events, categories end function h.parseEntry(entryInput) local entry = {} local categories = "" if #entryInput == 4 then local eventConditionsInput = table.remove(entryInput, 2) local eventConditions, events, categories2 = h.parseEvents(eventConditionsInput) entry.eventConditions = eventConditions entry.events = events categories = categories2 end if #entryInput == 3 then entry.time = entryInput[1] entry.actions = entryInput[3] local subject, notes = utilsMarkup.separateMarkup(entryInput[2]) local link = Term.link(subject, "MM3D") entry.location = link..notes elseif #entryInput == 1 then entry.timecard = entryInput[1] latestTimecard = entryInput[1] else h.warn("Invalid entry: %s", utilsTable.print(entryInput)) entry = nil categories = "" end return entry, categories end function h.parseEvents(eventConditionsInput) local events = {} local eventConditions local categories = "" for eventConditionInput in string.gmatch(eventConditionsInput, "%[([^%]]+)%]") do		local operator = string.match(eventConditionInput, "^([^:]+):") local eventName = string.gsub(eventConditionInput, "^([^:]+):", "") local event = Data.events[eventName] local eventState, categories = h.eventState(operator) if eventState == nil then h.warn("Invalid event condition . Recognized operators are   and  .", eventCondition) categories = categories.."" elseif event == nil then h.warn("Unrecognized event name . See Module:MM Schedule/Data for supported events.", eventName) categories = categories.."" else eventConditions = eventConditions or {} eventConditions[eventName] = eventState event = utilsTable.merge({ name = eventName }, event) table.insert(events, event) end end return eventConditions, events, categories end function h.eventState(operator) if operator == nil then return true elseif operator == "Not" then return false elseif operator == "Late" then return "late" else return nil end end

function h.buildTree(events, entries) -- TODO return entries end

function h.printSchedule(initialSchedule, scheduleTree) -- TODO end

p.Templates = { ["MM Schedule"] = { params = { ["..."] = {				name = "schedule", type = "content", trim = true, },		},	} }

return p