CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/link_readme.py
Views: 1400
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
# Requirement:
5
# python3 -m pip install lxml
6
7
import re
8
from time import sleep
9
from urllib.request import urlopen
10
from urllib.error import HTTPError
11
from lxml.html import parse
12
13
footer_delimiter = "\n\n[comment]: # (LINK_LIST_BEGIN_HERE)\n"
14
footer = ""
15
linked_id = []
16
present_id = []
17
18
def add_bracket(match):
19
first_char = match.group(1)
20
id = match.group(2)
21
replace = first_char + "[#"+id+"]"
22
return replace
23
24
def add_link(match):
25
id = match.group(1)
26
present_id.append(id)
27
replace = "#" + id
28
if id in linked_id:
29
return replace
30
31
url = "https://github.com/hrydgard/ppsspp/issues/"+id
32
title = None
33
while title is None:
34
try:
35
p = parse(urlopen(url))
36
title = p.find(".//title").text.split('by')[0].split('·')[0].strip()
37
title = re.sub(r"\"", r'\\"', title)
38
except HTTPError:
39
print("Something went wrong, retrying in 10 sec...")
40
sleep(10)
41
42
global footer
43
addition = "[#"+id+"]: https://github.com/hrydgard/ppsspp/issues/"+id+" \""+title+"\""
44
footer += addition+"\n"
45
linked_id.append(id)
46
print("Linked: " + addition)
47
return replace
48
49
def already_added_id(match):
50
linked_id.append(match.group(1))
51
return "[#" + match.group(1) + "]:"
52
53
def remove_old_link(line):
54
# Ignore extra new lines at the end
55
if line.find("#") == -1:
56
return ""
57
58
id = line[line.find("[#")+2 : line.find("]:")]
59
if id in present_id:
60
return line + "\n"
61
else:
62
print("Removed: #" + id)
63
return ""
64
65
def update(file_name):
66
global footer
67
footer = ""
68
global linked_id
69
linked_id = []
70
global present_id
71
present_id = []
72
73
f = open(file_name, "r+")
74
cont = f.read()
75
76
# We don't want to match issues id in title so stop before the link list
77
d = cont.find(footer_delimiter)
78
if (d != -1):
79
footer = cont[d + len(footer_delimiter):]
80
cont = cont[0 : d]
81
re.sub(r"\[#(\d+)\]:", already_added_id, footer)
82
if footer[-1] != "\n":
83
footer += "\n"
84
85
# Add brackets if missing
86
added_bracket = re.sub(r"([^[])#(\d+)", add_bracket, cont)
87
88
# Add links if missing
89
updated = re.sub(r"#(\d+)", add_link, added_bracket)
90
91
# Remove old unused link
92
updated_footer = ""
93
for line in footer.split("\n"):
94
updated_footer += remove_old_link(line)
95
96
# Remove extra new lines at the end
97
while updated_footer[-1] == "\n":
98
updated_footer = updated_footer[0:-1]
99
100
f.seek(0)
101
f.write(updated)
102
f.write(footer_delimiter)
103
f.write(updated_footer)
104
f.truncate()
105
f.close()
106
107
update("README.md")
108
update("history.md")
109
110