Module:USN fleet totals/data explained

require('strict');

local patterns_tags =

local Article_content;local grand_total = 0;local planned_total = 0;

local wikitables_t =

local wikitable_names_t = ;local fleet_wikitables_t = ; -- only these for n_of_mlocal headers_t =

local totals_t =

local n_of_m_t = -- table of total counts (m in 'n of m')local retirements_year_max = 0;

----------------------------< A R T I C L E _ C O N T E N T _ G E T >----------------------------------------

get article content, remove certain html-like tags and their content so that this code doesn't include any citationtemplates inside the tags as valid tagets; they are not.

local function article_content_get if not Article_content then Article_content = mw.title.new ('List of current ships of the United States Navy'):getContent; for _, tag in ipairs (patterns_tags) do Article_content = Article_content:gsub (tag, ); -- remove certain html-like tags and their content end while Article_content:match ('([\r\n]+|%-) *[\r\n]+|%-') do -- are there multiple row markers without intervening column data? Article_content = Article_content:gsub ('([\r\n]+|%-) *[\r\n]+|%-', '%1'); -- remove duplicate row markers end endend

--... |} delimited wikitable

local function column_index_get (wikitable) local i = 0; local type_col, note_col; for label in wikitable:gmatch ('[\r\n](![^\r\n]+)') do -- spin through each header row i = i + 1; -- bump the indexer if label:match ('! *Type') then -- if this header row is for 'Type' type_col = i; -- save the indexer as column number elseif label:match ('! *Note') then -- if this header row is for 'Note' note_col = i; -- save the indexer as column number end end return type_col, note_col; end

---) and fetch the column numbers for Type and Note. Then find each row markup, countlines until Type column data are located, extract the contents (ship type) and add entry to the appropriate wikitablein wikitables_t; ship type already present, bump the count.

Continue to locate the Note column. If Note columnholds a (commissioned ships) or (non=commissioned ships) template, add shiptype to ; ship type already present, bump the count.

local function ship_types_get (wikitable, wikitable_index) local find_pattern = '|%-'; local type_label_index, note_label_index; local tstart, tend = wikitable:find (find_pattern); -- find the table row marker in ; should be column headers local i=0;

if tstart then -- if we found row marker (|-) for headers type_label_index, note_label_index = column_index_get (wikitable); -- try to find index of type label (!Type) and note label (!Note) tstart, tend = wikitable:find (find_pattern, tend); -- look for the next row else return nil; -- TODO: error message? end

while tstart do local i = 0; local column_text; -- the text that is the wikitable column markup + content (a row of text that is from: ) local rstart = tend; -- row start and end return values from string.find local rend; local pattern = '[\r\n]+|([^\r\n]*)'; -- newline, pipe, everything before the next newline local ship_type; -- the ship type; we need this for the Note column

while rstart do -- nil if not found rstart, rend, column_text = wikitable:find (pattern, rstart); -- get the row's individual column contents

if rstart then i = i + 1; -- bump the column counter

if type_label_index

i then -- is this the column that has the ship type entry (Type)? ship_type = column_text; -- save a copy of the Type column text for use with Note for the expected-to-be-retired section of this function

if wikitables_t[wikitable_names_t[wikitable_index]][ship_type] then -- if we found a ship type that we already know about: wikitables_t[wikitable_names_t[wikitable_index]][ship_type] = wikitables_t[wikitable_names_t[wikitable_index]][ship_type] + 1; -- bump the count else wikitables_t[wikitable_names_t[wikitable_index]][ship_type] = 1; -- add ship type to the table with a count of 1 else end totals_t[wikitable_names_t[wikitable_index]] = totals_t[wikitable_names_t[wikitable_index]] + 1; -- tally

elseif note_label_index