code: def _protect_hyperlink_in_caption(lines):
      def _get_num_extra_left_braces(l, num_extra_left_braces):
          num = num_extra_left_braces
          for char in l:
              if char == "{":
                  num += 1
              elif char == "}":
                  num -= 1
                  if num == 0:
                      return 0
          return num
  
      i = 0
      while i < len(lines):
          if lines[i].startswith("\\caption{") or lines[i].startswith(
              "\\sphinxcaption{"
          ):
              num_extra_left_braces = _get_num_extra_left_braces(
                  lines[i], 0
              )
              if num_extra_left_braces == 0:
                  j = i
              else:
                  j = i + 1
                  while j < len(lines):
                      num_extra_left_braces = (
                          _get_num_extra_left_braces(
                              lines[j], num_extra_left_braces
                          )
                      )
                      if num_extra_left_braces == 0:
                          break
                      j += 1
              # lines[i] -- lines[j] are within \caption{} or \sphinxcaption{}
              for index in range(i, j + 1):
                  lines[index] = lines[index].replace(
                      "\\hyperlink", "\\protect\\hyperlink"
                  )
              i = j + 1
          else:
              i += 1
  
file_path: d2l-en/static/post_latex/main.py
imports: import os
  import re
  import regex
  import sys
label_desc: Use image node_id 8 for calling a global function with example usage: _protect_hyperlink_in_caption(lines) and returns: num, int