Modul:Vorlage:ArmFrGrandeGuerre

Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


local ok, tomes = pcall(require, "Module:Vorlage:ArmFrGrandeGuerre/data")
if (not ok) then return debug.traceback("Module:Vorlage:ArmFrGrandeGuerre/data fehlt!") end

function linkbook(frame, book, text)
  if (type(book) ~= "table") then return end
  if (type(text) ~= "string") then return end
  if (not book.id or book.id == "#") then return text end

  local ok, ret = pcall(frame.expandTemplate, frame, { title = 'Gallica', args = { ID = book.id, Text = text, KeinText = 1 } })
  if ok then return ret else return text end
end

function precisAndAnnexes(frame, refobj)
  local k, v
  local r = ""
  for k, v in ipairs(refobj.precis) do
    if (k == 1) then if (#refobj.precis > 1) then r = r .. ": Précis editions " .. linkbook(frame, v, v.year) else r = r .. ": Précis " .. linkbook(frame, v, v.year) end
    else r = r .. " und " .. linkbook(frame, v, v.year)
    end
  end
  if (refobj.annexes) then
    for k, v in ipairs(refobj.annexes) do
      if (k == 1) then
        if (string.sub(v.code, 1, 1) == "A") then r = r .. "; Annexes " .. linkbook(frame, v, string.sub(v.code, 2))
        else r = r .. "; Cartes " .. linkbook(frame, v, string.sub(v.code, 2))
        end
        if (string.sub(v.code, 1, 1) == "A" and k < #refobj.annexes and string.sub(refobj.annexes[k + 1].code, 1, 1) == "C") then
          r = r .. "; Cartes "
        end
      elseif (string.sub(v.code, 1, 1) == "A" and k < #refobj.annexes and string.sub(refobj.annexes[k + 1].code, 1, 1) == "C") then
        r = r .. " und " .. linkbook(frame, v, string.sub(v.code, 2)) .. "; Cartes "
      elseif (k == #refobj.annexes and string.sub(v.code, 1, 1) == string.sub(refobj.annexes[k - 1].code, 1, 1)) then
        r = r .. " und " .. linkbook(frame, v, string.sub(v.code, 2))
      elseif (string.sub(v.code, 1, 1) == "C" and string.sub(refobj.annexes[k - 1].code, 1, 1) == "A") then
        r = r .. linkbook(frame, v, string.sub(v.code, 2))
      else
        r = r .. ", " .. linkbook(frame, v, string.sub(v.code, 2))
      end
    end
  end
  return r
end

local p = {}

function p.formattome(frame)
  local tome = tonumber(frame.args["tome"]) or 0
  local prefix = frame.args.prefix or ""

  if (tome < 1 or tome > #tomes) then return "<span class=\"error\">Parameter für den Band fehlt oder ist fehlerhaft!</span>" end
  local reftome = tomes[tome]

  local r = prefix .. "<cite>''Les Armées françaises dans la Grande guerre''</cite>, Tome " .. reftome.nr .. ": ''" .. reftome.title .. "''"
  if (reftome.years and reftome.years ~= "") then r = r .. " (" .. reftome.years .. ")." end

  local k, v
  if (not reftome.volumes) then r = r .. precisAndAnnexes(frame, reftome) .. "."
  else
    for k, v in ipairs(reftome.volumes) do
      r = r .. "\n" .. prefix .. "* Vol. " .. k
      if (v.title) then r = r .. ": ''" .. v.title .. "''" end
      if (v.years and v.years ~= "") then r = r .. " (" .. v.years .. ")." end
      r = r .. precisAndAnnexes(frame, v) .. "."
    end
  end
  return r
end

function p.formatvolume(frame)
  local tome    = tonumber(frame.args["tome"]) or 0
  local volume  = tonumber(frame.args["vol"]) or 0

  local reftome = {}
  local refvolume = {}
  local refobj = {}

  if (tome < 1 or tome > #tomes) then return "<span class=\"error\">Parameter für den Band fehlt oder ist fehlerhaft!</span>"
  else
    reftome = tomes[tome]
    refobj = reftome
  end

  if (reftome.volumes) then
    if (#reftome.volumes > 1 and (volume < 1 or volume > #reftome.volumes)) then
      return "<span class=\"error\">Parameter für den Teilband fehlt oder ist fehlerhaft!</span>"
    else
      refvolume = reftome.volumes[volume]
      refobj = refvolume
    end
  else -- Don't care?
  end

  local r = "''Les Armées françaises dans la Grande guerre'' (AFGG), Tome " .. reftome.nr
  r = r .. ", Volume " .. volume .. ": ''" .. refobj.title .. "''"
  if (refobj.years) then r = r .. " (" .. refobj.years .. ")" end

  r = r .. precisAndAnnexes(frame, refobj)

  return r
end

function p.formatbook(frame)
  local tome        = tonumber(frame.args["tome"]) or 0
  local volume      = tonumber(frame.args["vol"]) or 0
  local edition     = tonumber(frame.args["ed"]) or 0
  local annex       = frame.args["annex"] or ""
  local pages       = frame.args["pages"] or ""
  local chapter     = frame.args["chapter"] or ""
  local format      = frame.args["format"] or "semi"
  local gallicapage = frame.args["gallicapage"] or nil

  local refobj = {}
  local reftome = {}
  local refvolume = {}
  local annextitle = ""
  local refyear = ""
  local refid = ""

  if (tome < 1 or tome > #tomes) then return "<span class=\"error\">Parameter für den Band fehlt oder ist inkorrekt!</span>"
  else
    reftome = tomes[tome]
    refobj = reftome
  end

  if (reftome.volumes) then
    if (#reftome.volumes == 1) then
      volume = 1
      refvolume = reftome.volumes[1]
      refobj = refvolume
    elseif (#reftome.volumes > 1 and (volume < 1 or volume > #reftome.volumes)) then
      return "<span class=\"error\">Parameter für den Teilband fehlt oder ist inkorrekt!</span>"
    else
      refvolume = reftome.volumes[volume]
      refobj = refvolume
    end
  else -- Don't care?
  end

  if (#refobj.precis > 1 and string.len(annex) == 0) then
    if (edition < 1 or edition > #refobj.precis) then return "<span class=\"error\">Parameter für die Auflage fehlt oder ist inkorrekt!</span>"
    else
      Auflage = edition
      refyear = refobj.precis[edition].year
      refid = refobj.precis[edition].id
    end
  else
    refyear = refobj.precis[1].year
    refid = refobj.precis[1].id
  end

  if (string.len(annex) > 0) then
    if (tonumber(string.sub(annex, 1, 1)) ~= nil) then annex = "A" .. annex end -- annex volume by default, map volume only explicitly
    local cur
    if (not refobj.annexes) then return "<span class=\"error\">Parameter für die Anlage ist fehlerhaft!</span>"
    else
      for k, v in ipairs(refobj.annexes) do
        if (tostring(annex) == v.code) then
          cur = v.code
          refid = v.id
          if (v.year and string.len(v.year) > 0) then refyear = v.year end

          if (string.sub(annex, 1, 1) == "A") then annextitle = "Annexes"
          elseif (string.sub(annex, 1, 1) == "C") then annextitle = "Cartes"
          end

          if (string.sub(annex, 2)) then annextitle = annextitle .. ", vol. " .. string.sub(annex, 2) end
          break
        end
      end --for
      if (annex ~= tostring(cur)) then return "<span class=\"error\">Parameter für die Anlage ist fehlerhaft!</span>" end
    end --else
  end

  if ((not refyear) or string.len(refyear) < 4) then refyear = refobj.year end
  if ((not refyear) or string.len(refyear) < 4) then refyear = refobj.years end

  if ((not refid) or string.len(refid) < 13) then refid = refobj.id end

  if format == "kurz" then
    local r = "<cite><abbr title='Les Armées françaises dans la Grande guerre'>AFGG</abbr></cite> " .. reftome.nr
    if (refvolume.title) then r = r .. "/" .. volume end
    if (string.len(annextitle) > 0) then r = r .. ", " .. annextitle end
    if (string.len(refyear) > 0) then r = r .. " (" .. refyear .. ")" end
    if (string.len(chapter) > 0) then r = r .. ", Kapitel " .. chapter end
    if (string.len(pages) > 0) then r = r .. ", S. " .. pages end
    if (type(refid) == "string" and refid ~= "#") then
      local ret = mw.getCurrentFrame():expandTemplate( { title = 'Gallica', args = { ID = refid, Seite = gallicapage } })
      r = r .. ", " .. ret
    end
    return r .. "."
  elseif format == "semi" then
    local r = "<cite>''Les Armées françaises dans la Grande guerre'' (<abbr>AFGG</abbr>)</cite>, Tome " .. reftome.nr
    if (refvolume.title) then r = r .. "/Vol. " .. volume .. ": ''<cite>" .. refvolume.title .. "</cite>''" end
    if (string.len(annextitle) > 0) then r = r .. ", " .. annextitle end
    if (string.len(refyear) > 0) then r = r .. ", Paris " .. refyear end
    if (string.len(chapter) > 0) then r = r .. ", Kapitel " .. chapter end
    if (string.len(pages) > 0) then r = r .. ", S. " .. pages end
    if (type(refid) == "string" and refid ~= "#") then
      local ret = mw.getCurrentFrame():expandTemplate( { title = 'Gallica', args = { ID = refid, Seite = gallicapage } })
      r = r .. ", " .. ret
    end
    return r .. "."
  elseif format == "lang" then
    local litargs = {
      HrsgReihe="Ministère de la guerre – État-major de l’armée – service historique",
      Reihe="Les Armées françaises dans la Grande guerre [AFGG]",
      Verlag="Imprimerie nationale",
      Ort="Paris"
    }
    local Band = reftome.nr
    local Titel = reftome.title
    if (refvolume.title) then
      Titel = Titel .. " / " .. refvolume.title
    end
    if (volume > 0) then
      Band = Band .. "/" .. volume
    end
    if (string.len(annextitle) > 0) then
      Band = Band .. ", " .. annextitle
    end
    if (type(refid) == "string" and refid ~= "#") then
      local ret = mw.getCurrentFrame():expandTemplate( { title = 'Gallica', args = { ID = refid, Seite = gallicapage } })
      if (ok) then litargs["Online"] = ret end
    end
    litargs["Titel"] = Titel
    litargs["BandReihe"] = Band
    if (refyear and string.len(refyear) > 0) then litargs["Datum"] = refyear end
    if (edition > 0) then litargs["Auflage"] = edition end
    if (string.len(chapter) > 0) then litargs["Kapitel"] = chapter end
    if (string.len(pages) > 0) then litargs["Seiten"] = pages end
    ret = mw.getCurrentFrame():expandTemplate({ title = 'Literatur', args = litargs })
    return ret
  end
end

return p