Module:Grammar

From Caves of Qud Wiki
Revision as of 21:28, 30 July 2019 by Teamtoto (talk | contribs)
Jump to navigation Jump to search

local p = {}
local cargo = mw.ext.cargo

function p.Main(frame)
for a, b in string.gmatch(frame.args[1], "(%w*)[:.](%w*)") do
  if (a == "player") then
     return "(player's " .. b .. ")"
  elseif (a =="pronouns") then
     local results = p.parse(b, frame.args[2])
     if not result then 
		return "No results!"
	end
     local formatted = p.formatResult(results)
     local returned = replace
     for _, row in ipairs(formatted) do
         for x, y in ipairs(row) do
            returned = y
         end
     end
     return returned
  elseif (a =="verb") then
     return "todo"
  else
     return "ERROR"
  end
return "error"
end
end

function p.parse(field, pronoun)
    tables='Pronouns'
    fields=possessive
    args = {
        where="pronoun=\"" .. "she/her" .. "\" AND _pageName=\"Gender and Pronouns\"",
        limit='1'
    }
    local result= cargo.query(tables,fields,args)
if not next(result) then
		-- this just checks if the table is non-empty
		-- the following code: 
		--   if result == {}
		-- will NOT work because Lua == when used on two tables only returns true if you are comparing
		-- one table to literally itself, and NOT another table that has the same structure & values as it
		-- however consider the following code:
		-- a = {}
		-- b = a
		-- in this case, a does equal b
		return nil
	else
		return result
	end
end

function p.formatResult(result)
	local formatted = {}
	-- this loop structure is pretty much always how you will process cargo data.
	-- you will loop over the ipairs iterator of the results table and apply a bunch of processing to the row, and save it.
	-- once it's been processed you will then print it.
	for k, row in ipairs(result) do
		formatted[k] = row
	end
	return formatted
end

return p