Module:Software

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Create] Documentation
local languages = require("Module:Languages")

local p = {}

FEATURES = { -- maps Template:Software yes/no parameters to description strings (used to build the category names)
	sendPosition = 'send your position',
	uploadOSMData = 'upload changes to OSM',
	tracking = 'record GPS tracks',
}

-- debug with e.g. =p.featureCategories(nil, {platform='foo;bar;android', sendPosition='yes'})
function p.featureCategories (frame, debugArgs)
	local args = debugArgs or frame:getParent().args

	if languages.languageFromTitle(mw.title.getCurrentTitle()) ~= 'en' then
		-- TODO: add category with language prefix if category exists
		return ''
	end
	
	local sortKey
	
	if not args.license then
		sortKey = '?'
	elseif string.lower(args.license):find('proprietary') then
		if args.status == 'broken' then
			return ''
		end
		
		sortKey = 'Proprietary'
	else
		if args.status == 'unmaintained' then
			sortKey = 'Unmaintained'
		elseif args.status == 'broken' then
			sortKey = 'X'
		else
			sortKey = 'Open source' -- educated guess
		end
	end
	
	local txt = ''
	
	-- TODO: add category 3D software

	if not args.platform then
		return txt
	end
	
	local platforms = {}
	for _, item in pairs(mw.text.split(args.platform, '[;,]')) do
		key = mw.ustring.lower(
			mw.text.split( -- take the first word to detect e.g. "Android 5.0+" as android
				mw.text.trim(item), ' '
			)[1]
		)
		platforms[key] = true
	end
	
	if platforms.android then
		for arg, desc in pairs(FEATURES) do
			if args[arg] and args[arg] ~= '' and args[arg] ~= 'no' then
		    	txt = txt .. '[[Category:Android apps that can ' .. desc .. '|' .. sortKey .. ']]'
		    end
		end
	end
	
	return txt
end

function p.getTranslation(frame, params)
	
	local args = params or frame.args
	
    -- The page containing TemplateData
    local pageTitle = "Template:Software/doc"
    local pageContent = mw.title.new(pageTitle):getContent()

    -- Ensure the page exists and has content
    if not pageContent then
        return "TemplateData page not found or empty"
    end

    -- Extract the JSON part from the <templatedata> block
    local jsonStart = pageContent:find("<templatedata>")
    local jsonEnd = pageContent:find("</templatedata>")
    
    if not jsonStart or not jsonEnd then
        return "TemplateData block not found"
    end
    
    local jsonString = pageContent:sub(jsonStart + 14, jsonEnd - 1)
	
    -- Convert the JSON string into a Lua table
    local success, jsonData = pcall(mw.text.jsonDecode, jsonString)
    if not success or not jsonData then
        return "Error parsing JSON: " .. jsonString
    end

    -- Example: Access the label of the 'lang' parameter, fallback to english
    local userLang = args.lang or "en"
    local label = jsonData.params[args.param].label[userLang] or jsonData.params[args.param].label["en"]

    if not label then
        return "Param not found or empty"
    end

    return label
end

function p.feature(frame)
    return "|\n! scope=\"row\" | " .. p.getTranslation(nil,{param=frame.args[1],lang=frame:getParent().args.lang}) .. "\n| " .. frame.args[2]
end


return p