def METHOD_NAME(self, pagename, addctx, templatename, event_arg=None):
    # This is mostly copied from StandaloneHTMLBuilder. However, instead
    # of rendering the template and saving the html, create a context
    # dict and pickle it.
    ctx = self.globalcontext.copy()
    ctx['pagename'] = pagename
    def pathto(otheruri, resource=False,
               baseuri=self.get_target_uri(pagename)):
        if resource and '://' in otheruri:
            return otheruri
        elif not resource:
            otheruri = self.get_target_uri(otheruri)
            return relative_uri(baseuri, otheruri) or '#'
        else:
            return '/' + posixpath.join(self.virtual_staticdir, otheruri)
    ctx['pathto'] = pathto
    ctx['hasdoc'] = lambda name: name in self.env.all_docs
    ctx['encoding'] = self.config.html_output_encoding
    ctx['toctree'] = lambda **kw: self._get_local_toctree(pagename, **kw)
    self.add_sidebars(pagename, ctx)
    ctx.update(addctx)
    newtmpl = self.app.emit_firstresult('html-page-context', pagename,
                                        templatename, ctx, event_arg)
    if newtmpl:
        templatename = newtmpl
    # create a dict that will be pickled and used by webapps
    doc_ctx = {
        'body': ctx.get('body', ''),
        'title': ctx.get('title', ''),
        'css': ctx.get('css', ''),
        'script': ctx.get('script', ''),
    }
    # partially render the html template to get at interesting macros
    template = self.templates.environment.get_template(templatename)
    template_module = template.make_module(ctx)
    for item in ['sidebar', 'relbar', 'script', 'css']:
        if hasattr(template_module, item):
            doc_ctx[item] = getattr(template_module, item)()
    return ctx, doc_ctx