Skip to content

Commit f5f1a35

Browse files
committed
Expose inventory
1 parent dc42bfb commit f5f1a35

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

mkdocstrings/handlers/crystal/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
from mkdocstrings.handlers.base import BaseHandler
44

5+
from . import inventory
56
from .collector import CrystalCollector
67
from .renderer import CrystalRenderer
78

89

910
class CrystalHandler(BaseHandler):
10-
pass
11+
load_inventory = staticmethod(inventory.list_object_urls)
1112

1213

1314
def get_handler(

mkdocstrings/handlers/crystal/collector.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import collections
22
import dataclasses
33
import functools
4-
import json
54
import logging
65
import os
76
import re
@@ -14,6 +13,7 @@
1413
from cached_property import cached_property
1514
from mkdocstrings.handlers.base import BaseCollector, CollectionError
1615

16+
from . import inventory
1717
from .items import DocConstant, DocItem, DocLocation, DocMapping, DocMethod, DocModule, DocType
1818

1919
try:
@@ -68,9 +68,8 @@ def root(self) -> "DocRoot":
6868
"""The top-level namespace, represented as a fake module."""
6969
try:
7070
with self._proc:
71-
data = json.load(self._proc.stdout)
72-
data["program"]["full_name"] = ""
73-
root = DocRoot(data["program"], None, None)
71+
root = inventory.read(self._proc.stdout)
72+
root.__class__ = DocRoot
7473
root.source_locations = self._source_locations
7574
return root
7675
finally:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import itertools
2+
import json
3+
import posixpath
4+
5+
from .items import DocModule
6+
7+
8+
def read(file) -> DocModule:
9+
data = json.load(file)
10+
data["program"]["full_name"] = ""
11+
return DocModule(data["program"], None, None)
12+
13+
14+
def list_objects(obj):
15+
path = obj.data["path"]
16+
17+
if obj.full_name:
18+
yield obj.abs_id, path
19+
20+
for const in obj.constants:
21+
yield const.abs_id, path + "#" + const.name
22+
23+
for meth in itertools.chain(
24+
obj.constructors, obj.class_methods, obj.instance_methods, obj.macros
25+
):
26+
yield meth.abs_id, path + "#" + meth.data["html_id"]
27+
28+
for typ in obj.types:
29+
yield from list_objects(typ)
30+
31+
32+
def list_object_urls(file, url="", base_url=None, **kwargs):
33+
if base_url is None:
34+
if url.endswith("/index.json"):
35+
base_url = url[: -len("/index.json")]
36+
else:
37+
base_url = url
38+
39+
for abs_id, path in list_objects(read(file)):
40+
yield abs_id, posixpath.join(base_url, path)

0 commit comments

Comments
 (0)