mirror of
https://github.com/mustbeperfect/definitive-opensource.git
synced 2026-04-20 03:54:24 +02:00
Functioning generation system
This commit is contained in:
@@ -1,51 +1,102 @@
|
||||
import json
|
||||
|
||||
def load_data():
|
||||
with open("source/data/categories.json", "r", encoding="utf-8") as f:
|
||||
categories_data = json.load(f)
|
||||
with open("source/data/applications.json", "r", encoding="utf-8") as f:
|
||||
applications_data = json.load(f)
|
||||
return categories_data, applications_data
|
||||
def slugify(name):
|
||||
"""
|
||||
Convert a string to an anchor-friendly slug.
|
||||
"""
|
||||
return name.lower().replace(" ", "-").replace("(", "").replace(")", "")
|
||||
|
||||
def generate_markdown(platform_filter=None):
|
||||
categories_data, applications_data = load_data()
|
||||
categories = sorted(categories_data["categories"], key=lambda x: x["Name"].lower())
|
||||
subcategories = sorted(categories_data["subcategories"], key=lambda x: x["Name"].lower())
|
||||
applications = sorted(applications_data["applications"], key=lambda x: x["name"].lower())
|
||||
def extract_repo_path(link):
|
||||
"""
|
||||
Extract the GitHub repository path from the URL.
|
||||
Expects links of the form: https://github.com/username/repo
|
||||
"""
|
||||
parts = link.rstrip("/").split("/")
|
||||
if len(parts) >= 5:
|
||||
return f"{parts[-2]}/{parts[-1]}"
|
||||
return ""
|
||||
|
||||
def generate_contents(platform="all"):
|
||||
# Load categories and applications JSON data.
|
||||
with open("source/data/categories.json", "r", encoding="utf-8") as f:
|
||||
cat_data = json.load(f)
|
||||
with open("source/data/applications.json", "r", encoding="utf-8") as f:
|
||||
app_data = json.load(f)
|
||||
|
||||
markdown_content = []
|
||||
markdown_content.append("## Contents\n")
|
||||
categories = cat_data.get("categories", [])
|
||||
subcategories = cat_data.get("subcategories", [])
|
||||
applications = app_data.get("applications", [])
|
||||
|
||||
category_map = {cat["id"]: cat["Name"] for cat in categories}
|
||||
subcategory_map = {sub["id"]: sub for sub in subcategories}
|
||||
# Build a mapping of parent category id to parent category name.
|
||||
parent_map = {cat["id"]: cat["Name"] for cat in categories}
|
||||
|
||||
categorized_apps = {sub["id"]: [] for sub in subcategories}
|
||||
# Group subcategories by their parent.
|
||||
subcat_by_parent = {}
|
||||
for sub in subcategories:
|
||||
parent = sub.get("parent", "other")
|
||||
subcat_by_parent.setdefault(parent, []).append({
|
||||
"Name": sub["Name"],
|
||||
"id": sub["id"]
|
||||
})
|
||||
# Sort subcategories alphabetically in each parent group.
|
||||
for key in subcat_by_parent:
|
||||
subcat_by_parent[key].sort(key=lambda x: x["Name"].lower())
|
||||
|
||||
# Filter and group applications by their subcategory (identified by the app's "category" field).
|
||||
apps_by_subcat = {}
|
||||
for app in applications:
|
||||
if app["category"] in categorized_apps:
|
||||
if platform_filter:
|
||||
if platform_filter in [p.strip('`').lower() for p in app["platforms"]]:
|
||||
categorized_apps[app["category"]].append(app)
|
||||
else:
|
||||
categorized_apps[app["category"]].append(app)
|
||||
include = False
|
||||
if platform == "all":
|
||||
include = True
|
||||
else:
|
||||
# Normalize platform tags by stripping backticks and comparing in lower-case.
|
||||
app_platforms = [p.strip("`").lower() for p in app.get("platforms", [])]
|
||||
target = platform.lower()
|
||||
if target in app_platforms:
|
||||
include = True
|
||||
# For macos, linux, and windows, also include if "cross" is present.
|
||||
if target in ["macos", "linux", "windows"] and "cross" in app_platforms:
|
||||
include = True
|
||||
if not include:
|
||||
continue
|
||||
|
||||
cat_id = app.get("category", "uncategorized")
|
||||
apps_by_subcat.setdefault(cat_id, []).append(app)
|
||||
|
||||
for cat in categories:
|
||||
cat_id = cat["id"]
|
||||
markdown_content.append(f"# {cat['Name']} - [Go to top](#contents)\n")
|
||||
for sub in subcategories:
|
||||
if sub["parent"] == cat_id and categorized_apps[sub["id"]]:
|
||||
markdown_content.append(f"### {sub['Name']}\n")
|
||||
markdown_content.append("| Name | Description | Platform | Stars |\n| --- | --- | --- | --- |")
|
||||
for app in categorized_apps[sub["id"]]:
|
||||
platforms = " ".join(app["platforms"])
|
||||
stars_badge = f"[-2]}/{app['link'].split('/')[-1]}?style=for-the-badge&label=%20&color=white)"
|
||||
tags = " ".join(app["tags"])
|
||||
markdown_content.append(f"| [{app['name']}]({app['link']}) {tags} | {app['description']} | {platforms} | {stars_badge} |")
|
||||
markdown_content.append("")
|
||||
# Sort applications within each subcategory alphabetically by name.
|
||||
for key in apps_by_subcat:
|
||||
apps_by_subcat[key].sort(key=lambda x: x["name"].lower())
|
||||
|
||||
return "\n".join(markdown_content)
|
||||
# Build Markdown output.
|
||||
md_output = ""
|
||||
# Process parent categories: sort alphabetically (excluding "other", which is added last).
|
||||
parent_items = [(pid, parent_map.get(pid, pid)) for pid in subcat_by_parent if pid != "other"]
|
||||
parent_items.sort(key=lambda x: x[1].lower())
|
||||
if "other" in subcat_by_parent:
|
||||
parent_items.append(("other", "Other"))
|
||||
|
||||
for pid, pname in parent_items:
|
||||
md_output += f"# {pname} - [Go to top](#contents)\n\n"
|
||||
# For each subcategory under the parent category.
|
||||
for sub in subcat_by_parent.get(pid, []):
|
||||
subname = sub["Name"]
|
||||
md_output += f"### {subname}\n\n"
|
||||
md_output += "| Name | Description | Platform | Stars |\n"
|
||||
md_output += "| --- | --- | --- | --- |\n"
|
||||
# List all apps for the given subcategory.
|
||||
apps = apps_by_subcat.get(sub["id"], [])
|
||||
for app in apps:
|
||||
name = app.get("name", "")
|
||||
description = app.get("description", "")
|
||||
link = app.get("link", "#")
|
||||
# Join the platform tags as provided.
|
||||
app_platforms = " ".join(app.get("platforms", []))
|
||||
repo_path = extract_repo_path(link)
|
||||
stars_badge = f"" if repo_path else ""
|
||||
md_output += f"| [{name}]({link}) | {description} | {app_platforms} | {stars_badge} |\n"
|
||||
md_output += "\n"
|
||||
return md_output
|
||||
|
||||
if __name__ == "__main__":
|
||||
markdown_output = generate_markdown()
|
||||
with open("contents.md", "w", encoding="utf-8") as f:
|
||||
f.write(markdown_output)
|
||||
print("Generated contents.md")
|
||||
# For testing, default to 'all' platforms.
|
||||
print(generate_contents("all"))
|
||||
|
||||
@@ -1,35 +1,49 @@
|
||||
import json
|
||||
import os
|
||||
from tableofcontents_generator import generate_table_of_contents
|
||||
from contents_generator import generate_contents
|
||||
|
||||
from tableofcontents_generator import generate_tableofcontents
|
||||
from contents_generator import generate_markdown
|
||||
# List of target platforms
|
||||
platforms = ["all", "windows", "macos", "linux", "selfhost"]
|
||||
|
||||
def load_file(filename):
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
# Map platform to the header file to use
|
||||
header_files = {
|
||||
"all": "source/components/header.md",
|
||||
"windows": "source/components/windowsheader.md",
|
||||
"macos": "source/components/macosheader.md",
|
||||
"linux": "source/components/linuxheader.md",
|
||||
"selfhost": "source/components/selfhostheader.md"
|
||||
}
|
||||
|
||||
def generate_readme(platform="all"):
|
||||
header_map = {
|
||||
"macos": "source/components/macosheader.md",
|
||||
"windows": "source/components/windowsheader.md",
|
||||
"selfhost": "source/components/selfhostheader.md",
|
||||
}
|
||||
def generate_readme_for_platform(platform):
|
||||
content = ""
|
||||
header_file = header_files.get(platform, "source/components/header.md")
|
||||
|
||||
header_file = header_map.get(platform, "source/components/header.md")
|
||||
header = load_file(header_file)
|
||||
tags = load_file("source/components/tags.md")
|
||||
footer = load_file("source/components/footer.md")
|
||||
# Inject header (for 'all', use header.md; for others, use the platform-specific header)
|
||||
with open(header_file, "r", encoding="utf-8") as f:
|
||||
content += f.read() + "\n"
|
||||
|
||||
toc = generate_tableofcontents()
|
||||
content = generate_markdown(platform)
|
||||
# Inject tags.md
|
||||
with open("source/components/tags.md", "r", encoding="utf-8") as f:
|
||||
content += f.read() + "\n"
|
||||
|
||||
readme_content = "\n".join([header, tags, toc, content, footer])
|
||||
# Generate Table of Contents using the imported function
|
||||
toc_md = generate_table_of_contents()
|
||||
content += toc_md + "\n"
|
||||
|
||||
output_file = f"readmes/{platform}.md" if platform != "all" else "source/testing/test.md"
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write(readme_content)
|
||||
print(f"Generated {output_file}")
|
||||
# Generate the actual list of contents (Markdown list) for the given platform
|
||||
contents_md = generate_contents(platform)
|
||||
content += contents_md + "\n"
|
||||
|
||||
# Inject footer.md
|
||||
with open("source/components/footer.md", "r", encoding="utf-8") as f:
|
||||
content += f.read() + "\n"
|
||||
|
||||
# Write output file; for "all" use README.md, otherwise README-<platform>.md
|
||||
output_filename = "source/testing/test.md" if platform == "all" else f"readmes/{platform}.md"
|
||||
with open(output_filename, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
print(f"Generated {output_filename}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for platform in ["all", "windows", "macos", "linux", "selfhost"]:
|
||||
generate_readme(platform)
|
||||
for platform in platforms:
|
||||
generate_readme_for_platform(platform)
|
||||
|
||||
@@ -1,50 +1,67 @@
|
||||
import json
|
||||
|
||||
def load_categories():
|
||||
with open("source/data/categories.json", "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
def slugify(name):
|
||||
#Create an anchor-friendly slug from a string.
|
||||
return name.lower().replace(" ", "-").replace("(", "").replace(")", "")
|
||||
|
||||
def generate_tableofcontents():
|
||||
data = load_categories()
|
||||
categories = sorted(data["categories"], key=lambda x: x["Name"].lower())
|
||||
subcategories = sorted(data["subcategories"], key=lambda x: x["Name"].lower())
|
||||
def generate_table_of_contents():
|
||||
# Load the categories JSON data
|
||||
with open("source/data/categories.json", "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
categories = data.get("categories", [])
|
||||
subcategories = data.get("subcategories", [])
|
||||
|
||||
toc = ["## Contents\n"]
|
||||
# Build the alphabetical list (ignoring parent categories)
|
||||
subcat_names = [sub["Name"] for sub in subcategories]
|
||||
subcat_names.sort(key=lambda x: x.lower())
|
||||
alphabetical_md = ""
|
||||
for name in subcat_names:
|
||||
alphabetical_md += f"- [{name}](#{slugify(name)})\n"
|
||||
|
||||
# Alphabetical section
|
||||
toc.append("<details>\n <summary><b>Alphabetical</b></summary> <br />\n")
|
||||
# Build the categorized list.
|
||||
# Create a mapping from parent id to parent name.
|
||||
parent_map = {cat["id"]: cat["Name"] for cat in categories}
|
||||
# Group subcategories by their parent id.
|
||||
grouped = {}
|
||||
for sub in subcategories:
|
||||
toc.append(f" - [{sub['Name']}](#{sub['id']})")
|
||||
toc.append("</details>\n")
|
||||
parent = sub.get("parent", "other")
|
||||
grouped.setdefault(parent, []).append(sub["Name"])
|
||||
# Sort each group's subcategories alphabetically.
|
||||
for key in grouped:
|
||||
grouped[key].sort(key=lambda x: x.lower())
|
||||
# Sort parent categories (exclude "other", which is appended at the end)
|
||||
parents = [(pid, parent_map.get(pid, "Other")) for pid in grouped if pid != "other"]
|
||||
parents.sort(key=lambda x: x[1].lower())
|
||||
if "other" in grouped:
|
||||
parents.append(("other", "Other"))
|
||||
|
||||
# Categorized section
|
||||
toc.append("<details open>\n <summary><b>Categorized</b></summary> <br />\n")
|
||||
categorized_md_lines = []
|
||||
for pid, pname in parents:
|
||||
categorized_md_lines.append(f"- {pname}")
|
||||
for subname in grouped[pid]:
|
||||
categorized_md_lines.append(f" - [{subname}](#{slugify(subname)})")
|
||||
|
||||
category_map = {cat["id"]: cat["Name"] for cat in categories}
|
||||
organized_subcategories = {}
|
||||
# Append fixed sections at the end of the categorized TOC.
|
||||
fixed_sections = ["Removed Projects", "FAQ", "Honorable Mentions of Closed-Source Software"]
|
||||
for item in fixed_sections:
|
||||
categorized_md_lines.append(f"- [{item}](#{slugify(item)})")
|
||||
|
||||
for sub in subcategories:
|
||||
parent = sub["parent"]
|
||||
if parent not in organized_subcategories:
|
||||
organized_subcategories[parent] = []
|
||||
organized_subcategories[parent].append(sub)
|
||||
categorized_md = "\n".join(categorized_md_lines)
|
||||
|
||||
for cat in categories:
|
||||
cat_id = cat["id"]
|
||||
toc.append(f" - {cat['Name']}")
|
||||
if cat_id in organized_subcategories:
|
||||
for sub in organized_subcategories[cat_id]:
|
||||
toc.append(f" - [{sub['Name']}](#{sub['id']})")
|
||||
|
||||
# Other category at the end
|
||||
if "other" in organized_subcategories:
|
||||
toc.append(" - [Other](#other)")
|
||||
|
||||
toc.append("</details>\n")
|
||||
return "\n".join(toc)
|
||||
toc = f"""## Table of Contents
|
||||
|
||||
<details>
|
||||
<summary><b>Alphabetical</b></summary> <br />
|
||||
{alphabetical_md}
|
||||
</details>
|
||||
|
||||
<details open>
|
||||
<summary><b>Categorized</b></summary> <br />
|
||||
{categorized_md}
|
||||
</details>
|
||||
"""
|
||||
return toc
|
||||
|
||||
if __name__ == "__main__":
|
||||
toc_content = generate_tableofcontents()
|
||||
with open("tableofcontents.md", "w", encoding="utf-8") as f:
|
||||
f.write(toc_content)
|
||||
print("Generated tableofcontents.md")
|
||||
# For testing the TOC generator
|
||||
print(generate_table_of_contents())
|
||||
|
||||
Reference in New Issue
Block a user