Модуль:Песочница/Pok: различия между версиями

м Содержимое страницы заменено на «local p = {} function p.count(frame) local page = frame.args[1] local word = frame.args[2] local title = mw.title.new(page) if not title then return "Страница не найдена" end local text = title:getContent() or "" local _, count = text:gsub("%f[%w]" .. word .. "%f[%W]", "") return count end return p»
Метка: замена
мНет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}


function p.count(frame)
function p.occurrences(frame)
    local page = frame.args[1]
  local args = frame.args
     local word = frame.args[2]
  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 then
    return 'Страница не найдена'
  end


    local title = mw.title.new(page)
  local content = title:getContent() or ''
    if not title then
  local word = args.word or args.w or ''
        return "Страница не найдена"
  if word == '' then return 'Укажите word=...' end
    end


    local text = title:getContent() or ""
  local lc = mw.ustring.lower(content)
    local _, count = text:gsub("%f[%w]" .. word .. "%f[%W]", "")
  local target = mw.ustring.lower(word)


     return count
  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
end


return p
return p