From 1ff1f7b841e469d66a770a366c0e51f5ae21f265 Mon Sep 17 00:00:00 2001 From: daizu Date: Wed, 17 Jun 2020 10:59:27 +0900 Subject: [PATCH] Add link from toc to source. --- CHANGELOG.md | 1 + docs/appendix/filter.md | 16 +++++++++++++ mkapi/core/postprocess.py | 40 +++++++++++++++++++++----------- mkapi/core/structure.py | 5 ++++ mkapi/templates/module.jinja2 | 8 +++---- mkapi/theme/css/mkapi-common.css | 19 ++++++++++++++- mkdocs.yml | 1 + 7 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 docs/appendix/filter.md diff --git a/CHANGELOG.md b/CHANGELOG.md index a94ea06e..c5d58cc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * BugFix: Include decorated functions if decorator uses `functools.wraps()`. * `all` filter for package or module to display all of the members and add links to them. +* Add link from table of contents to source. ## Version 1.0.3 (2020-06-16) diff --git a/docs/appendix/filter.md b/docs/appendix/filter.md new file mode 100644 index 00000000..bf5b840f --- /dev/null +++ b/docs/appendix/filter.md @@ -0,0 +1,16 @@ +# Filters and Varialbes + +## Filters + +* `upper` +* `plain` +* `all` +* `link` +* `apilink` +* `sourcelink` + + +## Varialbes + +* `{default}` +* `{class}` diff --git a/mkapi/core/postprocess.py b/mkapi/core/postprocess.py index c78455d5..2f577dd0 100644 --- a/mkapi/core/postprocess.py +++ b/mkapi/core/postprocess.py @@ -3,6 +3,26 @@ from mkapi.core.base import Inline, Item, Section, Type from mkapi.core.node import Node from mkapi.core.renderer import renderer +from mkapi.core.structure import Object + + +def sourcelink(object: Object) -> str: + if "property" in object.kind: + link = f'' + else: + link = "" + link += f'' + return link + + +def source_link_from_section_item(item: Item, object: Object): + link = sourcelink(object) + + def callback(inline, link=link): + return inline.html + link + + item.description.callback = callback def transform_property(node: Node, filters: List[str] = None): @@ -18,16 +38,8 @@ def transform_property(node: Node, filters: List[str] = None): type = object.type description = member.docstring.sections[0].markdown item = Item(name, type, Inline(description), kind=kind) - if filters and "sourcelink" in filters: - link = f'' - link += f'SOURCE' - - def callback(inline, link=link): - return inline.html + link - - item.description.callback = callback + source_link_from_section_item(item, object) section.items.append(item) else: members.append(member) @@ -75,7 +87,7 @@ def is_member(kind): description = "" item = Item(object.name, type, Inline(description), kind) item.markdown, url, signature = "", "", "" - if filters and ("link" in filters or 'all' in filters): + if filters and ("link" in filters or "all" in filters): url = "#" + object.id elif filters and "apilink" in filters: url = "../" + node.object.id + "#" + object.id @@ -83,20 +95,22 @@ def is_member(kind): args = [item.name for item in object.signature.parameters.items] signature = "(" + ",".join(args) + ")" item.html = renderer.render_object_member(object.name, url, signature) + if filters and "sourcelink" in filters: + source_link_from_section_item(item, object) section.items.append(item) node.docstring.set_section(section) def transform_class(node: Node, filters: Optional[List[str]] = None): transform_property(node, filters) - transform_members(node, "class", ["link"]) - transform_members(node, "method", ["link"]) + transform_members(node, "class", filters) + transform_members(node, "method", filters) def transform_module(node: Node, filters: Optional[List[str]] = None): transform_members(node, "class", filters) transform_members(node, "function", filters) - if not filters or 'all' not in filters: + if not filters or "all" not in filters: node.members = [] diff --git a/mkapi/core/structure.py b/mkapi/core/structure.py index 117530d3..a03692f9 100644 --- a/mkapi/core/structure.py +++ b/mkapi/core/structure.py @@ -55,6 +55,11 @@ def __post_init__(self): else: self.markdown = name + def __repr__(self): + class_name = self.__class__.__name__ + id = self.id + return f"{class_name}({id!r})" + def __iter__(self) -> Iterator[Base]: yield from self.type yield self diff --git a/mkapi/templates/module.jinja2 b/mkapi/templates/module.jinja2 index c3f9b7ac..5bdf645a 100644 --- a/mkapi/templates/module.jinja2 +++ b/mkapi/templates/module.jinja2 @@ -1,17 +1,17 @@ -# ![mkapi]({{ module.object.id }}{{ module_filter }}|link|plain) +# ![mkapi]({{ module.object.id }}{{ module_filter }}|plain|link|sourcelink) {% if module.object.kind == 'module' -%} {% for node in module.node.members -%} {% if 'noheading' in object_filter -%} -![mkapi]({{ node.object.id }}{{ object_filter }}|sourcelink) +![mkapi]({{ node.object.id }}{{ object_filter }}|link|sourcelink) {% else -%} -## ![mkapi]({{ node.object.id }}{{ object_filter }}|sourcelink) +## ![mkapi]({{ node.object.id }}{{ object_filter }}|link|sourcelink) {% endif -%} {% endfor -%} {% else -%} {% for member in module.members -%} {% if member.docstring -%} -## ![mkapi]({{ member.object.id }}{{ module_filter }}|apilink|plain) +## ![mkapi]({{ member.object.id }}{{ module_filter }}|plain|apilink|sourcelink) {% endif -%} {% endfor -%} {% endif -%} diff --git a/mkapi/theme/css/mkapi-common.css b/mkapi/theme/css/mkapi-common.css index 48b3cf98..80f6900b 100644 --- a/mkapi/theme/css/mkapi-common.css +++ b/mkapi/theme/css/mkapi-common.css @@ -416,8 +416,25 @@ a.mkapi-src-link, a.mkapi-docs-link { color: #BEBEBE; } -a.mkapi-src-link:hover, a.mkapi-docs-link:hover { +a.mkapi-args-src-link { + font-size: 0.7rem; + font-weight: normal; + margin-left: 10px; + padding: 1px 3px; + border: 1px solid #E0E0E0; + background: #F8F8F8; + border-radius: 5px; + color: #E0E0E0; +} + +a.mkapi-src-link:hover, a.mkapi-args-src-link:hover, a.mkapi-docs-link:hover { border: 1px solid #FF9999; background: #FFF0F0; color: red; } + +a.mkapi-args-src-link::before { + font-family: "Font Awesome 5 Free"; + content: "\f121"; + font-weight: 600; +} diff --git a/mkdocs.yml b/mkdocs.yml index 44cbd9f6..4845225d 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -41,6 +41,7 @@ nav: - appendix/inherit.md - appendix/order.md - appendix/decorator.md + - appendix/filter.md - API: mkapi/api/mkapi|upper|strict markdown_extensions: