Skip to content

Commit

Permalink
Add link from toc to source.
Browse files Browse the repository at this point in the history
  • Loading branch information
daizutabi committed Jun 17, 2020
1 parent d77af75 commit 1ff1f7b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions docs/appendix/filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Filters and Varialbes

## Filters

* `upper`
* `plain`
* `all`
* `link`
* `apilink`
* `sourcelink`


## Varialbes

* `{default}`
* `{class}`
40 changes: 27 additions & 13 deletions mkapi/core/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<span id="{object.id}"></span>'
else:
link = ""
link += f'<a class="mkapi-args-src-link" href="../source/{object.module}'
link += f'#{object.id}" title="Source for {object.id}"></a>'
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):
Expand All @@ -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'<span id="{object.id}"></span>'
link += f'<a class="mkapi-src-link" href="../source/{object.module}'
link += f'#{object.id}" title="{object.id}">SOURCE</a>'

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)
Expand Down Expand Up @@ -75,28 +87,30 @@ 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
if object.kind not in ["class", "dataclass"]:
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 = []


Expand Down
5 changes: 5 additions & 0 deletions mkapi/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions mkapi/templates/module.jinja2
Original file line number Diff line number Diff line change
@@ -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 -%}
19 changes: 18 additions & 1 deletion mkapi/theme/css/mkapi-common.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1ff1f7b

Please sign in to comment.