code: def _replace_chars_in_chapter_title_and_caption(lines):
      CAP_CHAP = {"\\chapter{", "\\section{", "\\caption{"}
  
      def _get_replaced(s):
          BEFORES = ["’", "“", "”", "–"]
          AFTERS = ["'", "``", "''", "--"]
          for before, after in zip(BEFORES, AFTERS):
              s = s.replace(before, after)
          return s
  
      i = 0
      while i < len(lines):
          if any(
              lines[i].startswith(cap_chap) for cap_chap in CAP_CHAP
          ):
              # Replace within, e.g., \chapter{XX{}X}, where XX{}X may span across multiple lines
              # num_lefts: num of { encountered
              num_lefts = 0
              found_end = False
              while not found_end:
                  j_start = 0
                  j_end = len(lines[i])
                  for j, char in enumerate(lines[i]):
                      if char == "{":
                          num_lefts += 1
                          if num_lefts == 1:
                              j_start = j + 1
                      elif char == "}":
                          num_lefts -= 1
                          if num_lefts == 0:
                              j_end = j
                              found_end = True
                              break
                  lines[i] = (
                      lines[i][:j_start]
                      + _get_replaced(lines[i][j_start:j_end])
                      + lines[i][j_end:]
                  )
                  if not found_end:
                      i += 1
          i += 1
  
      # \section{Encoder--Decoder} -> \section{Encoder\(-\)Decoder}
      for i, l in enumerate(lines):
          if l.startswith("\\chapter{") or l.startswith("\\section{"):
              lines[i] = lines[i].replace("--", "\(-\)")
  
file_path: d2l-en/static/post_latex/main.py
imports: import os
  import re
  import regex
  import sys
label_desc: Use image node_id 4 for calling a global function with example usage: _replace_chars_in_chapter_title_and_caption(lines) and returns: s