'Collect items for the template context of a page.'

def get_doc_context(self, docname, body, metatags):

    prev = next = None
    parents = []
    rellinks = self.globalcontext['rellinks'][:]
    related = self.relations.get(docname)
    titles = self.env.titles
    if (related and related[2]):
        try:
            next = {'link': self.get_relative_uri(docname, related[2]), 'title': self.render_partial(titles[related[2]])['title']}
            rellinks.append((related[2], next['title'], 'N', _('next')))
        except KeyError:
            next = None
    if (related and related[1]):
        try:
            prev = {'link': self.get_relative_uri(docname, related[1]), 'title': self.render_partial(titles[related[1]])['title']}
            rellinks.append((related[1], prev['title'], 'P', _('previous')))
        except KeyError:
            prev = None
    while (related and related[0]):
        try:
            parents.append({'link': self.get_relative_uri(docname, related[0]), 'title': self.render_partial(titles[related[0]])['title']})
        except KeyError:
            pass
        related = self.relations.get(related[0])
    if parents:
        parents.pop()
    parents.reverse()
    title = self.env.longtitles.get(docname)
    title = ((title and self.render_partial(title)['title']) or '')
    source_suffix = path.splitext(self.env.doc2path(docname))[1]
    if self.config.html_copy_source:
        sourcename = (docname + source_suffix)
        if (source_suffix != self.config.html_sourcelink_suffix):
            sourcename += self.config.html_sourcelink_suffix
    else:
        sourcename = ''
    meta = self.env.metadata.get(docname)
    self_toc = TocTree(self.env).get_toc_for(docname, self)
    toc = self.render_partial(self_toc)['fragment']
    return dict(parents=parents, prev=prev, next=next, title=title, meta=meta, body=body, metatags=metatags, rellinks=rellinks, sourcename=sourcename, toc=toc, display_toc=(self.env.toc_num_entries[docname] > 1), page_source_suffix=source_suffix)
