collectBinderItem
  :: Map.Map T.Text T.Text -> XML.Node -> [(Int, T.Text)] -> IO Section
collectBinderItem labels binderItem path = do
  children <-
    mapM (\(index, child) -> do
              collectBinderItem labels child
                                (path ++ [(index, binderItemTitle child)]))
         (zip [1 ..] ((fromNode binderItem)
                      $/ element "Children"
                      &/ element "BinderItem"
                      &| node))
  let labelID = T.concat ((fromNode binderItem)
                          $/ element "MetaData"
                          &/ element "LabelID"
                          &/ content)
      maybeLabel = Map.lookup labelID labels
      sectionType' =
        case maybeLabel of
          Just "Overview" -> InParent False
          Just "Synopsis" -> InParent True
          Just "Reference" -> Numbered
          Just "Tutorial" -> Numbered
          Just "Parameters" -> InParent True
          Just "Semantics" -> InParent True
          Just "Xref" -> InParent True
          Just "Manpage" -> Anonymous
          Just "Appendix" -> Lettered
          _ -> Numbered
      documentID = T.concat ((fromNode binderItem)
                             $| attribute "ID")
  return $!! Section {
                 sectionPath = path,
                 sectionType = sectionType',
                 sectionDocumentID = documentID,
                 sectionChildren = children
               }
collectBinderItem
  :: Map.Map T.Text T.Text -> XML.Node -> [(Int, T.Text)] -> IO Section
collectBinderItem labels binderItem path = do
  children <-
    mapM (\(index, child) -> do
              collectBinderItem labels child
                                (path ++ [(index, binderItemTitle child)]))
         (zip [1 ..] ((fromNode binderItem)
                      $/ element "Children"
                      &/ element "BinderItem"
                      &| node))
  let labelID = T.concat ((fromNode binderItem)
                          $/ element "MetaData"
                          &/ element "LabelID"
                          &/ content)
      maybeLabel = Map.lookup labelID labels
      sectionType' =
        case maybeLabel of
          Just "Overview" -> InParent False
          Just "Synopsis" -> InParent True
          Just "Reference" -> Numbered
          Just "Tutorial" -> Numbered
          Just "Parameters" -> InParent True
          Just "Semantics" -> InParent True
          Just "Xref" -> InParent True
          Just "Manpage" -> Anonymous
          Just "Appendix" -> Lettered
          _ -> Numbered
      documentID = T.concat ((fromNode binderItem)
                             $| attribute "ID")
  return $!! Section {
                 sectionPath = path,
                 sectionType = sectionType',
                 sectionDocumentID = documentID,
                 sectionChildren = children
               }
collectBinderItem labels binderItem path = do
  children <-
    mapM (\(index, child) -> do
              collectBinderItem labels child
                                (path ++ [(index, binderItemTitle child)]))
         (zip [1 ..] ((fromNode binderItem)
                      $/ element "Children"
                      &/ element "BinderItem"
                      &| node))
  let labelID = T.concat ((fromNode binderItem)
                          $/ element "MetaData"
                          &/ element "LabelID"
                          &/ content)
      maybeLabel = Map.lookup labelID labels
      sectionType' =
        case maybeLabel of
          Just "Overview" -> InParent False
          Just "Synopsis" -> InParent True
          Just "Reference" -> Numbered
          Just "Tutorial" -> Numbered
          Just "Parameters" -> InParent True
          Just "Semantics" -> InParent True
          Just "Xref" -> InParent True
          Just "Manpage" -> Anonymous
          Just "Appendix" -> Lettered
          _ -> Numbered
      documentID = T.concat ((fromNode binderItem)
                             $| attribute "ID")
  return $!! Section {
                 sectionPath = path,
                 sectionType = sectionType',
                 sectionDocumentID = documentID,
                 sectionChildren = children
               }