-
Notifications
You must be signed in to change notification settings - Fork 27
/
انتج-ملف-اقرأني
executable file
·169 lines (138 loc) · 5.83 KB
/
انتج-ملف-اقرأني
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
import re
import os
import time
import math
from tqdm import tqdm
from dotenv import dotenv_values
from notion.client import NotionClient
# fmt: off
__dirname__ = os.path.dirname(__file__)
env = dotenv_values(os.path.join(__dirname__, ".بيئة"))
README_FILE = os.path.join(__dirname__, "README.md")
CACHE_DIR = os.path.join(__dirname__, ".cache")
NOTION_API_VERSION = "2021-05-13"
NOTION_API_URL = "https://api.notion.com/v1"
NOTION_PAGE_ID = "3f677c2054ee49efad36e321cd88e1ac"
notion_client = None
page_block = None
# fmt: on
readme_template = """<div dir=rtl>
![غلاف](./غلاف.png)
> ملف بي.دي.إف منسق الشكل (ينصح به): [./تجميعة.pdf](./تجميعة.pdf)
{المحتوى}
"""
if os.path.isfile(CACHE_DIR):
raise Exception("file exists in the same place of cache dir: " + CACHE_DIR)
if not os.path.isdir(CACHE_DIR):
os.mkdir(CACHE_DIR)
def get_markdown(block, indent="", indent_unit=" ", root=False):
content = ""
block_prefix = "\n\n" + indent
numlist_counter = 0
current_child = None
prev_child = None
next_child = None
current_child_index = 0
progress_len = len(block.children)
progress = tqdm(total=progress_len) if root else None
def consume_child():
nonlocal current_child_index, current_child, next_child, prev_child
if current_child_index > 0:
prev_child = block.children[current_child_index - 1]
else:
prev_child = None
if current_child_index < len(block.children) - 1:
next_child = block.children[current_child_index + 1]
else:
next_child = None
current_child = block.children[current_child_index]
current_child_index += 1
if progress:
progress.update(current_child_index + 1)
return current_child
while current_child_index < progress_len:
child = consume_child()
child_type = child.type # performance considerations
if child_type != "numbered_list":
numlist_counter = 0
if child_type == "text":
content += block_prefix + "" + child.title
elif child_type == "table_of_contents":
content += block_prefix + "<!---toc start-->\n<!---toc end-->"
elif child_type == "divider":
content += block_prefix + "---"
elif child_type == "header":
content += block_prefix + "# " + child.title
elif child_type == "sub_header":
content += block_prefix + "## " + child.title
elif child_type == "sub_sub_header":
content += block_prefix + "### " + child.title
elif child_type == "column_list":
content += "" # TODO: use table in markdown
elif child_type == "quote":
content += block_prefix + "> " + child.title
elif child_type == "bulleted_list":
item_prefix = block_prefix
if prev_child and prev_child.type == "bulleted_list":
item_prefix = f"\n{indent}"
content += f"{item_prefix}- " + child.title
elif child_type == "numbered_list":
numlist_counter += 1
item_prefix = block_prefix
if numlist_counter > 1:
item_prefix = f"\n{indent}"
content += f"{item_prefix}{numlist_counter}. " + child.title
elif child_type == "bookmark":
# remove "Contribute to <repo> development..."
desc = re.sub(
r"\s*Contribute to .*? development by creating an account on GitHub\.", "", child.description)
bookmark = {}
bookmark["link"] = child.link
bookmark["description"] = desc
first_is_latin = re.search(
r"^\s*[a-zA-Z]", bookmark["description"])
direction = "ltr" if first_is_latin else "rtl"
if next_child and next_child.type == "quote":
bookmark["description"] = next_child.title
consume_child()
content += f"{block_prefix}- {bookmark['link']}"
if bookmark['description']:
content += f"{block_prefix} <span dir={direction}>{bookmark['description']}</span>"
if next_child and next_child.type == "bulleted_list":
subcontent = get_markdown(
next_child, indent=indent + indent_unit)
content += subcontent
consume_child()
if child.children:
if child_type == "numbered_list" or child_type == "bulleted_list":
subcontent = get_markdown(
child, indent=indent + indent_unit + " "*(int(math.log10(numlist_counter)) + 1))
subcontent = subcontent[1:] # remove the one of the two "\n"
else:
subcontent = get_markdown(child, indent=indent + indent_unit)
content += subcontent
return content
def get_page_data():
start_time = time.time()
print("📑 جاري احضار البيانات بواسطة مكتبة notionpy...")
page_data = get_page_data_notionpy()
end_time = time.time()
print("✅️ أُحضرت البيانات خلال: ",
"{:2.2}".format(end_time - start_time), "ث")
return page_data
def get_page_data_notionpy():
global notion_client, page_bock
notion_client = notion_client or NotionClient(
token_v2=env["NOTION_COOKIE_TOKEN_V2"]
)
page_block = notion_client.get_block(NOTION_PAGE_ID)
return page_block
if __name__ == "__main__":
page_data = get_page_data()
page_md = get_markdown(page_data, root=True)
readme_content = readme_template.format(المحتوى=page_md)
with open(README_FILE, "w") as f:
f.write(readme_content)
mdtoc_path = os.path.join(__dirname__, "env/bin/mdtoc")
os.system(f"{mdtoc_path} '{README_FILE}'")