-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_docs.py
More file actions
97 lines (76 loc) · 1.96 KB
/
update_docs.py
File metadata and controls
97 lines (76 loc) · 1.96 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
import json
import pickle
import subprocess
import sys
from argparse import ArgumentParser
from pathlib import Path
import requests
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
try:
with open("docs/.cache", "rb") as f:
cache = pickle.load(f)
except:
cache = {}
files = list(Path("src").glob("id*.py"))
solutions = {}
problem_ids = sorted([int(file.stem[2:]) for file in files])
if not force and set(solutions.keys()) < set(cache.keys()):
print(f"⛔ Skipped: no update needed")
sys.exit()
try:
subprocess.call(
[
"black",
".",
]
)
except:
pass
prompt = "📖 Refreshing Docs"
text = """---
hide:
- toc
---
<figure markdown>
{ width="100" }
</figure>
# Project Euler
Solutions to [Project Euler](https://projecteuler.net)
"""
for problem_id in tqdm(problem_ids, prompt):
url = f"https://projecteuler.net/problem={problem_id}"
if problem_id in cache:
name = cache[problem_id]
else:
html = requests.get(url)
soup = BeautifulSoup(html.content, "html.parser")
name = soup.find("h2").text
cache[problem_id] = name
text += f"""
## Problem {problem_id} - [{name}]({url})
??? success "Solution"
=== "Python"
```py linenums="1"
--8<-- "src/id{problem_id}.py"
```
"""
with open(".all-contributorsrc") as f:
data = json.load(f)
num_contributors = len(data["contributors"])
text += f"""
---
!!! note ""
Thanks to all {num_contributors} [contributors](https://github.com/coding-armadillo/project-euler#contributors-).
"""
with open("docs/index.md", "w") as f:
f.write(text)
with open("docs/.cache", "wb") as f:
pickle.dump(cache, f)