Skip to content

Commit 9887fbf

Browse files
authored
fix: 型情報へのリンクを修正 (#94)
1 parent 9168117 commit 9887fbf

File tree

5 files changed

+87
-15
lines changed

5 files changed

+87
-15
lines changed

gen.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,74 @@ class PageDict(TypedDict):
3636
children: list["PageDict"]
3737

3838

39+
def type2href(parameter_type: str) -> str | None:
40+
"""型名からリンクを取得する"""
41+
foundation_set = set(
42+
(
43+
"arguments",
44+
"array",
45+
"auto",
46+
"bool",
47+
"bytes",
48+
"content",
49+
"datetime",
50+
"decimal",
51+
"dictionary",
52+
"duration",
53+
"float",
54+
"function",
55+
"int",
56+
"label",
57+
"module",
58+
"none",
59+
"plugin",
60+
"regex",
61+
"selector",
62+
"str",
63+
"type",
64+
"version",
65+
)
66+
)
67+
layout_set = set(
68+
(
69+
"alignment",
70+
"angle",
71+
"direction",
72+
"fraction",
73+
"length",
74+
"ratio",
75+
"relative",
76+
)
77+
)
78+
visualize_set = set(
79+
(
80+
"color",
81+
"gradient",
82+
"pattern",
83+
"stroke",
84+
)
85+
)
86+
introspection_set = set(
87+
(
88+
"counter",
89+
"location",
90+
"state",
91+
)
92+
)
93+
if parameter_type in foundation_set:
94+
return f"foundations/{parameter_type}"
95+
elif parameter_type in layout_set:
96+
return f"layout/{parameter_type}"
97+
elif parameter_type in visualize_set:
98+
return f"visualize/{parameter_type}"
99+
elif parameter_type in introspection_set:
100+
return f"introspection/{parameter_type}"
101+
else:
102+
return None
103+
104+
39105
def type2class(parameter_type: str) -> str:
40-
"""関数の引数の型名からCSSのクラス名を取得する"""
106+
"""型名からCSSのクラス名を取得する"""
41107
type2class_map: dict[str, str] = {
42108
"none": "pill-kw",
43109
"auto": "pill-kw",
@@ -155,6 +221,7 @@ def _render_to_file(
155221
path=path,
156222
prev=previous_page,
157223
next=next_page,
224+
type2href=type2href,
158225
type2class=type2class,
159226
gen_path=gen_path,
160227
**page,

templates/func_template.html.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
<h2 id="parameters">
1010
{{ macros.tooltip_display('引数', 'Parameters are the inputs to a function. They are specified in parentheses after the function name.', prefix='parameters') }}
1111
</h2>
12-
{{ macros.function_definition_display(body['content'], type2class, gen_path, prefix=body['content']['name']) }}
12+
{{ macros.function_definition_display(body['content'], type2href, type2class, gen_path, prefix=body['content']['name']) }}
1313
{% if body['content']['example'] %}
1414
{{ body['content']['example'] | safe }}
1515
{% endif %}
16-
{{ macros.function_params_display(body['content'], type2class, gen_path, prefix=body['content']['name']) }}
16+
{{ macros.function_params_display(body['content'], type2href, type2class, gen_path, prefix=body['content']['name']) }}
1717

1818

1919
{% if body['content']['scope'].__len__() > 0 %}
@@ -23,7 +23,7 @@
2323
{% endif %}
2424
{% for method in body['content']['scope'] %}
2525
<h3 id="{{ prefix }}-{{ method['name'] }}" class="method-head"><span><code>{{ method['name'] }}</code></span></h3>
26-
{{ macros.function_display(method, type2class, gen_path, prefix='definitions-' + method['name']) }}
26+
{{ macros.function_display(method, type2href, type2class, gen_path, prefix='definitions-' + method['name']) }}
2727
{% endfor %}
2828

2929
{% endblock %}

templates/group_template.html.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<h2 id="functions">Calculation</h2>
99
{% for method in body['content']['functions'] %}
1010
<h3 id="{{ prefix }}-{{ method['name'] }}" class="method-head"><span><code>{{ method['name'] }}</code></span></h3>
11-
{{ macros.function_display(method, type2class, gen_path, prefix='definitions-' + method['name'], is_example_folding=false) }}
11+
{{ macros.function_display(method, type2href, type2class, gen_path, prefix='definitions-' + method['name'], is_example_folding=false) }}
1212
{% endfor %}
1313

1414
{% endblock %}

templates/macros.html.j2

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,28 @@
99
</div>
1010
{% endmacro %}
1111

12-
{% macro function_definition_display(function, type2class, gen_path, prefix='') %}
12+
{% macro function_definition_display(function, type2href, type2class, gen_path, prefix='') %}
1313
<div class="code code-definition {{ 'single-arg' if function['params'].__len__() <= 1 else '' }}">{% if function['self'] %}self.{% else %}{{ gen_path(function) }}{% endif %}<span
1414
class="typ-func">{{ function['name'] }}</span>(<div class="arguments">{% for param in function['params'] %}<span
1515
class="overview-param">{% if not param['positional'] %}<a href="#parameters-{{ param['name'] }}">{{ param['name']
1616
}}<!-- -->: </a>{% endif %}{%
17-
for t in param['types'] %}<a href="/docs/reference/types/{{ t }}" class="pill {{ type2class(t) }}">{{ t }}</a>{% endfor %}{{',' if
17+
for t in param['types'] %}{% set href = type2href(t) %}{% if href %}<a href="/docs/reference/{{ type2href(t) }}" class="pill {{ type2class(t) }}">{{ t }}</a>{% else %}<span class="pill {{ type2class(t) }}">{{ t }}</span>{% endif %}{% endfor %}{{',' if
1818
function['params'].__len__() > 1 else '' }} </span>{% endfor %}</div>) {% if function['returns'] %}<!-- -->-&gt; {% for ret in function['returns']
19-
%}<a href="/docs/reference/types/{{ ret }}" class="pill {{ type2class(ret) }}">{{ ret }}</a>{% endfor %}{% endif %}</div>
19+
%}{% set href = type2href(ret) %}{% if href %}<a href="/docs/reference/{{ type2href(ret) }}" class="pill {{ type2class(ret) }}">{{ ret }}</a>{% else %}<span class="pill {{ type2class(ret) }}">{{ ret }}</span>{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %}</div>
2020
{% endmacro %}
2121

22-
{% macro function_params_display(function, type2class, gen_path, prefix='') %}
22+
{% macro function_params_display(function, type2href, type2class, gen_path, prefix='') %}
2323
{% for param in function['params'] %}
2424
<h4 id="{{ prefix }}-{{ function['name'] }}-parameters-{{ param['name'] }}"><code>{{ param['name'] }}</code>
2525
<div class="additional-info">
2626
<div>
2727
{% for t in param['types'] %}
28-
<a href="/docs/reference/types/{{ t }}" class="pill {{ type2class(t) }}">{{ t }}</a>
28+
{% set href = type2href(t) %}
29+
{% if href %}
30+
<a href="/docs/reference/{{ href }}" class="pill {{ type2class(t) }}">{{ t }}</a>
31+
{% else %}
32+
<span class="pill {{ type2class(t) }}">{{ t }}</span>
33+
{% endif %}
2934
{% endfor %}
3035
</div>
3136
{% if param['required'] %}<small>Required</small>{% endif %}{% if param['positional'] %}<small><span
@@ -92,9 +97,9 @@
9297
{% endfor %}
9398
{% endmacro %}
9499

95-
{% macro function_display(function, type2class, gen_path, prefix='', is_example_folding=true) %}
100+
{% macro function_display(function, type2href, type2class, gen_path, prefix='', is_example_folding=true) %}
96101
{{ function['details'] | safe }}
97-
{{ function_definition_display(function, type2class, gen_path, prefix) }}
102+
{{ function_definition_display(function, type2href, type2class, gen_path, prefix) }}
98103
{% if function['example'] and is_example_folding %}
99104
<details class="folding-example">
100105
<summary><img src="/assets/icons/16-arrow-right.svg" alt="" width="16" height="16">View example</summary>
@@ -106,5 +111,5 @@
106111
{% if function['example'] and not is_example_folding %}
107112
{{ function['example'] | safe }}
108113
{% endif %}
109-
{{ function_params_display(function, type2class, gen_path, prefix) }}
114+
{{ function_params_display(function, type2href, type2class, gen_path, prefix) }}
110115
{% endmacro %}

templates/type_template.html.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<h2 id="constructor">
1111
{{ macros.tooltip_display('コンストラクタ', 'If a type has a constructor, you can call it like a function to create a new value of the type.', prefix='constructor') }}
1212
</h2>
13-
{{ macros.function_display(body['content']['constructor'], type2class, gen_path, prefix='constructor', is_example_folding=false) }}
13+
{{ macros.function_display(body['content']['constructor'], type2href, type2class, gen_path, prefix='constructor', is_example_folding=false) }}
1414
{% endif %}
1515

1616

@@ -21,7 +21,7 @@
2121
{% endif %}
2222
{% for method in body['content']['scope'] %}
2323
<h3 id="{{ prefix }}-{{ method['name'] }}" class="method-head"><span><code>{{ method['name'] }}</code></span></h3>
24-
{{ macros.function_display(method, type2class, gen_path, prefix='definitions-' + method['name']) }}
24+
{{ macros.function_display(method, type2href, type2class, gen_path, prefix='definitions-' + method['name']) }}
2525
{% endfor %}
2626

2727
{% endblock %}

0 commit comments

Comments
 (0)