Module:Show Path Items

Une page de Wikiquote, le recueil des citations libres.

La documentation pour ce module peut être créée à Module:Show Path Items/Documentation

local path = require "Module:Wikidata/Chemin"
local p = {}

local item_rendering_template = "WD Q"
local property_rendering_template = "WD P"

p.format_list_path_items = function(start, path_str , frame, max)

	
	max = tonumber(max) or 10
	
	local path_obj = path.PropertyPath:new(path_str)
	local res = ""
	local i = 0
	
	if path_obj.node then
		local it = path_obj:iterate(start)
		for elem in it do
			i = i + 1
			if i > max then
				break
			end
			local template = nil
			if elem:has_an_item() then
				template = item_rendering_template
			elseif elem:has_a_property() then
				template = property_rendering_template
			end
			if template then
				res = res .. "* " .. frame:expandTemplate{
					title = template, 
					args = { 
						[1] = elem:entity_value() 
					} 
				} .. "\n"
			else
				res = res .. "* " .. mw.wikibase.formatValue(elem:snak()) .. "\n"
			end
		end
		if i == max + 1 then
			res = res .. "* ...(?)"
		end
	else
		res = res .. "parse error !!!!"
	end
	return res
end

function table.slice(tbl, first, last, step)
  local sliced = {}
  
  for i = first or 1, last or #tbl, step or 1 do
    sliced[#sliced+1] = tbl[i]
  end

  return sliced
end

p.slice = table.slice

local function count_args(table)
	local nargs = 0
	for num, _ in ipairs(table) do
		nargs = num
	end
	return nargs
end

p.show_path_items = function(frame)
	frame = frame:getParent()
	
	local start = frame.args[1]
	local bound
	
	local nargs = count_args(frame.args)
	
	if tonumber(frame.args[nargs]) then
		max = tonumber(frame.args[nargs])
		bound = nargs - 1
	else
		bound = nargs
	end
	
	local slice = table.slice(frame.args, 2, bound)
	
	local path_str = table.concat(slice, "|")

	return p.format_list_path_items(start, path_str, frame, max)
	
end

return p