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

Материал из Space Station 14 Вики
м Содержимое страницы заменено на «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

Версия от 11:20, 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 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