Im writing a sphinx extension and I have been struggling with this for about two weeks now.  I have a directive that im writing, but the thing that's different is the fields in it can't be processed until the entire document is cached in their doctrees.  Then a method fires thats linked to the doctree-resolved event and that in turn pulls a list of pages from the configuration and processes the nodes in each page.  there is a counter that needs to increment from one page to another.  so if page 1 ends with a count of 9, page 2 needs to start with count 10.

I have most of it working, but my issue is how in the world do i update a node in the doctree?  Here's the method im working on, yes i know the counter isn't complete and a few other issues, but im concentrating on updating a node's body at the moment.

def process_tripday_nodes(self, doctree, fromdocname):
    This method takes the processed doctrees and loops thru the trip nodes,
    updating them with the information they need to display and writing the html

    env = self.builder.env
    dStartDate = datetime.datetime.strptime(self.config.tripday_startdate, "%m/%d/%Y")

    dayCounter = 0

    # im checking this because I only want this processed once
    if not self.config.tripday_processed_nodes:
        # now I loop the list of trip legs in the file and process
        # them in order.  Order is important here
        for legname in self.config.tripday_legs:
            print("TripDay Processing Leg: " + legname)
            # Load the tree for the specified leg from the doctree cache
            tree = env.get_doctree(legname)
            # loop the tripday nodes in the doctree
            for node in tree.traverse(tripday):

                content = []
                endofday = dStartDate + datetime.timedelta(days=dayCounter)
                print("End Of Day: " + endofday.strftime("%m/%d/%Y"))

                text = '<div class="dayno">'
                text += '<div class="day-day">Day</div>'
                text += '<div class="day-no">%s</div>' % dayCounter
                text += '<div class="day-no">%s</div>' % endofday.strftime("%m/%d/%Y")
                text += '</div>'

                newnode = node.deepcopy()

                #newnode.rawsource = text
                newnode['paragraph'] = text

        self.config.tripday_processed_nodes = True

You want it fast, cheap, or right.  Pick two!!