-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmerge_docs.py
More file actions
75 lines (60 loc) · 2.58 KB
/
merge_docs.py
File metadata and controls
75 lines (60 loc) · 2.58 KB
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
#!/usr/bin/env python3
import json
from pathlib import Path
fname = 'docs.json'
def prepend_prefix(item, prefix):
"""Recursively prepend prefix to pages."""
if isinstance(item, str):
return f"{prefix}/{item}"
if 'pages' in item:
return {**item, 'pages': [prepend_prefix(p, prefix) for p in item['pages']]}
return item
def merge_navigation(main_nav, sub_nav, prefix):
"""Merge sub navigation into main with prefix."""
# Find the anchor that matches this repo
anchor_name_map = {
'nixtla': 'TimeGPT',
'statsforecast': 'StatsForecast',
'mlforecast': 'MLForecast',
'neuralforecast': 'NeuralForecast',
'hierarchicalforecast': 'HierarchicalForecast',
'utilsforecast': 'UtilsForecast',
'datasetsforecast': 'DatasetsForecast',
'coreforecast': 'CoreForecast'
}
anchor_name = anchor_name_map.get(prefix)
if not anchor_name:
return main_nav
# Find the matching anchor in main navigation
anchors = main_nav.get('anchors', [])
target_anchor = None
for anchor in anchors:
if anchor.get('anchor') == anchor_name:
target_anchor = anchor
break
if target_anchor:
# Replace groups and pages from sub navigation into the target anchor
for key in ['groups', 'pages']:
if key in sub_nav:
target_anchor[key] = [prepend_prefix(i, prefix) for i in sub_nav[key]]
# Also update root-level groups if they exist
# Remove old groups for this prefix and add new ones
if 'groups' in main_nav and 'groups' in sub_nav:
# Filter out existing groups for this repo
main_nav['groups'] = [g for g in main_nav['groups']
if not any(isinstance(p, str) and p.startswith(f"{prefix}/")
for p in g.get('pages', []))]
# Add new groups
main_nav['groups'].extend([prepend_prefix(i, prefix) for i in sub_nav['groups']])
return main_nav
repos = ['nixtla', 'statsforecast', 'mlforecast', 'neuralforecast',
'hierarchicalforecast', 'utilsforecast', 'datasetsforecast', 'coreforecast']
main_config = json.loads(Path(fname).read_text())
for repo in repos:
config_path = Path(repo) / fname
if config_path.exists():
sub_config = json.loads(config_path.read_text())
if 'navigation' in sub_config:
main_config.setdefault('navigation', {})
merge_navigation(main_config['navigation'], sub_config['navigation'], repo)
Path(fname).write_text(json.dumps(main_config, indent=2))