<commit_before>'use strict'

const path = require('path')
const eadRaw = require('fs').readFileSync(path.join(__dirname, '/aids-rp.xml'), 'utf-8')

const htmlparser2 = require('htmlparser2')

let tagStack = []
let fileLevelTagStack = []
let dataMapStack = []
let dataMap = new Map()
let collectionNumber = ''
let creator = ''
let creatorType = ''
let creatorSource = 'local'

const displayData = (data) => {
  if (data.get('DONOTDISPLAY')) { return }

  if (!collectionNumber) {
    throw new Error('no collection number found')
  }

  const output = []
  // File path: leave blank
  output.push('')

  // Title
  const unittitlePrefix = dataMapStack.reduce(
    (rv, value) => rv + `${value.get('unittitle')}: `,
    ''
  )

  output.push(`${unittitlePrefix}${data.get('unittitle')}`)

  // Alternative title: leave blank
  output.push('')

  // Identifier: leave blank
  output.push('')

  // Local identifier
  const container = data.get('container').replace(':', '_')
  if (!container) {
    throw new Error('no container found')
  }
  output.push(`${collectionNumber}_${container}`.toLowerCase())

  // Type: leave blank
  output.push('')

  // Campus/unit: leave blank
  output.push('')

  // Date
  output.push(data.get('unitdate'))

  // Date type: always "created"
  output.push('created')

  // Single: leave blank
  output.push('')

  // Inclusive Start: leave blank
  output.push('')

  // Inclusive End: leave blank
  output.push('')

  // Publication/origin: leave blank
  output.push('')

  // Creator 1 Name
  // Our finding aids only seem to have one creator.
  output.push(creator)

  // Creator 1 Name Type
  output.push(creatorType)

  // Creator 1 Role: leave blank
  output.push('')

  // Creator 1 Source
  output.push(creatorSource)

  // Creator 1 Authority ID: leave blank (for now at least)
  output.push('')

  // Leave Creator 2 * blank
  output.push('') // name
  output.push('') // name type
  output.push('') // role
  output.push('') // source
  output.push('') // authority id

  console.log(output.join(`\t`))
}

const handlers = {
  /*
    See possible events at:
      https://github.com/fb55/htmlparser2/wiki/Parser-options#events
    See fields we need at:
      https://docs.google.com/spreadsheets/d/1zmJVEZtdJ_6cwmdyWYR7dXMGYFKp_TGiyRV9zQgGbXE/edit
    See desired output format at:
      https://docs.google.com/spreadsheets/d/1zmJVEZtdJ_6cwmdyWYR7dXMGYFKp_TGiyRV9zQgGbXE/edit?usp=sharing
   */
  onopentag: function (name, attribs) {
    tagStack.push({name, attribs})
    if (attribs.level === 'file') {
      if (dataMap.size > 0) {
        // This map encloses another map, so do not display on line by itself.
        dataMap.set('DONOTDISPLAY', true)
        dataMapStack.push(dataMap)
        dataMap = new Map()
      }
      fileLevelTagStack.push(name)
    }
  },
  ontext: function (text) {
    const thisTag = tagStack[tagStack.length - 1]
    if (thisTag) {
      if (thisTag.name === 'unitid' && thisTag.attribs.label === 'Collection number') { collectionNumber = text.trim().replace(/\s+/g, '') }
    }
    if (tagStack.filter((tag) => { return tag.name === 'origination' && tag.attribs.label === 'Creator' }).length > 0) {<commit_msg>contributor<commit_after>'use strict'

const path = require('path')
const eadRaw = require('fs').readFileSync(path.join(__dirname, '/aids-rp.xml'), 'utf-8')

const htmlparser2 = require('htmlparser2')

let tagStack = []
let fileLevelTagStack = []
let dataMapStack = []
let dataMap = new Map()
let collectionNumber = ''
let creator = ''
let creatorType = ''
let creatorSource = 'local'

const displayData = (data) => {
  if (data.get('DONOTDISPLAY')) { return }

  if (!collectionNumber) {
    throw new Error('no collection number found')
  }

  const output = []
  // File path: leave blank
  output.push('')

  // Title
  const unittitlePrefix = dataMapStack.reduce(
    (rv, value) => rv + `${value.get('unittitle')}: `,
    ''
  )

  output.push(`${unittitlePrefix}${data.get('unittitle')}`)

  // Alternative title: leave blank
  output.push('')

  // Identifier: leave blank
  output.push('')

  // Local identifier
  const container = data.get('container').replace(':', '_')
  if (!container) {
    throw new Error('no container found')
  }
  output.push(`${collectionNumber}_${container}`.toLowerCase())

  // Type: leave blank
  output.push('')

  // Campus/unit: leave blank
  output.push('')

  // Date
  output.push(data.get('unitdate'))

  // Date type: always "created"
  output.push('created')

  // Single: leave blank
  output.push('')

  // Inclusive Start: leave blank
  output.push('')

  // Inclusive End: leave blank
  output.push('')

  // Publication/origin: leave blank
  output.push('')

  // Creator 1 Name
  // Our finding aids only seem to have one creator.
  output.push(creator)

  // Creator 1 Name Type
  output.push(creatorType)

  // Creator 1 Role: leave blank
  output.push('')

  // Creator 1 Source
  output.push(creatorSource)

  // Creator 1 Authority ID: leave blank (for now at least)
  output.push('')

  // Leave Creator 2 * blank
  output.push('') // name
  output.push('') // name type
  output.push('') // role
  output.push('') // source
  output.push('') // authority id

  // Leave Contributor * blank
  output.push('') // name
  output.push('') // name type
  output.push('') // role
  output.push('') // source
  output.push('') // authority id

  console.log(output.join(`\t`))
}

const handlers = {
  /*
    See possible events at:
      https://github.com/fb55/htmlparser2/wiki/Parser-options#events
    See fields we need at:
      https://docs.google.com/spreadsheets/d/1zmJVEZtdJ_6cwmdyWYR7dXMGYFKp_TGiyRV9zQgGbXE/edit
    See desired output format at:
      https://docs.google.com/spreadsheets/d/1zmJVEZtdJ_6cwmdyWYR7dXMGYFKp_TGiyRV9zQgGbXE/edit?usp=sharing
   */
  onopentag: function (name, attribs) {
    tagStack.push({name, attribs})
    if (attribs.level === 'file') {
      if (dataMap.size > 0) {
        // This map encloses another map, so do not display on line by itself.
        dataMap.set('DONOTDISPLAY', true)
        dataMapStack.push(dataMap)
        dataMap = new Map()
      }
      fileLevelTagStack.push(name)
    }
  },
  ontext: function (text) {
    const thisTag = tagStack[tagStack.length - 1]
    if (thisTag) {
      if (thisTag.name === 'unitid' && thisTag.attribs.label === 'Collection number') { collectionNumber = text.trim().replace(/\s+/g, '') }
    }
    if (tagStack.filter((tag) => { return tag.name === 'origination' && tag.attribs.label === 'Creator' }).length > 0) {
{'fraction_non_alphanumeric': 0.16095107453132146, 'fraction_numerical': 0.007011126352690139, 'mean_word_length': 3.1610653138871276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3118721', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2180, 'n_words': 515}