-
Notifications
You must be signed in to change notification settings - Fork 4
/
build_readme.py
68 lines (51 loc) · 2.31 KB
/
build_readme.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
import os
import re
import urllib.parse
import xml.etree.ElementTree as ET
'''
Script to generate a README file from a list of OPML files.
This is run after and an updated OPML file to update the README file.
'''
def extract_rss_sources(file_path):
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
opml_content = ET.fromstring(content)
opml_items = opml_content.findall(".//outline")
rss_sources = []
for item in opml_items:
title = item.get("title")
xml_url = item.get("xmlUrl")
if title and xml_url:
title = title.replace("|", r"\|")
rss_sources.append((title, xml_url))
return rss_sources
def create_readme(directory, template_file):
with open(template_file, "r") as template:
readme_content = template.read()
for root, dirs, files in os.walk(directory):
if '.git' in dirs:
dirs.remove('.git') # Exclude the .git directory from further processing
if '.github' in dirs:
dirs.remove('.github')
if root != directory:
# Get the relative path for the directory
relative_path = os.path.relpath(root, directory)
readme_content += f"## {relative_path}\n\n"
if files:
for file in files:
file_path = os.path.join(root, file)
file_name = os.path.splitext(file)[0] # Get the file name without extension
rss_sources = extract_rss_sources(file_path)
relative_file_path = os.path.relpath(file_path, directory)
relative_file_path = relative_file_path.replace(" ", "%20")
if rss_sources:
readme_content += f"### [{file_name}]({relative_file_path})\n\n"
readme_content += "| Title | URL |\n"
readme_content += "|-------|-----|\n"
for title, url in rss_sources:
readme_content += f"| {title} | [{url}]({url}) |\n"
with open("README.md", "w") as readme_file:
readme_file.write(readme_content)
directory_to_document = os.path.dirname(__file__)
template_file = "readme_header.md"
create_readme(directory_to_document, template_file)