Module:CraftInline

From Ylvapedia

Documentation for this module may be created at Module:CraftInline/doc

local p = {}
local utils = require('Module:Utils')
local items = require('Module:Item')

local function getArg(args, key)
  return utils.nilCheck(args[key])
end

local function callsplit(value)
  return value and mw.text.split(value, '%s*,%s*') or {}
end

function p.generatecraftInline(inlineArgs)
  local i18n = mw.loadData('Module:CraftingTable/i18n')
  local lang = inlineArgs['ang'] or 'JA'
  local mode = inlineArgs['mode'] or 'inline'
  local size = inlineArgs['size'] or '32x32px'
  local icon = inlineArgs['icon']
  local highlight = inlineArgs['highlight']--true or nil
  local highlightParent = inlineArgs['highlightParent']
  local ElementNote = utils.nilCheck(inlineArgs['ElementNote'])

  local pageItem = inlineArgs['ask'] and string.format('[[Elin:%s]]', inlineArgs['ask'])
  local sizeMap = {
    ['xxx-large'] = '32px',
    ['xx-large'] = '32px',
    ['x-large'] = '32px',
    ['large'] = '32px',
    ['medium'] = '32px',
    ['small'] = '28px',
    ['x-small'] = '24px',
    ['xx-small'] = '20px',
  }
  local imgsize = sizeMap[size] or '32px'

  local Recipe = {}
  for i = 1, 3 do
    Recipe[i] = {
      CraftingStation = getArg(inlineArgs, i..'-1'),
      Ingredient = getArg(inlineArgs, i..'-2'),
      IngredientNum = getArg(inlineArgs, i..'-3'),
      IngredientExtra = getArg(inlineArgs, i..'-4'),
    }
  end

  --askは未完
  local allResults = {}
  if pageItem then
    mw.log(inlineArgs['ask'])
    --queryの先頭行に[[Elin:page name]]や[[Category:カテゴリ名]]や[[プロパティ::+]]だの何だの入れること
    --何のプロパティ取得するか分岐できる
    local entityAttributes = {
      'Name',
      'Full Name',
      'ItemLv',
      'Type',
      'CraftingStation',
      'Ingredient',
      'IngredientNum',
      'IngredientExtra',
      'CraftingStation2',
      'Ingredient2',
      'IngredientNum2',
      'IngredientExtra2',
      'CraftingStation3',
      'Ingredient3',
      'IngredientNum3',
      'IngredientExtra3',
    } or {}
    -- build query
    local query = {}
    table.insert(query, pageItem)
    for _, v in pairs( entityAttributes ) do
        table.insert( query, '?' .. v )
    end
    query.mainlabel = '-'
    query.limit = 99
    local resultN = mw.smw.ask(query)

    for _, row in pairs(resultN) do
      table.insert(allResults, row)
    end
    mw.logObject(allResults)
  end

  local CraftingStationResult = {}
  local IngredientResult = {}
  local usedCraftingStations = {}
  --for1 start
  for tableNum = 1, 3, 1 do
    local recipe = Recipe[tableNum]
    CraftingStationResult[tableNum] = {}
    IngredientResult[tableNum] = {}
    --====== CraftingStation ======
    local stations = callsplit(recipe.CraftingStation)
    if #stations > 0 then
      local result = {}
      if recipe.CraftingStation and string.find(recipe.CraftingStation,'/') then
        error('The / is used for CraftingStation')
      else
        if usedCraftingStations[recipe.CraftingStation] then
        else
          if not (mode == 'inline' or mode == 'inlinebr' or mode == 'inlinehr') then
            usedCraftingStations[recipe.CraftingStation] = true
          end
          for i, item in ipairs(stations) do
  
            if i > 1 then
              if mode == 'inline' or mode == 'inlinebr' or mode == 'inlinehr' then
                table.insert(result,'<span class="CSs-separator">&nbsp;</span>')
              elseif mode == 'table' or mode == 'br' or mode == 'ingstd' then
                table.insert(result,'<br />')
              end
            end
  
            if utils.nilCheck(item) then
              local found = false
              if highlightParent and #highlightParent > 0 then
                for _, parentItem in ipairs(highlightParent) do
                  if parentItem == item then
                    found = true
                    break
                  end
                end
              end
              local String = items.generateItem({ lang = lang, name = item, icon = icon })
              if found then
                String = '<span class="item-highlight">'..String..'</span>'
              end
              table.insert(result,String)
            end
          end
          CraftingStationResult[tableNum] = tostring(table.concat(result))
        end
      end
    end
    --====== Ingredient ======
    local ingredients = callsplit(recipe.Ingredient)
    local ingredientNums = callsplit(recipe.IngredientNum)
    local ingredientExtras = callsplit(recipe.IngredientExtra)
    if #ingredients > 0 then
      local result = {}
      local count = 0
      if mode == 'ingstd' and tableNum > 1 then
      else
        for i, item in ipairs(ingredients) do
  
          if i > 1 then
            if mode == 'inline' or mode == 'inlinebr' or mode == 'inlinehr' then
              table.insert(result,'<span class="plus">&nbsp;&nbsp;&plus;&nbsp;</span>')
            elseif mode == 'br' then
              table.insert(result,'<br />')
            elseif mode == 'table' then
              table.insert(result,'<div class="plus or-separator">&plus;</div>')
            elseif mode == 'ingstd' then
              table.insert(result,'</td><td>')
            end
          end
  
          if utils.nilCheck(item) and string.find(item,'/') then
            local ingredientsChild = item and mw.text.split(item, '%s*/%s*') or {}
            local ingredientNumsChild = ingredientNums[i] and mw.text.split(ingredientNums[i], '%s*/%s*') or {}
            local ingredientExtrasChild = ingredientExtras[i] and mw.text.split(ingredientExtras[i], '%s*/%s*') or {}
  
            for i2, item2 in ipairs(ingredientsChild) do
              if i2 > 1 then
                if mode == 'inline' or mode == 'inlinebr' or mode == 'inlinehr' or mode == 'br' then
                  table.insert(result,'<span class="or">'..i18n[lang]['Or']..'</span>')
                else
                  table.insert(result,'<div class="or-separator">'..i18n[lang]['Or']..'</div>')
                end
              end
              local itemName = item2
              local itemNum = ingredientNumsChild[i2] or nil
              local itemMat
              local itemSkin
              local itemVar
              local itemEle
              itemName, itemMat, itemSkin, itemVar, itemEle = items.getItem(itemName)
              local String = items.generateItem({
                lang = lang,
                name = itemName or item2,
                num = itemNum,
                variety = itemVar,
                itemMat = itemMat,
                skin = itemSkin,
                icon = icon,
                element = itemEle
              })
              local itemext = ingredientExtrasChild[i] or ingredientExtrasChild
              if itemext == i18n[lang]['TitleExtra'] then String = String..i18n[lang]['TextExtra']
              elseif itemext == i18n[lang]['TitleGourmet'] then String = String..i18n[lang]['TextGourmet'] end
              local found = false
              if highlightParent and #highlightParent > 0 then
                for _, parentItem in ipairs(highlightParent) do
                  if parentItem == item2 then
                    found = true
                    break
                  end
                end
              end
              if found then
                String = '<span class="item-highlight">'..String..'</span>'
              elseif itemext then
                String = '<span class="item-block" style="white-space: nowrap;">'..String..'</span>'
              end
              table.insert(result,String)
            end
          elseif utils.nilCheck(item) then
            local itemName = item
            local itemNum = ingredientNums[i] or ingredientNums or nil
            local itemMat
            local itemSkin
            local itemVar
            local itemEle
            itemName, itemMat, itemSkin, itemVar, itemEle = items.getItem(itemName)
            local String = items.generateItem({
              lang = lang,
              name = item,
              num = itemNum,
              variety = itemVar,
              itemMat = itemMat,
              skin = itemSkin,
              icon = icon,
              element = itemEle
            })
            local itemext = ingredientExtras[i] or ingredientExtras or ''
            if itemext == i18n[lang]['TitleExtra'] then String = String..i18n[lang]['TextExtra']
            elseif itemext == i18n[lang]['TitleGourmet'] then String = String..i18n[lang]['TextGourmet'] end
            local found = false
            if highlightParent and #highlightParent > 0 then
              for _, parentItem in ipairs(highlightParent) do
                if parentItem == item then
                  found = true
                  break
                end
              end
            end
            if found then
              String = '<span class="item-highlight">'..String..'</span>'
              elseif itemext then
                String = '<span class="item-block" style="white-space: nowrap;">'..String..'</span>'
            end
            table.insert(result,String)
          end
          if mode == 'ingstd' then count = i end
        end
        if ElementNote then
          String = '<br /><span class="" style="font-size: smaller; line-height: normal;"><b>必要特性 : </b>'..ElementNote..'</span>'
          table.insert(result,String)
        end
        if mode == 'ingstd' and count < 5  then
          local repetitions = 5 - count
          if repetitions > 0 then
            for _ = 1, repetitions do
              table.insert(result, '</td><td>')
            end
          end
        end
        IngredientResult[tableNum] = tostring(table.concat(result))
        --for2 end
      end
    end
  end
  --for1 end

  --===== print html =====
  local table
  local html

  if mode == 'inline' or mode == 'inlinebr' or mode == 'inlinehr' or mode == 'br' then
    html = mw.html.create('span'):addClass('mode-'..mode)
  elseif mode == 'table' then
    html = mw.html.create('div'):addClass('CraftingTable'):cssText('float: left; display: ruby;')
  else
    html = mw.html.create('span'):addClass('nomode')
  end

  if utils.nilCheck(size) then
    html:cssText('font-size:'..size..';')
  end

  if mode == 'ingstd' then
    if #CraftingStationResult[1] > 0 then
      html:tag('td'):wikitext(CraftingStationResult[1])
    else
    end
    if #IngredientResult[1] > 0 then
      html:tag('td'):wikitext(IngredientResult[1])
    else
    end
    html:wikitext('</td></tr>')
  else

    for tableNum = 1, 3, 1 do
      if mode == 'inline' or mode == 'inlinebr' or mode == 'inlinehr' or mode == 'br' then
        if tableNum == 2 or tableNum == 3 then
          if #CraftingStationResult[tableNum] > 0 or #IngredientResult[tableNum] > 0 then
            if mode == 'inlinebr' then
              html:wikitext('<br /><span style="mode-inlinebr">')
            elseif mode == 'inlinehr' then
              html:wikitext('<hr class="mode-inline-hr" /><span style="mode-inlinebr">')
            elseif mode == 'br' then
              html:wikitext('<br />')
            elseif mode == 'inline' then
              html:wikitext('<span class="inline-separator2">&nbsp;</span>')--/
            end
          end
        end
        if #CraftingStationResult[tableNum] > 0 then
          html:wikitext(CraftingStationResult[tableNum])
        end
        if #CraftingStationResult[tableNum] > 0 and #IngredientResult[tableNum] > 0 then
          if mode == 'inline' or mode == 'inlinebr' or mode == 'inlinehr' then
            html:wikitext('<span class="inline-separator1">&nbsp;</span>')--:
          end
        end
        if #IngredientResult[tableNum] > 0 then
          html:wikitext(IngredientResult[tableNum])
        end
        if mode == 'inlinebr' or mode == 'inlinehr' then
          html:wikitext('</span>')
        end

      elseif mode == 'table' then
        if #IngredientResult[tableNum] > 0 then
          if tableNum > 1 then
            table = html:tag('table'):addClass('wikitable'):cssText('margin-left:1em;')
          else
            table = html:tag('table'):addClass('wikitable')
          end
          table:tag('tr'):tag('th'):wikitext(i18n[lang]['TitleCraftingStation']):tag('th'):wikitext(i18n[lang]['TitleIngredient']):tag('tr')
          td = table:tag('td')
          if #IngredientResult[tableNum] > 0 then
            td:wikitext(CraftingStationResult[tableNum])
          end
          td2 = table:tag('td'):cssText('text-align: center;')
          if #IngredientResult[tableNum] > 0 then
            td2:wikitext(IngredientResult[tableNum])
          end
        end
      else
        --mode設定ミスの場合
      end
    end

    if mode == 'table' then
      html:wikitext('</div><div style="clear:both;"></div>')
    end
  end

  return tostring(html)
end

function p.craftInline(f)
  local args = f:getParent().args
  local inlineData = {
    lang = f.args['lang'] or 'JA',
    mode = args['mode'] or 'inline',
    icon = args['icon'],
    size = utils.getDefaultIfEmpty(args['size'], '32x32px'),
    ask = args['ask'],
    highlight = args['highlight'],
    highlightParent = args['highlightParent'],
    ElementNote = args['ElementNote'],
    ['1-1'] = args['1-1'],
    ['1-2'] = args['1-2'],
    ['1-3'] = args['1-3'],
    ['1-4'] = args['1-4'],
    ['2-1'] = args['2-1'],
    ['2-2'] = args['2-2'],
    ['2-3'] = args['2-3'],
    ['2-4'] = args['2-4'],
    ['3-1'] = args['3-1'],
    ['3-2'] = args['3-2'],
    ['3-3'] = args['3-3'],
    ['3-4'] = args['3-4']
  }
  return p.generatecraftInline(inlineData)
end

return p