Skip to content

Commit 06ad65e

Browse files
committed
Improve codegen scripts
1 parent cb3adda commit 06ad65e

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

compiler/api/compiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def get_docstring_arg_type(t: str, is_list: bool = False, is_pyrogram_type: bool
6767
n = len(t) - 1
6868

6969
t = (("e" if is_list else "E") + "ither " if n else "") + ", ".join(
70-
":obj:`{1} <{0}.{1}>`".format(
70+
":obj:`~{}.{}`".format(
7171
"pyrogram.types" if is_pyrogram_type else "pyrogram.api.types",
7272
i.replace("pyrogram.", "")
7373
)
@@ -88,7 +88,7 @@ def get_references(t: str):
8888
n = len(t) - 1
8989

9090
t = ", ".join(
91-
":obj:`{0} <pyrogram.api.functions.{0}>`".format(i)
91+
":obj:`~pyrogram.api.functions.{}`".format(i)
9292
for i in t
9393
)
9494

compiler/docs/compiler.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import ast
2020
import os
21+
import re
2122
import shutil
2223

2324
HOME = "compiler/docs"
@@ -29,8 +30,10 @@
2930
FUNCTIONS_BASE = "functions"
3031
TYPES_BASE = "types"
3132

32-
shutil.rmtree(TYPES_BASE, ignore_errors=True)
33-
shutil.rmtree(FUNCTIONS_BASE, ignore_errors=True)
33+
34+
def snek(s: str):
35+
s = re.sub(r"(.)([A-Z][a-z]+)", r"\1_\2", s)
36+
return re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", s).lower()
3437

3538

3639
def generate(source_path, base):
@@ -50,9 +53,11 @@ def build(path, level=0):
5053
for node in ast.walk(p):
5154
if isinstance(node, ast.ClassDef):
5255
name = node.name
56+
break
57+
else:
58+
continue
5359

54-
# name = "".join([str(j.title()) for j in os.path.splitext(i)[0].split("_")])
55-
full_path = os.path.basename(path) + "/" + name + ".rst"
60+
full_path = os.path.basename(path) + "/" + snek(name).replace("_", "-") + ".rst"
5661

5762
if level:
5863
full_path = base + "/" + full_path
@@ -65,7 +70,7 @@ def build(path, level=0):
6570
title=name,
6671
title_markup="=" * len(name),
6772
full_class_path="pyrogram.api.{}".format(
68-
os.path.splitext(full_path)[0].replace("/", ".")
73+
".".join(full_path.split("/")[:-1]) + "." + name
6974
)
7075
)
7176
)
@@ -82,7 +87,7 @@ def build(path, level=0):
8287
entities = []
8388

8489
for i in v:
85-
entities.append(i)
90+
entities.append(snek(i).replace("_", "-"))
8691

8792
if k != base:
8893
inner_path = base + "/" + k + "/index" + ".rst"
@@ -98,6 +103,7 @@ def build(path, level=0):
98103
with open(DESTINATION + "/" + inner_path, "w", encoding="utf-8") as f:
99104
if k == base:
100105
f.write(":tocdepth: 1\n\n")
106+
k = "Raw " + k
101107

102108
f.write(
103109
toctree.format(
@@ -115,6 +121,8 @@ def start():
115121
global page_template
116122
global toctree
117123

124+
shutil.rmtree(DESTINATION, ignore_errors=True)
125+
118126
with open(HOME + "/template/page.txt", encoding="utf-8") as f:
119127
page_template = f.read()
120128

0 commit comments

Comments
 (0)