Skip to content

Commit

Permalink
Package doc includes toc of modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
daizutabi committed Jun 13, 2020
1 parent f00c1f5 commit 9383a5f
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion mkapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.8.5"
__version__ = "0.8.6"

from mkapi.core.module import get_module
from mkapi.core.node import get_node
Expand Down
4 changes: 2 additions & 2 deletions mkapi/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Module(Tree):
collected in this list.
"""

parent: Optional["Module"] = field(default=None, init=False)
members: List["Module"] = field(init=False)
parent: Optional["Module"] = field(default=None, init=False, repr=False)
members: List["Module"] = field(init=False, repr=False)
objects: List[str] = field(default_factory=list, init=False)

def __post_init__(self):
Expand Down
12 changes: 3 additions & 9 deletions mkapi/core/node.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""This modules provides Node class that has tree structure."""
import inspect
from dataclasses import dataclass, field
from functools import lru_cache
from typing import Any, Iterator, List, Optional

from mkapi.core.base import Base, Object
Expand All @@ -22,8 +21,8 @@ class Node(Tree):
members: Member Node instances.
"""

parent: Optional["Node"] = field(default=None, init=False)
members: List["Node"] = field(init=False)
parent: Optional["Node"] = field(default=None, init=False, repr=False)
members: List["Node"] = field(init=False, repr=False)
sourcefile_index: int = 0

def __post_init__(self):
Expand Down Expand Up @@ -187,11 +186,6 @@ def get_members(obj: Any) -> List[Node]:
return sorted(members, key=lambda x: (-x.sourcefile_index, x.lineno))


@lru_cache(maxsize=1000)
def _get_node(obj, recursive, sourcefile_index) -> Node:
return Node(obj, recursive, sourcefile_index)


def get_node(name, recursive: bool = True, sourcefile_index: int = 0) -> Node:
"""Returns a Node instace by name or object.
Expand All @@ -205,4 +199,4 @@ def get_node(name, recursive: bool = True, sourcefile_index: int = 0) -> Node:
else:
obj = name

return _get_node(obj, recursive, sourcefile_index)
return Node(obj, recursive, sourcefile_index)
2 changes: 1 addition & 1 deletion mkapi/core/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from typing import Iterator, List

from mkapi import utils
from mkapi.core import postprocess
from mkapi.core.base import Base, Section
from mkapi.core.inherit import inherit_by_filters
from mkapi.core.linker import resolve_link
from mkapi.core.node import Node, get_node
from mkapi.core.regex import MKAPI_PATTERN, NODE_PATTERN, node_markdown
from mkapi.core.renderer import renderer
from mkapi.core import postprocess


@dataclass
Expand Down
2 changes: 2 additions & 0 deletions mkapi/core/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def is_member(kind):
item.markdown, url, signature = "", "", ""
if filters and "link" in filters:
url = "#" + object.id
elif filters and "apilink" in filters:
url = "../" + node.object.id + "#" + object.id
if object.kind not in ["class", "dataclass"]:
signature = "(" + ",".join(object.signature.parameters.keys()) + ")"
item.html = renderer.render_object_member(object.name, url, signature)
Expand Down
6 changes: 2 additions & 4 deletions mkapi/core/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,15 @@ def render_object(self, object: Object, filters: List[str] = None) -> str:
object: Object instance.
filters: Filters.
"""
if filters is None:
filters = []
context = linker.resolve_object(object.html)
if context.get("level"):
if object.kind in ["module", "package"]:
if filters is None:
filters = []
filters.append("plain")
template = self.templates["object_heading"]
else:
template = self.templates["object_div"]
if filters is None:
filters = []
return template.render(context, object=object, filters=filters)

def render_object_member(self, name: str, url: str, signature: str) -> str:
Expand Down
8 changes: 4 additions & 4 deletions mkapi/core/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class Tree:
sourcefile: str = field(init=False)
lineno: int = field(init=False)
object: Object = field(init=False)
docstring: Docstring = field(init=False)
parent: Any = field(default=None, init=False)
members: List[Any] = field(init=False)
recursive: bool = True
docstring: Docstring = field(init=False, repr=False)
parent: Any = field(default=None, init=False, repr=False)
members: List[Any] = field(init=False, repr=False)
recursive: bool = field(default=True, repr=False)

def __post_init__(self):
obj = self.obj
Expand Down
2 changes: 1 addition & 1 deletion mkapi/plugins/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def add_page(module: Module) -> str:
nav.append({package: modules})
package = m.object.id
modules = {}
if m.docstring:
if m.docstring or any(s.docstring for s in m.members):
modules[m.object.id] = add_page(m)
else:
modules[m.object.id] = add_page(m)
Expand Down
8 changes: 8 additions & 0 deletions mkapi/templates/module.jinja2
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# ![mkapi]({{ module.object.id }}{{ module_filter }}|link|plain)

{% if module.object.kind == 'module' %}
{% for object in module.objects %}
{% if 'noheading' in object_filter %}
![mkapi]({{ object }}{{ object_filter }})
{% else %}
## ![mkapi]({{ object }}{{ object_filter }})
{% endif %}
{% endfor %}
{% else %}
{% for member in module.members %}
{% if member.docstring %}
## ![mkapi]({{ member.object.id }}{{ module_filter }}|apilink|plain)
{% endif %}
{% endfor %}
{% endif %}
6 changes: 4 additions & 2 deletions mkapi/theme/css/mkapi-common.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ span.mkapi-object-prefix a {
color: inherit;
}

code.mkapi-object-prefix a:hover {
code.mkapi-object-prefix a:hover,
span.mkapi-object-prefix a:hover {
background: #FFDDCC;
}

Expand All @@ -95,7 +96,8 @@ span.mkapi-object-name a {
color: inherit;
}

code.mkapi-object-name a:hover {
code.mkapi-object-name a:hover,
span.mkapi-object-name a:hover {
background: #FFDDCC;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/core/test_core_inherit.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ def set_name(self, name: str):
self.name = name.upper()


@pytest.fixture()
@pytest.fixture(scope='module')
def a():
return mkapi.get_node(A)


@pytest.fixture()
@pytest.fixture(scope='module')
def b():
return mkapi.get_node(B)


@pytest.fixture()
@pytest.fixture(scope='module')
def c():
return mkapi.get_node(C)


@pytest.fixture()
@pytest.fixture(scope='module')
def d():
return mkapi.get_node(D)

Expand Down

0 comments on commit 9383a5f

Please sign in to comment.