docs: def _handle_heading_end(self):
          """Handle the end of a section heading at the head of the string."""
          reset = self._head
          self._head += 1
          best = 1
          while self._read() == "=":
              best += 1
              self._head += 1
          current = int(log(self._context / contexts.HEADING_LEVEL_1, 2)) + 1
          level = min(current, min(best, 6))
  
          try:  # Try to check for a heading closure after this one
              after, after_level = self._parse(self._context)
          except BadRoute:
              if level < best:
                  self._emit_text("=" * (best - level))
              self._head = reset + best - 1
              return self._pop(), level
          else:  # Found another closure
              self._emit_text("=" * best)
              self._emit_all(after)
              return self._pop(), after_level