CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/link_readme.py
Views: 1400
#!/usr/bin/env python1# -*- coding: utf-8 -*-23# Requirement:4# python3 -m pip install lxml56import re7from time import sleep8from urllib.request import urlopen9from urllib.error import HTTPError10from lxml.html import parse1112footer_delimiter = "\n\n[comment]: # (LINK_LIST_BEGIN_HERE)\n"13footer = ""14linked_id = []15present_id = []1617def add_bracket(match):18first_char = match.group(1)19id = match.group(2)20replace = first_char + "[#"+id+"]"21return replace2223def add_link(match):24id = match.group(1)25present_id.append(id)26replace = "#" + id27if id in linked_id:28return replace2930url = "https://github.com/hrydgard/ppsspp/issues/"+id31title = None32while title is None:33try:34p = parse(urlopen(url))35title = p.find(".//title").text.split('by')[0].split('·')[0].strip()36title = re.sub(r"\"", r'\\"', title)37except HTTPError:38print("Something went wrong, retrying in 10 sec...")39sleep(10)4041global footer42addition = "[#"+id+"]: https://github.com/hrydgard/ppsspp/issues/"+id+" \""+title+"\""43footer += addition+"\n"44linked_id.append(id)45print("Linked: " + addition)46return replace4748def already_added_id(match):49linked_id.append(match.group(1))50return "[#" + match.group(1) + "]:"5152def remove_old_link(line):53# Ignore extra new lines at the end54if line.find("#") == -1:55return ""5657id = line[line.find("[#")+2 : line.find("]:")]58if id in present_id:59return line + "\n"60else:61print("Removed: #" + id)62return ""6364def update(file_name):65global footer66footer = ""67global linked_id68linked_id = []69global present_id70present_id = []7172f = open(file_name, "r+")73cont = f.read()7475# We don't want to match issues id in title so stop before the link list76d = cont.find(footer_delimiter)77if (d != -1):78footer = cont[d + len(footer_delimiter):]79cont = cont[0 : d]80re.sub(r"\[#(\d+)\]:", already_added_id, footer)81if footer[-1] != "\n":82footer += "\n"8384# Add brackets if missing85added_bracket = re.sub(r"([^[])#(\d+)", add_bracket, cont)8687# Add links if missing88updated = re.sub(r"#(\d+)", add_link, added_bracket)8990# Remove old unused link91updated_footer = ""92for line in footer.split("\n"):93updated_footer += remove_old_link(line)9495# Remove extra new lines at the end96while updated_footer[-1] == "\n":97updated_footer = updated_footer[0:-1]9899f.seek(0)100f.write(updated)101f.write(footer_delimiter)102f.write(updated_footer)103f.truncate()104f.close()105106update("README.md")107update("history.md")108109110