<commit_before>Components.utils.import('resource://gre/modules/Services.jsm')
Components.utils.import('resource://gre/modules/AddonManager.jsm')
Components.utils.import('resource://zotero/config.js')

Zotero.BetterBibTeX = {
  serializer: Components.classes['@mozilla.org/xmlextras/xmlserializer;1'].createInstance(Components.interfaces.nsIDOMSerializer)
  document: Components.classes['@mozilla.org/xul/xul-document;1'].getService(Components.interfaces.nsIDOMDocument)
  Cache: new loki('betterbibtex.db', {env: 'BROWSER'})
}

Zotero.BetterBibTeX.error = (msg...) ->
  @_log.apply(@, [0].concat(msg))
Zotero.BetterBibTeX.warn = (msg...) ->
  @_log.apply(@, [1].concat(msg))

Zotero.BetterBibTeX.debug_off = ->
Zotero.BetterBibTeX.debug = Zotero.BetterBibTeX.debug_on = (msg...) ->
  @_log.apply(@, [5].concat(msg))

Zotero.BetterBibTeX.log_off = ->
Zotero.BetterBibTeX.log = Zotero.BetterBibTeX.log_on = (msg...) ->
  @_log.apply(@, [3].concat(msg))

Zotero.BetterBibTeX.debugMode = ->
  if @pref.get('debug')
    Zotero.Debug.setStore(true)
    Zotero.Prefs.set('debug.store', true)
    @debug = @debug_on
    @log = @log_on
    @flash('Debug mode active', 'Debug mode is active. This will affect performance.')
  else
    @debug = @debug_off
    @log = @log_off

Zotero.BetterBibTeX.parseDate = (dateish, locale) ->
  return unless typeof dateish == 'string'
  dateish = dateish.trim()
  return null unless dateish

  return {literal: dateish} if dateish.indexOf('[') >= 0 || dateish in ['/', '_']

  # try EDTF date range
  compact = dateish.replace(/\s/g, '')
  for sep in ['/', '_']
    range = compact.split(sep)
    if range.length == 2
      dateparts = []
      for date in range
        switch
          when date == ''
            dateparts.push([0, 0])
            continue

          when date.match(/^-?[0-9]{1,4}([-/\.][0-9]{1,2}([-/\.][0-9]{1,2})?)?$/)
            yearsign = 1
            if date[0] == '-'
              yearsign = -1
              date = date.slice(1)
            date = date.split(/[-/\.]/)

          when date.match(/^([0-9]{1,2}[-/\.])?[0-9]{1,2}[-/\.][0-9]{1,4}?$/)
            date = date.split(/[-/\.]/)
            date.reverse()
            yearsign = 1

          else
            continue

        date = (parseInt(d) for d in date)
        date[0] *= yearsign
        [date[1], date[2]] = [date[2], date[1]] if date.length == 3 && date[1] > 12
        dateparts.push(date)

      return {'date-parts': dateparts} if dateparts[0] && dateparts[1]

  range = dateish.split('_')
  range = [dateish] unless range.length in [1,2]

  locale ||= Zotero.locale
  dateparts = []
  for date in range
    date = date.trim()
    if date == ''
      dateparts.push([0, 0])
      continue

    @CultureStrings[locale] = @CultureStrings[locale.toLowerCase()] || @CultureStrings.en if !@CultureStrings[locale]

    for k, v of @CultureStrings[locale]<commit_msg>csl locales<commit_after>Components.utils.import('resource://gre/modules/Services.jsm')
Components.utils.import('resource://gre/modules/AddonManager.jsm')
Components.utils.import('resource://zotero/config.js')

Zotero.BetterBibTeX = {
  serializer: Components.classes['@mozilla.org/xmlextras/xmlserializer;1'].createInstance(Components.interfaces.nsIDOMSerializer)
  document: Components.classes['@mozilla.org/xul/xul-document;1'].getService(Components.interfaces.nsIDOMDocument)
  Cache: new loki('betterbibtex.db', {env: 'BROWSER'})
}

Zotero.BetterBibTeX.error = (msg...) ->
  @_log.apply(@, [0].concat(msg))
Zotero.BetterBibTeX.warn = (msg...) ->
  @_log.apply(@, [1].concat(msg))

Zotero.BetterBibTeX.debug_off = ->
Zotero.BetterBibTeX.debug = Zotero.BetterBibTeX.debug_on = (msg...) ->
  @_log.apply(@, [5].concat(msg))

Zotero.BetterBibTeX.log_off = ->
Zotero.BetterBibTeX.log = Zotero.BetterBibTeX.log_on = (msg...) ->
  @_log.apply(@, [3].concat(msg))

Zotero.BetterBibTeX.debugMode = ->
  if @pref.get('debug')
    Zotero.Debug.setStore(true)
    Zotero.Prefs.set('debug.store', true)
    @debug = @debug_on
    @log = @log_on
    @flash('Debug mode active', 'Debug mode is active. This will affect performance.')
  else
    @debug = @debug_off
    @log = @log_off

Zotero.BetterBibTeX.parseDate = (dateish, locale) ->
  return unless typeof dateish == 'string'
  dateish = dateish.trim()
  return null unless dateish

  return {literal: dateish} if dateish.indexOf('[') >= 0 || dateish in ['/', '_']

  # try EDTF date range
  compact = dateish.replace(/\s/g, '')
  for sep in ['/', '_']
    range = compact.split(sep)
    if range.length == 2
      dateparts = []
      for date in range
        switch
          when date == ''
            dateparts.push([0, 0])
            continue

          when date.match(/^-?[0-9]{1,4}([-/\.][0-9]{1,2}([-/\.][0-9]{1,2})?)?$/)
            yearsign = 1
            if date[0] == '-'
              yearsign = -1
              date = date.slice(1)
            date = date.split(/[-/\.]/)

          when date.match(/^([0-9]{1,2}[-/\.])?[0-9]{1,2}[-/\.][0-9]{1,4}?$/)
            date = date.split(/[-/\.]/)
            date.reverse()
            yearsign = 1

          else
            continue

        date = (parseInt(d) for d in date)
        date[0] *= yearsign
        [date[1], date[2]] = [date[2], date[1]] if date.length == 3 && date[1] > 12
        dateparts.push(date)

      return {'date-parts': dateparts} if dateparts[0] && dateparts[1]

  range = dateish.split('_')
  range = [dateish] unless range.length in [1,2]

  locale ||= Zotero.locale
  dateparts = []
  for date in range
    date = date.trim()
    if date == ''
      dateparts.push([0, 0])
      continue

    @CSLLocales[locale] = @CSLLocales[locale.toLowerCase()] || @CSLLocales.en if !@CSLLocales[locale]

    for k, v of @CSLLocales[locale]
{'fraction_non_alphanumeric': 0.18702290076335878, 'fraction_numerical': 0.018736988202637056, 'mean_word_length': 2.7532552083333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2403314', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2036, 'n_words': 366}