پرش به محتوا

پودمان:Wikidata for infobox/occupations

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
--
-- Author: Alireza (User:علیرضا)
-- Distributed under the terms of the CC-BY-SA 4.0
--
----
--
-- Module:Wikidata for infobox/occupations/politician
--

local infoboxImage = require("Module:InfoboxImage").InfoboxImage
-- Exceptions are data which are not numbered just like labels.
local excepts = {"headerstyle", "headerclass", "labelstyle", "datastyle", "belowclass", "below", "belowstyle", "caption", "name", "child", "bodyclass", "subbox", "bodystyle", "title", "titleclass", "titlestyle", "above", "aboveclass", "abovestyle", "imagestyle", "captionstyle", "imageclass"}
local p = {}

-- Following functions to iterate over ordered tables obtained from http://lua-users.org/wiki/SortedIteration
local function __genOrderedIndex( t )
  local orderedIndex = {}
  for key in pairs(t) do
    table.insert( orderedIndex, key )
  end
  table.sort( orderedIndex )
  return orderedIndex
end

local function orderedNext(t, state)
  -- Equivalent of the next function, but returns the keys in the alphabetic
  -- order. We use a temporary ordered key table that is stored in the
  -- table being iterated.

  if state == nil then
    -- the first time, generate the index
    t.__orderedIndex = __genOrderedIndex( t )
    local key = t.__orderedIndex[1]
    return key, t[key]
  end
  -- fetch the next value
  local key = nil
  for i = 1,#t.__orderedIndex do
    if t.__orderedIndex[i] == state then
      key = t.__orderedIndex[i+1]
    end
  end

  if key then
    return key, t[key]
  end

  -- no more value to return, cleanup
  t.__orderedIndex = nil
  return
end

-- Use this function to iterate over ordered tables if you want to pass them to the Module:Infobox
local function orderedPairs(t)
  -- Equivalent of the pairs() function on tables. Allows to iterate
  -- in order
  return orderedNext, t, nil
end

local function makeInfoboxImage(data)
  return infoboxImage({args=data})
end

-- [[x|]] does not work, so we remove | character.
local function fixLinks(string)
  return string.gsub(string,"|]]","]]")
end

-- Remove %s from string because no data has given to them.
local function clearNoValue(data)
  if type(data) == "string" then
    return fixLinks(mw.ustring.gsub(data, "%%s", ""))
  else
    for l, d in pairs(data) do
      if type(d) == "table" then
        for ll, dd in pairs(d) do
          data[l][ll] = fixLinks(mw.ustring.gsub(dd, "%%s", ""))
        end
      else
        data[l] = fixLinks(mw.ustring.gsub(d, "%%s", ""))
      end
    end
  end

  return data
end

function p.makeCompatible(data)
  local num = 1
  local output = {}

  for l, d in orderedPairs(data) do
    d = clearNoValue(d)
    -- Add exceptions to output
    local wasException = false
    for _, exv in ipairs(excepts) do
      if l == exv then
        output[l] = d
        wasException = true
        break
      end
    end
    if not wasException then
      -- Others
      -- It is possible to pass more than one image.
      if string.find(l,"image") then
        output[l] = makeInfoboxImage(d)
      else
        if l == "empty header" then
          output["header"..num] = d
        else
          -- Headers have labels.
          output["header"..num] = l
          for ll, dd in orderedPairs(d) do
            dd = clearNoValue(dd)
            num = num + 1
            output["label"..num] = ll
            -- The label has style
            if type(dd) == "table" then
              if ll == "Signature" then
                output["data"..num] = makeInfoboxImage(dd)
              else
                for ltn, ltv in orderedPairs(dd) do
                  if ltn == "style" then
                    output["style"..num] = ltv
                  elseif ltn == "data" then
                    output["data"..num] = clearNoValue(ltv)
                  end
                end
              end
            else
              -- The label only has data.
              output["data"..num] = dd
            end
          end
        end
        num = num + 1
      end
    end
  end

  return output
end

--function p.formatData(infoboxData, data)
--  -- Format strings with given data.
--  for h, d in pairs(data) do
--    if type(d) == "table" then
--      for l, dd in pairs(d) do
--        if type(dd) == "table" then
--          for ll, ddd in pairs(dd) do
--            if type(infoboxData[h][l][ll]) ~= "nil" then
--              infoboxData[h][l][ll] = mw.ustring.format(data[h][l][ll], ddd)
--            end
--          end
--        else
--          if type(infoboxData[h][l]) ~= "nil" then
--            infoboxData[h][l] = mw.ustring.format(data[h][l], dd)
--          end
--        end
--      end
--    else
--      if type(infoboxData[h]) ~= "nil" then
--        infoboxData[h] = mw.ustring.format(data[h], d)
--      end
--    end
--  end

--  return data
--end

return p