Módulo:mostrar-bloques-unicode

Documentación del módulo

Con el presente módulo se producen las tablas de Apéndice:Caracteres Unicode y todas sus subpáginas. Utiliza Módulo:unicode-base.

Basado en el módulo {{character list}} del Wiktionary en inglés (se agradece).
Esta documentación está transcluida desde Módulo:mostrar-bloques-unicode/doc.
Los editores pueden experimentar en la zona de pruebas de este módulo.
Por favor, añade las categorías e interwikis a la subpágina de documentación. Subpáginas de este módulo.
-- tomado de https://en.wiktionary.org/wiki/Module:character_list

local m_unicode = require("Módulo:unicode-base")
local m_uni_alias = mw.loadData("Módulo:unicode-base/alias")
local general_category_data = mw.loadData("Módulo:unicode-base/categorías")
local general_category_aliases = general_category_data.long_names
local script_data = mw.loadData("Módulo:unicode-base/scripts")
local char_to_script = require("Módulo:scripts/charAScript").charAScript
local Array = require("Módulo:arreglo")
local u = require("Módulo:String").char

local export = {}

local Unicode_version = "15.0"

local function get_data_for_code_point_range(block_start, block_end, filterer)
	local cps = {}
	for cp = block_start, block_end do
		if not filterer or filterer(cp) then
			local data = {}
			data.aliases = m_uni_alias[cp]
			for _, item in ipairs { "name", "script", "category", "image", "image_emoji" } do
				data[item] = m_unicode["lookup_" .. item](cp)
			end
			data.cp = cp
			table.insert(cps, data)
		end
	end
	return cps
end

-- Copied from [[Módulo:unicode-base]].
local function binary_range_search(codepoint, ranges)
	local low, mid, high
	low, high = 1, ranges.length or require "Módulo:tabla".length(ranges)
	while low <= high do
		mid = math.floor((low + high) / 2)
		local range = ranges[mid]
		if codepoint < range[1] then
			high = mid - 1
		elseif codepoint <= range[2] then
			return range, mid
		else
			low = mid + 1
		end
	end
	return nil, mid
end

-- The data_module argument must be a table
-- with the fields "ranges" and "singles".
-- If all code points in the inclusive range between start_code_point and
-- end_code_point have the same property value in the data module, or no value,
-- return this value.
local function get_shared_value(start_code_point, end_code_point, data_module)
	local code_point = start_code_point
	local previous_value
	local singles, ranges = data_module.singles, data_module.ranges
	while code_point <= end_code_point do
		local singles_value = singles[code_point]
		if singles_value then
			if previous_value then
				if singles_value ~= previous_value then
					return nil
				end
			else
				previous_value = singles_value
			end
			code_point = code_point + 1
		else
			local range = binary_range_search(code_point, ranges)
			if range then
				if previous_value then
					if range[3] ~= previous_value then
						return nil
					end
				else
					previous_value = range[3]
				end
				code_point = range[2] + 1
			else
				code_point = code_point + 1
			end
		end
	end
	return previous_value
end

function export.show_header(frame)
	local block_name = frame.args.block
	local names = {}
	local i, block_start, block_end
	
	for j, name, start, ending in m_unicode.enum_blocks() do
		names[j] = name
		if block_name == name then
			i, block_start, block_end = j, start, ending
		end
	end

	if not i then
		error('Invalid Unicode block name: ' .. block_name)	
	end
	
	local function appendix_link(block_name, left_arrow)
		return block_name
			and ("'''[[Apéndice:Caracteres Unicode/%s|%s %s]]'''")
				:format(
					block_name,
					left_arrow and "⟵"  or block_name,
					left_arrow and block_name or "⟶")
					 
			or ""
	end
	
	local general_category =
		get_shared_value(block_start, block_end, general_category_data)
	local script = get_shared_value(block_start, block_end, script_data)
	
	local text = (
		'{| style="width: 100%%;"\n' ..
		' | style="width: 30%%; text-align: left;"   | %s\n' ..
		' | style="text-align: center;" | <h2>%s</h2>\n' ..
		' | style="width: 30%%; text-align: right;"  | %s\n' ..
		' |}\nEsta página muestra los caracteres del bloque “[http://unicode.org/charts/PDF/U%04X.pdf %s]” del estándar Unicode ' ..
		Unicode_version .. ', que incluye los códigos entre U+%04X y U+%04X.\n' ..
 	'[[Categoría:Bloques Unicode|%s]]' ..
	'[[Categoría:Bloques Unicode/%s| ]]'):format(
			appendix_link(names[i - 1], true),
			names[i],
			appendix_link(names[i + 1], false),
			block_start, names[i], block_start, block_end,
			names[i],
			names[i]
		)
	if general_category or script then
		text = text .. (' Todos los caracteres del presente bloque ')
		local items = {}
		if general_category then
			table.insert(items, ('son de la categoría “%s” (%s)')
			:format(general_category, general_category_aliases[general_category]:gsub('_', ' ')))
		end
		if script then
			local alias = script_data.aliases[script]
			local Wikipedia_article
			local script_obj = require "Módulo:scripts".getByCode(script)
			--if script_obj then
			--	Wikipedia_article = script_obj:getWikipediaArticle() -- CUIDADO: esto no sirve, busca los nombres en inglés de los artículos de Wikipedia
			--else
			--	Wikipedia_article = alias .. ' script'
			--end
			table.insert(items, ('son del script “%s” (%s)') --así que no lo voy a poner como enlace
				:format(script, alias))
		end
		text = text .. table.concat(items, " y ") .. "."
	end
	
	return text
end

function export.show_blocks(frame)
	local result = {}
	local start_codepoint, end_codepoint = tonumber(frame.args[1]), tonumber(frame.args[2])
	
	table.insert(result, '{| class="wikitable" style="width: 100%;"\n! width="10%;" | Inicio\n! width="10%;" | Fin\n ! Nombre de bloque\n')
	for i, name, block_start, block_end in m_unicode.enum_blocks() do
		if (block_start >= start_codepoint) and (block_end <= end_codepoint) then
			table.insert(result, (
				'|-\n|U+%04X\n|U+%04X\n|[[Apéndice:Caracteres Unicode/%s|%s]]\n'
			):format(block_start, block_end, name, name))
		end
	end
	table.insert(result, "|}")
	
	return table.concat(result)
end

function export.show(frame)
	local result = {}
	local args = frame.args
	
	local block_start, block_end
	if args.block then
		block_start, block_end = m_unicode.get_block_range(args.block)
		if not block_start then
			error("Invalid Unicode block specified")
		end
	elseif args[1] and args[2] then
		block_start, block_end = tonumber(args[1]), tonumber(args[2])
		if not (block_start and block_end) then
			error("Invalid number specified")
		end
	else
		error("Must give a Unicode block or character range")
	end
	
	local function present_codepoint(codepoint)
		if not m_unicode.is_printable(codepoint) then
			return '<small>no imprimible</small>' -- lo más sensato
			
			-- Esto para mí no tiene sentido, pone un link en los caracteres que son no imprimibles, por qué??
			--local character = u(codepoint)
			--local text = '<small>no imprimible</small>'
			--if mw.title.new(character) then
			--	return "[[" .. character .. "|" .. text .. "]]"
			--else
			--	return text
			--end
		end
		
		local link_target = m_unicode.get_entry_title(codepoint)

		local display = ("%s&#%u;"):format(m_unicode.is_combining(codepoint) and "&#x25cc;" or "", codepoint)
		if m_unicode.is_whitespace(codepoint) then
			display = "]" .. display .. "["	
		end

		return
			(link_target and '[[:%s|<span class="character-sample %s">%s</span>]]'
			or '<!-- %s --><span class="character-sample %s">%s</span>'):format(
				link_target or "", char_to_script(codepoint), display
			)
	end
	
	local cps = get_data_for_code_point_range(block_start, block_end, m_unicode.is_assigned)
	
	local headers = {}
	
	local emoji_image_exists = false
	
	local image_module = ("Módulo:unicode-base/img/%03X"):format(math.floor(block_start / 0x1000))
	local emoji_image_module
	if pcall(mw.loadData, ("Módulo:unicode-base/emoj/%03X"):format(math.floor(block_start / 0x1000))) then
		emoji_image_module = ("Módulo:unicode-base/emoj/%03X"):format(math.floor(block_start / 0x1000))
		for _, data in ipairs(cps) do
			if data.image_emoji then
				emoji_image_exists = true
			end
		end
	end
	
	
	table.insert(result, [=[
{| class="wikitable sortable"
! width="12%" data-sort-type="number" | Codepoint
]=]
	)
	if emoji_image_exists then
		table.insert(result, [=[
! width="5%"  | Imagen (modo texto)<br><sup>[[Special:EditPage/]=] .. image_module .. [=[|editar]]</sup>
! width="5%"  | Imagen (modo emoji)<br><sup>[[Special:EditPage/]=] .. emoji_image_module .. [=[|editar]]</sup>
]=]
		)
	else
		table.insert(result, [=[
! width="5%"  | Imagen<br><sup>[[Special:EditPage/]=] .. image_module .. [=[|editar]]</sup>
]=]
		)
	end
	table.insert(result, [=[
! width="5%"  | Caracter
]=]
	)
	local all_with_same_general_category = Array(cps)
		:all(function(data) return data.category == cps[1].category end)
	local all_with_same_script = Array(cps)
		:all(function(data) return data.script == cps[1].script end)
	if not all_with_same_general_category then
		table.insert(result, ' ! Categoría general\n') -- estaba [[w:Categoría general|Categoría<br />general]]
	end
	if not all_with_same_script then
		table.insert(result, ' ! Script\n') -- estaba [[w:Script (Unicode)|Script]]
	end
	table.insert(result, ' ! Nombre Unicode\n')
	
	for _, data in ipairs(cps) do
		local alt_names = ""
		local cp = data.cp
		
		if data.aliases then
			local aliases = {
				["corregido"  ] = {},
				["control"     ] = {},
				["alternativo"   ] = {},
				["artificio"     ] = {},
				["abreviatura"] = {},
			}
			
			for _, info in ipairs(data.aliases) do
				table.insert(aliases[info[1]], "<small>" .. info[2] .. "</small>")
			end
			
			for i, name in ipairs(aliases.alternativo) do
				alt_names = alt_names .. (' aka %s'):format(name)
			end

			if #aliases.control > 0 then
				alt_names = alt_names .. '<br/><small>Nombre Unicode: ' .. table.concat(aliases.control, " o ") .. '</small>'
				--alt_names = alt_names .. '; nombre Unicode: ' .. table.concat(aliases.control, " or ")
			end

			for i, name in ipairs(aliases.corregido) do
				alt_names = alt_names .. ('<br/><small>Nombre corregido: %s</small>'):format(name)
			end
			
			for i, name in ipairs(aliases.artificio) do
				alt_names = alt_names .. ('<br/><small>Nombre del artificio: %s</small>'):format(name)
			end

			if #aliases.abreviatura > 0 then
				alt_names = alt_names .. ' (' .. table.concat(aliases.abreviatura, ", ") .. ')'
			end
		end

		local current_image, current_image_emoji
		if data.image then
			current_image = ('[[File:%s|40x35px]]'):format(data.image)
		else
			current_image = ''
		end	
		if emoji_image_exists then
			if data.image_emoji then
				current_image_emoji = ('[[File:%s|40x35px]]'):format(data.image_emoji)
			else
				current_image_emoji = ''
			end
		end
		
		table.insert(result, (
			' |- id="U-%04X"\n' ..
			' | data-sort-value="%u" | U+%04X <small>(%u)</small>\n' ..
			' | %s \n'
		):format(
			cp, cp, cp, cp,
			current_image
		))
		if emoji_image_exists then
			table.insert(result, (
				' | %s \n'
			):format(
				current_image_emoji
			))
		end
		table.insert(result, (
			' | %s \n'
		):format(
			present_codepoint(cp),
			data.category .. '<br />(' ..
				general_category_aliases[data.category]:gsub('_', ' ') ..
				')'
		))
		if not all_with_same_general_category then
			table.insert(result, (' | %s<br /><small>(%s)</small> \n')
				:format(
					data.category,
					general_category_aliases[data.category]:gsub('_', ' ')))
		end
		if not all_with_same_script then
			table.insert(result, (' | %s<br /><small>(%s)</small> \n')
				:format(
					data.script,
					script_data.aliases[data.script]))
		end
		table.insert(result, (' | <small>%s</small>%s\n')
			:format(
				mw.text.nowiki(data.name),
				alt_names))
			
	end
	
	table.insert(result,
		' |}'
	)
	
	table.insert(result, require("Módulo:TemplateStyles")("Plantilla:mostrar-unicode/estilo.css"))

	return table.concat(result)
end

return export