Modul:Wiki: Unterschied zwischen den Versionen

(Verbesserung pageexists())
Markierung: Zurückgesetzt
(fix wikiTextLimit() fett/kursiv)
 
(17 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 41: Zeile 41:
end
end


function p.pageexists(page)
function p.pageExists(page)
return page ~= "" and mw.title.new(page).exists
return page ~= nil and page ~= "" and string.find(page, "[#<>%[%]%|%{%}]") == nil and mw.title.new(tostring(page)).exists
end
 
function p.getArgs(args, argslist)
local result = {}
local empty = true
for _, a in ipairs(argslist) do
local v = args[a] or ""
if v ~= "" then
result[a] = v
empty = false
end
end
return result, empty
end
 
function p.wikiTextLimit(text, limit)
-- liefert längenbegrenzten Wikitext zurück
-- schneidet bei Leerzeichen ab
-- beachtet dabei Links [http:/... linktext] [[page|linktext]]
-- ab fett/kursiv wird immer begrenzt
-- Kommandozeile zum Debuggen: =p.wikiTextLimit("12345 7890", 9)
local text_limited = ""
local text_cumul = ""
local text_cumul_len = 0
local text_cumul_predelim_len = 0
local bracket_count = 0
local link_text = false
local link_delim_found = false
local last_char = ""
local fett_kursiv = false
local text_len = mw.ustring.len(text)
if text_len <= limit then
text_limited = text
else
for i = 1, text_len do
local char = mw.ustring.sub(text, i, i)
fett_kursiv = (char == "'" and last_char == "'")
if char == "[" and bracket_count < 2 then -- Klammer auf
bracket_count = bracket_count + 1
text_cumul_predelim_len = text_cumul_len
elseif char == "]" and bracket_count > 0 then -- Klammer zu
bracket_count = bracket_count - 1
link_text = false
if not link_delim_found then -- bis dahin Leerzeichen bzw. Pipe nicht gefunden?
text_cumul_len = text_cumul_predelim_len -- dann Text hinzuzählen
elseif bracket_count == 0 then
link_delim_found = false
end
elseif (char == " " and bracket_count == 1) or -- [http:/... linktext] Leerzeichen-Erkennung
  (char == "|" and bracket_count == 2) then -- [[page|linktext]] Pipe-Erkennung
link_text = true -- ab hier wieder Text
link_delim_found = true
else
if bracket_count == 0 or link_text then -- normaler Text
text_cumul_len = text_cumul_len + 1
if char == " " then -- Auf Leerzeichen getroffen
text_limited = text_cumul
end
end
text_cumul_predelim_len = text_cumul_predelim_len + 1 -- Auf Verdacht Link mitzählen
end
--[[ mw.log(
"i=" .. i .. " char=" .. char .. " last_char=" .. last_char ..
" fett_kursiv=" .. tostring(fett_kursiv) ..
" bracket=" .. bracket_count ..
" link_text=" .. tostring(link_text) ..
" link_delim_found=" .. tostring(link_delim_found) ..
" text_cumul_len=" .. text_cumul_len ..
" text_cumul_predelim_len=" .. text_cumul_predelim_len ..
" text_limited=" .. text_limited) ]]
if text_cumul_len > limit or fett_kursiv then
break
end
text_cumul = text_cumul .. char
last_char = char
-- mw.log("text_cumul=" .. text_cumul)
end
if text_cumul_len <= limit and not fett_kursiv then
text_limited = text
end
end
-- mw.log("textLimit(\"" .. text .. "\", " .. limit .. ")=\"" .. text_limited .. "\"")
return text_limited
end
end


return p
return p

Aktuelle Version vom 27. November 2025, 09:27 Uhr

Diese Modul stellt MediaWiki-Funktionen bereit, die so schneller erreichbar sind, als dass man sie erst durch den Parser z.B. via frame:callParserFunction() schicken müsste.

Unterseiten

Siehe auch


local p = {}
local str = require("Modul:String")

function p.fullpagename()
	return tostring(mw.title.getCurrentTitle())
end

function p.pagename()
	local fullpagename = p.fullpagename()
	local split = str.split2(fullpagename, ":")
	return split[#split] -- letztes Element in der table nach Doppelpunkt
end

function p.subpagename()
	local pagename = p.pagename()
	local split = str.split2(pagename, "/")
	return split[#split] -- letztes Element in der table nach letztem Slash
end

function p.setPagenames(table)
--	mw.log("setPagenames(table)")
--	mw.logObject(table)
	local pagesnames = {}
	-- fullpagename
	pagesnames.fullpagename = tostring(mw.title.getCurrentTitle())
	-- pagename
	local split = str.split2(pagesnames.fullpagename, ":")
	pagesnames.pagename = split[#split] -- letztes Element in der table nach Doppelpunkt
	-- subpagename
	split = str.split2(pagesnames.pagename, "/")
	pagesnames.subpagename = split[#split] -- letztes Element in der table nach letztem Slash
	-- table setzen
	if type(table) == "table" then
		table.fullpagename = pagesnames.fullpagename
		table.pagename = pagesnames.pagename
		table.subpagename = pagesnames.subpagename
	end
--	mw.logObject(table)
--	mw.logObject(pagesnames)
	return pagesnames
end

function p.pageExists(page)
	return page ~= nil and page ~= "" and string.find(page, "[#<>%[%]%|%{%}]") == nil and mw.title.new(tostring(page)).exists
end

function p.getArgs(args, argslist)
	local result = {}
	local empty = true
	for _, a in ipairs(argslist) do
		local v = args[a] or ""
		if v ~= "" then
			result[a] = v
			empty = false
		end
	end
	return result, empty
end

function p.wikiTextLimit(text, limit)
	-- liefert längenbegrenzten Wikitext zurück
	-- schneidet bei Leerzeichen ab
	-- beachtet dabei Links [http:/... linktext] [[page|linktext]]
	-- ab fett/kursiv wird immer begrenzt
	-- Kommandozeile zum Debuggen: =p.wikiTextLimit("12345 7890", 9)
	local text_limited = ""
	local text_cumul = ""
	local text_cumul_len = 0
	local text_cumul_predelim_len = 0
	local bracket_count = 0
	local link_text = false
	local link_delim_found = false
	local last_char = ""
	local fett_kursiv = false
	local text_len = mw.ustring.len(text)
	if text_len <= limit then
		text_limited = text
	else
		for i = 1, text_len do
			local char = mw.ustring.sub(text, i, i)
			fett_kursiv = (char == "'" and last_char == "'")
			if char == "[" and bracket_count < 2 then								-- Klammer auf
				bracket_count = bracket_count + 1
				text_cumul_predelim_len = text_cumul_len
			elseif char == "]" and bracket_count > 0 then							-- Klammer zu
				bracket_count = bracket_count - 1
				link_text = false
				if not link_delim_found then										-- bis dahin Leerzeichen bzw. Pipe nicht gefunden?
					text_cumul_len = text_cumul_predelim_len						-- dann Text hinzuzählen
				elseif bracket_count == 0 then
					link_delim_found = false
				end
			elseif (char == " " and bracket_count == 1) or							-- [http:/... linktext] Leerzeichen-Erkennung
				   (char == "|" and bracket_count == 2) then						-- [[page|linktext]] Pipe-Erkennung
				link_text = true													-- ab hier wieder Text
				link_delim_found = true
			else
				if bracket_count == 0 or link_text then								-- normaler Text
					text_cumul_len = text_cumul_len + 1
					if char == " " then												-- Auf Leerzeichen getroffen
						text_limited = text_cumul
					end
				end
				text_cumul_predelim_len = text_cumul_predelim_len + 1				-- Auf Verdacht Link mitzählen
			end
--[[			mw.log(
				"i=" .. i .. " char=" .. char .. " last_char=" .. last_char ..
				" fett_kursiv=" .. tostring(fett_kursiv) ..
				" bracket=" .. bracket_count ..
				" link_text=" .. tostring(link_text) ..
				" link_delim_found=" .. tostring(link_delim_found) ..
				" text_cumul_len=" .. text_cumul_len ..
				" text_cumul_predelim_len=" .. text_cumul_predelim_len ..
				" text_limited=" .. text_limited) ]]
			if text_cumul_len > limit or fett_kursiv then
				break
			end
			text_cumul = text_cumul .. char
			last_char = char
--			mw.log("text_cumul=" .. text_cumul)
		end
		if text_cumul_len <= limit and not fett_kursiv then
			text_limited = text
		end
	end
--	mw.log("textLimit(\"" .. text .. "\", " .. limit .. ")=\"" .. text_limited .. "\"")
	return text_limited
end

return p