From 9398f83b0c2d4efd815bc19164ec9197edecec36 Mon Sep 17 00:00:00 2001 From: daizutabi Date: Thu, 8 Feb 2024 22:20:05 +0900 Subject: [PATCH 1/2] jinja2 template --- mkdocs.yml | 2 +- src/mkapi/objects.py | 2 +- src/mkapi/templates/object.jinja2 | 64 +++++++++++------------------ src/mkapi/themes/mkapi-material.css | 41 +++++++++--------- tests/test_renderers.py | 44 ++++++++++---------- 5 files changed, 72 insertions(+), 81 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index cec14ea0..24cd54f8 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -56,7 +56,7 @@ nav: - usage/object.md - usage/page.md - usage/config.md - - API: /mkapi.*** + # - API: /mkapi.*** - Examples: /examples.** # - Schemdraw: /schemdraw.*** # - Polars: /polars.*** diff --git a/src/mkapi/objects.py b/src/mkapi/objects.py index ffb9a4e0..d3cc43ad 100644 --- a/src/mkapi/objects.py +++ b/src/mkapi/objects.py @@ -113,7 +113,7 @@ def create_attribute( if doc.text.str and (lines := doc.text.str.splitlines()): # noqa: SIM102 if ":" in lines[0]: _, lines[0] = lines[0].split(":", maxsplit=1) - doc.text.str = "\n".join(lines) + doc.text.str = "\n".join(lines).strip() else: doc = Docstring("", Type(), assign.text, []) name, type_, default = assign.name, assign.type, assign.default diff --git a/src/mkapi/templates/object.jinja2 b/src/mkapi/templates/object.jinja2 index 1ab02d95..20cab3e0 100644 --- a/src/mkapi/templates/object.jinja2 +++ b/src/mkapi/templates/object.jinja2 @@ -25,55 +25,41 @@ : {{ obj.type.markdown|safe }} {%- endif -%} -{{ "\n" }}

+

{% if obj.kind in ["class", "dataclass"] and obj.bases -%}

Bases : - {% for base in obj.bases %}{{ base.type.markdown|safe }} - {%- if not loop.last %}, {% else %} -

{% endif %} - {% endfor -%} -{% endif %} +{% for base in obj.bases %}{{ base.type.markdown|safe }} + {%- if not loop.last %}, {% endif %} +{% endfor %}

+{% endif -%} -{% if doc.text.markdown %} -
{{ doc.text.markdown|safe }} -
-{% endif %} -{% for section in doc.sections if section.name != "Bases" %} - -{% if section.name and not section.kind %} -
-{{ section.name }} -
-{% endif %} +{% for section in doc.sections %} +{%- if section.name and not section.kind -%} +
{{ section.name }}
+{%- endif %} -{% if section.text.markdown %} -
{{ section.text.markdown|safe }} -
-{% endif %} - -{% if section.items %}
    {% endif %} -{% for item in section.items %} -
  • -{%- if item.name -%} -{{ item.name.replace("_", "\\_") }} -{%- endif -%} -{%- if item.name and item.type.markdown %} : {% endif %} -{%- if item.type.markdown -%} -{{ item.type.markdown|safe }} -{%- endif -%} -{%- if item.text.markdown %} - — +{% if section.items -%} +
      + {% for item in section.items %} +
    • + {% if item.name -%} + {{ item.name.replace("_", "\\_") }} + {%- endif -%} + {%- if item.name and item.type.markdown %} : {% endif %} + {% if item.type.markdown -%} + {{ item.type.markdown|safe }} + {%- endif -%} + {%- if item.text.markdown %} — {{ item.text.markdown|safe}} -{%- endif %} -
    • + {%- endif %} + {% endfor %} - -{% if section.items %}
    {% endif %} - +
+{% endif %} {% endfor %} \ No newline at end of file diff --git a/src/mkapi/themes/mkapi-material.css b/src/mkapi/themes/mkapi-material.css index e5d80fd2..47d425a6 100644 --- a/src/mkapi/themes/mkapi-material.css +++ b/src/mkapi/themes/mkapi-material.css @@ -31,35 +31,35 @@ p.mkapi-object { font-weight: 600; font-size: 1.1em; } -.mkapi-object-name .prefix { +.mkapi-prefix { color: var(--md-code-hl-name-color); } -.mkapi-object-name .dot { +.mkapi-dot { color: var(--md-code-hl-comment-color); } -.mkapi-signature .arg { +.mkapi-arg { color: var(--md-code-hl-name-color); font-style: italic; } -.mkapi-signature .default, .ann, .return, .star { +.mkapi-default, .mkapi-ann, .mkapi-return, .mkapi-star { color: var(--md-code-hl-variable-color); } -.mkapi-signature .paren { +.mkapi-paren { color: var(--md-code-hl-punctuation-color); margin-left: 2px; margin-right: 2px; } -.mkapi-signature .sep { +.mkapi-star, .mkapi-slash { color: var(--md-code-hl-punctuation-color); margin-left: 0px; margin-right: 2px; } -.mkapi-signature .comma, .colon { +.mkapi-comma, .mkapi-colon { color: var(--md-code-hl-punctuation-color); margin-left: 2px; margin-right: 0px; } -.mkapi-signature .equal, .arrow { +.mkapi-equal, .mkapi-arrow { color: var(--md-code-hl-punctuation-color); margin-left: 0px; margin-right: 0px; @@ -69,24 +69,27 @@ p.mkapi-object-bases { font-size: 85%; margin-block-start: 0px; } +.mkapi-section { + margin-bottom: 8px; + +} +.mkapi-section + p { + margin-block-start: 0px; +} +.mkapi-section + ul , +.mkapi-section + ol { + margin-block-start: 0px; +} + .mkapi-section-name { - color: var(--md-default-fg-color--light); border-bottom: thin solid var(--md-default-fg-color--light); font-size: 1.05em; - font-weight: 400; - margin-top: 16px; -} -ul.mkapi-section-items { - margin-top: 6pt; } .mkapi-item-name { font-weight: 600; } -.mkapi-item-type,.mkapi-object-type { - color: var(--md-code-hl-variable-color); -} -li.mkapi-section-item li { - margin-bottom: 2px; +.mkapi-item-type, .mkapi-object-type { + color: var(--md-default-fg-color--light); } .mkapi-example-input pre { margin-bottom: 0px; diff --git a/tests/test_renderers.py b/tests/test_renderers.py index a2f974b3..f1e714f9 100644 --- a/tests/test_renderers.py +++ b/tests/test_renderers.py @@ -38,7 +38,7 @@ def test_render_moduled(): s = '

\n <' assert s in m assert '>\n package\n <' in m - assert "styles\n

" in m + assert "styles

" in m def test_render_method(): @@ -49,7 +49,7 @@ def test_render_method(): assert s in m assert '>\n method\n <' in m assert '>\n )\n

' in m + assert '>)

' in m def test_render_attribute(): @@ -60,37 +60,39 @@ def test_render_attribute(): assert s in m assert '>\n attribute\n <' in m assert '\n :\n' in m - assert ':\n int\n

' in m + assert ':\n int

' in m def test_render_class(): obj = get_object("mkapi.objects.Class") assert isinstance(obj, Class) bases = obj.bases - # obj.bases = obj.bases * 3 - assert obj + obj.bases = bases * 2 m = render(obj, 2, []) - s = "\n

\n" + s = "

\n" assert s in m - i = m.index(s) - print(m[i : i + 500]) - # assert 0 - + assert '.Callable], \n' in m + # assert '.Callable]\n

\n\n
\n method\n <' in m - # assert '>\n )\n

' in m -# i = m.index(s) -# print(m[i : i + 500]) +# def test_render_doc_text(): +# obj = get_object("examples.styles.google.ExampleClass") +# assert obj +# m = render(obj, 2, []) +# s = '
\nThe summary' +# assert s in m -# assert 0 -# # name = "polars.config.Config.set_tbl_cell_alignment" -# # obj = get_object(name) + +# def test_render_section(): +# obj = get_object("examples.styles.google.ExampleClass") +# assert obj +# m = render(obj, 2, []) +# print(m) +# assert '
\n\n
\n\n
    ' in m +# assert "\n \n\n \n\n
\n" in m # def test_render_sourcelink(): From c62fd8d2ab230a3c05661485b99a81e9dedad383 Mon Sep 17 00:00:00 2001 From: daizutabi Date: Thu, 8 Feb 2024 22:21:59 +0900 Subject: [PATCH 2/2] doc --- mkdocs.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index 24cd54f8..5b9499af 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -58,8 +58,8 @@ nav: - usage/config.md # - API: /mkapi.*** - Examples: /examples.** - # - Schemdraw: /schemdraw.*** - # - Polars: /polars.*** - # - Altair: /altair.*** + - Schemdraw: /schemdraw.*** + - Polars: /polars.*** + - Altair: /altair.*** extra_css: - stylesheets/extra.css \ No newline at end of file