-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebarBuilder.php
102 lines (72 loc) · 3.27 KB
/
sidebarBuilder.php
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
<?php
declare(strict_types=1);
use League\Flysystem\StorageAttributes;
require_once 'vendor/autoload.php';
const LIST_SYMBOL = '*';
$sitemapGen = new \Icamys\SitemapGenerator\SitemapGenerator('https://www.condorcet.io', __DIR__.'/docs/');
$sitemapGen->addURL('/');
$sitemapGen->addURL('/GithubReadme');
$adapter = new League\Flysystem\Local\LocalFilesystemAdapter(__DIR__.'/docs');
$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'))
->sortByPath();
// var_dump($listing->toArray());
$summaryMD = '';
$summaryMD .= "* [**Condorcet - Presentation**](/GithubReadme)\n";
$lastPath = false;
foreach ($listing as $file) {
$fileContent = file_get_contents(__DIR__.'/docs/'.$file->path());
$re = '/#(.*)\n/m';
preg_match_all($re, $fileContent, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
// var_dump($matches);
$firstMatch = reset($matches);
$title = is_array($firstMatch) ? $firstMatch[1] : false;
if (is_string($title)) {
$path = explode(DIRECTORY_SEPARATOR, $file->path());
$fileName = array_pop($path);
$folder = end($path);
$depth = count($path);
$depth < 0 && $depth = 0;
$title = removeIndex(trim($title));
$depth === 0 && $title = '**'.$title.'**';
$title === '**Start**' && $title = '<span class="condorcet_secondary" style="font-weight:700;">**Start**</span>';
if ($folder !== $lastPath && is_string($folder)) {
$lastPath = $folder;
$pathTitle = preg_replace('/([a-z])([A-Z])/m', '$1 $2', $folder);
$pathTitle = removeIndex($pathTitle);
($depth > 0 ? $depth - 1 : 0) === 0 && $pathTitle = '**'.$pathTitle.'**';
count($path) < 2 && $summaryMD .= "\n";
$summaryMD .= str_repeat(' ', $depth > 0 ? $depth - 1 : 0).LIST_SYMBOL." {$pathTitle} \n";
count($path) < 2 && $summaryMD .= "\n";
}
$thePath = !str_contains($file->path(), '1.Start') ? $file->path() : 'README';
$summaryMD .= str_repeat(' ', $depth).LIST_SYMBOL." [{$title}]({$thePath}) \n";
$sitemapGen->addURL(str_replace('.md', '', '/'.$thePath));
} else {
throw new Exception($thePath.' have no title');
}
}
$summaryMD .= "\n";
$summaryMD .= "* [**Voting Methods**](VotingMethods)\n";
$sitemapGen->addURL('/VotingMethods');
$summaryMD .= "* [**API References**](ApiReferences)\n";
$sitemapGen->addURL('/ApiReferences');
$summaryMD .= "* [**Changelog**](Changelog)\n";
$sitemapGen->addURL('/Changelog');
var_dump($summaryMD);
$filesystem->write('_sidebar.md', $summaryMD);
// file_put_contents('docs/404.html', file_get_contents('docs/index.html'));
// Sitemap
$sitemapGen->flush();
$sitemapGen->finalize();
$sitemapGen->updateRobots();
function removeIndex(string $title): string
{
$title = trim($title);
return preg_replace('/^([0-9]\.)/m', '', $title) ?? $title;
}