Модуль:Песочница/Pok: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 10: | Строка 10: | ||
title = mw.title.getCurrentTitle() | title = mw.title.getCurrentTitle() | ||
end | end | ||
if not title then | |||
if not title or not title.exists then | |||
return 'Страница не найдена' | return 'Страница не найдена' | ||
end | end | ||
| Строка 20: | Строка 21: | ||
local lc = mw.ustring.lower(content) | local lc = mw.ustring.lower(content) | ||
local target = mw.ustring.lower(word) | local target = mw.ustring.lower(word) | ||
local esc = mw.ustring.gsub(target, "([^%w])", "%%%1") | local esc = mw.ustring.gsub(target, "([^%w])", "%%%1") | ||
local pattern = '%f[%w]' .. esc .. '%f[%W]' | local pattern = '%f[%w]' .. esc .. '%f[%W]' | ||
Версия от 11:23, 21 января 2026
Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc
local p = {}
function p.occurrences(frame)
local args = frame.args
local pageArg = args.page or args[1]
local title
if pageArg and pageArg ~= "" then
title = mw.title.new(pageArg)
else
title = mw.title.getCurrentTitle()
end
if not title or not title.exists then
return 'Страница не найдена'
end
local content = title:getContent() or ''
local word = args.word or args.w or ''
if word == '' then return 'Укажите word=...' end
local lc = mw.ustring.lower(content)
local target = mw.ustring.lower(word)
local esc = mw.ustring.gsub(target, "([^%w])", "%%%1")
local pattern = '%f[%w]' .. esc .. '%f[%W]'
local count = 0
for _ in mw.ustring.gmatch(lc, pattern) do
count = count + 1
end
return tostring(count)
end
return p