Module:MM Schedule

local p = {} local h = {}

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) mw.logObject(events) mw.logObject(entries) local scheduleTree = h.buildTree(events, entries) local schedule = h.printSchedule(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 local entryEvents = utilsTable.keys(entry.eventConditions or {}) events = utilsTable.union({events, entryEvents}) table.insert(entries, entry) end end 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, categories2 = h.parseEvents(eventConditionsInput) entry.eventConditions = eventConditions 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 eventConditions local categories = "" for eventConditionInput in string.gmatch(eventConditionsInput, "%[([^%]]+)%]") do		local operator = string.match(eventConditionInput, "^([^:]+):") local event = string.gsub(eventConditionInput, "^([^:]+):", "") local eventState, categories = h.eventState(operator) if eventState == nil then h.warn("Invalid event condition . Recognized operators are   and  .", eventCondition) categories = categories.."" end eventConditions = eventConditions or {} eventConditions[event] = eventState end return eventConditions, 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 end

function h.printSchedule(scheduleTree) -- TODO end

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

return p