def METHOD_NAME(thefile, keyword, modules, rel_mod_dir):
   pattern = "\.\. begin_%s.*\.\. end_%s" % (keyword, keyword)
   replacement= gen_toc(mods, keyword, rel_mod_dir)
   index_contents = ""
   with open(thefile, mode='r', encoding='utf-8') as index:
      index_contents = index.read()
   new_contents = re.sub(pattern, replacement, index_contents, flags=re.S)
   with open(thefile, mode='w', encoding='utf-8') as newindex:
      newindex.write(new_contents)