Модуль:Песочница/Pok

Материал из Space Station 14 Вики

Для документации этого модуля может быть создана страница Модуль:Песочница/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