Module:Element: Difference between revisions

From   
Content added Content deleted
(Created page with "--[[ Please remember to use the talk page before changing this template! When the first Lua version was written this was used on more than 5000 pages, now it is probably some 3000 more. A change will cause a re-rendering of all pages. Keep that in mind! ]]-- local p = {} local translations = mw.loadData('Module:Element/translation') -- start for Template:Area function p.area(frame) -- extracting arguments local areaId = frame.args[1] local areaName = frame.a...")
 
No edit summary
 
Line 131: Line 131:
else
else
output[4] = '[[File:Osm element ' .. elementDescription .. '.svg|20px|link='
output[4] = '[[File:Osm element ' .. elementDescription .. '.png|20px|link='
if langcodeInput ~= nil and langcodeInput ~= 'en' then
if langcodeInput ~= nil and langcodeInput ~= 'en' then
output[5] = langcodeInput .. ':' .. string.upper(string.sub(elementDescription, 1, 1))
output[5] = langcodeInput .. ':' .. string.upper(string.sub(elementDescription, 1, 1))

Latest revision as of 14:19, 30 May 2024

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

--[[ Please remember to use the talk page before changing this template! 
	 When the first Lua version was written this was used on more than 5000 pages,
	 now it is probably some 3000 more. 
	 A change will cause a re-rendering of all pages. Keep that in mind! ]]--

local p = {}
local translations = mw.loadData('Module:Element/translation')

-- start for Template:Area
function p.area(frame)
	-- extracting arguments
	local areaId = frame.args[1]
	local areaName = frame.args[2]
	local toolbarSetting = frame.args["tools"]
	local langcodeInput = frame.args["lang"]
	
	-- checking for correct parameters
	assert(tonumber(areaId), "Given way id parameter is not a number")
	if areaName == "" then
		areaName = nil
	elseif areaName ~= nil then
		areaName = mw.text.trim(areaName)
	end
	areaId = mw.text.trim(areaId)
	if langcodeInput ~= nil then langcodeInput = string.lower(langcodeInput) end
	
	return elementText("area", areaId, areaName, toolbarSetting, langcodeInput)
end

-- start for Template:Node
function p.node(frame)
	-- extracting arguments
	local nodeId = frame.args[1]
	local nodeName = frame.args[2]
	local toolbarSetting = frame.args["tools"]
	local langcodeInput = frame.args["lang"]
	
	-- checking for correct parameters
	assert(tonumber(nodeId), "Given node id parameter is not a number")
	if nodeName == "" then
		nodeName = nil 
	elseif nodeName ~= nil then
		nodeName = mw.text.trim(nodeName)
	end
	nodeId = mw.text.trim(nodeId)
	if langcodeInput ~= nil then langcodeInput = string.lower(langcodeInput) end
	
	return elementText("node", nodeId, nodeName, toolbarSetting, langcodeInput)
end

-- start for Template:Relation
function p.relation(frame)
	-- extracting arguments
	local relationId = frame.args[1]
	local relationName = frame.args[2]
	local toolbarSetting = frame.args["tools"]
	local langcodeInput = frame.args["lang"]
	
	-- checking for correct parameters
	assert(tonumber(relationId), "Given relation id parameter is not a number")
	if relationName == "" then
		relationName = nil 
	elseif relationName ~= nil then
		relationName = mw.text.trim(relationName)
	end
	relationId = mw.text.trim(relationId)
	if langcodeInput ~= nil then langcodeInput = string.lower(langcodeInput) end
	
	return elementText("relation", relationId, relationName, toolbarSetting, langcodeInput)
end

-- start for Template:Way
function p.way(frame)
	-- extracting arguments
	local wayId = frame.args[1]
	local wayName = frame.args[2]
	local toolbarSetting = frame.args["tools"]
	local langcodeInput = frame.args["lang"]
	
	-- checking for correct parameters
	assert(tonumber(wayId), "Given way id parameter is not a number")
	if wayName == "" then
		wayName = nil 
	elseif wayName ~= nil then
		wayName = mw.text.trim(wayName)
	end
	wayId = mw.text.trim(wayId)
	if langcodeInput ~= nil then langcodeInput = string.lower(langcodeInput) end
	
	return elementText("way", wayId, wayName, toolbarSetting, langcodeInput)
end

-- Main function compiling the wiki text
function elementText(elementType, elementId, elementName, toolbarSetting, langcodeInput)
	local output = {}
	local elementDescription = elementType
	if elementType == "area" then elementType = "way" end -- non-existing area type
	
	output[1] = '<span class="plainlinks"><bdi class="sortkey" style="display:none;speak:none">'
    if elementName == nil then
        output[2] = elementId
    else
        output[2] = elementName
    end
	output[3] = '</bdi>'
	
	if(toolbarSetting == "mini") then 
		output[4] = '[//osm.org/' .. elementType ..'/' .. elementId
		if elementName == nil then
			output[5] = '<span title="browse ' .. elementDescription .. '">' .. elementId
		else
			output[5] = '<span title="browse ' .. elementDescription .. ' ' .. elementId
			 .. '">' .. elementName
		end
		output[6] = '</span>]'
	
	elseif (toolbarSetting == "short") then
		output[4] = '<small>[//osm.org/' .. elementType .. '/' .. elementId
		if elementName == nil then
			output[5] = '<span title="browse ' .. elementDescription .. '">' .. elementId
		else
			output[5] = '<span title="browse ' .. elementDescription .. ' ' .. elementId
			 .. '">' .. elementName
		end
		output[6] = '</span>]&nbsp;<tt class="noprint" style="background:#EEF">(' ..
			'[//osm.org/edit?editor=id&' .. elementType .. '=' .. elementId .. 
			'<span title="iD">i</span>]&nbsp;'
		output[7] = '[http://localhost:8111/import?url=https://api.osm.org/api/0.6/'
			 .. elementType .. '/' .. elementId .. '/full <span title="JOSM">J</span>]'
		output[8] = ')</tt></small>'
	
	else
		output[4] = '[[File:Osm element ' .. elementDescription .. '.png|20px|link='
		if langcodeInput ~= nil and langcodeInput ~= 'en' then
			output[5] = langcodeInput .. ':' .. string.upper(string.sub(elementDescription, 1, 1))
				 .. string.sub(elementDescription, 2, -1)
			--[[ no case distinction in page namespaces, 
				no check for page existance for the sake of faster processing ]]--
		else
			output[5] = elementDescription
		end
		output[6] = '|' .. elementDescription .. ']]&nbsp;'
		output[7] = '[//osm.org/' .. elementType .. '/' .. elementId
			.. ' <span title="browse '.. elementDescription
		if elementName == nil then
			output[8] = '">' .. elementId
		else
			output[8] = ' ' .. elementId .. '">' .. elementName
		end
		output[9] = '</span>]'
		
		if toolbarSetting == "all" then
			local pageLanguage = langcode(langcodeInput)
			local translationComplete = true
			local transHistory = translations.translateHistory[pageLanguage]
			if not transHistory then
				translationComplete = false
				transHistory = translations.translateHistory['en']
			end
			
			output[10] = ' <small>([//osm.org/edit?editor=id&' 
				 .. elementType .. '=' .. elementId .. ' iD] '
			output[11] = '[http://localhost:8111/import?url=https://api.osm.org/api/0.6/'
				 .. elementType .. '/' .. elementId .. '/full JOSM] '
			output[12] = '[http://osm.virtuelle-loipe.de/history/?type='
				 .. elementType .. '&ref=' .. elementId .. ' ' .. transHistory .. ']'
			if elementType == 'relation' then -- relation analysis
				local transAnalyse = translations.translateAnalyse[pageLanguage]
				if not transAnalyse then
					translationComplete = false
					transAnalyse = translations.translateAnalyse['en']
				end
				output[13] = ' [http://ra.osmsurround.org/analyze.jsp?relationId='
					.. elementId .. ' ' .. transAnalyse ..']'
			end
			table.insert(output, ')</small>')
			if not translationComplete then
				local fullEnglishName = languageName(pageLanguage)
				if string.len(fullEnglishName) ~= 0 then
					table.insert(output, '[[Category:Pages unavailable in '
						.. fullEnglishName .. ']] [[File:Translate.svg|x10px|link='
						.. 'Module:Element#Adding_a_Language|Help translate this to '
						.. fullEnglishName .. ']]')
				end
			end

		elseif toolbarSetting == "josm" then
			output[10] = ' <small>([http://localhost:8111/import?url='
				 .. 'https://api.openstreetmap.org/api/0.6/' .. elementType
			output[11] = '/' .. elementId .. '/full JOSM])</small>'
		end
	end
	
	table.insert(output, "</span>") -- ends class plainlinks
	return tostring(table.concat(output))
end

-- this uses the external module 'OsmPageTitleParser': 
function langcode(langcodeInput)
	--[[ Priorities:
		1 user input
		2 dedicated language namespace
		3 language title prefix
		4 'en' as default
	]]--

	if langcodeInput then
		return string.lower(langcodeInput)
	end

	local langModule = require('Module:OsmPageTitleParser')
	return tostring((langModule.parseTitle(mw.title.getCurrentTitle()).language):getCode())
end

function languageName(code)
	local constantsModule = mw.loadData('Module:OSM Constants')
	-- should be already loaded (needed by OsmPageTitleParser as well)
	local customLangName = constantsModule.customLangCodes[code]
	
	if customLangName ~= nil then
		return customLangName
	else
		return mw.language.fetchLanguageName(code, 'en')
	end
end
	
return p