@@ -28,11 +28,11 @@ class UrlItem:
2828
2929
3030@attr .s (slots = True )
31- class TocItem :
31+ class TocTree :
3232 """An individual toctree within a document."""
3333
3434 # TODO validate uniqueness of docnames (at least one item)
35- sections : List [Union [GlobItem , FileItem , UrlItem ]] = attr .ib (
35+ items : List [Union [GlobItem , FileItem , UrlItem ]] = attr .ib (
3636 validator = deep_iterable (
3737 instance_of ((GlobItem , FileItem , UrlItem )), instance_of (list )
3838 )
@@ -49,47 +49,43 @@ class TocItem:
4949 titlesonly : bool = attr .ib (True , kw_only = True , validator = instance_of (bool ))
5050
5151 def files (self ) -> List [str ]:
52- return [
53- str (section ) for section in self .sections if isinstance (section , FileItem )
54- ]
52+ return [str (item ) for item in self .items if isinstance (item , FileItem )]
5553
5654 def globs (self ) -> List [str ]:
57- return [
58- str (section ) for section in self .sections if isinstance (section , GlobItem )
59- ]
55+ return [str (item ) for item in self .items if isinstance (item , GlobItem )]
6056
6157
6258@attr .s (slots = True )
63- class DocItem :
59+ class Document :
6460 """A document in the site map."""
6561
6662 docname : str = attr .ib (validator = instance_of (str ))
6763 title : Optional [str ] = attr .ib (None , validator = optional (instance_of (str )))
6864 # TODO validate uniqueness of docnames across all parts (and none should be the docname)
69- parts : List [TocItem ] = attr .ib (
70- factory = list , validator = deep_iterable (instance_of (TocItem ), instance_of (list ))
65+ subtrees : List [TocTree ] = attr .ib (
66+ factory = list , validator = deep_iterable (instance_of (TocTree ), instance_of (list ))
7167 )
7268
7369 def child_files (self ) -> List [str ]:
7470 """Return all children files."""
75- return [name for part in self .parts for name in part .files ()]
71+ return [name for tree in self .subtrees for name in tree .files ()]
7672
7773 def child_globs (self ) -> List [str ]:
7874 """Return all children globs."""
79- return [name for part in self .parts for name in part .globs ()]
75+ return [name for tree in self .subtrees for name in tree .globs ()]
8076
8177
8278class SiteMap (MutableMapping ):
8379 """A mapping of documents to their toctrees (or None if terminal)."""
8480
85- def __init__ (self , root : DocItem , meta : Optional [Dict [str , Any ]] = None ) -> None :
86- self ._docs : Dict [str , DocItem ] = {}
81+ def __init__ (self , root : Document , meta : Optional [Dict [str , Any ]] = None ) -> None :
82+ self ._docs : Dict [str , Document ] = {}
8783 self [root .docname ] = root
88- self ._root : DocItem = root
84+ self ._root : Document = root
8985 self ._meta : Dict [str , Any ] = meta or {}
9086
9187 @property
92- def root (self ) -> DocItem :
88+ def root (self ) -> Document :
9389 """Return the root document."""
9490 return self ._root
9591
@@ -102,10 +98,10 @@ def globs(self) -> Set[str]:
10298 """Return set of all globs present across all toctrees."""
10399 return {glob for item in self ._docs .values () for glob in item .child_globs ()}
104100
105- def __getitem__ (self , docname : str ) -> DocItem :
101+ def __getitem__ (self , docname : str ) -> Document :
106102 return self ._docs [docname ]
107103
108- def __setitem__ (self , docname : str , item : DocItem ) -> None :
104+ def __setitem__ (self , docname : str , item : Document ) -> None :
109105 assert item .docname == docname
110106 self ._docs [docname ] = item
111107
@@ -130,17 +126,12 @@ def _serializer(inst: Any, field: attr.Attribute, value: Any) -> Any:
130126 return str (value )
131127 return value
132128
133- def as_json (
134- self , root_key : str = "_root" , meta_key : str = "_meta"
135- ) -> Dict [str , Any ]:
129+ def as_json (self ) -> Dict [str , Any ]:
136130 """Return JSON serialized site-map representation."""
137- dct = {
138- k : attr .asdict (v , value_serializer = self ._serializer ) if v else v
139- for k , v in self ._docs .items ()
131+ doc_dict = {
132+ k : attr .asdict (self ._docs [k ], value_serializer = self ._serializer )
133+ if self ._docs [k ]
134+ else self ._docs [k ]
135+ for k in sorted (self ._docs )
140136 }
141- assert root_key not in dct
142- dct [root_key ] = self .root .docname
143- if self .meta :
144- assert meta_key not in dct
145- dct [meta_key ] = self .meta
146- return dct
137+ return {"root" : self .root .docname , "documents" : doc_dict , "meta" : self .meta }
0 commit comments