<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.stringify = (obj, replacer, spaces, cycleReplacer) ->
  str = JSON.stringify(obj, @stringifier(replacer, cycleReplacer), spaces)

  if Array.isArray(obj)
    hybrid = false
    keys = Object.keys(obj)
    if keys.length > 0
      o = {}
      for key in keys
        continue if key.match(/^\d+$/)
        o[key] = obj[key]
        hybrid = true
      str += '+' + @stringify(o) if hybrid
  return str

Zotero.BetterBibTeX.stringifier = (replacer, cycleReplacer) ->
  stack = []
  keys = []
  if cycleReplacer == null
    cycleReplacer = (key, value) ->
      return '[Circular ~]' if stack[0] == value
      return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']'

  return (key, value) ->
    if stack.length > 0
      thisPos = stack.indexOf(this)
      if ~thisPos then stack.splice(thisPos + 1) else stack.push(this)
      if ~thisPos then keys.splice(thisPos, Infinity, key) else keys.push(key)
      value = cycleReplacer.call(this, key, value) if ~stack.indexOf(value)
    else
      stack.push(value)

    return value if replacer == null || replacer == undefined
    return replacer.call(this, key, value)

Zotero.BetterBibTeX._log = (level, msg...) ->
  str = []
  for m in msg
    switch
      when (typeof m) in ['boolean', 'string', 'number']
        str.push('' + m)
      when m instanceof Error
        str.push("<Exception: #{m.msg}#{if m.stack then '\n' + m.stack else ''}>")
      else
        str.push(Zotero.BetterBibTeX.stringify(m))
  str = (s for s in str when s != '')
  str = str.join(' ')

  if level == 0
    Zotero.logError(msg)
  else
    Zotero.debug('[better' + '-' + 'bibtex] ' + str, level)

Zotero.BetterBibTeX.extensionConflicts = ->
  AddonManager.getAddonByID('<EMAIL>', (zutilo) ->
    return unless zutilo
    return if Services.vc.compare(zutilo.version, '1.2.10.1') > 0
    Zotero.BetterBibTeX.removeTranslators()
    Zotero.BetterBibTeX.disabled = '''
      Better BibTeX has been disabled because it has detected conflicting extension "zutilo" 1.2.10.1 or
      earlier. If have proposed a fix at

      https://github.com/willsALMANJ/Zutilo/issues/42

      Once that has been implemented, Better BibTeX will start up as usual. In the meantime, beta7 from 
      
      https://addons.mozilla.org/en-US/firefox/addon/zutilo-utility-for-zotero/versions/

      should work; alternately, you can uninstall Zutilo.
    '''
    Zotero.BetterBibTeX.flash('Better BibTeX has been disabled', Zotero.BetterBibTeX.disabled)
  )

  AddonManager.getAddonByID('{359f0058-a6ca-443e-8dd8-09868141bebc}', (recoll) ->
    return unless recoll
    return if Services.vc.compare(recoll.version, '1.2.3') > 0
    Zotero.BetterBibTeX.removeTranslators()
    Zotero.BetterBibTeX.disabled = '''
      Better BibTeX has been disabled because it has detected conflicting extension "recoll-firefox" 1.2.3 or
      earlier. If have proposed a fix for recall-firefox at

      https://sourceforge.net/p/recollfirefox/discussion/general/thread/a31d3c89/

      Once that has been implemented, Better BibTeX will start up as usual.  Alternately, you can uninstall Recoll Firefox.

      In the meantime, unfortunately, Better BibTeX and recoll-firefox cannot co-exist, and the previous workaround
      Better BibTeX had in place conflicts with a Mozilla policy all Fireox extensions must soon comply with.
    '''
    Zotero.BetterBibTeX.flash('Better BibTeX has been disabled', Zotero.BetterBibTeX.disabled)
  )

  if ZOTERO_CONFIG.VERSION?.match(/\.SOURCE$/)
    #@flash(
    Zotero.logError(<commit_msg>disable BBT when it sees Zutilo #311<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.stringify = (obj, replacer, spaces, cycleReplacer) ->
  str = JSON.stringify(obj, @stringifier(replacer, cycleReplacer), spaces)

  if Array.isArray(obj)
    hybrid = false
    keys = Object.keys(obj)
    if keys.length > 0
      o = {}
      for key in keys
        continue if key.match(/^\d+$/)
        o[key] = obj[key]
        hybrid = true
      str += '+' + @stringify(o) if hybrid
  return str

Zotero.BetterBibTeX.stringifier = (replacer, cycleReplacer) ->
  stack = []
  keys = []
  if cycleReplacer == null
    cycleReplacer = (key, value) ->
      return '[Circular ~]' if stack[0] == value
      return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']'

  return (key, value) ->
    if stack.length > 0
      thisPos = stack.indexOf(this)
      if ~thisPos then stack.splice(thisPos + 1) else stack.push(this)
      if ~thisPos then keys.splice(thisPos, Infinity, key) else keys.push(key)
      value = cycleReplacer.call(this, key, value) if ~stack.indexOf(value)
    else
      stack.push(value)

    return value if replacer == null || replacer == undefined
    return replacer.call(this, key, value)

Zotero.BetterBibTeX._log = (level, msg...) ->
  str = []
  for m in msg
    switch
      when (typeof m) in ['boolean', 'string', 'number']
        str.push('' + m)
      when m instanceof Error
        str.push("<Exception: #{m.msg}#{if m.stack then '\n' + m.stack else ''}>")
      else
        str.push(Zotero.BetterBibTeX.stringify(m))
  str = (s for s in str when s != '')
  str = str.join(' ')

  if level == 0
    Zotero.logError(msg)
  else
    Zotero.debug('[better' + '-' + 'bibtex] ' + str, level)

Zotero.BetterBibTeX.extensionConflicts = ->
  AddonManager.getAddonByID('<EMAIL>', (zutilo) ->
    return unless zutilo
    return if Services.vc.compare(zutilo.version, '1.2.10.1') > 0
    Zotero.BetterBibTeX.removeTranslators()
    Zotero.BetterBibTeX.disabled = '''
      Better BibTeX has been disabled because it has detected conflicting extension "zutilo" 1.2.10.1 or
      earlier. If have proposed a fix at

      https://github.com/willsALMANJ/Zutilo/issues/42

      Once that has been implemented, Better BibTeX will start up as usual. In the meantime, beta7 from

      https://addons.mozilla.org/en-US/firefox/addon/zutilo-utility-for-zotero/versions/

      should work; alternately, you can uninstall Zutilo.
    '''
    Zotero.BetterBibTeX.flash('Better BibTeX has been disabled', Zotero.BetterBibTeX.disabled)
  )

  AddonManager.getAddonByID('{359f0058-a6ca-443e-8dd8-09868141bebc}', (recoll) ->
    return unless recoll
    return if Services.vc.compare(recoll.version, '1.2.3') > 0
    Zotero.BetterBibTeX.removeTranslators()
    Zotero.BetterBibTeX.disabled = '''
      Better BibTeX has been disabled because it has detected conflicting extension "recoll-firefox" 1.2.3 or
      earlier. If have proposed a fix for recall-firefox at

      https://sourceforge.net/p/recollfirefox/discussion/general/thread/a31d3c89/

      Once that has been implemented, Better BibTeX will start up as usual.  Alternately, you can uninstall Recoll Firefox.

      In the meantime, unfortunately, Better BibTeX and recoll-firefox cannot co-exist, and the previous workaround
      Better BibTeX had in place conflicts with a Mozilla policy all Fireox extensions must soon comply with.
    '''
    Zotero.BetterBibTeX.flash('Better BibTeX has been disabled', Zotero.BetterBibTeX.disabled)
  )

  if ZOTERO_CONFIG.VERSION?.match(/\.SOURCE$/)
    #@flash(
    Zotero.logError(
{'fraction_non_alphanumeric': 0.13838362959900785, 'fraction_numerical': 0.012505167424555602, 'mean_word_length': 3.3946412352406905, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 4, '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': '6271317', 'n_tokens_mistral': 3527, 'n_tokens_neox': 3255, 'n_words': 745}