bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / utils / bsc_md / indexing.py

indexing.py (2384B)


      1 import os
      2 import re
      3 
      4 from .config import IGNORED_FOLDERS, PROTECTED_INDEX_FILES
      5 
      6 
      7 def _get_md_title(md_path: str) -> str:
      8     try:
      9         with open(md_path, encoding='utf-8') as f:
     10             content = f.read()
     11         # Extracts actual title text, ignoring prepended SVGs or icons
     12         m = re.search(r'^title:\s*\'(?:.*?</svg>\s*)?(.+)\'$', content, re.MULTILINE)
     13         if m:
     14             return m.group(1).strip()
     15     except OSError:
     16         pass
     17     return os.path.splitext(os.path.basename(md_path))[0]
     18 
     19 
     20 def create_folder_indexes(docs_root):
     21     for root, dirs, files in os.walk(docs_root):
     22         dirs[:] = [d for d in dirs if d not in IGNORED_FOLDERS]
     23 
     24         folder_name = os.path.basename(root)
     25 
     26         if folder_name in IGNORED_FOLDERS:
     27             continue
     28 
     29         index_path = os.path.normpath(os.path.join(root, 'index.md'))
     30 
     31         if index_path in PROTECTED_INDEX_FILES and os.path.exists(index_path):
     32             continue
     33 
     34         md_files = sorted(
     35             f for f in files
     36             if f.endswith('.md') and f.lower() not in {'index.md', 'readme.md', 'default.md', 'home.md', 'tags.md'}
     37         )
     38         subdirs = sorted(dirs)
     39 
     40         if root == docs_root:
     41             title = "BSc Code Index"
     42             intro  = "Select a category from the left sidebar or the table below."
     43         else:
     44             title = folder_name.replace('_', ' ').replace('-', ' ').title()
     45             intro  = f"Files and sub-folders in **{title}**."
     46 
     47         with open(index_path, 'w', encoding='utf-8') as f:
     48             f.write(f"# {title}\n\n{intro}\n\n")
     49 
     50             if subdirs:
     51                 f.write("## Folders\n\n")
     52                 f.write("| # | Folder | Link |\n")
     53                 f.write("|---|---|---|\n")
     54                 for idx, d in enumerate(subdirs, 1):
     55                     dir_title = d.replace('_', ' ').replace('-', ' ').title()
     56                     f.write(f"| {idx} | {dir_title} | [Open]({d}/index.md) |\n")
     57                 f.write("\n")
     58 
     59             if md_files:
     60                 f.write("## Files\n\n")
     61                 f.write("| # | File | Link |\n")
     62                 f.write("|---|---|---|\n")
     63                 for idx, md_file in enumerate(md_files, 1):
     64                     src_name = _get_md_title(os.path.join(root, md_file))
     65                     f.write(f"| {idx} | `{src_name}` | [View Code]({md_file}) |\n")
     66                 f.write("\n")
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror