コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

モジュール:天保暦/書式

モジュールの解説[作成]
local Format = {}

function Format.format (frame)
  return format (unpack (toarray (frame.args)))
end

--[[--------------------------------------------------------------------------]]

function format (template, year, month, day)
  local y = tonumber (year)
  local m = tonumber (month)
  local d = tonumber (day)
  
  local zero = "<span style=\"speak:none;visibility:hidden;color:transparent\">0</span>"
  
  local _y = tostring (y)
  local _y02 = string.format ("%02d", y)
  local _yz2 = string.gsub (string.format ("%2d", y), " ", zero)
  
  template = string.gsub (template, "{年}", _y)
  template = string.gsub (template, "{年:02桁}", _y02)
  template = string.gsub (template, "{年:2桁}", _yz2)
  
  local _m = tostring (m)
  local _m02 = string.format ("%02d", m)
  local _mz2 = string.gsub (string.format ("%2d", m), " ", zero)
  
  template = string.gsub (template, "{月}", _m)
  template = string.gsub (template, "{月:02桁}", _m02)
  template = string.gsub (template, "{月:2桁}", _mz2)
  
  local _d = tostring (d)
  local _d02 = string.format ("%02d", d)
  local _dz2 = string.gsub (string.format ("%2d", d), " ", zero)
  
  template = string.gsub (template, "{日}", _d)
  template = string.gsub (template, "{日:02桁}", _d02)
  template = string.gsub (template, "{日:2桁}", _dz2)
 
  return template
end

--[[--------------------------------------------------------------------------]]

function toarray(table)
  local values = {}
  for key,value in pairs(table) do
    key = tonumber(key)
    if key then
      values[key] = value
    end
  end
  return values
end

return Format