<commit_before>define (require) ->
  Backbone = require('backbone')
  toc = require('cs!collections/toc')
  Collection = require('cs!models/content/collection')
  Page = require('cs!models/content/page')
  require('backbone-associations')

  MEDIA_TYPES =
    'application/vnd.org.cnx.collection' : 'book'
    'application/vnd.org.cnx.module': 'page'

  return class Content extends Collection
    relations: [{
      type: Backbone.Many
      key: 'contents'
      relatedModel: (relation, attributes) ->
        return (attrs, options) ->
          if _.isArray(attrs.contents)
            return new Collection(attrs)

          return new Page(attrs)
    }, {
      type: Backbone.Many
      key: 'authors'
      collectionType: Backbone.Collection
    }, {
      type: Backbone.One
      key: 'currentPage'
      relatedModel: Page
    }, {
      type: Backbone.Many
      key: 'toc'
      collectionType: Backbone.Collection
    }]

    initialize: (options = {}) ->
      toc.reset()
      @set('toc', toc)

      @fetch
        success: () => @load(options.page)

    parse: (response) ->
      type = response.type = MEDIA_TYPES[response.mediaType]

      # Keep the id with the desired version number included
      delete response.id

      # Only setup a toc for a book
      if type isnt 'book' then return response

      response.contents = response.tree.contents

      depth = 0
      page = 1

      # Traverse a book's tree and set book, depth, unit, parent, subcollection, and page
      # information on each node of the tree prior to the tree being processed
      # by backbone-associations.
      traverse = (o = {}) =>
        for item, index in o.contents
          item.book = @
          item.depth = depth

          if depth
            item.parent = o
            item.unit = "#{o.unit}-#{index+1}"
          else
            item.unit = "#{index+1}"

          # Determine if the item is a subcollection or a page
          if item.contents
            item.subcollection = true
            delete item.id # Get rid of the 'subcol' id so the subcollection is unique
            depth++
            traverse(item)
          else
            item.page = page++

        depth--

      traverse(response.tree)

      # Total number of pages in the book
      response.pages = page - 1

      return response

    load: (page) ->
      if @get('type') is 'book'
        @setPage(page or 1) # Default to page 1
      else
        @set('currentPage', new Page({id: @id}))
        @get('currentPage').fetch()
        @trigger('changePage')<commit_msg>Always trigger changePage when new content is fetched<commit_after>define (require) ->
  Backbone = require('backbone')
  toc = require('cs!collections/toc')
  Collection = require('cs!models/content/collection')
  Page = require('cs!models/content/page')
  require('backbone-associations')

  MEDIA_TYPES =
    'application/vnd.org.cnx.collection' : 'book'
    'application/vnd.org.cnx.module': 'page'

  return class Content extends Collection
    relations: [{
      type: Backbone.Many
      key: 'contents'
      relatedModel: (relation, attributes) ->
        return (attrs, options) ->
          if _.isArray(attrs.contents)
            return new Collection(attrs)

          return new Page(attrs)
    }, {
      type: Backbone.Many
      key: 'authors'
      collectionType: Backbone.Collection
    }, {
      type: Backbone.One
      key: 'currentPage'
      relatedModel: Page
    }, {
      type: Backbone.Many
      key: 'toc'
      collectionType: Backbone.Collection
    }]

    initialize: (options = {}) ->
      toc.reset()
      @set('toc', toc)

      @fetch
        success: () => @load(options.page)

    parse: (response) ->
      type = response.type = MEDIA_TYPES[response.mediaType]

      # Keep the id with the desired version number included
      delete response.id

      # Only setup a toc for a book
      if type isnt 'book' then return response

      response.contents = response.tree.contents

      depth = 0
      page = 1

      # Traverse a book's tree and set book, depth, unit, parent, subcollection, and page
      # information on each node of the tree prior to the tree being processed
      # by backbone-associations.
      traverse = (o = {}) =>
        for item, index in o.contents
          item.book = @
          item.depth = depth

          if depth
            item.parent = o
            item.unit = "#{o.unit}-#{index+1}"
          else
            item.unit = "#{index+1}"

          # Determine if the item is a subcollection or a page
          if item.contents
            item.subcollection = true
            delete item.id # Get rid of the 'subcol' id so the subcollection is unique
            depth++
            traverse(item)
          else
            item.page = page++

        depth--

      traverse(response.tree)

      # Total number of pages in the book
      response.pages = page - 1

      return response

    load: (page) ->
      if @get('type') is 'book'
        @setPage(page or 1) # Default to page 1
      else
        @set('currentPage', new Page({id: @id}))
        @get('currentPage').fetch
          success: () =>
            @trigger('changePage')
{'fraction_non_alphanumeric': 0.10825828702816631, 'fraction_numerical': 0.002682506227246599, 'mean_word_length': 2.0121177149451817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '5608863', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1450, 'n_words': 437}