code: private Block generateTree(List<HeaderBlock> headers, int start, int depth, boolean numbered,
          String documentReference)
      {
          Block tocBlock = null;
  
          int currentLevel = start - 1;
          Block currentBlock = null;
          for (HeaderBlock headerBlock : headers) {
              int headerLevel = headerBlock.getLevel().getAsInt();
  
              if (headerLevel >= start && headerLevel <= depth) {
                  // Move to next header in toc tree
  
                  if (currentLevel < headerLevel) {
                      while (currentLevel < headerLevel) {
                          if (currentBlock instanceof ListBLock) {
                              currentBlock = addItemBlock(currentBlock, null, documentReference);
                          }
  
                          currentBlock = createChildListBlock(numbered, currentBlock);
                          ++currentLevel;
                      }
                  } else {
                      while (currentLevel > headerLevel) {
                          currentBlock = currentBlock.getParent().getParent();
                          --currentLevel;
                      }
                      currentBlock = currentBlock.getParent();
                  }
  
                  currentBlock = addItemBlock(currentBlock, headerBlock, documentReference);
              }
          }
  
          if (currentBlock != null) {
              tocBlock = currentBlock.getRoot();
          }
  
          return tocBlock;
      }
docs: Convert headers into list block tree.
  
  @param headers the headers to convert.
  @param start the "start" parameter value.
  @param depth the "depth" parameter value.
  @param numbered the "numbered" parameter value.
  @return the root block of generated block tree or null if no header was matching the specified parameters