Z powodu wzmożonej ilości spamu, konta na stan obecny nie są tworzone automatycznie. Wnioski o konta użytkowników są akceptowane przez administrację. W celu stworzenia konta, prosimy kierować się na tą stronę. W biografii wpiszcie cokolwiek, co potwierdzi, że nie jesteście botem. Prosimy też o zajrzenie na nasz kanał na Discordzie.

Moduł:Sclist

Z Touhou Wiki
Przejdź do nawigacji Przejdź do wyszukiwania

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Sclist/doc

--[[
Script for automated indexing and embedding of spellcards.
List of links is generated by {{#invoke:sclist|GetTable}}.
If a page contains special section "label" then the first row is being used as a label over the "Lista X" links in index.

To embed a list of spellcards, use {{#invoke:sclist|GetCards|<Character>}} (the name has to be identical to the "Character" in section name)
There are also optional parameters that adjust the generated code:
* type = <plain|scroll|hide|hide2> (hide2 is default)
** height = 300px (for type=scroll)
* showlinks = <yes|no> (only "yes" will cause displaying the links, undefined and any other value means "no")
--]]

--[====[ libraries ]====]

local common = require("Module:Common")

mw.text2 = require("Module:MW.text")

--[====[ helper functions ]====]

-- page parsing - drops the header and splits the page into sections, assuming that:
-- ==[[Character]]==
--  starts a card list
-- == Special ==
--  starts a special section named "special"
-- (the presence and lack of spaces is important!)
local function parse_page(content)
  local tst1 = mw.text2.split(content, '\n')

  local special = {}
  local users = {}
  local cards = {}

  local curcard = {}

  for k,v in pairs(tst1) do
    s,e,t = string.find(v, '^==([^=].+[^=])==$')
    if s ~= nil then
      s,e,n = string.find(t, '%[%[(.+)%]%]')
      if n == nil then
        s,e,n = string.find(string.lower(t), '%s+(.+)%s+')
        curcard = {}
        if common.isset(n) then special[n] = curcard end
      else
        curcard = {}
        cards[#cards+1] = curcard
        users[#cards] = n
      end
    else
      curcard[#curcard+1] = v
    end
  end

  return users,cards,special
end

--[====[ exported functions ]====]

-- builds a list of cards for given character
local function GetCards(frame)
  local uname = frame.args[1]
  if uname ~= nil then uname = mw.text.trim(uname) end

  if not common.isset(uname) then return frame:preprocess('{{mbox|type=delete|text=Nie podano imienia postaci!}}') end

  local lshow = (frame.args['showlinks'] == 'yes')
  local ltype = frame.args['type']
  if not common.isset(ltype) then ltype = 'hide2' end

  local ret = {""}
  local ulinks = {}

  local pnum = 1
  local pname = ''
  local users,cards,other

  while true do
    pname = frame.args['root']
    if not common.isset(pname) then pname = 'Lista Kart Zaklęć' end
    pname = pname .. '/Lista ' .. pnum

    if not common.exists(pname) then break end
    users,cards,other = parse_page(frame:expandTemplate{title = ':' .. pname})

    for k,v in pairs(users) do
      if uname == v then
        local content = table.concat(cards[k], '\n')
        local links = ''

        if lshow then
          local lt = {""}
          lt[#lt+1] = '[[Lista Kart Zaklęć]]'
          lt[#lt+1] = ' - [[' .. pname .. '#' .. v .. '|' .. v .. ']]'
          lt[#lt+1] = '{{#ifeq:{{PAGENAME}}|' .. v .. '||&#32;([[' .. v .. '{{!}}profil postaci]])}}'
          links = table.concat(lt)
        end
        
        if ltype == 'plain' then
          if lshow then
            ret[#ret+1] = links .. '<br/>'
          end
          ret[#ret+1] = content
          
        elseif ltype == 'scroll' then
          local height = frame.args['height']
          if not common.isset(height) then height = '300px' end
          
          if lshow then
            ret[#ret+1] = links .. '<br/>'
          end
          ret[#ret+1] = '<div style="overflow: scroll; overflow-x: visible; max-height: '
          ret[#ret+1] = height
          ret[#ret+1] = '; border: solid 1px #c0c0ff;">\n'
          ret[#ret+1] = content
          ret[#ret+1] = '\n</div>'

        elseif ltype == 'hide' then
          if lshow then
            ret[#ret+1] = links .. '<br/>'
          end
          ret[#ret+1] = '<toggledisplay showtext="Pokaż listę kart" hidetext="Ukryj listę kart">\n'
          ret[#ret+1] = content
          ret[#ret+1] = '\n</toggledisplay>'

        else --if ltype == 'hide2' then
          ret[#ret+1] = '{{hidden begin|title='
          if lshow then
            ret[#ret+1] = links
          else
            ret[#ret+1] = 'Lista Kart Zaklęć: ' .. v
          end
          ret[#ret+1] = '|titlestyle=background-color:#C9EFC2; text-align:center;}}\n'
          ret[#ret+1] = content
          ret[#ret+1] = '\n{{hidden end}}'
        end
        return frame:preprocess(table.concat(ret))
      end
    end
    pnum = pnum + 1
  end

  return frame:preprocess('{{mbox|type=content|text=Brak listy kart dla postaci ' .. uname .. '.}}')
end

-- builds an index of characters
local function GetTable(frame)
  local ret = {""}

  local pnum = 1
  local pname = ''
  local users,cards,other
  local ucnt = 1

  while true do
    pname = frame.args['root']
    if not common.isset(pname) then pname = 'Lista Kart Zaklęć' end
    pname = pname .. '/Lista ' .. pnum

    if not common.exists(pname) then break end
    users,cards,other = parse_page(frame:expandTemplate{title = ':' .. pname})

    if common.isset(other['label']) then
      ret[#ret+1] = "\n<center>{{ruby|'''[[" .. pname .. "|Lista " .. pnum .. "]]'''|" .. other['label'][1] .. "}}</center>"
    else
      ret[#ret+1] = "\n<center>'''[[" .. pname .. "|Lista " .. pnum .. "]]'''</center>"
    end

    ret[#ret+1] = '<ol start="' .. ucnt .. '">'
    for k,v in pairs(users) do
      ret[#ret+1] = '\n<li>[[' .. pname .. '#' .. v .. '|' .. v .. ']]</li>'
      ucnt = ucnt + 1
    end
    ret[#ret+1] = '</ol>'
    pnum = pnum + 1
  end

  return frame:preprocess(table.concat(ret))
end

--[====[ wrapper functions ]====]
local function GetCardsTemplate(frame)
  return GetCards(frame:getParent())
end

--[====[ export local functions ]====]
return {
  ['GetCards'] = GetCards,
  ['GetCardsTemplate'] = GetCardsTemplate,
  ['GetTable'] = GetTable
}

-- [[Kategoria:Skrypty Lua|{{PAGENAME}}]]