15.101
Bearbeitungen
(- getParentArgs obsolete) |
(+ wikiTextLimit()) |
||
| Zeile 56: | Zeile 56: | ||
end | end | ||
return result, empty | 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]] | |||
-- 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 text_len = mw.ustring.len(text) | |||
for i = 1, text_len do | |||
char = mw.ustring.sub(text, i, i) | |||
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 | |||
if text_cumul_len > limit then | |||
break | |||
end | |||
text_cumul = text_cumul .. char | |||
--[[ mw.log( | |||
"i=" .. i .. " char=" .. char .. " bracket=" .. bracket_count .. | |||
" link_text=" .. tostring(link_text) .. | |||
" link_delim_found=" .. tostring(link_delim_found) .. | |||
" text_cumul=" .. text_cumul .. | |||
" text_cumul_len=" .. text_cumul_len .. | |||
" text_cumul_predelim_len=" .. text_cumul_predelim_len .. | |||
" text_limited=" .. text_limited) ]] | |||
end | |||
if text_cumul_len <= limit then | |||
text_limited = text | |||
end | |||
-- mw.log("textLimit(\"" .. text .. "\", " .. limit .. ")=\"" .. text_limited .. "\"") | |||
return text_limited | |||
end | end | ||
return p | return p | ||