Jump to content

Module:Jcttop/core: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Minor syntax fix to solve issue with hardcoded junction lists
Jackmcbarn (talk | contribs)
use mw.html and other fixes from sandbox
Line 1: Line 1:
local p = {}
local p = {}


local format = mw.ustring.format -- Local version of string formatting function
local format = string.format -- Local version of string formatting function
local HtmlBuilder = require "Module:HtmlBuilder" -- Import module to build HTML code
local getArgs = require('Module:Arguments').getArgs -- Import module function to work with passed arguments


local function hatnote(args)
local function hatnote(args)
local insert = table.insert
local insert = table.insert
local text = {}
local text = {args.region_note}
local tense
local tense
insert(text, args.region_note)
local hatnoteArg = args.hatnote
local hatnoteArg = args.hatnote
if hatnoteArg == 'off' then
if hatnoteArg == 'off' then
Line 22: Line 19:
if indep_city or sub1 or sub2 then
if indep_city or sub1 or sub2 then
local region = args.region
local region = args.region
local type = args.type or 'route'
tense = args.former == 'yes'
tense = args.former == 'yes'
local verb = tense and 'was' or 'is'
local verb = tense and 'was' or 'is'
insert(text, format("The entire %s %s in ", type, verb))
insert(text, format("The entire %s %s in ", args.type or 'route', verb))
if indep_city then
if indep_city then
insert(text, format("[[%s, %s|%s]]", indep_city, region, indep_city))
insert(text, format("[[%s, %s|%s]]", indep_city, region, indep_city))
Line 36: Line 32:
insert(text, format(" (%s)", area))
insert(text, format(" (%s)", area))
end
end
local sub1dab = args.sub1dab == 'yes'
if args.sub1dab == 'yes' then
if sub1dab then
insert(text, format(", %s %s", sub1, sub1name))
insert(text, format(", %s %s", sub1, sub1name))
end
end
insert(text, format(", %s|%s]]", region, sub2))
insert(text, format(", %s|%s]]", region, sub2))
end
if sub1 and sub2 then
insert(text, ', ')
end
end
if sub1 then
if sub1 then
if sub2 then
insert(text, ', ')
end
insert(text, format("[[%s %s, %s|%s %s]]", sub1, sub1name, region, sub1, sub1name))
insert(text, format("[[%s %s, %s|%s %s]]", sub1, sub1name, region, sub1, sub1name))
end
end
Line 56: Line 51:
if args.unnum == 'yes' then
if args.unnum == 'yes' then
local verb = tense and 'were' or 'are'
insert(text, format("All exits %s unnumbered.", tense and 'were' or 'are'))
insert(text, format("All exits %s unnumbered.", verb))
end
end
Line 64: Line 58:


local function header(args)
local function header(args)
local row = HtmlBuilder.create().tag('tr')
local row = mw.html.create('tr')
local region_col = args.region_col
local region_col = args.region_col
if region_col then
if region_col then
row.tag('th').attr('scope', 'col').wikitext(mw.language.getContentLanguage():ucfirst(region_col))
row:tag('th'):attr('scope', 'col'):wikitext(mw.language.getContentLanguage():ucfirst(region_col))
end
end
local nosub1 = args.nosub1 == 'yes'
local indep_city = args.indep_city
local indep_city = args.indep_city
if not(nosub1 or args.sub1 or indep_city) then
if not(args.nosub1 == 'yes' or args.sub1 or indep_city) then
local tag = row.tag('th').attr('scope', 'col')
local tag = row:tag('th'):attr('scope', 'col')
local sub1disp = args.sub1disp
local sub1disp = args.sub1disp
if sub1disp then
if sub1disp then
tag.wikitext(sub1disp)
tag:wikitext(sub1disp)
else
else
tag.wikitext(args.sub1name).wikitext(args.sub1_ref)
tag:wikitext(args.sub1name).wikitext(args.sub1_ref)
end
end
end
end
if not(args.sub2 or indep_city) then
if not(args.sub2 or indep_city) then
local tag = row.tag('th').attr('scope', 'col')
row:tag('th'):attr('scope', 'col')
local location_def = args.location_def or 'Location'
:wikitext(args.location_def or 'Location'):wikitext(args.sub2_ref)
tag.wikitext(location_def).wikitext(args.sub2_ref)
end
end
local altunit = args.altunit
local altunit = args.altunit
if altunit then
if altunit then
row.tag('th').attr('scope', 'col').wikitext(altunit).wikitext(args.altunit_ref)
row:tag('th'):attr('scope', 'col'):wikitext(altunit):wikitext(args.altunit_ref)
else
else
local unit = args.length or args.unit
local unit = args.length or args.unit
if unit ~= 'off' then
if unit ~= 'off' then
row.tag('th').attr('scope', 'col').wikitext(unit).wikitext(args.length_ref).done().tag('th').attr('scope', 'col').wikitext(args.unit2)
row:tag('th'):attr('scope', 'col'):wikitext(unit):wikitext(args.length_ref):done():tag('th'):attr('scope', 'col'):wikitext(args.unit2)
end
end
end
end
Line 100: Line 92:
local exit = args[1]
local exit = args[1]
if exit == 'old' then
if exit == 'old' then
local old_def = args.old_def or 'Old exit'
row:tag('th'):attr('scope', 'col'):wikitext(args.old_def or 'Old exit'):wikitext(args.old_ref)
row.tag('th').attr('scope', 'col').wikitext(old_def).wikitext(args.old_ref)
row:tag('th'):attr('scope', 'col'):wikitext(args.exit_def or 'New exit'):wikitext(args.exit_ref)
local exit_def = args.exit_def or 'New exit'
row.tag('th').attr('scope', 'col').wikitext(exit_def).wikitext(args.exit_ref)
elseif exit == 'exit' then
elseif exit == 'exit' then
local exit_def = args.exit_def or 'Exit'
row:tag('th'):attr('scope', 'col'):wikitext(args.exit_def or 'Exit'):wikitext(args.exit_ref)
row.tag('th').attr('scope', 'col').wikitext(exit_def).wikitext(args.exit_ref)
end
end
if args[2] == 'name' then
if args[2] == 'name' then
local name_def = args.name_def or 'Name'
row:tag('th'):attr('scope', 'col'):wikitext(args.name_def or 'Name'):wikitext(args.name_ref)
row.tag('th').attr('scope', 'col').wikitext(name_def).wikitext(args.name_ref)
end
end
local dest_def = args.dest_def or 'Destinations'
row:tag('th'):attr('scope', 'col'):wikitext(args.dest_def or 'Destinations'):wikitext(args.dest_ref)
row.tag('th').attr('scope', 'col').wikitext(dest_def).wikitext(args.dest_ref)
local notes_def = args.notes_def or 'Notes'
row:tag('th'):attr('scope', 'col'):wikitext(args.notes_def or 'Notes'):wikitext(args.notes_ref)
row.tag('th').attr('scope', 'col').wikitext(notes_def).wikitext(args.notes_ref)
return '\n{| class="plainrowheaders wikitable hlist"\n' .. tostring(row)
return '\n{| class="plainrowheaders wikitable hlist"\n' .. tostring(row)
Line 131: Line 117:
function p.jcttop(frame)
function p.jcttop(frame)
-- Entry function for {{jcttop/core}}
-- Entry function for {{jcttop/core}}
return p._jcttop(require('Module:Arguments').getArgs(frame)) -- Simply call another function with those arguments to actually create the header.
local args = getArgs(frame) -- Gather passed arguments into easy-to-use table.
return p._jcttop(args) -- Simply call another function with those arguments to actually create the header.
end
end



Revision as of 02:39, 5 December 2014

local p = {}

local format = string.format -- Local version of string formatting function

local function hatnote(args)
	local insert = table.insert
	local text = {args.region_note}
	local tense
	
	local hatnoteArg = args.hatnote
	if hatnoteArg == 'off' then
		insert(text, '')
	elseif hatnoteArg then
		insert(text, hatnoteArg .. " ")
	else
		local indep_city = args.indep_city
		local sub1 = args.sub1
		local sub2 = args.sub2
		if indep_city or sub1 or sub2 then
			local region = args.region
			tense = args.former == 'yes'
			local verb = tense and 'was' or 'is'
			insert(text, format("The entire %s %s in ", args.type or 'route', verb))
			if indep_city then
				insert(text, format("[[%s, %s|%s]]", indep_city, region, indep_city))
			else
				local sub1name = args.sub1name
				if sub2 then
					insert(text, "[[" .. sub2)
					local area = args.area
					if area then
						insert(text, format(" (%s)", area))
					end
					if args.sub1dab == 'yes' then
						insert(text, format(", %s %s", sub1, sub1name))
					end
					insert(text, format(", %s|%s]]", region, sub2))
				end
				if sub1 then
					if sub2 then
						insert(text, ', ')
					end
					insert(text, format("[[%s %s, %s|%s %s]]", sub1, sub1name, region, sub1, sub1name))
				end
			end
		insert(text, '. ')
		insert(text, args.sub1_ref)
		insert(text, args.sub2_ref)
		end
	end
	
	if args.unnum == 'yes' then
		insert(text, format("All exits %s unnumbered.", tense and 'were' or 'are'))
	end
	
	return mw.text.trim(table.concat(text))
end

local function header(args)
	local row = mw.html.create('tr')
	local region_col = args.region_col
	if region_col then
		row:tag('th'):attr('scope', 'col'):wikitext(mw.language.getContentLanguage():ucfirst(region_col))
	end
	
	local indep_city = args.indep_city
	if not(args.nosub1 == 'yes' or args.sub1 or indep_city) then
		local tag = row:tag('th'):attr('scope', 'col')
		local sub1disp = args.sub1disp
		if sub1disp then
			tag:wikitext(sub1disp)
		else
			tag:wikitext(args.sub1name).wikitext(args.sub1_ref)
		end
	end
	
	if not(args.sub2 or indep_city) then
		row:tag('th'):attr('scope', 'col')
			:wikitext(args.location_def or 'Location'):wikitext(args.sub2_ref)
	end
	
	local altunit = args.altunit
	if altunit then
		row:tag('th'):attr('scope', 'col'):wikitext(altunit):wikitext(args.altunit_ref)
	else
		local unit = args.length or args.unit
		if unit ~= 'off' then
			row:tag('th'):attr('scope', 'col'):wikitext(unit):wikitext(args.length_ref):done():tag('th'):attr('scope', 'col'):wikitext(args.unit2)
		end
	end
	
	local exit = args[1]
	if exit == 'old' then
		row:tag('th'):attr('scope', 'col'):wikitext(args.old_def or 'Old exit'):wikitext(args.old_ref)
		row:tag('th'):attr('scope', 'col'):wikitext(args.exit_def or 'New exit'):wikitext(args.exit_ref)
	elseif exit == 'exit' then
		row:tag('th'):attr('scope', 'col'):wikitext(args.exit_def or 'Exit'):wikitext(args.exit_ref)
	end
	
	if args[2] == 'name' then
		row:tag('th'):attr('scope', 'col'):wikitext(args.name_def or 'Name'):wikitext(args.name_ref)
	end
	
	row:tag('th'):attr('scope', 'col'):wikitext(args.dest_def or 'Destinations'):wikitext(args.dest_ref)
	
	row:tag('th'):attr('scope', 'col'):wikitext(args.notes_def or 'Notes'):wikitext(args.notes_ref)
	
	return '\n{| class="plainrowheaders wikitable hlist"\n' .. tostring(row)
end

function p._jcttop(args)
	-- This function calls two other functions to generate a hatnote and header row.
	-- This function is accessible from other Lua modules.
	return hatnote(args) .. header(args)
end

function p.jcttop(frame)
	-- Entry function for {{jcttop/core}}
	return p._jcttop(require('Module:Arguments').getArgs(frame)) -- Simply call another function with those arguments to actually create the header.
end

return p