Thibaud Colas 1 день тому
батько
коміт
74ebc6cb6f
1 змінених файлів з 28 додано та 28 видалено
  1. 28 28
      .github/api.py

+ 28 - 28
.github/api.py

@@ -16,37 +16,37 @@ import json
 from datetime import datetime, timezone
 from pathlib import Path
 
-API_PATH = '/api/v1/readme.json'
+API_PATH = "/api/v1/readme.json"
 
 
 def parse_line(line: str, category: str) -> dict[str, str]:
     """Parse a single line from the README into a structured dictionary."""
     print(line)
-    name = line.split('](')[0][3:]
-    url = line.split('](')[1].split(')')[0]
-    description = '' if line[-1] == ')' else line.split(') ')[1][2:]
+    name = line.split("](")[0][3:]
+    url = line.split("](")[1].split(")")[0]
+    description = "" if line[-1] == ")" else line.split(") ")[1][2:]
 
     return {
-        'name': name,
-        'description': description,
-        'url': url,
-        'category': category,
+        "name": name,
+        "description": description,
+        "url": url,
+        "category": category,
     }
 
 
-def parse_section(section: str, category: str = '') -> list[dict[str, str]]:
+def parse_section(section: str, category: str = "") -> list[dict[str, str]]:
     """Parse a section of lines into a list of structured items."""
-    return [parse_line(line, category) for line in section.split('\n')]
+    return [parse_line(line, category) for line in section.split("\n")]
 
 
 def parse_subsections(section: str) -> list[dict[str, str]]:
     """Parse a section containing multiple subsections."""
-    subsections = section.split('### ')[1:]
+    subsections = section.split("### ")[1:]
 
     items = []
 
     for subsection in subsections:
-        split_section = subsection.split('\n\n')
+        split_section = subsection.split("\n\n")
         section_title = split_section[0]
 
         items += parse_section(split_section[1], section_title)
@@ -56,44 +56,44 @@ def parse_subsections(section: str) -> list[dict[str, str]]:
 
 def cut_section(readme: str, start: str) -> str:
     """Extract a specific section from the README."""
-    return readme.split(f'## {start}\n\n')[1].split('\n\n## ')[0]
+    return readme.split(f"## {start}\n\n")[1].split("\n\n## ")[0]
 
 
 def parse_readme(readme: str) -> dict:
     """Parse the entire README into a structured dictionary."""
     return {
-        'apps': parse_subsections(cut_section(readme, 'Apps')),
-        'tools': parse_subsections(cut_section(readme, 'Tools')),
-        'resources': parse_subsections(cut_section(readme, 'Resources')),
-        'sites': parse_section(cut_section(readme, 'Open-source sites')),
-        'metadata': {
-            'updated': datetime.now(timezone.utc).isoformat(),
+        "apps": parse_subsections(cut_section(readme, "Apps")),
+        "tools": parse_subsections(cut_section(readme, "Tools")),
+        "resources": parse_subsections(cut_section(readme, "Resources")),
+        "sites": parse_section(cut_section(readme, "Open-source sites")),
+        "metadata": {
+            "updated": datetime.now(timezone.utc).isoformat(),
         },
     }
 
 
-if __name__ == '__main__':
-    readme_path = Path('README.md')
+if __name__ == "__main__":
+    readme_path = Path("README.md")
 
     try:
-        readme = readme_path.read_text(encoding='utf-8')
+        readme = readme_path.read_text(encoding="utf-8")
         parsed_readme = parse_readme(readme)
 
-        json_path = Path(f'./dist{API_PATH}')
+        json_path = Path(f"./dist{API_PATH}")
         json_path.parent.mkdir(parents=True, exist_ok=True)
 
         readme_payload = json.dumps(parsed_readme, indent=2, ensure_ascii=False)
         print(readme_payload)
 
-        json_path.write_text(readme_payload, encoding='utf-8')
+        json_path.write_text(readme_payload, encoding="utf-8")
 
     except FileNotFoundError as e:
-        print(f'Error: Could not find file - {e}')
+        print(f"Error: Could not find file - {e}")
         raise
     except (KeyError, IndexError) as e:
-        print(f'Error: README formatting issue - {e}')
-        print('Is the README well formatted?')
+        print(f"Error: README formatting issue - {e}")
+        print("Is the README well formatted?")
         raise
     except Exception as e:
-        print(f'Unexpected error: {e}')
+        print(f"Unexpected error: {e}")
         raise