<commit_before>class app.Router
  $.extend @prototype, Events

  @routes: [
    ['*',              'before'  ]
    ['/',              'root'    ]
    ['/offline',       'offline' ]
    ['/about',         'about'   ]
    ['/news',          'news'    ]
    ['/help',          'help'    ]
    ['/:doc-:type/',   'type'    ]
    ['/:doc/',         'doc'     ]
    ['/:doc/:path(*)', 'entry'   ]
    ['*',              'notFound']
  ]

  constructor: ->
    for [path, method] in @constructor.routes
      page path, @[method].bind(@)
    @setInitialPath()

  start: ->
    page.start()
    return

  show: (path) ->
    page.show(path)
    return

  triggerRoute: (name) ->
    @trigger name, @context
    @trigger 'after', name, @context
    return

  before: (context, next) ->
    @context = context
    @trigger 'before', context
    next()
    return

  doc: (context, next) ->
    if doc = app.docs.findBySlug(context.params.doc) or app.disabledDocs.findBySlug(context.params.doc)
      context.doc = doc
      context.entry = doc.toEntry()
      @triggerRoute 'entry'
    else
      next()
    return

  type: (context, next) ->
    doc = app.docs.findBySlug(context.params.doc)

    if type = doc?.types.findBy 'slug', context.params.type
      context.doc = doc
      context.type = type
      @triggerRoute 'type'
    else
      next()
    return

  entry: (context, next) ->
    doc = app.docs.findBySlug(context.params.doc)

    if entry = doc?.findEntryByPathAndHash(context.params.path, context.hash)
      context.doc = doc
      context.entry = entry
      @triggerRoute 'entry'
    else
      next()
    return

  root: ->
    if app.isSingleDoc()
      setTimeout (-> window.location = '/'), 0
    else
      @triggerRoute 'root'
    return

  offline: ->
    @triggerRoute 'offline'
    return

  about: (context) ->
    context.page = 'about'
    @triggerRoute 'page'
    return

  news: (context) ->
    context.page = 'news'
    @triggerRoute 'page'
    return

  help: (context) ->
    context.page = 'help'
    @triggerRoute 'page'
    return

  notFound: (context) ->
    @triggerRoute 'notFound'
    return

  isRoot: ->
    location.pathname is '/'

  isDocIndex: ->
    @context.doc and @context.entry is @context.doc.toEntry()

  setInitialPath: ->
    # Remove superfluous forward slashes at the beginning of the path
    if (path = location.pathname.replace /^\/{2,}/g, '/') isnt location.pathname
      page.replace path + location.search + location.hash, null, true

    if @isRoot()
      if path = @getInitialPathFromHash()
        page.replace path + location.search, null, true
      else if path = @getInitialPathFromCookie()
        page.replace path + location.search + location.hash, null, true
    return

  getInitialPathFromHash: ->
    try
      (new RegExp "#/(.+)").exec(decodeURIComponent location.hash)?[1]
    catch

  getInitialPathFromCookie: ->
    if path = Cookies.get('initial_path')
      Cookies.expire('initial_path')
      path<commit_msg>Fix error in app.Router#isDocIndex<commit_after>class app.Router
  $.extend @prototype, Events

  @routes: [
    ['*',              'before'  ]
    ['/',              'root'    ]
    ['/offline',       'offline' ]
    ['/about',         'about'   ]
    ['/news',          'news'    ]
    ['/help',          'help'    ]
    ['/:doc-:type/',   'type'    ]
    ['/:doc/',         'doc'     ]
    ['/:doc/:path(*)', 'entry'   ]
    ['*',              'notFound']
  ]

  constructor: ->
    for [path, method] in @constructor.routes
      page path, @[method].bind(@)
    @setInitialPath()

  start: ->
    page.start()
    return

  show: (path) ->
    page.show(path)
    return

  triggerRoute: (name) ->
    @trigger name, @context
    @trigger 'after', name, @context
    return

  before: (context, next) ->
    @context = context
    @trigger 'before', context
    next()
    return

  doc: (context, next) ->
    if doc = app.docs.findBySlug(context.params.doc) or app.disabledDocs.findBySlug(context.params.doc)
      context.doc = doc
      context.entry = doc.toEntry()
      @triggerRoute 'entry'
    else
      next()
    return

  type: (context, next) ->
    doc = app.docs.findBySlug(context.params.doc)

    if type = doc?.types.findBy 'slug', context.params.type
      context.doc = doc
      context.type = type
      @triggerRoute 'type'
    else
      next()
    return

  entry: (context, next) ->
    doc = app.docs.findBySlug(context.params.doc)

    if entry = doc?.findEntryByPathAndHash(context.params.path, context.hash)
      context.doc = doc
      context.entry = entry
      @triggerRoute 'entry'
    else
      next()
    return

  root: ->
    if app.isSingleDoc()
      setTimeout (-> window.location = '/'), 0
    else
      @triggerRoute 'root'
    return

  offline: ->
    @triggerRoute 'offline'
    return

  about: (context) ->
    context.page = 'about'
    @triggerRoute 'page'
    return

  news: (context) ->
    context.page = 'news'
    @triggerRoute 'page'
    return

  help: (context) ->
    context.page = 'help'
    @triggerRoute 'page'
    return

  notFound: (context) ->
    @triggerRoute 'notFound'
    return

  isRoot: ->
    location.pathname is '/'

  isDocIndex: ->
    @context and @context.doc and @context.entry is @context.doc.toEntry()

  setInitialPath: ->
    # Remove superfluous forward slashes at the beginning of the path
    if (path = location.pathname.replace /^\/{2,}/g, '/') isnt location.pathname
      page.replace path + location.search + location.hash, null, true

    if @isRoot()
      if path = @getInitialPathFromHash()
        page.replace path + location.search, null, true
      else if path = @getInitialPathFromCookie()
        page.replace path + location.search + location.hash, null, true
    return

  getInitialPathFromHash: ->
    try
      (new RegExp "#/(.+)").exec(decodeURIComponent location.hash)?[1]
    catch

  getInitialPathFromCookie: ->
    if path = Cookies.get('initial_path')
      Cookies.expire('initial_path')
      path
{'fraction_non_alphanumeric': 0.14392430278884463, 'fraction_numerical': 0.00099601593625498, 'mean_word_length': 2.2202030999465525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '5508364', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1934, 'n_words': 444}