Beispiel #1
0
 def build_toc(node, depth=1):
     # type: (nodes.Node, int) -> List[nodes.Node]
     entries = []
     for sectionnode in node:
         # find all toctree nodes in this section and add them
         # to the toc (just copying the toctree node which is then
         # resolved in self.get_and_resolve_doctree)
         if isinstance(sectionnode, addnodes.only):
             onlynode = addnodes.only(expr=sectionnode['expr'])
             blist = build_toc(sectionnode, depth)
             if blist:
                 onlynode += blist.children  # type: ignore
                 entries.append(onlynode)
             continue
         if not isinstance(sectionnode, nodes.section):
             for toctreenode in traverse_in_section(
                     sectionnode, addnodes.toctree):
                 item = toctreenode.copy()
                 entries.append(item)
                 # important: do the inventory stuff
                 TocTree(app.env).note(docname, toctreenode)
             continue
         title = sectionnode[0]
         # copy the contents of the section title, but without references
         # and unnecessary stuff
         visitor = SphinxContentsFilter(doctree)
         title.walkabout(visitor)
         nodetext = visitor.get_entry_text()
         if not numentries[0]:
             # for the very first toc entry, don't add an anchor
             # as it is the file's title anyway
             anchorname = ''
         else:
             anchorname = '#' + sectionnode['ids'][0]
         numentries[0] += 1
         # make these nodes:
         # list_item -> compact_paragraph -> reference
         reference = nodes.reference('',
                                     '',
                                     internal=True,
                                     refuri=docname,
                                     anchorname=anchorname,
                                     *nodetext)
         para = addnodes.compact_paragraph('', '', reference)
         item = nodes.list_item('', para)
         sub_item = build_toc(sectionnode, depth + 1)
         item += sub_item
         entries.append(item)
     if entries:
         return nodes.bullet_list('', *entries)
     return []
Beispiel #2
0
 def build_toc(node, depth=1):
     # type: (nodes.Element, int) -> nodes.bullet_list
     entries = []  # type: List[nodes.Element]
     for sectionnode in node:
         # find all toctree nodes in this section and add them
         # to the toc (just copying the toctree node which is then
         # resolved in self.get_and_resolve_doctree)
         if isinstance(sectionnode, nodes.section):
             title = sectionnode[0]
             # copy the contents of the section title, but without references
             # and unnecessary stuff
             visitor = SphinxContentsFilter(doctree)
             title.walkabout(visitor)
             nodetext = visitor.get_entry_text()
             if not numentries[0]:
                 # for the very first toc entry, don't add an anchor
                 # as it is the file's title anyway
                 anchorname = ''
             else:
                 anchorname = '#' + sectionnode['ids'][0]
             numentries[0] += 1
             # make these nodes:
             # list_item -> compact_paragraph -> reference
             reference = nodes.reference(
                 '', '', internal=True, refuri=docname,
                 anchorname=anchorname, *nodetext)
             para = addnodes.compact_paragraph('', '', reference)
             item = nodes.list_item('', para)  # type: nodes.Element
             sub_item = build_toc(sectionnode, depth + 1)
             if sub_item:
                 item += sub_item
             entries.append(item)
         elif isinstance(sectionnode, addnodes.only):
             onlynode = addnodes.only(expr=sectionnode['expr'])
             blist = build_toc(sectionnode, depth)
             if blist:
                 onlynode += blist.children
                 entries.append(onlynode)
         elif isinstance(sectionnode, nodes.Element):
             for toctreenode in traverse_in_section(sectionnode,
                                                    addnodes.toctree):
                 item = toctreenode.copy()
                 entries.append(item)
                 # important: do the inventory stuff
                 TocTree(app.env).note(docname, toctreenode)
     if entries:
         return nodes.bullet_list('', *entries)
     return None
Beispiel #3
0
 def process_doc(self, app: Sphinx, doctree: nodes.document) -> None:
     """Add a title node to the document (just copy the first section title),
     and store that title in the environment.
     """
     titlenode = nodes.title()
     longtitlenode = titlenode
     # explicit title set with title directive; use this only for
     # the <title> tag in HTML output
     if 'title' in doctree:
         longtitlenode = nodes.title()
         longtitlenode += nodes.Text(doctree['title'])
     # look for first section title and use that as the title
     for node in doctree.traverse(nodes.section):
         visitor = SphinxContentsFilter(doctree)
         node[0].walkabout(visitor)
         titlenode += visitor.get_entry_text()
         break
     else:
         # document has no title
         titlenode += nodes.Text(doctree.get('title', '<no title>'))
     app.env.titles[app.env.docname] = titlenode
     app.env.longtitles[app.env.docname] = longtitlenode
Beispiel #4
0
 def create_title_from(self, docname, document):
     """Add a title node to the document (just copy the first section title),
     and store that title in the environment.
     """
     titlenode = nodes.title()
     longtitlenode = titlenode
     # explicit title set with title directive; use this only for
     # the <title> tag in HTML output
     if 'title' in document:
         longtitlenode = nodes.title()
         longtitlenode += nodes.Text(document['title'])
     # look for first section title and use that as the title
     for node in document.traverse(nodes.section):
         visitor = SphinxContentsFilter(document)
         node[0].walkabout(visitor)
         titlenode += visitor.get_entry_text()
         break
     else:
         # document has no title
         titlenode += nodes.Text('<no title>')
     self.titles[docname] = titlenode
     self.longtitles[docname] = longtitlenode
Beispiel #5
0
 def create_title_from(self, docname, document):
     """Add a title node to the document (just copy the first section title),
     and store that title in the environment.
     """
     titlenode = nodes.title()
     longtitlenode = titlenode
     # explicit title set with title directive; use this only for
     # the <title> tag in HTML output
     if 'title' in document:
         longtitlenode = nodes.title()
         longtitlenode += nodes.Text(document['title'])
     # look for first section title and use that as the title
     for node in document.traverse(nodes.section):
         visitor = SphinxContentsFilter(document)
         node[0].walkabout(visitor)
         titlenode += visitor.get_entry_text()
         break
     else:
         # document has no title
         titlenode += nodes.Text('<no title>')
     self.titles[docname] = titlenode
     self.longtitles[docname] = longtitlenode
Beispiel #6
0
 def process_doc(self, app, doctree):
     # type: (Sphinx, nodes.Node) -> None
     """Add a title node to the document (just copy the first section title),
     and store that title in the environment.
     """
     titlenode = nodes.title()
     longtitlenode = titlenode
     # explicit title set with title directive; use this only for
     # the <title> tag in HTML output
     if 'title' in doctree:
         longtitlenode = nodes.title()
         longtitlenode += nodes.Text(doctree['title'])
     # look for first section title and use that as the title
     for node in doctree.traverse(nodes.section):
         visitor = SphinxContentsFilter(doctree)
         node[0].walkabout(visitor)
         titlenode += visitor.get_entry_text()
         break
     else:
         # document has no title
         titlenode += nodes.Text('<no title>')
     app.env.titles[app.env.docname] = titlenode
     app.env.longtitles[app.env.docname] = longtitlenode
		def build_toc(node: nodes.Element, depth: int = 1) -> Optional[nodes.bullet_list]:
			"""
			Build the table of contents.

			:param node:
			:param depth:
			"""

			entries: List[nodes.Element] = []
			item: nodes.Element
			toctree_plus_types = set(app.env.config.toctree_plus_types)

			for sectionnode in node:
				# find all toctree nodes in this section and add them
				# to the toc (just copying the toctree node which is then
				# resolved in self.get_and_resolve_doctree)

				if isinstance(sectionnode, nodes.section):
					title = sectionnode[0]
					# copy the contents of the section title, but without references
					# and unnecessary stuff
					visitor = SphinxContentsFilter(doctree)
					title.walkabout(visitor)
					nodetext = visitor.get_entry_text()

					if not numentries[0]:
						# for the very first toc entry, don't add an anchor
						# as it is the file's title anyway
						anchorname = ''
					else:
						anchorname = f'#{sectionnode["ids"][0]}'

					numentries[0] += 1

					# make these nodes:
					# list_item -> compact_paragraph -> reference
					reference = nodes.reference(
							'',
							'',
							internal=True,
							refuri=docname,
							anchorname=anchorname,
							*nodetext,
							)

					para = addnodes.compact_paragraph('', '', reference)
					item = nodes.list_item('', para)
					sub_item = build_toc(sectionnode, depth + 1)

					if sub_item:
						item += sub_item

					entries.append(item)

				elif isinstance(sectionnode, addnodes.desc):
					# Add class, function and method directives to toctree.
					# (doesn't currently work for method directives - are they nested?)

					if sectionnode.attributes["objtype"] in toctree_plus_types:

						attributes = sectionnode.children[0].attributes
						if not attributes["ids"]:
							# Has no anchor
							continue

						title = attributes.get("fullname", sectionnode.children[0].astext())

						if sectionnode.attributes["objtype"] in {"method", "attribute"}:
							# TODO: remove special case
							title = title.split('.', 1)[-1]

						anchorname = f'#{attributes["ids"][0]}'

						reference = nodes.reference(
								'',
								'',
								internal=True,
								refuri=docname,
								anchorname=anchorname,
								*[nodes.literal(text=title)],
								)
						para = addnodes.compact_paragraph('', '', reference)
						item = nodes.list_item('', para)

						sub_item = build_toc(sectionnode.children[1], depth + 1)

						if sub_item:
							item += sub_item

						entries.append(item)

					elif TOCTREE_PLUS_DEBUG:
						print(sectionnode)
						pprint(sectionnode.attributes["objtype"])

				elif isinstance(sectionnode, addnodes.only):
					onlynode = addnodes.only(expr=sectionnode["expr"])
					blist = build_toc(sectionnode, depth)
					if blist:
						onlynode += blist.children
						entries.append(onlynode)

				elif isinstance(sectionnode, nodes.Element):

					for toctreenode in traverse_in_section(sectionnode, addnodes.toctree):
						item = toctreenode.copy()
						entries.append(item)

						# important: do the inventory stuff
						TocTree(app.env).note(docname, toctreenode)

			if entries:
				return nodes.bullet_list('', *entries)
			else:
				return None