@coroutine<EOL>def patch_anchors(parser, show_progressbar):
files = defaultdict(list)<EOL>try:<EOL><INDENT>while True:<EOL><INDENT>pentry = (yield)<EOL>try:<EOL><INDENT>fname, anchor = pentry.path.split("<STR_LIT:#>")<EOL>files[fname].append(<EOL>TOCEntry(name=pentry.name, type=pentry.type, anchor=anchor)<EOL>)<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>except GeneratorExit:<EOL><INDENT>pass<EOL><DEDENT>def patch_files(files):<EOL><INDENT>for fname, entries in files:<EOL><INDENT>full_path = os.path.join(parser.doc_path, fname)<EOL>with codecs.open(full_path, mode="<STR_LIT:r>", encoding="<STR_LIT:utf-8>") as fp:<EOL><INDENT>soup = BeautifulSoup(fp, "<STR_LIT>")<EOL>for entry in entries:<EOL><INDENT>if not parser.find_and_patch_entry(soup, entry):<EOL><INDENT>log.debug(<EOL>"<STR_LIT>".format(<EOL>entry.anchor, click.format_filename(fname)<EOL>)<EOL>)<EOL><DEDENT><DEDENT><DEDENT>with open(full_path, mode="<STR_LIT:wb>") as fp:<EOL><INDENT>fp.write(soup.encode("<STR_LIT:utf-8>"))<EOL><DEDENT><DEDENT><DEDENT>if show_progressbar is True:<EOL><INDENT>with click.progressbar(<EOL>files.items(),<EOL>width=<NUM_LIT:0>,<EOL>length=len(files),<EOL>label="<STR_LIT>",<EOL>) as pbar:<EOL><INDENT>patch_files(pbar)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>patch_files(files.items())<EOL><DEDENT>
Consume ``ParseEntry``s then patch docs for TOCs by calling
*parser*'s ``find_and_patch_entry``.