<commit_before>require('./calendar.css')
import createCalendarHTML from './calendar'

module.exports = function calendarPlugin (md, options) {
  let name = 'calendar',
    startMarkerStr = '#[' + name + '=',
    endMarkerStr = '#[/' + name + ']',
    PARAM_REGEX = /^(\d+)[ ]+(\d+)$/,
    DATE_REGEX = /<!--\s*(\d+)\s*-->/,
    EVENT_REGEX = /@\[(.*?)\](.*)/

  /*************************************************************
   * Default validate and render function
   */

  function renderDefault (tokens, idx, _options, env, self) {
    const data = tokens[idx].info
    const table = createCalendarHTML(data)
    return '<div class="markdown-it-calendar">' + table + '</div>'
  }

  /*************************************************************
   * Helper functions
   */
  function isValidDate (d) {
    // time structure must be {year: 2017, month: 12, date: 30, time: "13:14"}
    try {
      let result = new Date(d.year, d.month - 1, d.date)
      result.setFullYear(d.year) // ensure every year can be used
      let valid = d.year === result.getFullYear() &&
              d.month === (result.getMonth() + 1) &&
              d.date === result.getDate()
      return valid ? result : 'Invalid'
    } catch (err) {
      return false
    }
    return false
  }

  function isValidParam (params) {
    // return true if params is valid
    try {
      params = params.trim()
      params = params.match(PARAM_REGEX)
      if (params) {
        var year = parseInt(params[1])
        var month = parseInt(params[2])
        return year >= 0 && year <= 100000 && month >= 1 && month <= 12
      } else {
        return false
      }
    } catch (e) {
      return false
    }
  }

  function parseStartLine (src, start, end) {
    // Return earlier if not match
    let valid = src[end - 1] === ']'
    if (!valid) {
      return false
    }

    valid = src.substring(start, start + startMarkerStr.length) === startMarkerStr
    if (!valid) {
      return false
    }

    valid = isValidParam(src.substring(start + startMarkerStr.length, end - 1))
    if (!valid) {
      return false
    }
    let params = src.substring(start + startMarkerStr.length, end - 1).trim().match(PARAM_REGEX)

    return {
      year: parseInt(params[1]),
      month: parseInt(params[2])
    }
  }

  function parseEndLine (src, start, end) {
    return src.substring(start, end).trim() == endMarkerStr
  }

  function parseDate (src, start, end, time) {
    // extract a valid Date
    let lineStr = src.substring(start, end).trim()
    try {
      let date = lineStr.match(DATE_REGEX)
      let localTime = Object.assign({}, time)
      localTime['date'] = parseInt(date[1])
      return isValidDate(localTime)
    } catch (err) {
      return false
    }
    return false
  }

  function parseEvent (src, start, end) {
    let lineStr = src.substring(start, end).trim()
    try {
      let event = lineStr.match(EVENT_REGEX)
      return {
        title: event[1],
        description: event[2]
      }
    } catch (err) {
      return false
    }
    return false
  }

  function addToken (state, params) {
    let token = state.push(params.type, params.tag || 'div', params.nesting)
    token.markup = params.markup || ''
    token.block = params.block || true
    token.content = params.content || ''
    if ('info' in params) {
      token.info = params.info
    }
    if ('map' in params) {
      token.map = params.map
    }
    return token
  }

  /*************************************************************/

  options = options || {}

  let render = options.render || renderDefault

  /*************************************************************
   * Rule function
   */
  function calendarRule (state, startLine, endLine, silent) {
    let currentLine, currentDay,
      autoClosed = 0,
      token,
      start = state.bMarks[startLine] + state.tShift[startLine],
      end = state.eMarks[startLine],
      renderInfo = {
        Date: {},
        Content: {}
      }

    // check the first line is correct
    let date = parseStartLine(state.src, start, end)
    if (date === false) {
      return false
    }<commit_msg>delete useless Function<commit_after>require('./calendar.css')
import createCalendarHTML from './calendar'

module.exports = function calendarPlugin (md, options) {
  let name = 'calendar',
    startMarkerStr = '#[' + name + '=',
    endMarkerStr = '#[/' + name + ']',
    PARAM_REGEX = /^(\d+)[ ]+(\d+)$/,
    DATE_REGEX = /<!--\s*(\d+)\s*-->/,
    EVENT_REGEX = /@\[(.*?)\](.*)/

  options = options || {}
  let render = options.render || renderDefault
  /*************************************************************
   * Default validate and render function
   */

  function renderDefault (tokens, idx, _options, env, self) {
    const data = tokens[idx].info
    const table = createCalendarHTML(data)
    return '<div class="markdown-it-calendar">' + table + '</div>'
  }

  /*************************************************************
   * Helper functions
   */
  function isValidDate (d) {
    // time structure must be {year: 2017, month: 12, date: 30, time: "13:14"}
    try {
      let result = new Date(d.year, d.month - 1, d.date)
      result.setFullYear(d.year) // ensure every year can be used
      let valid = d.year === result.getFullYear() &&
              d.month === (result.getMonth() + 1) &&
              d.date === result.getDate()
      return valid ? result : 'Invalid'
    } catch (err) {
      return false
    }
    return false
  }

  function isValidParams (params) {
    // return true if params is valid
    try {
      params = params.trim()
      params = params.match(PARAM_REGEX)
      if (params) {
        var year = parseInt(params[1])
        var month = parseInt(params[2])
        return year >= 0 && year <= 100000 && month >= 1 && month <= 12
      } else {
        return false
      }
    } catch (e) {
      return false
    }
  }

  function parseStartLine (src, start, end) {
    // Return earlier if not match
    let valid = src[end - 1] === ']'
    if (!valid) {
      return false
    }

    valid = src.substring(start, start + startMarkerStr.length) === startMarkerStr
    if (!valid) {
      return false
    }

    valid = isValidParams(src.substring(start + startMarkerStr.length, end - 1))
    if (!valid) {
      return false
    }
    let params = src.substring(start + startMarkerStr.length, end - 1).trim().match(PARAM_REGEX)

    return {
      year: parseInt(params[1]),
      month: parseInt(params[2])
    }
  }

  function parseEndLine (src, start, end) {
    return src.substring(start, end).trim() == endMarkerStr
  }

  function parseDate (src, start, end, time) {
    // extract a valid Date
    let lineStr = src.substring(start, end).trim()
    try {
      let date = lineStr.match(DATE_REGEX)
      let localTime = Object.assign({}, time)
      localTime['date'] = parseInt(date[1])
      return isValidDate(localTime)
    } catch (err) {
      return false
    }
    return false
  }

  function parseEvent (src, start, end) {
    let lineStr = src.substring(start, end).trim()
    try {
      let event = lineStr.match(EVENT_REGEX)
      return {
        title: event[1],
        description: event[2]
      }
    } catch (err) {
      return false
    }
    return false
  }

  function addToken (state, params) {
    let token = state.push(params.type, params.tag || 'div', params.nesting)
    token.markup = params.markup || ''
    token.block = params.block || true
    token.content = params.content || ''
    if ('info' in params) {
      token.info = params.info
    }
    if ('map' in params) {
      token.map = params.map
    }
    return token
  }

  /*************************************************************
   * Rule function
   */
  function calendarRule (state, startLine, endLine, silent) {
    let currentLine, currentDay,
      autoClosed = 0,
      token,
      start = state.bMarks[startLine] + state.tShift[startLine],
      end = state.eMarks[startLine],
      renderInfo = {
        Date: {},
        Content: {}
      }

    // check the first line is correct
    let date = parseStartLine(state.src, start, end)
    if (date === false) {
      return false
    }
{'fraction_non_alphanumeric': 0.18674625411134121, 'fraction_numerical': 0.008527226215129736, 'mean_word_length': 2.4626739772248, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6225023', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2452, 'n_words': 646}