Module:ENReader: Difference between revisions
From Terra Invicta Official Wiki
Created page with "--- ---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 `CSVReader.main('Template:Data_EN`)`" lo..." |
No edit summary |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
--- | --- | ||
---Convert the specified EN file on the wiki into a Lua table. | ---Convert the specified EN file on the wiki into a Lua table. | ||
--- | --- | ||
---@module ENReader | ---@module ENReader | ||
local ENReader = {} | local ENReader = {} | ||
--region Dependencies | --region Dependencies | ||
| Line 14: | Line 14: | ||
--region Private constants | --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 ` | 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_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: " | ||
| Line 23: | Line 23: | ||
local MINIMUM_LENGTH_THAT_SUGGESTS_VALID_CONTENT = 4 | local MINIMUM_LENGTH_THAT_SUGGESTS_VALID_CONTENT = 4 | ||
local sfind = string.find | |||
local ssub = string.sub | |||
local slen = string.len | |||
--endregion | --endregion | ||
| Line 36: | Line 40: | ||
--region Private methods | --region Private methods | ||
-- Splits the lines of the EN file. | -- Splits the EN file into lines. | ||
function | 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 ret = {} | ||
local e, n = | local e, n = sfind( text, '=', 1, true) | ||
if not e then | if not e then | ||
return {'',''} | return {'',''} | ||
end | end | ||
ret[1] = | ret[1] = ssub(text, 1, e-1) | ||
local ce, cn = | local ce, cn = sfind( text, '//', 1, true) | ||
if not ce then | if not ce then | ||
ret[2] = | ret[2] = ssub(text, e+1) | ||
else | else | ||
ret[2] = | ret[2] = ssub(text, e+1, ce-1) | ||
end | end | ||
return ret | return ret | ||
| Line 91: | Line 120: | ||
local decodedTable = {} -- Stores the table to return | local decodedTable = {} -- Stores the table to return | ||
local ENlines = | local ENlines = fileSplit(unparsedContent) -- split the EN into separate lines. | ||
for j, ENline in ipairs(ENlines) do -- Consider each | for j, ENline in ipairs(ENlines) do -- Consider each EN line one by one: | ||
local | local ENlineSplit = lineSplit(ENline) | ||
decodedTable[ | decodedTable[ENlineSplit[1]] = ENlineSplit[2] | ||
end | end | ||
| Line 102: | Line 131: | ||
---Loads | ---Loads EN data from the specified page on the wiki into a wiki table. | ||
---@param wikiPageName string the title of the wiki page, including | ---@param wikiPageName string the title of the wiki page, including | ||
---@return table | ---@return table | ||
| Line 119: | Line 148: | ||
--endregion | --endregion | ||
return | return ENReader | ||
Latest revision as of 06:18, 27 September 2025
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


