Module:Table tools

local export = {}

local m_links = require("Module:links")

local u = mw.ustring.char
local notes_ranges = {
  -- First three represent symbols in ISO-8859-1
  -- Including ÷ (U+00F7) × (U+00D7) § (U+00B7) ¤ (U+00A4)
  {0xA1,0xBF},
  {0xD7,0xD7}, -- ×
  {0xF7,0xF7}, -- ÷
  -- Next two are "General Punctuation" minus non-spacing chars
  -- First one includes † (U+2020) ‡ (U+2021) • (U+2022)  ※ (U+203B) ⁕ (U+2055)
  {0x2010,0x2027},
  {0x2030,0x205E},
  -- Next one is "Superscripts and Subscripts" and "Currency Symbols"
  {0x2070,0x20CF},
  -- Next one is a whole series of symbol ranges
  {0x2100,0x2B5F},
  -- Next one is "Supplemental Punctuation"
  {0x2E00,0x2E3F}
}

local unicode_ranges = {}
for _, range in ipairs(notes_ranges) do
  table.insert(unicode_ranges, u(range[1]) .. "-" .. u(range[2]))
end
local unicode_range_str = table.concat(unicode_ranges, "")
local notes_re = "[%*%~%@%#%$%%%^%&%+0-9_ " .. unicode_range_str .. "]*"

local function manipulate_entry(entries, f)
	entries = entries or ""
	entries = mw.text.split(mw.ustring.gsub(entries, "^%s*(.-)%s*$", "%1"), "%s*,%s*")
	
	local sep = ""
	local ret = ""
	
	for _, entry in ipairs(entries) do
		ret = ret .. sep .. (entry == "-" and "—" or entry == "" and "" or f(entry))
		sep = ", "
	end
	
	return ret
end

local function gather_args(frame)
	local args = {}
	for key, val in pairs(frame.args) do
		if val ~= "" then
			args[key] = val
		end
	end
	local i = 1
	for _, val in ipairs(frame:getParent().args) do
		if val and val ~= "" then
			while args[i] do
				i = i + 1
			end
			args[i] = val
			i = i + 1
		end
	end
	local lang = args["lang"]
	
	if not lang then
		lang = args[1]
		local n = 1
		while args[n] do
			args[n] = args[n + 1]
			n = n + 1
		end
	end
	
	return lang, args
end

function export.separate_notes(entry)
	local notes
	entry, notes = mw.ustring.match(entry, "^(.-)(" .. notes_re .. ")$")
	return entry, notes
end

function export.superscript_notes(notes)
	if notes ~= "" then
		notes = "<sup>" .. mw.ustring.gsub(notes, "_", " ") .. "</sup>"
	end
	return notes
end
	
function export.get_notes(entry)
	local notes
	entry, notes = export.separate_notes(entry)
	notes = export.superscript_notes(notes)	
	return entry, notes
end

function export.separate_initial_notes(entry)
	local notes
	notes, entry = mw.ustring.match(entry, "^(" .. notes_re .. ")(.*)$")
	return notes, entry
end
	
function export.get_initial_notes(entry)
	local notes
	notes, entry = export.separate_initial_notes(entry)
	notes = export.superscript_notes(notes)	
	return notes, entry
end

function export.linkify_entry(lang, entries, allow_self_link, prep)
	if type(lang) == "table" then
		local args
		lang, args = gather_args(lang)
		if (args["prep"] or "") ~= "" then
			local mod, func = unpack(mw.text.split(args["prep"], "#", true))
			prep = require("Module:" .. mod)[func]
		end
		entries = args[1]
		allow_self_link = (args["allowSelfLink"] or "") ~= ""
	end
	lang = require("Module:languages").getByCode(lang)
	
	local function f(entry)
		local e, notes = export.get_notes(entry)
		local ep = prep and prep(e)
		return m_links.language_link({lang = lang, term = ep or e, alt = ep and e}, allow_self_link) .. notes
	end
	
	return manipulate_entry(entries, f)
end

function export.translit_entry(lang, entries)
	if type(lang) == "table" then
		local args
		lang, args = gather_args(lang)
		entries = args[1]
	end
	lang = require("Module:languages").getByCode(lang)

	local function f(entry)
		local e, notes = export.get_notes(entry)
		return lang:transliterate(e) .. notes
	end
	
	return manipulate_entry(m_links.remove_links(entries), f)
end

function export.format_entry(lang, entries)
	if type(lang) == "table" then
		local args
		lang, args = gather_args(lang)
		entries = args[1]
	end
	
	return manipulate_entry(m_links.remove_links(entries), function(entry) local e, n = export.get_notes(entry); return e .. n end)
end

function export.first_entry(lang, entries)
	if type(lang) == "table" then
		local args
		lang, args = gather_args(lang)
		entries = args[1]
	end

	local entry = mw.text.split(mw.ustring.gsub(entries, "^%s*(.-)%s*$", "%1"), "%s*,%s*")[1]
	
	local e, notes = export.get_notes(entry)
	return e .. notes
end

return export

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.