Skip to content

Commit 0176142

Browse files
Fix function syntax logic
1 parent 14e4a5d commit 0176142

File tree

2 files changed

+159
-72
lines changed

2 files changed

+159
-72
lines changed

web/resources/function.html

Lines changed: 77 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,106 @@
1+
{% set type_colors = {
2+
'shared': '#7575ff',
3+
'client': '#FF0000',
4+
'server': '#FF7F00'
5+
} %}
6+
{% set info_color = '#ccc' %}
7+
18
<!-- Title & Description -->
2-
{% if function.type_name == 'shared' %}
3-
<div class="function-type-title" style="color: #7575ff;">Shared function</div>
4-
<h1 style="border-bottom: 3px solid #7575ff; padding-bottom: 0.1em;">{{ function.name }}</h1>
5-
{{ function['shared'].description_html }}
6-
{% elif function.type_name == 'client' %}
7-
<div class="function-type-title" style="color: #FF0000;">Client-side function</div>
8-
<h1 style="border-bottom: 3px solid #FF0000;">{{ function.name }}</h1>
9-
{{ function['client'].description_html }}
10-
{% elif function.type_name == 'server' %}
11-
<div class="function-type-title" style="color: #FF7F00;">Server-side function</div>
12-
<h1 style="border-bottom: 3px solid #FF7F00;">{{ function.name }}</h1>
13-
{{ function['server'].description_html }}
14-
{% endif %}
9+
<div class="function-type-title" style="color: {{type_colors[function.type_name]}};">{{ function.type_name|capitalize }} function</div>
10+
<h1 style="border-bottom: 3px solid {{type_colors[function.type_name]}}; padding-bottom: 0.1em;">{{ function.name }}</h1>
11+
12+
{{ function[function.type_name].description_html }}
1513

1614
<!-- Syntax -->
1715
<h2>Syntax</h2>
18-
{% for type_name in ['shared', 'client', 'server'] %}
19-
{% if function[type_name] %}
20-
{% if function[type_name].returns %}
21-
{% set return_type = function[type_name].returns["values"][0].type %}
22-
{% else %}
23-
{% set return_type = 'void' %}
16+
{% if function.syntaxes.single %}
17+
{% set syntax = function.syntaxes.single %}
18+
<div style="padding-left: 1em; margin-top: 1em; border: 1px solid {{ info_color }}; border-radius: 5px;">
19+
20+
<pre><code class="language-lua">{{ syntax.returns.values_type or 'void' }} {{ function.name }} ( {% for parameter in syntax.arguments.required %}{{ parameter.type }} {{ parameter.name }}{% if not loop.last %}, {% endif %}{% endfor %}{% for parameter in syntax.arguments.optional %}{% if syntax.arguments.required %}, {% endif %}{% if loop.first %}[ {% endif %}{{ parameter.type }} {{ parameter.name }} = {{ parameter.default }}{% if loop.last %} ]{% endif %}{% endfor %} )</code></pre>
21+
22+
{% if syntax.arguments.required %}
23+
<h3>Required arguments:</h3>
24+
<ul>
25+
{% for item in syntax.arguments.required %}
26+
<li>{{ item.type }} <strong>{{ item.name }}</strong> : {{ item.description_html }}</li>
27+
{% endfor %}
28+
</ul>
29+
{% endif %}
30+
31+
{% if syntax.arguments.optional %}
32+
<h3>Optional arguments:</h3>
33+
<ul>
34+
{% for item in syntax.arguments.optional %}
35+
<li>{{ item.type }} <strong>{{ item.name }}</strong> (default: <em>{{ item.default }}</em>) : {{ item.description_html }}</li>
36+
{% endfor %}
37+
</ul>
38+
{% endif %}
39+
40+
<h3>Returns:</h3>
41+
{% if syntax.returns.description_html %}
42+
<p>{{ syntax.returns.description_html }}</p>
2443
{% endif %}
44+
<ul>
45+
{% for item in syntax.returns['values'] %}
46+
<li>{{ item.type }} {{ item.name }}</li>
47+
{% endfor %}
48+
</ul>
2549

26-
{% if function[type_name].parameters %}
27-
<pre><code class="language-lua">{{ return_type }} {{ function[type_name].name }} ( {% set first_optional = true %}{% for item in function[type_name].parameters %}{% if item.default is defined %}{% if first_optional %}[ {% set first_optional = false %}{% endif %}{{ item.type }} {{ item.name }}{% if item.default is defined %} = {{ item.default }}{% endif %}{% if not loop.last %}, {% endif %}{% if loop.last %} ]{% endif %}{% else %}{{ item.type }} {{ item.name }}{% if not loop.last %}, {% endif %}{% endif %}{% endfor %} )</code></pre>
28-
{% set required_arguments = function[type_name].parameters | selectattr('default', 'equalto', undefined) | list %}
29-
{% set optional_arguments = function[type_name].parameters | selectattr('default', 'ne', undefined) | list %}
50+
</div>
51+
{% else %}
52+
{% for type_name in ['server', 'client'] %}
53+
{% set syntax = function.syntaxes[type_name] %}
54+
{% if syntax %}
55+
<div style="padding-left: 1em; margin-top: 1em; border: 1px solid {{ type_colors[type_name] }}; border-radius: 5px;">
56+
<h3 style="color: {{ type_colors[type_name] }};">{{ type_name|capitalize }}:</h3>
3057

31-
{% if required_arguments %}
32-
<h3>Required arguments:</h3>
58+
<pre><code class="language-lua">{{ syntax.returns.values_type or 'void' }} {{ function.name }} ( {% for parameter in syntax.arguments.required %}{{ parameter.type }} {{ parameter.name }}{% if not loop.last %}, {% endif %}{% endfor %}{% for parameter in syntax.arguments.optional %}{% if syntax.arguments.required %}, {% endif %}{% if loop.first %}[ {% endif %}{{ parameter.type }} {{ parameter.name }} = {{ parameter.default }}{% if loop.last %} ]{% endif %}{% endfor %} )</code></pre>
59+
60+
{% if syntax.arguments.required %}
61+
<h4>Required arguments:</h4>
3362
<ul>
34-
{% for item in required_arguments %}
63+
{% for item in syntax.arguments.required %}
3564
<li>{{ item.type }} <strong>{{ item.name }}</strong> : {{ item.description_html }}</li>
3665
{% endfor %}
3766
</ul>
3867
{% endif %}
3968

40-
{% if optional_arguments %}
41-
<h3>Optional arguments:</h3>
69+
{% if syntax.arguments.optional %}
70+
<h4>Optional arguments:</h4>
4271
<ul>
43-
{% for item in optional_arguments %}
72+
{% for item in syntax.arguments.optional %}
4473
<li>{{ item.type }} <strong>{{ item.name }}</strong> (default: <em>{{ item.default }}</em>) : {{ item.description_html }}</li>
4574
{% endfor %}
4675
</ul>
4776
{% endif %}
48-
{% endif %}
4977

50-
{% endif %}
51-
{% endfor %}
52-
53-
<!-- Returns -->
54-
{% for type_name in ['shared', 'client', 'server'] %}
55-
{% if function[type_name] %}
56-
{% if function[type_name].returns %}
57-
<h2>Returns</h2>
58-
{% if function[type_name].returns.description_html %}
59-
<p>{{ function[type_name].returns.description_html }}</p>
78+
<h4>Returns:</h4>
79+
{% if syntax.returns.description_html %}
80+
<p>{{ syntax.returns.description_html }}</p>
81+
{% endif %}
82+
<ul>
83+
{% for item in syntax.returns['values'] %}
84+
<li>{{ item.type }} {{ item.name }}</li>
85+
{% endfor %}
86+
</ul>
87+
</div>
6088
{% endif %}
61-
<ul>
62-
{% for item in function[type_name].returns["values"] %}
63-
<li>{{ item.type }} {{ item.name }}</li>
64-
{% endfor %}
65-
</ul>
66-
{% endif %}
89+
{% endfor %}
6790
{% endif %}
68-
{% endfor %}
6991

7092
<!-- Preview Images -->
7193
{% if function.has_preview_image %}
72-
<h2>Images</h2>
7394
{% for type_name in ['shared', 'client', 'server'] %}
7495
{% if function[type_name] %}
7596

7697
{% if function[type_name].preview_images %}
77-
<div>
78-
<ul>
98+
<div style="padding-left: 1em; margin-top: 1em; border: 1px solid {{ info_color }}; border-radius: 5px;">
7999
{% for image in function[type_name].preview_images %}
80-
<li>
81-
{% if image.description_html %}
82-
<p>{{ image.description_html }}</p>
83-
{% endif %}
84-
<img style="max-height: 150px;" src="{{ image.path_html }}">
85-
</li>
100+
{% if image.description_html %}
101+
<p>{{ image.description_html }}</p>
102+
{% endif %}
103+
<img style="max-height: 150px;" src="{{ image.path_html }}">
86104
{% endfor %}
87105
</ul>
88106
</div>
@@ -98,7 +116,7 @@ <h2>Issues</h2>
98116
{% if function[type_name] %}
99117

100118
{% if function[type_name].issues %}
101-
<div>
119+
<div style="padding-left: 1em; margin-top: 1em; border: 1px solid {{ info_color }}; border-radius: 5px;">
102120
<ul>
103121
{% for issue in function[type_name].issues %}
104122
<li>
@@ -121,25 +139,15 @@ <h2>Examples</h2>
121139
{% if function[type_name] %}
122140

123141
{% if function[type_name].examples %}
124-
<ul>
125142
{% for example in function[type_name].examples %}
126-
<li><h3>Example {{ example.number }}
127-
{% if type_name == 'client' %}
128-
<span style="color: #FF0000;">(Client-side)</span>
129-
{% elif type_name == 'server' %}
130-
<span style="color: #FF7F00;">(Server-side)</span>
131-
{% else %}
132-
<span style="color: #7575ff;">(Shared)</span>
133-
{% endif %}
134-
</h3></li>
135-
<div style="margin-left: 1em;">
143+
<div style="padding-left: 1em; margin-top: 1em; border: 1px solid {{ type_colors[type_name] }}; border-radius: 5px;">
144+
<h3>Example {{ example.number }} <span style="color: {{ type_colors[type_name] }};">({{ type_name|capitalize }})</span></h3>
136145
{% if example.description_html %}
137146
{{ example.description_html }}
138147
{% endif %}
139148
<pre><code class="language-lua">{{ example.code }}</code></pre>
140149
</div>
141150
{% endfor %}
142-
</ul>
143151
{% endif %}
144152
{% endif %}
145153
{% endfor %}

web/scripts/builder.py

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ def parse_functions(self):
7878
function["type_name"] = function_type_name
7979

8080
function = self.parse_function_examples(function)
81+
8182
function = self.parse_function_preview_images(function)
8283

83-
example_number = 1
84+
# Parse markdown to HTML in function
8485
for type_name in ['shared', 'client', 'server']:
8586
type_info = function.get(type_name, {})
8687
if not type_info:
@@ -91,8 +92,6 @@ def parse_functions(self):
9192

9293
if 'examples' in type_info:
9394
for example in type_info['examples']:
94-
example["number"] = example_number
95-
example_number += 1
9695
if 'description' in example:
9796
example['description_html'] = utils.to_html(example['description'])
9897

@@ -114,6 +113,83 @@ def parse_functions(self):
114113
for parameter in type_info['parameters']:
115114
parameter['description_html'] = utils.to_html(parameter['description'], single_paragraph=True)
116115

116+
# Prepare parameters & returns for syntax display
117+
syntaxes = {
118+
'single': None,
119+
'server': None,
120+
'client': None,
121+
}
122+
123+
parameters = []
124+
returns = None
125+
has_single_syntax = True
126+
127+
if function.get('shared'):
128+
# Function may have different syntax for client/server
129+
last_syntax_type = None
130+
for type_name in ['shared', 'client', 'server']:
131+
type_info = function.get(type_name)
132+
if type_info and (type_info.get('parameters') or type_info.get('returns')):
133+
if last_syntax_type and last_syntax_type != type_name:
134+
has_single_syntax = False
135+
break
136+
last_syntax_type = type_name
137+
else:
138+
has_single_syntax = True
139+
140+
def parse_parameters_and_returns(parameters, returns):
141+
syntax = {
142+
'arguments': {
143+
'required': [],
144+
'optional': []
145+
},
146+
'returns': {
147+
'values_type': None,
148+
'description': None,
149+
'values': []
150+
}
151+
}
152+
for parameter in parameters:
153+
if parameter.get('default'):
154+
syntax['arguments']['optional'].append(parameter)
155+
else:
156+
syntax['arguments']['required'].append(parameter)
157+
if returns:
158+
syntax['returns']['description'] = returns.get('description', None)
159+
for value in returns.get('values'):
160+
syntax['returns']['values'].append(value)
161+
syntax['returns']['values_type'] = syntax['returns']['values'][0].get('type')
162+
163+
return syntax
164+
165+
if has_single_syntax:
166+
# Function has one single syntax defined in shared/client/server
167+
type_info = function.get('shared') or function.get('client') or function.get('server')
168+
parameters = type_info.get('parameters', [])
169+
returns = type_info.get('returns', None)
170+
171+
syntaxes['single'] = parse_parameters_and_returns(parameters, returns)
172+
else:
173+
# Get shared parameters and returns
174+
shared = function.get('shared')
175+
shared_parameters = shared.get('parameters', [])
176+
shared_returns = shared.get('returns', None)
177+
178+
# Get client parameters and returns, complete missing with shared
179+
client = function.get('client')
180+
client_parameters = client.get('parameters') or shared_parameters
181+
client_returns = client.get('returns') or shared_returns
182+
183+
# Get server parameters and returns, complete missing with shared
184+
server = function.get('server')
185+
server_parameters = server.get('parameters') or shared_parameters
186+
server_returns = server.get('returns') or shared_returns
187+
188+
syntaxes['client'] = parse_parameters_and_returns(client_parameters, client_returns)
189+
syntaxes['server'] = parse_parameters_and_returns(server_parameters, server_returns)
190+
191+
function['syntaxes'] = syntaxes
192+
117193
self.functions.append(function)
118194
except Exception as e:
119195
self.logger.exception(e)
@@ -149,6 +225,7 @@ def resolve_relative_or_repo_absolute_path(self, folder, path):
149225

150226
def parse_function_examples(self, function):
151227
examples = {}
228+
example_number = 1
152229
for type_name in ['shared', 'client', 'server']:
153230
type_info = function.get(type_name, {})
154231
if not type_info:
@@ -168,10 +245,12 @@ def parse_function_examples(self, function):
168245
example_code = file.read()
169246

170247
examples.append({
248+
'number': example_number,
171249
'path': example_path,
172250
'description': example.get('description', ''),
173251
'code': example_code
174252
})
253+
example_number += 1
175254
type_info['examples'] = examples
176255

177256
return function

0 commit comments

Comments
 (0)