forked from coding-armadillo/kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_docs.py
187 lines (156 loc) · 4.38 KB
/
update_docs.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import json
import pickle
import subprocess
from argparse import ArgumentParser
from collections import Counter
from datetime import date
from pathlib import Path
import matplotlib.pyplot as plt
import requests
import seaborn as sns
import urllib3
from bs4 import BeautifulSoup
from tqdm import tqdm
parser = ArgumentParser(
prog="UpdateDocs",
description="A helper script to update docs",
)
parser.add_argument("-f", "--force", action="store_true")
args, _ = parser.parse_known_args()
force = args.force
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
folders = ["easy", "medium", "hard"]
language_map = {
"py": "Python",
"cpp": "C++",
"java": "Java",
"rs": "Rust",
"go": "Go",
"hs": "Haskell",
"js": "JavaScript",
"kt": "Kotlin",
}
summary = {}
all_files = []
try:
with open("docs/.cache", "rb") as f:
cache = pickle.load(f)
except:
cache = {}
for folder in folders:
uri = f"src/{folder}"
if not Path(uri).exists():
Path(uri).mkdir(exist_ok=True)
files = list(Path(uri).glob("*"))
solutions = {}
for file in files:
if file.stem not in solutions:
solutions[file.stem] = [file.name]
else:
solutions[file.stem].append(file.name)
all_files.append(file.name)
total = len(solutions)
summary[folder] = total
if not force and set(solutions.keys()) < set(cache.keys()):
print(f"⛔ Skipped {uri}: no update needed")
print()
continue
try:
subprocess.call(
[
"black",
uri,
]
)
except:
pass
try:
subprocess.call(
[
"clang-format",
"-i",
f"{Path('src')/folder/'*.cpp'}",
"-style=Microsoft",
]
)
except:
pass
s = "s" if total > 1 else ""
print()
print(f"🔍 Found {total} solution{s} from {uri}")
print()
now = date.today()
text = """---
hide:
- toc
---
"""
text += f"""
# Difficulty - {folder.capitalize()} (as of {now})
"""
prompt = "📖 Refreshing Docs"
for solution, languages in tqdm(sorted(solutions.items()), desc=prompt):
url = f"https://open.kattis.com/problems/{solution}"
if solution in cache:
name = cache[solution]
else:
html = requests.get(url, verify=False)
soup = BeautifulSoup(html.content, "html.parser")
name = soup.find("h1").text
cache[solution] = name
if len(languages) > 1:
suffix = f"s in {len(languages)} languages"
else:
ext = languages[0].split(".")[-1]
suffix = f" in {language_map[ext]}"
card = f"""
## [{name}]({url})
??? success "Solution{suffix}"
"""
for language in sorted(languages):
ext = language.split(".")[-1]
card += f"""
=== "{language_map[ext]}"
```{ext} linenums="1"
--8<-- "{uri}/{language}"
```
"""
text += card
print()
with open(f"docs/{folder}.md", "w", encoding="utf-8") as f:
f.write(text)
with open(".all-contributorsrc") as f:
data = json.load(f)
num_contributors = len(data["contributors"])
with open("docs/index.md", "w") as f:
text = """
<figure markdown>
![Logo](https://open.kattis.com/images/site-logo){ width="100" }
</figure>
# Kattis
## Summary by Difficulty
"""
for folder, total in summary.items():
text += f"""
- [{folder.capitalize()} ^{total}^]({folder}.md)
"""
text += f"""
## Summary by Initial
![summary-by-initial](summary-by-initial.png)
## Summary by Language
![summary-by-language](summary-by-language.png)
---
!!! note ""
Thanks to all {num_contributors} [contributors](https://github.com/coding-armadillo/kattis#contributors-).
"""
f.write(text.lstrip())
with open("docs/.cache", "wb") as f:
pickle.dump(cache, f)
s = dict(Counter([k[0] for k in cache.values()]).most_common())
ax = sns.barplot(x=list(s.keys()), y=list(s.values()))
ax.get_figure().savefig("docs/summary-by-initial.png", bbox_inches="tight")
plt.clf()
s = dict(Counter([language_map[f.split(".")[-1]] for f in all_files]).most_common())
ax = sns.barplot(x=list(s.keys()), y=list(s.values()))
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
ax.get_figure().savefig("docs/summary-by-language.png", bbox_inches="tight")