Module:ENReader

From Terra Invicta Official Wiki
Revision as of 06:18, 27 September 2025 by Redwah (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

---
---Convert the specified EN file on the wiki into a Lua table.
---
---@module ENReader
local ENReader = {}


--region Dependencies
-- none
--endregion



--region Private constants

local ERROR_MESSAGE_PARAMETER_MISSING = "ENReader requires as its parameter that you provide the name of a page on the wiki that contains valid EN data. Call the main function with a parameter that is the name of the desired page, like this:\n  `ENReader.main('Template:Data_EN`)`"

local ERROR_PREFIX_TITLE_INVALID_NAME = "ENReader failed to create a Mediawiki title object with the page name you provided. Double-check that the page name you provided is a properly formatted string and valid page title: "
local ERROR_PREFIX_TITLE_PAGE_DOES_NOT_EXIST = "ENReader checked the page name you provided, and the page does not exist. Double-check that the page name you provided is correct: "

local ERROR_PREFIX_CONTENT_NOT_FOUND = "ENReader did not find content on the page whose name you provided. Double check that the page exists and that it contains content: "
local ERROR_PREFIX_CONTENT_NOT_LONG_ENOUGH = "ENReader found content on the page whose name you provided, but it is too short to be useful. Double-check that you provided the correct name, and that there is content on the page: "

local MINIMUM_LENGTH_THAT_SUGGESTS_VALID_CONTENT = 4

local sfind = string.find
local ssub = string.sub
local slen = string.len

--endregion



--region Private member variables
--none
--endregion



--region Private methods

-- Splits the EN file into lines.
local fileSplit = function(text)
    local ret = {}
    local s, l = 1, slen( text )
    while s do
    	local e, n = sfind( text, '\n', s, true )
    	if not e then
    		ret[#ret+1] = ssub ( text, s )
    		s = nil
    	elseif n < e then
    		-- Empty separator!
    		ret[#ret+1] = ssub ( text, s, e )
    		if e < l then
    			s = e + 1
    		else
    			s = nil
    		end
    	else
    		ret[#ret+1] = e > s and ssub( text, s, e - 1 ) or ''
    		s = n + 1
    	end
    end
    return ret
end

-- Splits the individual lines of the EN file.
local lineSplit = function(text)
    local ret = {}
    local e, n = sfind( text, '=', 1, true)
    if not e then
    	return {'',''}
    end
    ret[1] = ssub(text, 1, e-1)
    local ce, cn = sfind( text, '//', 1, true)
    if not ce then
    	ret[2] = ssub(text, e+1)
    else
    	ret[2] = ssub(text, e+1, ce-1)
    end
    return ret
end

--endregion

--region Public methods

---Loads EN data from the specified page on the wiki into a Lua table.
---@param wikiPageName string the title of the wiki page, including
---@return table
function ENReader.convertENToLuaTable(wikiPageName)

	-- If Args Table from #invoke, then convert to String
	if type(wikiPageName) == 'table' then
		wikiPageName = wikiPageName.args[1]
	end

    -- Verify the parameter.
    if not wikiPageName or "" == wikiPageName then
        error(ERROR_MESSAGE_PARAMETER_MISSING)
    end

    -- Verify that the name is valid and that the page exists.
    local titleObject = mw.title.new(wikiPageName)
    if not titleObject then
        error(ERROR_PREFIX_TITLE_INVALID_NAME .. wikiPageName)
    end
    if not titleObject.exists then
        error(ERROR_PREFIX_TITLE_PAGE_DOES_NOT_EXIST .. wikiPageName)
    end

    -- Verify that the page content can be loaded and that there is something there to use.
    local unparsedContent = titleObject:getContent()
    if not unparsedContent then
        error(ERROR_PREFIX_CONTENT_NOT_FOUND .. wikiPageName)
    end
    if #unparsedContent < MINIMUM_LENGTH_THAT_SUGGESTS_VALID_CONTENT then
        error(ERROR_PREFIX_CONTENT_NOT_LONG_ENOUGH .. wikiPageName)
    end

	local decodedTable = {} -- Stores the table to return
	local ENlines = fileSplit(unparsedContent) -- split the EN into separate lines.

	for j, ENline in ipairs(ENlines) do -- Consider each EN line one by one:
		local ENlineSplit = lineSplit(ENline)
		decodedTable[ENlineSplit[1]] = ENlineSplit[2]
	end

    return decodedTable
end


---Loads EN data from the specified page on the wiki into a wiki table.
---@param wikiPageName string the title of the wiki page, including
---@return table
function ENReader.convertENToWikiTable(wikiPageName)
	local decodedTable = ENReader.convertENToLuaTable(wikiPageName)
	local returnString = '{| class="wikitable sortable mw-collapsible mw-collapsed" style="text-align: center;\n|-\n'
	for key, value in pairs(decodedTable) do
	    returnString = returnString .. '| ' .. key .. '\n' 
	    returnString = returnString .. '| ' .. value .. '\n' 
	    returnString = returnString .. '|-\n'
	end
	returnString = returnString .. '|}'
	return returnString
end

--endregion

return ENReader