Skip to content

Commit

Permalink
Before refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
daizutabi committed Jun 14, 2020
1 parent 9383a5f commit 05c82ca
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 68 deletions.
6 changes: 3 additions & 3 deletions docs/usage/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ def to_str(self, x: int) -> str:

# ## Constructing HTML

# Finally, construct HTML calling `Node.render()` that internally uses
# Finally, construct HTML calling `Node.get_html()` that internally uses
# [Jinja](https://jinja.palletsprojects.com/en/2.11.x/) library.

html = node.render()
html = node.get_html()
print(html[:300].strip())

# [Jupyter](https://jupyter.org/) allows us to see the rendered HTML.
Expand Down Expand Up @@ -173,4 +173,4 @@ def get_html(obj) -> str:
node.set_html(html)

# Render the node to create final HTML.
return node.render()
return node.get_html()
17 changes: 14 additions & 3 deletions mkapi/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mkapi.core.tree import Tree


@dataclass
@dataclass(repr=False)
class Module(Tree):
"""Module class represents an module.
Expand All @@ -20,8 +20,8 @@ class Module(Tree):
collected in this list.
"""

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

def __post_init__(self):
Expand All @@ -30,6 +30,11 @@ def __post_init__(self):
objects = get_objects(self.obj)
self.objects = [".".join([self.object.id, obj]) for obj in objects]

def __repr__(self):
s = super().__repr__()[:-1]
objects = len(self.objects)
return f"{s}, num_objects={objects})"

def __iter__(self) -> Iterator["Module"]:
if self.docstring:
yield self
Expand Down Expand Up @@ -61,6 +66,12 @@ def get_markdown(self, filters: List[str]) -> str: # type:ignore

return renderer.render_module(self, filters) # type:ignore

def get_source(self, filters: List[str]) -> str:
"""Returns a source for module."""
from mkapi.core.source import get_source

return get_source(self, filters)


def get_objects(obj) -> List[str]:
obj_source_file = inspect.getsourcefile(obj)
Expand Down
11 changes: 6 additions & 5 deletions mkapi/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mkapi.core.tree import Tree


@dataclass
@dataclass(repr=False)
class Node(Tree):
"""Node class represents an object.
Expand All @@ -21,8 +21,8 @@ class Node(Tree):
members: Member Node instances.
"""

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

def __post_init__(self):
Expand Down Expand Up @@ -88,11 +88,11 @@ def set_html(self, html: str):
for base, html in zip(self, html.split("<!-- mkapi:sep -->")):
base.set_html(html.strip())

def render(self) -> str:
def get_html(self, filters: List[str] = None) -> str:
"""Renders and returns HTML."""
from mkapi.core.renderer import renderer

return renderer.render(self) # type:ignore
return renderer.render(self, filters) # type:ignore


def get_kind(obj) -> str:
Expand Down Expand Up @@ -191,6 +191,7 @@ def get_node(name, recursive: bool = True, sourcefile_index: int = 0) -> Node:
Args:
name: Object name or object itself.
recursive: If True, member objects are collected recursively.
sourcefile_index: If `obj` is a member of class, this value is the index of
unique source files given by `mro()` of the class. Otherwise, 0.
"""
Expand Down
3 changes: 1 addition & 2 deletions mkapi/core/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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


@dataclass
Expand Down Expand Up @@ -78,6 +77,6 @@ def replace(match):
node = self.nodes[int(match.group(1))]
filters = match.group(2).split("|")
node.set_html(match.group(3))
return renderer.render(node, filters=filters)
return node.get_html(filters)

return re.sub(NODE_PATTERN, replace, html)
3 changes: 3 additions & 0 deletions mkapi/core/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def get_attributes(self):
self.attributes[name] = type
self.attributes_desc[name] = desc

def split(self, sep=','):
return str(self).split(sep)


def to_string(annotation, kind: str = "returns") -> str:
"""Returns string expression of annotation.
Expand Down
40 changes: 40 additions & 0 deletions mkapi/core/source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import ast
import inspect
from typing import List

import _ast
from mkapi.core.module import Module, get_module
from mkapi.core.object import get_object


def get_source(module: Module, filters: List[str] = None) -> str:
"""Returns a source for module."""
with open(module.sourcefile, "r") as f:
source = f.read().strip()
if not source:
return ""
# return f"[DOCS](../{module.object.id}.md#{module.object.id})\n\n~~~python\n{source}\n~~~\n"
return source


# module = get_module("mkapi.core.base")
#
# source = get_source(module)
#
# module.object.id
# module.objects
#
# obj = get_object(module.objects[0])
# inspect.getsourcelines(obj)
#
# node = ast.parse(source)
# for x in ast.iter_child_nodes(node):
# if isinstance(x, _ast.ClassDef):
# break
#
# x.lineno
# ast.get_source_segment(source, x)
# x.end_lineno
# dir(x)
# for a in ast.walk(x):
# print(a)
18 changes: 13 additions & 5 deletions mkapi/core/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ class Tree:
docstring: Docstring instance.
parent: Parent instance.
members: Member instances.
recursive: If True, member objects are collected recursively.
"""

obj: Any = field(repr=False)
obj: Any = field()
sourcefile: str = field(init=False)
lineno: int = field(init=False)
object: Object = field(init=False)
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)
docstring: Docstring = field(init=False)
parent: Any = field(default=None, init=False)
members: List[Any] = field(init=False)
recursive: bool = field(default=True)

def __post_init__(self):
obj = self.obj
Expand All @@ -54,6 +55,13 @@ def __post_init__(self):
for member in self.members:
member.parent = self

def __repr__(self):
class_name = self.__class__.__name__
id = self.object.id
sections = len(self.docstring.sections)
numbers = len(self.members)
return f"{class_name}({id!r}, num_sections={sections}, num_numbers={numbers})"

def __getitem__(self, index: Union[int, str]):
"""Returns a member Tree instance.
Expand Down
17 changes: 13 additions & 4 deletions mkapi/plugins/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def collect(path: str, docs_dir: str, config_dir) -> Tuple[list, list]:
logger.error(f"[MkApi] {abs_api_path} exists: Delete manually for safety.")
sys.exit(1)
os.mkdir(abs_api_path)
os.mkdir(os.path.join(abs_api_path, "source"))
atexit.register(lambda path=abs_api_path: rmtree(path))

root = os.path.join(config_dir, *paths)
Expand All @@ -46,12 +47,15 @@ def collect(path: str, docs_dir: str, config_dir) -> Tuple[list, list]:
modules: Dict[str, str] = {}
package = None

def add_page(module: Module) -> str:
def add_page(module: Module):
page_file = module.object.id + ".md"
abs_path = os.path.join(abs_api_path, page_file)
abs_api_paths.append(abs_path)
create_page(abs_path, module, filters)
return os.path.join(api_path, page_file)
modules[module.object.id] = os.path.join(api_path, page_file)

abs_path = os.path.join(abs_api_path, "source", page_file)
create_source_page(abs_path, module, filters)

for m in module:
if m.object.kind == "package":
Expand All @@ -60,9 +64,9 @@ def add_page(module: Module) -> str:
package = m.object.id
modules = {}
if m.docstring or any(s.docstring for s in m.members):
modules[m.object.id] = add_page(m)
add_page(m)
else:
modules[m.object.id] = add_page(m)
add_page(m)
if package and modules:
nav.append({package: modules})

Expand All @@ -74,6 +78,11 @@ def create_page(path: str, module: Module, filters: List[str]):
f.write(module.get_markdown(filters))


def create_source_page(path: str, module: Module, filters: List[str]):
with open(path, "w") as f:
f.write(module.get_source(filters))


def rmtree(path: str):
if not os.path.exists(path):
return
Expand Down
2 changes: 1 addition & 1 deletion mkapi/templates/args.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<ul class="mkapi-args-list">
{% for item in section.items %}
<li>{% if item.name %}<code class="mkapi-args-name{% if item.kind %} {{ item.kind }}" title="{{ item.kind }}{% endif %}">{{ item.html|safe }}</code>{% endif %}
<li{% if item.kind %} class="{{ item.kind }}"{% endif %}>{% if item.name %}<code class="mkapi-args-name">{{ item.html|safe }}</code>{% endif %}
{{ base_type(item) }}
<span class="mkapi-args-dash">&mdash;</span> <span class="mkapi-args-desc">{{ item.desc.html|safe }}</span></li>
{% endfor %}
Expand Down
16 changes: 12 additions & 4 deletions mkapi/templates/macros.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@
{%- if url and object.id != url[1:] %}</a>{% endif %}
{%- endmacro %}

{% macro object_signature(signature, tag='span', in_code=False) -%}
{% if signature -%}
{% if in_code %}</code>{% endif -%}
{% set args = signature.split(',') -%}
{% for arg in args -%}
<{{ tag }} class="mkapi-object-signature">{{ arg|trim }}{% if not loop.last %},{% endif %}{% if not (in_code and loop.last) %}</{{ tag }}>{% endif %}{% if not loop.last %} {% endif %}
{%- endfor -%}
{%- endif %}
{%- endmacro %}

{% macro object_body(object, prefix_url, name_url, tag, upper) -%}
{% if object.prefix and '.' not in object.qualname -%}
<{{ tag }} class="mkapi-object-prefix">{{ object_prefix(object, prefix_url, upper) }}</{{ tag }}>
{%- endif -%}
<{{ tag }} class="mkapi-object-name">{{ object_name(object, name_url, upper) }}</{{ tag }}>
{%- if object.signature %}<{{ tag }} class="mkapi-object-signature">{{ object.signature }}</{{ tag }}>{% endif %}
<{{ tag }} class="mkapi-object-name">{{ object_name(object, name_url, upper) }}</{{ tag }}>{{ object_signature(object.signature, tag) }}
{{ object_type(object) }}
{%- endmacro %}

{% macro object_member(name, url, signature) -%}
{% if url %}<a href="{{ url }}">{% endif -%}{{ name }}{% if url %}</a>{% endif %}
{%- if signature %}<span class="mkapi-object-signature">{{ signature }}</span>{% endif %}
{% if url %}<a href="{{ url }}">{% endif -%}{{ name }}{% if url %}</a>{% endif %}{{ object_signature(signature, tag='code', in_code=True) }}
{%- endmacro %}
24 changes: 12 additions & 12 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)

{% if module.object.kind == 'module' %}
{% for object in module.objects %}
{% if 'noheading' in object_filter %}
{% if module.object.kind == 'module' -%}
{% for object in module.objects -%}
{% if 'noheading' in object_filter -%}
![mkapi]({{ object }}{{ object_filter }})
{% else %}
{% else -%}
## ![mkapi]({{ object }}{{ object_filter }})
{% endif %}
{% endfor %}
{% else %}
{% for member in module.members %}
{% if member.docstring %}
{% endif -%}
{% endfor -%}
{% else -%}
{% for member in module.members -%}
{% if member.docstring -%}
## ![mkapi]({{ member.object.id }}{{ module_filter }}|apilink|plain)
{% endif %}
{% endfor %}
{% endif %}
{% endif -%}
{% endfor -%}
{% endif -%}
Loading

0 comments on commit 05c82ca

Please sign in to comment.