Modul:Zeichenfolge

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
verwendete Moduln:
SimpleStruct
SimpleDataAccess
Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


--[=[ Zeichenfolge 2022-07-03
Data Management Module for Access from Within Templates and Other Modules
providing infos for sign sequences usually using unicode for display
Author: Vollbracht

* data()	Wikidata information for current sign sequence in table form
]=]

--Module globals
local p = {service = {}}

local _, Parser = pcall(require, "Modul:SimpleStruct")
local _, Limited = pcall(require, "Modul:SimpleDataAccess")

--[[
	sequenceCPs(qual)
	returns a table of unicode codepoints of symbols in a sequence given by qual
]]
local sequenceCPs = function(qual)
	local source = Limited.qualifiersLineup(qual, "P527")
	local result = {}
	for _, cq in ipairs(source) do
		table.insert(result,Limited.indirectMSValue(cq, "P1299 P4213"))
	end
	return result
end

--[[
	short(qual)
	parameter:
		qual	wikiData qualifier of an item with either
				a P1299(depicted by) containing a unicode item, or
				a sequence of P527(has part or parts) containing character items
				character items ought to have one P1299 property each
	returns two elements:
	1.	Sitelink (or Label if Sitelink unavailable)
	2.	table of unicode codepoints
]]
p.service.short = function(qual)
	local rawChar = mw.wikibase.getSitelink(qual)
	if rawChar == nil then
		rawChar = mw.wikibase.getLabel(qual)
	end
	if rawChar == nil then
		return "Fehler", {"20; Bitte nur für Zeichenbeschreibung mit Wikidata einsetzen! [[Kategorie:Wikipedia:Qualitätssicherung Vorlageneinbindung fehlerhaft]] "}
	end
	local codepoint = Limited.indirectMSValue(qual, "P1299 P4213")
	if codepoint == "" then
		local cpl = sequenceCPs(qual)
		if cpl == {} then
			return "Fehler", {"20; Bitte nur für Zeichenbeschreibung mit Wikidata einsetzen! [[Kategorie:Wikipedia:Qualitätssicherung Vorlageneinbindung fehlerhaft]] "}
		end
		return rawChar, cpl
	end
	return rawChar, {codepoint}
end

--[[
	{{Zeichen|short|qual|charStyle}}
	returns a set of 2 <td> elements containing Sitelink or Label and character
	or characters defined by <qual>
]]
p.service.shortTDs = function(qual, charStyle)
	local link, cpl = p.service.short(qual)
	local chS = charStyle
	if chS == nil then chS = "" end
	local result = "<td " .. chS .. ">&#x"
	for i, cp in ipairs(cpl) do
		if i > 1 then result = result .. ';&nbsp;&#x' end
		local c=cp
		if c == "" then c = "0020" end
		result = result .. c
	end
	return result .. ";</td><td>[[" .. link .. "]]</td>"
end

--[[
	shortList(qList, charStyle)
	returns a table of 2 columns of short(qual, charStyle) listing all
	occurences of Wikidata qualifiers within qList string currently devided by
	an additional empty column
]]
local shortList = function(qList, charStyle)
	local list = {}
	for qual in qList:gmatch('[qQ]%d+') do
		table.insert(list, p.service.shortTDs(qual, charStyle))
	end
	if list[1] == nil then
		return ""
	end
	local delims = {
		'</tr><tr>',
		'<td>&nbsp;</td>',
		'',
		'<td>&nbsp;</td><td></td><td></td>'
	}
	local result = '<table style="width:100%;"><tr>'
	for i, v in ipairs(list) do
		result = result .. v
		if i == #list then
			result = result .. delims[i%2+3]
		else
			result = result .. delims[i%2+1]
		end
	end
	return result .. '</tr></table>'
end

--[[
	listOfShortLists(structureCode)
	returns a structure of pairs of list labels and list data strings
	structureCode may be:
		list of character qualifiers:
			Q1234 Q4567 Q7891
		labels altering with qualifier lists
			{label row} {label row} {Q1234 Q4567 Q7891}
			{label row} {Q1234 Q4567 Q7891}
		pairs of label and list where label may be given by qualifier
			{{Q147}, {Q1234 Q4567 Q7891}}
			{{Q258}, {Q1234 Q4567 Q7891}}
			{{label},{Q1234 Q4567 Q7891}}
		lists with labels (unpredictable order of lists; however each list is
		inernally ordered predictably)
			label1{Q1234 Q4567 Q7891}
			label2{Q1234 Q4567 Q7891}
	result is a list
		{
			{label, "Q1234 Q4567 Q7891"},
			{label, "Q1234 Q4567 Q7891"},
			{label, "Q1234 Q4567 Q7891"}
		}
		wherein label may be empty.
]]
p.service.listOfShortLists = function(structureCode)
	local lists = Parser.parse(structureCode)
	if lists[1] == structureCode then
		return {{"", structureCode}}
	end
	local result = {}
	local head = ""
	for i, pair in ipairs(lists) do
		t = type(pair) -- should be {{label} {list}}
		if t == "string" then
			-- no pair but single string instead
			-- handle {label row} and {list} elements (preffered syntax)
			if pair:find('[qQ]%d+') == nil then
				-- string is label
				if head == "" then
					-- string is first label part
					head = pair
				else
					-- add an additional label row
					head = head .. '<br />' .. pair
				end
			else
				-- string ("pair") is a list
				table.insert(result, {head, pair})
				-- empty head for next list
				head = ""
			end
		elseif t == "table" then
			if pair[1] == nil then
				-- handle table of label{list} elements (no preffered syntax)
				for label, list in pairs(pair) do
					table.insert(result, {label, list})
				end
			else
				-- handle {{label} {list}} elements
				local labelQ = pair[1]:match('[qQ]%d+')
				if labelQ == "" then
					table.insert(result, pair)
				else
					table.insert(result, {mw.wikibase.getLabel(labelQ), pair[2]})
				end
			end
		end
	end
	return result
end

--[[
	{{Zeichen|listOfShortLists
		|structureCode| tableStyles| thStyles| charStyles)}}
	returns an html table with rows prepared by service.listOfShortListsCore
	
	service.listOfShortListsCore: build html table rows
	params:
		structureCode: string representation of source
		thStyles: css style information for <th> elements in a string like
			style="[..]"
			class="[..]"
			class="[..]" style="[..]"
		charStyles: css style information for the <td> elements containing the
			actual single characters in a string as described for thStyles
	source representated by structureCode is a table containing
		{label, list} elements wherein list is a string containing atleast one
		substring starting with a 'Q' followed by decimal digits
	returns:
		table rows in a sequence of
		<tr><th colspan="5" thStyles>label</th></tr>
		folowed by a number ( >= 1 ) of rows containing 1 or 2
		<td charStyles>char</td><td>link</td>
]]
p.service.listOfShortListsCore = function(structureCode, thStyles, charStyles)
	if structureCode == nil then return "" end
	local source = p.service.listOfShortLists(structureCode)
	if #source == 0 then return "" end
	local thS = thStyles
	if thS == nil then thS = "" end
	local chS = charStyles
	if chS == nil then thS = "" end
	local result = ""
	for i, pair in ipairs(source) do
		-- handle label
		if pair[1] ~= "" then
			result = result .. '<tr><th colspan="2" ' .. thS .. '>'
			result = result .. pair[1] .. '</th></tr>'
		end
		-- hanle list
		result = result .. '<tr><td colspan="2">'
		result = result .. shortList(pair[2], chS) .. '</td></tr>'
	end
	return result
end

-- now surround this for export:
p.listOfShortLists = function(frame)
	local sc = frame.args.structureCode
	if sc == nil then return "" end
	local tableS = frame.args.tableStyles
	if tableS == nil then tableS = "" end
	local thS = frame.args.thStyles
	if thS == nil then thS = "" end
	local charS = frame.args.charStyles
	if charS == nil then charS = "" end
	local core = p.service.listOfShortListsCore(sc, thS, charS)
	if core == "" then return "" end
	return '<table ' .. tableS .. '>' .. core .. '</table>'
end

--[[
	data() / service.data()
	returns wikidata information for current page containing qualifier lineup
	of P527(has part or parts)
]]
p.service.data = function()
	local result = {}
	result.label = mw.wikibase.getLabel()
	result.description = mw.wikibase.getDescription()
	local character = mw.wikibase.getEntity()
	if character == nil then
		result.sequence = {"Q29485"}
	else
		result.sequence = Limited.qualifiersLineup(character, "P527")
	end
	return result
end

--[[
	language specific function returning table with German headers
	description in German:
	
	Zeichen|data|<Parameter>
	gibt in eine Tabelle wikidata-Informationen zur aktuellen Seite zurück
	Parameter:
		tableStyles=<string>
			Zeichenkette nach dem Schema 'class="<Liste>" style="<styles>"'
			optional
		thStyles=<string>
			Zeichenkette nach dem Schema 'class="<Liste>" style="<styles>"'
			optional
		charStyles=<string>
			Zeichenkette nach dem Schema 'class="<Liste>" style="<styles>"'
			optional, default: 'style="font-size:250%;"'
		addRows=<Liste>
			eine Liste von zusätzlichen Zeilen nach dem Schema
			'Name{<Wert>}'
			die Elemente können durch Leerzeichen, Zeilenumbrüche, etc.
			separiert sein. Werte dürfen keine geschweiften Klammern
			enthalten.
		rvStyles=<string>
			Zeichenkette nach dem Schema 'class="<Liste>" style="<styles>"'
			für die Werte der addRows
			optional
]]
p.data = function(frame)
	-- collect data
	local source = p.service.data()
	if source.sequence == {"Q29485"} then
		return "Fehler: Bitte nur auf Seiten zur Zeichenfolgenbeschreibung mit Wikidata einsetzen! [[Kategorie:Wikipedia:Qualitätssicherung Vorlageneinbindung fehlerhaft]]"
	end
	local charStyles = 'style="font-size:250%;"'
	if frame.args.charStyles then
		charStyles = frame.args.charStyles
	end
	local ths = ""
	if frame.args.thStyles ~= nil then
		ths = frame.args.thStyles
	end
	local list = p.service.listOfShortListsCore(frame.args.addList, ths, charStyles)
	-- build result table
	local result = '<table '
	if frame.args.tableStyles then
		result = result .. frame.args.tableStyles
	else
		result = result .. 'class="float-right infobox toptextcells toccolours"'
		result = result .. ' cellspacing="5"'
		if list == "" then
			result = result .. ' style="width:240px;"'
		else
			result = result .. ' style="width:300px;"'
		end
	end
	result = result .. '><tr><th colspan="2" ' .. ths .. '>'
	result = result .. source.label .. ':<br />' .. source.description
	result = result .. '</th></tr><tr>'
	if source.sequence == {} then
		result = result .. '<td colspan="2">(kein Folge)'
	else
		local charSeq = ""
		local cpSeq = ""
		local linkSeq = "[["
		local codeBlock = ""
		for i, chQ in ipairs(source.sequence) do
			local cp = Limited.indirectMSValue(chQ, "P1299 P4213")
			local cb = Limited.indirectMSValue(chQ, "P1299 P5522")
			local link = mw.wikibase.getSitelink(chQ)
			if link == nil then
				link = mw.wikibase.getLabel(chQ)
			end
			if i > 1 then
				charSeq = charSeq .. "&nbsp;"
				cpSeq = cpSeq .. "&nbsp;"
				linkSeq = linkSeq .. "]]<br />[["
				if codeBlock ~= cb then codeBlock = "" end
			else
				codeBlock = cb
			end
			charSeq = charSeq .. "&#x" .. cp .. ";"
			cpSeq = cpSeq .. "U+" .. cp
			linkSeq = linkSeq .. link
		end
		result = result .. '<td style="vertical-align: middle;">Zeichen:</td>'
		result = result .. '<td ' .. charStyles .. '>' .. charSeq
		result = result .. '</td></tr><tr><td>Codepunkte:</td>'
		result = result .. '<td>' .. cpSeq
		result = result .. '</td></tr><tr><td>Einzelzeichen:</td><td>'
		result = result .. frame:preprocess(linkSeq .. ']]') .. '</td></tr>'
		if codeBlock ~= "" then
			result = result .. '<tr><td>Codeblock:</td><td>'
			result = result .. frame:preprocess('[[' .. codeBlock .. ']]')
		end
	end
	result = result .. '</td></tr>'
	if frame.args.addRows then
		local rows = Parser.altParse(frame.args.addRows)
		if rows ~= nil then
			for _, v in ipairs(rows) do
				result = result .. '<tr><td style="vertical-align: middle;">'
				result = result .. v[1]
				result = result .. '</td><td ' .. frame.args.rvStyles .. '>'
				result = result .. v[2] .. '</td></tr>'
			end
		end
	end
	if list ~= "" then
		if frame.args.addListTitle then
			result = result .. '<tr><th colspan="2" '
			if frame.args.thStyles ~= nil then
				result = result .. frame.args.thStyles
			end
			result = result .. '>' .. frame.args.addListTitle .. '</th></tr>'
		end
		result = result .. list
	end
	result = result .. '</table>'
	return result
end

return p