On Apr 25, 5:45 am, Wei-Wei Guo <[email protected]> wrote: > Tuomas Räsänen 写道: > > Hi Tuomas, > > > > > Well, if I understood you correctly, I needed something similiar and > > ended up with this small extension: > > > toctree_items = [] > > > def html_page_context(app, pagename, templatename, context, doctree): > > context['toctree_items'] = toctree_items > > > def env_updated(app, env): > > master_doc = app.builder.config.master_doc > > toctree = env.tocs[master_doc].traverse(lambda n: n.tagname == > > 'toctree')[0] > > global toctree_items > > for link_label, pagename in toctree['entries']: > > if pagename == 'self': > > pagename = master_doc > > pagetitle = env.toctree_items[pagename].astext() > > toctree_items.insert(0, (pagename, pagetitle)) > > > def setup(app): > > app.connect('html-page-context', html_page_context) > > app.connect('env-updated', env_updated) > > > I didn't want the toctree to be included as <ul> in the html-output but > > I still needed that same info to generate navigation bars. > > > So I defined my toctrees to be hidden: > > > .. toctree:: > > :hidden: > > > And then dug the toctee information on 'env-updated' event (after all > > nodes has been resolved) and passed that to template as a part of it's > > context. I hope this helps a bit. That snippet isn't probably the best > > way to do it, but I'm still quite newbie with Sphinx. :) > > Thanks, but I cannot understand your code. Why is there no thing like > 'visit_toctree'? > I looked into sources of latex.py, html.py, and text.py in both builders/ and > writers/. > There is also rarely anything like visit_toctree. Does it mean toctree cannot > be > handled as a node? I also tried to manipulate toctree with visit_ like other > nodes, > but it is ignored. Here is my code: > > def visit_toctree(self, node): > node['maxdepth'] = 1 > self.body.append('anywords') > > I'm a newbie with Sphinx too. It's hard for me to get Sphinx's logic. >
I think there is many ways to achieve the desired result and my way was to post-process the doctree and pass the toctree to template engine (and this works only with html-builder). I guess one could make an instance of NodeVisitor which has visit_toctree and use that to modify the doctree. Or even add that visit_toctree method to the already existing visitor (builder.visitor)? But if you really need to modify the tree, perhaps the easiest way would be to hook on 'doctree-read' event. Moreover, core events are quite handy for different things also. And what comes to that my snippet, the real 'logic' is implemented in env_updated -callback. It gets the toctree-node from the environment and stores all of its entries. Then html_page_context just passes those entries to the template engine. See: http://sphinx.pocoo.org/ext/appapi.html#sphinx-core-events -- Tuomas --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sphinx-dev" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sphinx-dev?hl=en -~----------~----~----~----~------~----~------~--~---
