Skip to content

Commit 828f0e5

Browse files
Function page wip
1 parent 1087697 commit 828f0e5

File tree

3 files changed

+89
-76
lines changed

3 files changed

+89
-76
lines changed

web/resources/function.html

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,43 @@
22
{% if function.type_name == 'shared' %}
33
<div class="function-type-title" style="color: #7575ff;">Shared function</div>
44
<h1 style="border-bottom: 3px solid #7575ff; margin-bottom: 0.1em;">{{ function.name }}</h1>
5-
<p style="margin-top: 1em;">{{ function['shared'].description }}</p>
5+
<p style="margin-top: 1em;">{{ function['shared'].description_html }}</p>
66
{% elif function.type_name == 'client' %}
77
<div class="function-type-title" style="color: #FF0000;">Client-side function</div>
88
<h1 style="border-bottom: 3px solid #FF0000; margin-bottom: 0.1em;">{{ function.name }}</h1>
9-
<p style="margin-top: 1em;">{{ function['client'].description }}</p>
9+
<p style="margin-top: 1em;">{{ function['client'].description_html }}</p>
1010
{% elif function.type_name == 'server' %}
1111
<div class="function-type-title" style="color: #FF7F00;">Server-side function</div>
1212
<h1 style="border-bottom: 3px solid #FF7F00; margin-bottom: 0.1em;">{{ function.name }}</h1>
13-
<p style="margin-top: 1em;">{{ function['server'].description }}</p>
13+
<p style="margin-top: 1em;">{{ function['server'].description_html }}</p>
1414
{% endif %}
1515

1616
<!-- Syntax -->
17+
<h2>Syntax</h2>
1718
{% for type_name in ['shared', 'client', 'server'] %}
1819
{% if function[type_name] %}
1920
{% if function[type_name].parameters %}
20-
<h2>Syntax</h2>
21-
<p>{{ function[type_name].parameters.description }}</p>
22-
23-
<pre><code class="language-lua"> {{ function[type_name].name }}(todo :mreow:)</code></pre>
24-
21+
<pre><code class="language-lua">{{ function[type_name].name }}( {% for item in function[type_name].parameters %}{{ item.type }} {{ item.name }}, {% endfor %} )</code></pre>
2522
<ul>
26-
{% for item in function[type_name].parameters %}
27-
<li style="margin-bottom: 1em;">
28-
<p>{{ item.name }} : {{ item.description }}</p>
29-
</li>
30-
{% endfor %}
23+
{% for item in function[type_name].parameters %}
24+
<li>{{ item.type }} <strong>{{ item.name }}</strong> : {{ item.description_html }}</li>
25+
{% endfor %}
3126
</ul>
3227
{% endif %}
28+
3329
{% endif %}
3430
{% endfor %}
3531

36-
<!-- Returns -->
3732
{% for type_name in ['shared', 'client', 'server'] %}
3833
{% if function[type_name] %}
3934
{% if function[type_name].returns %}
4035
<h2>Returns</h2>
41-
<p>{{ function[type_name].returns.description }}</p>
36+
{% if function[type_name].returns.description_html %}
37+
<p>{{ function[type_name].returns.description_html }}</p>
38+
{% endif %}
4239
<ul>
4340
{% for item in function[type_name].returns["values"] %}
44-
<li style="margin-bottom: 1em;">
45-
<p>{{ item.type }} {{ item.name }}</p>
46-
</li>
41+
<li style="margin-bottom: 1em;">{{ item.type }} {{ item.name }}</li>
4742
{% endfor %}
4843
</ul>
4944
{% endif %}
@@ -76,9 +71,9 @@ <h2>Issues</h2>
7671
<ul>
7772
{% for issue in function[type_name].issues %}
7873
<li>
79-
<a target="_blank" href="https://github.com/multitheftauto/mtasa-blue/issues/{{ issue.id }}">
74+
<p><a target="_blank" href="https://github.com/multitheftauto/mtasa-blue/issues/{{ issue.id }}">
8075
mtasa-blue #{{ issue.id }}
81-
</a>: {{ issue.description }}
76+
</a>: {{ issue.description_html }}</p>
8277
</li>
8378
{% endfor %}
8479
</ul>
@@ -95,27 +90,25 @@ <h2>Examples</h2>
9590
{% if function[type_name] %}
9691

9792
{% if function[type_name].examples %}
98-
{% if type_name == 'client' %}
99-
<h3 style="color: #FF0000;">Client-side:</h3>
100-
<div style="border: 1px solid #FF0000; padding: 1em; margin-bottom: 0;">
101-
{% elif type_name == 'server' %}
102-
<h3 style="color: #FF7F00;">Server-side:</h3>
103-
<div style="border: 1px solid #FF7F00; padding: 1em; margin-bottom: 0;">
104-
{% else %}
105-
<h3 style="color: #7575ff;">Shared:</h3>
106-
<div style="border: 1px solid #7575ff; padding: 1em; margin-bottom: 0;">
107-
{% endif %}
108-
<ol>
109-
{% for example in function[type_name].examples %}
110-
<li style="margin-bottom: 1em;">
111-
{% if example.description %}
112-
<p>{{ example.description }}</p>
113-
{% endif %}
114-
<pre><code class="language-lua">{{ example.code }}</code></pre>
115-
</li>
116-
{% endfor %}
117-
</ol>
118-
</div>
93+
<ul>
94+
{% for example in function[type_name].examples %}
95+
<li><h3>Example {{ example.number }}
96+
{% if type_name == 'client' %}
97+
<span style="color: #FF0000;">(Client-side)</span>
98+
{% elif type_name == 'server' %}
99+
<span style="color: #FF7F00;">(Server-side)</span>
100+
{% else %}
101+
<span style="color: #7575ff;">(Shared)</span>
102+
{% endif %}
103+
</h3></li>
104+
<div style="margin-left: 1em;">
105+
{% if example.description_html %}
106+
{{ example.description_html }}
107+
{% endif %}
108+
<pre><code class="language-lua">{{ example.code }}</code></pre>
109+
</div>
110+
{% endfor %}
111+
</ul>
119112
{% endif %}
120113
{% endif %}
121114
{% endfor %}

web/scripts/builder.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import signal
88
from datetime import date
99
from pathlib import Path
10-
import markdown
1110

1211
import scripts.utils as utils
1312

@@ -67,48 +66,52 @@ def parse_functions(self):
6766
try:
6867
function = utils.load_and_validate_yaml(file_path, self.schema_function)
6968
if function:
70-
self.remove_function_repeated_defs(function)
69+
function = self.remove_function_repeated_defs(function)
7170

7271
function['real_path'] = file_path
7372
# Get name of parent folder
7473
function["folder"] = os.path.basename(os.path.dirname(file_path))
74+
75+
function_name = self.get_function_name(function)
76+
function["name"] = function_name
77+
function_type_name = self.get_function_type_name(function)
78+
function["type_name"] = function_type_name
79+
80+
function = self.parse_function_examples(function)
81+
function = self.parse_function_preview_images(function)
7582

83+
example_number = 1
7684
for type_name in ['shared', 'client', 'server']:
7785
type_info = function.get(type_name, {})
7886
if not type_info:
7987
continue
88+
89+
if 'description' in type_info:
90+
type_info['description_html'] = utils.to_html(type_info['description'])
91+
8092
if 'examples' in type_info:
81-
function["has_example"] = True
8293
for example in type_info['examples']:
94+
example["number"] = example_number
95+
example_number += 1
8396
if 'description' in example:
84-
example['description_html'] = markdown.markdown(example['description'])
97+
example['description_html'] = utils.to_html(example['description'])
8598

8699
if 'issues' in type_info:
87100
function["has_issue"] = True
88101
for issue in type_info['issues']:
89-
issue['description_html'] = markdown.markdown(issue['description'])
90-
91-
if 'description' in type_info:
92-
type_info['description_html'] = markdown.markdown(type_info['description'])
102+
issue['description_html'] = utils.to_html(issue['description'], single_paragraph=True)
93103

94104
if ('returns' in type_info) and ('description' in type_info['returns']):
95-
type_info['returns']['description_html'] = markdown.markdown(type_info['returns']['description'])
105+
type_info['returns']['description_html'] = utils.to_html(type_info['returns']['description'], single_paragraph=True)
96106

97107
if 'parameters' in type_info:
98108
for parameter in type_info['parameters']:
99-
parameter['description_html'] = markdown.markdown(parameter['description'])
100-
101-
function_name = self.get_function_name(function)
102-
function["name"] = function_name
103-
function_type_name = self.get_function_type_name(function)
104-
function["type_name"] = function_type_name
105-
106-
self.parse_function_examples(function)
107-
self.parse_function_preview_images(function)
109+
parameter['description_html'] = utils.to_html(parameter['description'], single_paragraph=True)
108110

109111
self.functions.append(function)
110112
except Exception as e:
111-
raise WikiBuilderError(f'Error loading function {file_path}: {e}')
113+
self.logger.exception(e)
114+
raise WikiBuilderError(f'Error loading function {file_path}')
112115

113116
def get_function_type(self, function):
114117
return function.get('shared') or function.get('client') or function.get('server')
@@ -122,16 +125,16 @@ def get_function_name(self, function):
122125
def remove_function_repeated_defs(self, function):
123126
# If a function is shared, remove client/server definitions that are the same as the shared one
124127
shared = function.get('shared')
125-
if not shared:
126-
return
127-
128-
for type_name in ['client', 'server']:
129-
type_info = function.get(type_name)
130-
if not type_info:
131-
continue
132-
for key in shared.keys():
133-
if key in type_info and shared[key] == type_info[key]:
134-
del type_info[key]
128+
if shared:
129+
for type_name in ['client', 'server']:
130+
type_info = function.get(type_name)
131+
if not type_info:
132+
continue
133+
for key in shared.keys():
134+
if key in type_info and shared[key] == type_info[key]:
135+
del type_info[key]
136+
137+
return function
135138

136139
def resolve_relative_or_repo_absolute_path(self, folder, path):
137140
if path.startswith('/'):
@@ -144,8 +147,12 @@ def parse_function_examples(self, function):
144147
type_info = function.get(type_name, {})
145148
if not type_info:
146149
continue
147-
examples[type_name] = []
148-
for example in type_info.get('examples', []):
150+
type_examples = type_info.get('examples')
151+
if not type_examples:
152+
continue
153+
function["has_example"] = True
154+
examples = []
155+
for example in type_examples:
149156
example_path = example.get('path')
150157
real_path = self.resolve_relative_or_repo_absolute_path(os.path.dirname(function.get('real_path')), example_path)
151158
if not os.path.exists(real_path):
@@ -154,12 +161,14 @@ def parse_function_examples(self, function):
154161
with open(real_path, 'r') as file:
155162
example_code = file.read()
156163

157-
examples[type_name].append({
164+
examples.append({
158165
'path': example_path,
159-
'description': example.get('description'),
166+
'description': example.get('description', ''),
160167
'code': example_code
161168
})
162-
type_info['examples'] = examples[type_name]
169+
type_info['examples'] = examples
170+
171+
return function
163172

164173
def parse_function_preview_images(self, function):
165174
preview_images = {}
@@ -195,6 +204,8 @@ def parse_function_preview_images(self, function):
195204
})
196205
type_info['preview_images'] = preview_images[type_name]
197206

207+
return function
208+
198209
def render_page(self, title, content):
199210
return self.layout_template.render(
200211
wiki_version = self.wiki_version,
@@ -243,7 +254,7 @@ def create_article(self, article_name, articles_folder='', custom_web_path=False
243254
article['content'] = content_file.read()
244255

245256
article_template = self.input_env.get_template('article.html')
246-
article["content_html"] = markdown.markdown(article['content'])
257+
article["content_html"] = utils.to_html(article['content'])
247258
html_content = self.render_page(
248259
article['title'],
249260
article_template.render(article=article)

web/scripts/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import yaml
22
from jsonschema import validate, ValidationError
3+
import markdown
4+
import re
35

46
def load_schema(schema_path):
57
with open(schema_path, 'r') as file:
@@ -11,3 +13,10 @@ def load_and_validate_yaml(file_path, schema):
1113
data = yaml.safe_load(file)
1214
validate(instance=data, schema=schema)
1315
return data
16+
17+
def to_html(markdown_text, single_paragraph=False):
18+
html = markdown.markdown(markdown_text)
19+
if single_paragraph:
20+
# Remove <p> tags
21+
html = re.sub(r'<p>(.*?)</p>', r'\1', html)
22+
return html

0 commit comments

Comments
 (0)