모듈:Calendar

최근 편집: 2016년 11월 13일 (일) 12:54
탕수육 (토론 | 기여)님의 2016년 11월 13일 (일) 12:54 판 (새 문서: local p = {} local function is_leap_year(year) return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0) end local function get_days_in_month(year, month) if month == 2 and...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

이 모듈에 대한 설명문서는 모듈:Calendar/설명문서에서 만들 수 있습니다

local p = {}

local function is_leap_year(year)
  return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
end

local function get_days_in_month(year, month)
  if month == 2 and is_leap_year(year) then
    return 29
  end

  local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  return days_in_month[month]
end

function p.tabular(frame)
  local year = frame.args.year + 0
  local month = frame.args.month + 0
  local days = get_days_in_month(year, month)
  local first_day_of_week = os.date("*t", os.time{year=year,month=month,day=1})["wday"]
  local n_cells = first_day_of_week - 1 + days
  local n_trailing_cells = 7 - (n_cells % 7)

  -- Generate date cells
  local cells = {}
  -- 1. Prepend place holders if needed
  for i = 1, first_day_of_week - 1 do
    table.insert(cells, "X")
  end
  -- 2. Insert date cells
  for i = 1, days do
    table.insert(cells, i)
  end
  -- 3. Append place holders if needed
  for i = 1, n_trailing_cells do
    table.insert(cells, "X")
  end

  -- Render as a table
  local html = {
    '<table class="fw-calendar">',
    '<tr>',
    '<th class="sun">일</th>',
    '<th class="mon">월</th>',
    '<th class="tue">화</th>',
    '<th class="wed">수</th>',
    '<th class="thu">목</th>',
    '<th class="fri">금</th>',
    '<th class="sat">토</th>',
    '</tr>'
  }
  local day_names = {'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'}
  local index = 1
  while cells[index] do
    table.insert(html, '<tr>')
    for i = 1, 7 do
      local cell = cells[index]
      local is_placeholder = cell == "X"
      if is_placeholder then
        table.insert(html, '<td class="placeholder ' .. day_names[i] .. '"> </td>')
      else
        table.insert(html, '<td class="' .. day_names[i] .. '">[[' .. month .. '월 ' .. cell .. '일|' .. cell .. ']]</td>')
      end
      index = index + 1
    end
    table.insert(html, '</tr>')
  end
  table.insert(html, '</table>')

  return table.concat(html, "")
end

return p