Skip to content

Commit 18d26f0

Browse files
committed
Added content builder
1 parent 90bf06b commit 18d26f0

File tree

6 files changed

+631
-0
lines changed

6 files changed

+631
-0
lines changed

.gitignore

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/

build.py

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import re
2+
import json
3+
from pathlib import Path
4+
from builder import *
5+
6+
# Functions
7+
8+
def persian (text):
9+
text = str(text)
10+
for x, y in zip("1234567890", "۱۲۳۴۵۶۷۸۹۰"):
11+
text = text.replace(x, y)
12+
return text
13+
14+
# Metadata
15+
16+
with open("metadata.json", encoding = "utf8") as handler:
17+
metadata = json.load(handler)
18+
19+
series = {
20+
item["id"]: item
21+
for item in metadata["series"]
22+
}
23+
24+
root = Path("problems")
25+
root.mkdir(parents = True, exist_ok = True)
26+
27+
# Problems
28+
29+
class Problem:
30+
def __init__ (self, path):
31+
if isinstance(path, Path):
32+
self.path = path
33+
else:
34+
self.path = Path(path)
35+
36+
if not self.path.exists():
37+
raise FileNotFoundError(path)
38+
39+
self.entries = {}
40+
41+
def parse (self):
42+
with path.open("r", encoding = "utf8") as handler:
43+
content = handler.read()
44+
45+
self.entries = re.findall(r'\[\_metadata\_\:(.*?)\]\:\-\s+\"(.*?)\"', content)
46+
self.entries = dict(self.entries)
47+
48+
return self.entries
49+
50+
def rename (self):
51+
if not self.entries:
52+
self.parse()
53+
name = entries["id"] + ".md"
54+
self.path.rename(path.parent / name)
55+
self.path = path.parent / name
56+
57+
58+
paths = sorted(root.glob("*.md"))
59+
60+
problems = {}
61+
62+
for path in paths:
63+
if path.name == "README.md":
64+
continue
65+
66+
problem = Problem(path)
67+
entries = problem.parse()
68+
69+
if entries["series"] not in problems:
70+
problems[entries["series"]] = []
71+
problems[entries["series"]].append(entries)
72+
73+
problem.rename()
74+
75+
# README.md
76+
77+
LEVELS = {
78+
"easy": "ساده",
79+
"medium": "متوسط",
80+
"hard": "سخت"
81+
}
82+
83+
output = ""
84+
85+
output += Heading("بانک سؤالات").render() + "\n\n"
86+
87+
output += Heading("بررسی اجمالی", 2).render() + "\n\n"
88+
89+
table = Table()
90+
91+
table.add_row(
92+
[
93+
"شماره",
94+
"نوع",
95+
"عنوان",
96+
"ساده",
97+
"متوسط",
98+
"سخت",
99+
"مجموع",
100+
""
101+
],
102+
header = True
103+
)
104+
105+
for i, (id, data) in enumerate(series.items()):
106+
table.add_row(
107+
[
108+
persian(i + 1),
109+
"سری" if data["type"] == "series" else "بسته جبرانی",
110+
data["title"]["fa"],
111+
persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "easy"]))),
112+
persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "medium"]))),
113+
persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "hard"]))),
114+
persian(str(len(problems.get(data["id"], [])))),
115+
Link("مشاهده", "#" + data["id"]).render()
116+
]
117+
)
118+
119+
output += table.render()
120+
121+
output += "\n\n"
122+
123+
for id, data in series.items():
124+
mode = "سری" if data["type"] == "series" else "بسته جبرانی"
125+
126+
problems_count = sum(
127+
len(y) for x, y in problems.items()
128+
)
129+
130+
output += Heading(
131+
persian(
132+
mode + " " + str(data["number"]) + ": " + data["title"]["fa"]
133+
),
134+
2,
135+
{
136+
"id": data["id"]
137+
}
138+
).render()
139+
140+
output += "\n\n"
141+
142+
prbs = problems.get(data["id"], [])
143+
144+
if prbs:
145+
easy = persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "easy"])))
146+
medium = persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "medium"])))
147+
hard = persian(str(len([x for x in problems.get(data["id"], []) if x["level"] == "hard"])))
148+
149+
output += f"برای این {mode}، {easy} سؤال ساده، {medium} سؤال متوسط و {hard} سؤال سخت و مجموعاً " + persian(len(prbs)) + f" سؤال طرح شده است."
150+
output += "\n\n"
151+
table = Table()
152+
table.add_row(
153+
[
154+
"شماره",
155+
"عنوان",
156+
"سطح",
157+
"طراح",
158+
""
159+
],
160+
header = True
161+
)
162+
163+
def get_level(level):
164+
if level == "easy":
165+
return 0
166+
if level == "medium":
167+
return 1
168+
return 2
169+
170+
prbs = sorted(
171+
sorted(
172+
sorted(
173+
prbs,
174+
key = lambda x: x["id"]
175+
),
176+
key = lambda x: x["author"]
177+
),
178+
key = lambda x: get_level(x["level"])
179+
)
180+
181+
for i, entries in enumerate(prbs):
182+
table.add_row(
183+
[
184+
persian(i + 1),
185+
entries["title"],
186+
LEVELS[entries["level"]],
187+
entries["author"],
188+
Link("مشاهده", entries["id"] + ".md").render()
189+
]
190+
)
191+
192+
output += table.render()
193+
else:
194+
output += f"تا حالا هیچ سؤالی برای این {mode} طرح نشده است."
195+
196+
with (root / "README.md").open("w", encoding = "utf-8") as handler:
197+
handler.write(output)

builder/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
3+
"""HTML Builder.
4+
5+
Copyright (c) 2023-2024 by Kazem Forghani. All rights reserved.
6+
"""
7+
8+
__author__ = "Kazem Forghani"
9+
__copyright__ = "Copyright 2023-2024, Kazem Forghani"
10+
__credits__ = ["Kazem Forghani"]
11+
__license__ = "CC0-1.0"
12+
__version__ = "1.0.0"
13+
__maintainer__ = "Kazem Forghani"
14+
__status__ = "Production"
15+
16+
from .components import *

0 commit comments

Comments
 (0)