You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kmftools/delfland/azure devops/toc_gen.py

54 lines
1.7 KiB

import requests
from requests.auth import HTTPBasicAuth
import os
import argparse
# Invoerparameters
parser = argparse.ArgumentParser(
description='Genereer TOC uit Azure DevOps Wiki-structuur')
parser.add_argument('--max-depth', type=int, default=3,
help='Maximale diepte van de TOC (standaard: 3)')
args = parser.parse_args()
# Configuratie
organization = "hhdelfland"
project = "Delfland.EAM_OBS_beheer"
wiki = "Delfland.EAM_OBS_beheer.wiki"
pat = os.getenv(
"AZURE_PAT") or "14S2VfW2iYhpYHC90zL64JHVy9Fst10qIbg2Dw5erzPT3FH8x6J9JQQJ99BEACAAAAA3TYdMAAASAZDO43sH"
url = f"https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/{wiki}/pages?api-version=7.1-preview.1&recursionLevel=full"
auth = HTTPBasicAuth('', pat)
# API-aanroep
response = requests.get(url, auth=auth)
# Functie om TOC te genereren met max diepte
def generate_toc(pages, depth=1, max_depth=3):
if depth > max_depth:
return ""
toc = ""
for page in sorted(pages, key=lambda p: p.get("order", 0)):
title = page["path"].split("/")[-1].replace("-", " ")
link = page["path"].strip("/").replace(" ", "%20") + ".md"
indent = " " * (depth - 1)
toc += f"{indent}- [{title}]({link})\n"
if "subPages" in page and page["subPages"]:
toc += generate_toc(page["subPages"], depth + 1, max_depth)
return toc
# Resultaat tonen
if response.status_code == 200:
data = response.json()
toc = generate_toc(data.get("subPages", []),
depth=5, max_depth=args.max_depth)
print("# Wiki TOC\n")
print(toc)
else:
print("Fout bij ophalen wiki-pagina's:", response.status_code)
print(response.text)