-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebarBuilder.php
More file actions
166 lines (132 loc) · 4.9 KB
/
sidebarBuilder.php
File metadata and controls
166 lines (132 loc) · 4.9 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
use League\Flysystem\StorageAttributes;
require_once 'vendor/autoload.php';
const LIST_SYMBOL = '*';
$adapter = new League\Flysystem\Local\LocalFilesystemAdapter(__DIR__ . '/docs/book');
$filesystem = new League\Flysystem\Filesystem($adapter);
$listing = $filesystem->listContents('.', true)
->filter(fn(StorageAttributes $attributes) => $attributes->isFile())
->filter(fn(StorageAttributes $attributes) => str_ends_with($attributes->path(), '.md'))
->filter(fn(StorageAttributes $attributes) => ! str_starts_with($attributes->path(), '_'))
->filter(fn(StorageAttributes $attributes) => ! str_contains($attributes->path(), 'SUMMARY'))
->filter(fn(StorageAttributes $attributes) => ! str_ends_with($attributes->path(), 'index.md'))
->sortByPath();
// Create an array structure for the sidebar
$sidebarItems = [];
// Add the first item - Condorcet Presentation
$sidebarItems[] = [
'text' => 'Complete Readme',
'link' => 'https://github.com/julien-boudry/Condorcet/blob/master/README.md',
];
// Store file information for processing
$fileEntries = [];
// First pass: collect all file information
foreach ($listing as $file) {
$fileContent = file_get_contents(__DIR__ . '/docs/book/' . $file->path());
$re = '/#(.*)\n/m';
preg_match_all($re, $fileContent, $matches, PREG_SET_ORDER, 0);
$firstMatch = reset($matches);
$title = is_array($firstMatch) ? $firstMatch[1] : false;
if (is_string($title)) {
$pathParts = explode(DIRECTORY_SEPARATOR, $file->path());
$fileName = array_pop($pathParts);
$title = removeIndex(trim($title));
$thePath = $file->path();
$thePath = 'book/' . $thePath;
$link = str_replace('.md', '', '/' . $thePath);
$fileEntries[] = [
'path' => $pathParts,
'title' => $title,
'link' => $link,
'fullPath' => $file->path(),
];
} else {
throw new Exception($file->path() . ' has no title');
}
}
// Build a nested structure based on directory hierarchy
$nestedSections = [];
// Process top-level sections first
foreach ($fileEntries as $entry) {
if (count($entry['path']) === 0 && $entry['title'] !== 'Start') {
// Top-level items
$sidebarItems[] = [
'text' => $entry['title'],
'link' => $entry['link'],
];
} elseif (count($entry['path']) > 0) {
// Group by first level directory
$mainSection = $entry['path'][0];
if (!isset($nestedSections[$mainSection])) {
$mainSectionTitle = preg_replace('/([a-z])([A-Z])/m', '$1 $2', $mainSection);
$mainSectionTitle = removeIndex($mainSectionTitle);
$nestedSections[$mainSection] = [
'text' => $mainSectionTitle,
'items' => [],
'subSections' => [],
];
}
// Handle different nesting levels
if (count($entry['path']) === 1) {
// Direct children of main section
$nestedSections[$mainSection]['items'][] = [
'text' => $entry['title'],
'link' => $entry['link'],
];
} elseif (count($entry['path']) > 1) {
// Subsection items
$subSection = $entry['path'][1];
if (!isset($nestedSections[$mainSection]['subSections'][$subSection])) {
$subSectionTitle = preg_replace('/([a-z])([A-Z])/m', '$1 $2', $subSection);
$subSectionTitle = removeIndex($subSectionTitle);
$nestedSections[$mainSection]['subSections'][$subSection] = [
'text' => $subSectionTitle,
'collapsed' => true,
'items' => [],
];
}
$nestedSections[$mainSection]['subSections'][$subSection]['items'][] = [
'text' => $entry['title'],
'link' => $entry['link'],
];
}
}
}
// Build the final structure
foreach ($nestedSections as $section) {
$sectionEntry = [
'text' => $section['text'],
'items' => $section['items'],
];
// Add subsections if they exist
foreach ($section['subSections'] as $subSection) {
$sectionEntry['items'][] = $subSection;
}
$sidebarItems[] = $sectionEntry;
}
// Add the additional static items
$sidebarItems[] = [
'text' => 'Voting Methods',
'link' => '/gh/VotingMethods',
];
$sidebarItems[] = [
'text' => 'API Reference',
'link' => '/api-reference/Index',
];
$sidebarItems[] = [
'text' => 'Changelog',
'link' => '/gh/Changelog',
];
// Convert to JSON
$jsonOutput = json_encode(
value: $sidebarItems,
flags: JSON_PRETTY_PRINT,
);
// Write to file
file_put_contents(__DIR__ . '/docs/.vitepress/sidebar.json', $jsonOutput);
function removeIndex(string $title): string
{
$title = trim($title);
return preg_replace('/^([0-9]\.)/m', '', $title) ?? $title;
}