-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgenerate_readme.py
executable file
·93 lines (78 loc) · 3.01 KB
/
generate_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
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
#!/bin/env python3
""""This is a script to generate a Markdown table for the README.md file"""
import os
import json
# Load JSON data from file
with open("hyde-themes.json", "r", encoding="utf-8") as file:
data = json.load(file)
# Sort the data list based on theme name
# data.sort(key=lambda theme: theme.get("THEME", "N/A"))
# Sort the data list based on the first element of COLORSCHEME
def hex_to_intensity(hex_color):
"""Convert hex color to intensity"""
hex_color = hex_color.lstrip("#")
r = int(hex_color[0:2], 16)
g = int(hex_color[2:4], 16)
b = int(hex_color[4:6], 16)
return 0.299 * r + 0.587 * g + 0.114 * b
data.sort(
key=lambda theme: hex_to_intensity(
theme.get("COLORSCHEME", ["#000000"])[0]
if isinstance(theme.get("COLORSCHEME"), list)
and len(theme.get("COLORSCHEME")) > 0
else "#000000"
)
)
# Initialize the Markdown table
MD_TABLE = "| Theme | Description | Author |\n"
MD_TABLE += "| --- | --- | --- |\n"
# Iterate through the sorted JSON data and populate the table
for theme in data:
theme_name = theme.get("THEME", "N/A")
description = theme.get("DESCRIPTION", "N/A")
author = theme.get("OWNER", "N/A").split("/")[-1]
link = theme.get("LINK", "#")
colorscheme = theme.get("COLORSCHEME", ["#000000", "#FFFFFF"])
# Generate the image link
BASE_URL = "https://placehold.co/180x50"
color1 = colorscheme[0][1:]
color2 = colorscheme[1][1:]
text = theme_name.replace(" ", "+")
IMAGE_LINK = f"{BASE_URL}/{color1}/{color2}?text={text}&font=Oswald"
# Add the row to the table
MD_TABLE += (
f"| []({link}) | "
f"{description} | "
f"[{author}]({theme.get('OWNER', '#')}) |\n"
)
# Add the footer note and end marker
MD_TABLE += "\n<!-- TABLE_END -->"
# Read the contents of README.md
with open("README.md", "r", encoding="utf-8") as readme_file:
readme_content = readme_file.read()
# Define the markers where the table content will be inserted
START_MARK = "<!-- TABLE_START -->"
END_MARK = "<!-- TABLE_END -->"
# Insert the table content between the markers
if START_MARK in readme_content and END_MARK in readme_content:
before_table = readme_content.split(START_MARK)[0] + START_MARK
after_table = readme_content.split(END_MARK)[1]
updated_readme_content = before_table + "\n" + MD_TABLE + after_table
else:
updated_readme_content = (
readme_content
+ "\n"
+ "# Theme Gallery\n"
+ "<!-- TABLE_START -->\n"
+ MD_TABLE
)
if not os.path.exists("README.md"):
os.chmod("README.md", 0o666)
# Check if the script has write permissions for README.md
if os.access("README.md", os.W_OK):
# Write the updated content back to README.md
with open("README.md", "w", encoding="utf-8") as readme_file:
readme_file.write(updated_readme_content)
print("README.md has been updated with the generated Markdown table.")
else:
print("Permission denied: 'README.md'. Please check the file permissions.")