code: def html_page_context(app, pagename, templatename, context, doctree):
      
      rendered_toc = get_rendered_toctree(app.builder, pagename)
      context['toc'] = rendered_toc
      context['display_toc'] = True  # force toctree to display
  
      if "toctree" not in context:
          # json builder doesn't use toctree func, so nothing to replace
          return
  
      def make_toctree(collapse=True):
          return get_rendered_toctree(app.builder,
                                      pagename,
                                      prune=False,
                                      collapse=collapse,
                                      )
  
      context['toctree'] = make_toctree
docs: Event handler for the html-page-context signal.
      Modifies the context directly.
       - Replaces the 'toc' value created by the HTML builder with one
         that shows all document titles and the local table of contents.
       - Sets display_toc to True so the table of contents is always
         displayed, even on empty pages.
       - Replaces the 'toctree' function with one that uses the entire
         document structure, ignores the maxdepth argument, and uses
         only prune and collapse.