33import logging
44import os
55
6+ from characteristic import attributes
67from six import iteritems
78from sphinx .ext .intersphinx import read_inventory_v2
9+ from zope .interface import implementer
810
911from . import types
10- from .base import _BaseParser , ParserEntry
11- from .sphinx import find_and_patch_entry
12+ from .utils import (
13+ APPLE_REF_TEMPLATE ,
14+ IParser ,
15+ ParserEntry ,
16+ has_file_with ,
17+ )
1218
1319
1420log = logging .getLogger (__name__ )
3642}
3743
3844
39- class InterSphinxParser (_BaseParser ):
45+ @implementer (IParser )
46+ @attributes (["doc_path" ])
47+ class InterSphinxParser (object ):
4048 """
4149 Parser for Sphinx-base documentation that generates an objects.inv file for
4250 the intersphinx extension.
4351 """
4452 name = "intersphinx"
4553
46- DETECT_FILE = "objects.inv"
47- DETECT_PATTERN = b"# Sphinx inventory version 2"
54+ @staticmethod
55+ def detect (path ):
56+ return has_file_with (
57+ path , "objects.inv" , b"# Sphinx inventory version 2"
58+ )
4859
4960 def parse (self ):
5061 """
51- Parse sphinx docs at self.docpath .
62+ Parse sphinx docs at self.doc_path .
5263
53- yield tuples of symbol name, type and path
64+ yield `ParserEntry`s.
5465 """
55- log .info ('Creating database...' )
56- with open (os .path .join (self .docpath , "objects.inv" ), "rb" ) as inv_f :
66+ with open (os .path .join (self .doc_path , "objects.inv" ), "rb" ) as inv_f :
5767 inv_f .readline () # skip version line that is verified in detection
5868 for pe in _inv_to_entries (
5969 read_inventory_v2 (inv_f , "" , os .path .join )
@@ -64,6 +74,23 @@ def find_and_patch_entry(self, soup, entry): # pragma: nocover
6474 return find_and_patch_entry (soup , entry )
6575
6676
77+ def find_and_patch_entry (soup , entry ):
78+ """
79+ Modify soup so Dash.app can generate TOCs on the fly.
80+ """
81+ link = soup .find ('a' , {'class' : 'headerlink' }, href = '#' + entry .anchor )
82+ tag = soup .new_tag ('a' )
83+ tag ['name' ] = APPLE_REF_TEMPLATE .format (entry .type , entry .name )
84+ if link :
85+ link .parent .insert (0 , tag )
86+ return True
87+ elif entry .anchor .startswith ('module-' ):
88+ soup .h1 .parent .insert (0 , tag )
89+ return True
90+ else :
91+ return False
92+
93+
6794def _inv_to_entries (inv ):
6895 """
6996 Iterate over a dictionary as returned from Sphinx's object.inv parser and
0 commit comments