<commit_before>Zotero.BetterBibTeX.CAYW = {}

class Zotero.BetterBibTeX.CAYW.Document
  constructor: ->
    @fields = []

  cleanup: ->
  activate: ->
    wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator)
    #ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1'].getService(Components.interfaces.nsIWindowWatcher)
    windows = wm.getEnumerator(null)
    while windows.hasMoreElements()
      win = windows.getNext().QueryInterface( Components.interfaces.nsIDOMChromeWindow )
      #win = ww.getChromeForWindow(win)
      win.minimize()

  displayAlert: -> 0
  cursorInField: -> null
  convert: ->
  setBibliographyStyle: ->

  canInsertField: (fieldType) ->
    Zotero.BetterBibTeX.debug('CAYW.Document.canInsertField:', fieldType)
    return true

  getDocumentData: -> ''

  setDocumentData: (data) ->
    Zotero.BetterBibTeX.debug('CAYW.Document.setDocumenData:', data)

  insertField: (fieldType, noteType) ->
    Zotero.BetterBibTeX.debug('CAYW.Document.insertField:', fieldType, noteType)
    field = Zotero.BetterBibTeX.CAYW.Field(fieldType, noteType)
    @fields.push(field)
    return field

  getFields: (fieldType) ->
    return (field for field in @fields when field.fieldType == fieldType)

  getFieldsAsync: (fieldType, observer) ->
    throw new Error('CAYW.Document.getFieldsAsync')


class Zotero.BetterBibTeX.CAYW.Field
  constructor: (@fieldType, @noteType) ->

  setCode: (@code) ->

class Zotero.BetterBibTeX.CAYW.CitationEditInterface
  constructor: (@deferred, config, @doc) ->
    @citation = {citationItems:[], properties:{}}
    @wrappedJSObject = @

    @config = {
      citeprefix: ''
      citepostfix: ''
      keyprefix: ''
      keypostfix: ''
      separator: ','
      clipboard: false
      format: ''
    }

    for own key of @config
      @config[key] = config[key] if config[key]

  getItems: -> Q.fcall(-> [])

  accept: (progressCallback) ->
    progressCallback.call(null, 100) if progressCallback
    citation = []

    # {"citationItems":[{"id":"5","locator":"page","prefix":"prefix","suffix":"suffix","suppress-author":true}],"properties":{}}
    # "label":"line","locator":"xx"
    citations = []
    for citation in @citation.citationItems
      citation.label = 'page' if !citation.label && citation.locator
      citekey = Zotero.BetterBibTeX.keymanager.get({itemID: citation.id}, 'on-export')
      continue unless citekey
      citation.citekey = citekey.citekey
      citations.push(citation)

    if Zotero.BetterBibTeX.CAYW.Formatter[@config.format]
      formatted = Zotero.BetterBibTeX.CAYW.Formatter[@config.format].call(null, citations)
    else
      formatted = []
      for citation in citations
        fromatted.push(@config.keyprefix + citation.citekey + @config.keypostfix)
      if formatted.length == 0
        formatted = ''
      else
        formatted = @config.citeprefix + formatted.join(@config.separator) + @config.citepostfix

    Zotero.Utilities.Internal.copyTextToClipboard(formatted) if @config.clipboard
    @deferred.fulfill(formatted)
    Zotero.Integration.currentWindow.close()

    @doc.activate()

Zotero.BetterBibTeX.CAYW.Formatter = {
  mmd: (citations) ->
    formatted = []
    for citation in citations
      if citation.prefix
        formatted.push("[#{citation.prefix}][##{citation.citekey}]")
      else
        formatted.push("[##{citation.citekey}][]")
    return formatted.join('')

  pandoc: (citations) ->
    formatted = []
    for citation in citations
      cite = ''
      cite += "#{citation.prefix} " if citation.prefix
      cite += '-' if citation['suppress-author']
      cite += "@#{citation.citekey}"
      cite += ", #{citation.label} #{citation.locator}" if citation.locator
      cite += " #{citation.suffix}" if citation.suffix
      formatted.push(cite)
    return '' if citation.length == 0
    return '[' + citation.join(';') + ']'

  'scannable-cite': (citations) ->
    scannableCiteLocator: {
      article: "art."
      chapter: "ch."
      subchapter: "subch."
      column: "col."
      figure: "fig."
      line: "l."
      note: "n."
      issue: "no."
      opus: "op."
      page: "p."
      paragraph: "para."
      subparagraph: "subpara."
      part: "pt."
      rule: "r."
      section: "sec."
      subsection: "subsec."
      Section: "Sec."
      'sub verbo': "sv."
      schedule: "sch."
      title: "tit."
      verse: "vrs."
      volume: "vol."
    }

    formatted = []
    for citation in citations
      item = Zotero.Items.get(citation.id)
      isLegal = Zotero.ItemTypes.getName(item.itemTypeID) in [ 'bill', 'case', 'gazette', 'hearing', 'patent', 'regulation', 'statute', 'treaty' ]

      id = switch
        when item.libraryID then "zg:#{item.libraryID}:#{item.key}"
        when Zotero.userID then "zu:#{Zotero.userID}:#{item.key}"
        else "zu:0:#{item.key}"
      locator = if citation.locator then "#{scannableCiteLocator[citation.label]} #{citation.locator}" else ''
      citation.prefix ?= ''
      citation.suffix ?= ''

      label = item.firstCreator
      label ||= item.getField('shortTitle')
      label ||= item.getField('title')

      date = Zotero.Date.strToDate(item.getField('date')).year
      date ||= item.getField('date')
      date ||= 'no date'

      label = "#{label} #{date}".trim()

      label = "-#{label}" if citation['suppress-author']
      formatted.push("{#{citation.prefix}|#{label}|#{locator}|#{citation.suffix}|#{id}}")
    return formatted.join('')
}<commit_msg>port of scannable cite translator<commit_after>Zotero.BetterBibTeX.CAYW = {}

class Zotero.BetterBibTeX.CAYW.Document
  constructor: ->
    @fields = []

  cleanup: ->
  activate: ->
    wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator)
    #ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1'].getService(Components.interfaces.nsIWindowWatcher)
    windows = wm.getEnumerator(null)
    while windows.hasMoreElements()
      win = windows.getNext().QueryInterface( Components.interfaces.nsIDOMChromeWindow )
      #win = ww.getChromeForWindow(win)
      win.minimize()

  displayAlert: -> 0
  cursorInField: -> null
  convert: ->
  setBibliographyStyle: ->

  canInsertField: (fieldType) ->
    Zotero.BetterBibTeX.debug('CAYW.Document.canInsertField:', fieldType)
    return true

  getDocumentData: -> ''

  setDocumentData: (data) ->
    Zotero.BetterBibTeX.debug('CAYW.Document.setDocumenData:', data)

  insertField: (fieldType, noteType) ->
    Zotero.BetterBibTeX.debug('CAYW.Document.insertField:', fieldType, noteType)
    field = Zotero.BetterBibTeX.CAYW.Field(fieldType, noteType)
    @fields.push(field)
    return field

  getFields: (fieldType) ->
    return (field for field in @fields when field.fieldType == fieldType)

  getFieldsAsync: (fieldType, observer) ->
    throw new Error('CAYW.Document.getFieldsAsync')


class Zotero.BetterBibTeX.CAYW.Field
  constructor: (@fieldType, @noteType) ->

  setCode: (@code) ->

class Zotero.BetterBibTeX.CAYW.CitationEditInterface
  constructor: (@deferred, config, @doc) ->
    @citation = {citationItems:[], properties:{}}
    @wrappedJSObject = @

    @config = {
      citeprefix: ''
      citepostfix: ''
      keyprefix: ''
      keypostfix: ''
      separator: ','
      clipboard: false
      format: ''
    }

    for own key of @config
      @config[key] = config[key] if config[key]

  getItems: -> Q.fcall(-> [])

  accept: (progressCallback) ->
    progressCallback.call(null, 100) if progressCallback
    citation = []

    # {"citationItems":[{"id":"5","locator":"page","prefix":"prefix","suffix":"suffix","suppress-author":true}],"properties":{}}
    # "label":"line","locator":"xx"
    citations = []
    for citation in @citation.citationItems
      citation.label = 'page' if !citation.label && citation.locator
      citekey = Zotero.BetterBibTeX.keymanager.get({itemID: citation.id}, 'on-export')
      continue unless citekey
      citation.citekey = citekey.citekey
      citations.push(citation)

    if Zotero.BetterBibTeX.CAYW.Formatter[@config.format]
      formatted = Zotero.BetterBibTeX.CAYW.Formatter[@config.format].call(null, citations)
    else
      formatted = []
      for citation in citations
        formatted.push(@config.keyprefix + citation.citekey + @config.keypostfix)
      if formatted.length == 0
        formatted = ''
      else
        formatted = @config.citeprefix + formatted.join(@config.separator) + @config.citepostfix

    Zotero.Utilities.Internal.copyTextToClipboard(formatted) if @config.clipboard
    @deferred.fulfill(formatted)
    Zotero.Integration.currentWindow.close()

    @doc.activate()

Zotero.BetterBibTeX.CAYW.Formatter = {
  mmd: (citations) ->
    formatted = []
    for citation in citations
      if citation.prefix
        formatted.push("[#{citation.prefix}][##{citation.citekey}]")
      else
        formatted.push("[##{citation.citekey}][]")
    return formatted.join('')

  pandoc: (citations) ->
    formatted = []
    for citation in citations
      cite = ''
      cite += "#{citation.prefix} " if citation.prefix
      cite += '-' if citation['suppress-author']
      cite += "@#{citation.citekey}"
      cite += ", #{citation.label} #{citation.locator}" if citation.locator
      cite += " #{citation.suffix}" if citation.suffix
      formatted.push(cite)
    return '' if citation.length == 0
    return '[' + citation.join(';') + ']'

  'scannable-cite': (citations) ->
    scannableCiteLocator: {
      article: "art."
      chapter: "ch."
      subchapter: "subch."
      column: "col."
      figure: "fig."
      line: "l."
      note: "n."
      issue: "no."
      opus: "op."
      page: "p."
      paragraph: "para."
      subparagraph: "subpara."
      part: "pt."
      rule: "r."
      section: "sec."
      subsection: "subsec."
      Section: "Sec."
      'sub verbo': "sv."
      schedule: "sch."
      title: "tit."
      verse: "vrs."
      volume: "vol."
    }

    class Mem
      constructor: (@isLegal) ->
        @lst = []

      set: (str, punc, slug) ->
        punc = '' unless punc
        switch
          when str        then @lst.push(str + punc)
          when !@isLegal  then @lst.push(slug)

      setlaw: (str, punc) ->
        punc = '' unless punc
        @lst.push(str + punc) if str && @isLegal

      get: -> @lst.join(' ')

    formatted = []
    for citation in citations
      item = Zotero.Items.get(citation.id)
      isLegal = Zotero.ItemTypes.getName(item.itemTypeID) in [ 'bill', 'case', 'gazette', 'hearing', 'patent', 'regulation', 'statute', 'treaty' ]

      id = switch
        when item.libraryID then "zg:#{item.libraryID}:#{item.key}"
        when Zotero.userID then "zu:#{Zotero.userID}:#{item.key}"
        else "zu:0:#{item.key}"
      locator = if citation.locator then "#{scannableCiteLocator[citation.label]} #{citation.locator}" else ''
      citation.prefix ?= ''
      citation.suffix ?= ''

      title = new Mem(isLegal)
      title.set(item.firstCreator, ',', 'anon.')

      includeTitle = false
      try # Prefs.get throws an error if the pref is not found
        includeTitle = Zotero.Prefs.get('translators.ODFScan.includeTitle')
      if includeTitle || !item.firstCreator
        title.set(item.getField('shortTitle') || item.getField('title'), ',', '(no title)')

      try
        title.setlaw(item.getField('authority'), ',')
      try
        title.setlaw(item.getField('volume'))
      try
        title.setlaw(item.getField('reporter'))
      title.setlaw(item.getField('pages'))

      year = new Mem(isLegal)
      try
        year.setlaw(item.getField('court'), ',')
      date = Zotero.Date.strToDate(item.getField('date'))
      year.set((if date.year then date.year else item.getField('date')), '', 'no date')

      label = (title.get() + ' ' + year.get()).trim()
      label = "-#{label}" if citation['suppress-author']

      formatted.push("{#{citation.prefix}|#{label}|#{locator}|#{citation.suffix}|#{id}}")
    return formatted.join('')
}
{'fraction_non_alphanumeric': 0.15739899198545815, 'fraction_numerical': 0.0016524828554903743, 'mean_word_length': 3.023936170212766, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 51, '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': '3099291', 'n_tokens_mistral': 4178, 'n_tokens_neox': 3872, 'n_words': 715}