Skip to content

Commit 60100d8

Browse files
authored
Another baby step in improving the documentation system. (#987)
* Adding docstrings to modules that otherwise would not be loaded in the documentation. * Adding a private function in MathicsMainDocumentation.doc_part to ensure that we walk over top-level modules to build the chapters.
1 parent 87e278b commit 60100d8

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

mathics/doc/common_doc.py

+44-3
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ def skip_doc(cls) -> bool:
304304
return cls.__name__.endswith("Box") or (hasattr(cls, "no_doc") and cls.no_doc)
305305

306306

307-
def skip_module_doc(module, modules_seen) -> bool:
307+
def skip_module_doc(module, must_be_skipped) -> bool:
308308
return (
309309
module.__doc__ is None
310-
or module in modules_seen
310+
or module in must_be_skipped
311311
or module.__name__.split(".")[0] not in ("mathics", "pymathics")
312312
or hasattr(module, "no_doc")
313313
and module.no_doc
@@ -1052,8 +1052,13 @@ def doc_part(self, title, modules, builtins_by_module, start):
10521052
"""
10531053

10541054
builtin_part = self.part_class(self, title, is_reference=start)
1055+
1056+
# This is used to ensure that we pass just once over each module.
1057+
# The algorithm we use to walk all the modules without repetitions
1058+
# relies on this, which in my opinion is hard to test and susceptible
1059+
# to errors. I guess we include it as a temporal fixing to handle
1060+
# packages inside ``mathics.builtin``.
10551061
modules_seen = set([])
1056-
submodule_names_seen = set([])
10571062

10581063
want_sorting = True
10591064
if want_sorting:
@@ -1065,6 +1070,34 @@ def doc_part(self, title, modules, builtins_by_module, start):
10651070
)
10661071
else:
10671072
module_collection_fn = lambda x: x
1073+
1074+
# For some weird reason, it seems that this produces an
1075+
# overflow error in test.builitin.directories.
1076+
'''
1077+
def filter_toplevel_modules(module_list):
1078+
"""
1079+
Keep just the modules at the top level.
1080+
"""
1081+
if len(module_list) == 0:
1082+
return module_list
1083+
1084+
modules_and_levels = sorted(
1085+
((module.__name__.count("."), module) for module in module_list),
1086+
key=lambda x: x[0],
1087+
)
1088+
top_level = modules_and_levels[0][0]
1089+
return (entry[1] for entry in modules_and_levels if entry[0] == top_level)
1090+
'''
1091+
# This ensures that the chapters are built
1092+
# from the top-level modules. Without this,
1093+
# if this happens is just by chance, or by
1094+
# an obscure combination between the sorting
1095+
# of the modules and the way in which visited
1096+
# modules are skipped.
1097+
#
1098+
# However, if I activate this, some tests are lost.
1099+
#
1100+
# modules = filter_toplevel_modules(modules)
10681101
for module in module_collection_fn(modules):
10691102
if skip_module_doc(module, modules_seen):
10701103
continue
@@ -1075,6 +1108,10 @@ def doc_part(self, title, modules, builtins_by_module, start):
10751108
builtins = builtins_by_module.get(module.__name__)
10761109
if module.__file__.endswith("__init__.py"):
10771110
# We have a Guide Section.
1111+
1112+
# This is used to check if a symbol is not duplicated inside
1113+
# a guide.
1114+
submodule_names_seen = set([])
10781115
name = get_doc_name_from_module(module)
10791116
guide_section = self.add_section(
10801117
chapter, name, module, operator=None, is_guide=True
@@ -1102,6 +1139,10 @@ def doc_part(self, title, modules, builtins_by_module, start):
11021139
continue
11031140

11041141
submodule_name = get_doc_name_from_module(submodule)
1142+
# This has the side effect that Symbols with the same
1143+
# short name but in different contexts be skipped.
1144+
# This happens with ``PlaintextImport`` that appears in
1145+
# the HTML and XML contexts.
11051146
if submodule_name in submodule_names_seen:
11061147
continue
11071148
section = self.add_section(

0 commit comments

Comments
 (0)