-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone-documentation.php
206 lines (186 loc) · 6.18 KB
/
clone-documentation.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
// Variables
$githubKey = getenv("GITHUB_KEY");
if ($githubKey === false) {
echo "The GITHUB_KEY environment variable is not defined.";
$githubKey = "XXX";
}
$repoURL = "https://[email protected]/dunglas/frankenphp.git";
// Constants
const DOCS_TO_CLONE = "docs";
const DESTINATION_DIRECTORY = __DIR__ . "/content";
const DOCS_DESTINATION = DESTINATION_DIRECTORY . "/docs";
const NAV_DESTINATION = __DIR__ . "/data/nav.yaml";
const TEMP_DIR = __DIR__ . "/temp-documentation";
// Regex for performing transformations
const LINKS_REGEX = '/\[([^\]]+)\]\(([^)]+)\)/';
// Function to transform the navigation from markdown to yaml
function markdownToYaml($markdownText)
{
$links = [];
preg_match_all(LINKS_REGEX, $markdownText, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$title = $match[1];
$url = $match[2];
$links[] = ["title" => $title, "url" => $url];
}
$yamlOutput = "links:\n";
foreach ($links as $link) {
$yamlOutput .= " - title: {$link['title']}\n url: {$link['url']}\n";
}
return $yamlOutput;
}
// Function to delete a directory and its contents
function deleteDirectory($dir)
{
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}
// Function to copy a directory
function copyDirectory($source, $dest)
{
if (!is_dir($source))
return false;
if (!is_dir($dest) && !mkdir($dest, 0755, true))
return false;
$dir = dir($source);
while (false !== $entry = $dir->read()) {
if ($entry == '.' || $entry == '..')
continue;
$sourcePath = "$source/$entry";
$destPath = "$dest/$entry";
if (is_dir($sourcePath)) {
if (!copyDirectory($sourcePath, $destPath))
return false;
} elseif (!copy($sourcePath, $destPath)) {
return false;
}
}
$dir->close();
return true;
}
// Function to delete ".md" at the end of the links and add /docs prefix when necessary
function fixLinks($content)
{
// Replacement function
$content = preg_replace_callback(LINKS_REGEX, function ($matches) {
$textLink = $matches[1];
$url = $matches[2];
if (preg_match('/^docs/', $url)) {
$url = preg_replace('/^docs/', '/docs', $url);
$url = str_replace('.md', '', $url);
if (substr($url, -1) !== '/') {
$url .= '/';
}
}
// Check if the URL does not start with "http"
elseif (!preg_match('/^http/', $url)) {
$url = str_replace('.md', '', $url);
if (strpos($url, '/') === 0) {
$url = "/docs" . $url;
} else {
$url = "/docs/" . $url;
}
if (substr($url, -1) !== '/') {
$url .= '/';
}
}
$url = str_replace('docs/CONTRIBUTING', 'docs/contributing', $url);
// Rebuild the link with the new path
return "[$textLink]($url)";
}, $content);
// Write the modified content to the file
return $content;
}
// Function to add layout and title on docs pages
function addFrontmatter($content)
{
if (preg_match('/#\s+([^\n]+)/', $content, $matches)) {
$navtitle = $matches[1];
if (str_starts_with(strtolower($matches[1]), 'frankenphp')) $title = $matches[1];
else $title = "FrankenPHP | " . $matches[1];
} else {
$navtitle = "";
$title = "FrankenPHP";
}
$content = "---\nlayout: docs\ntitle: \"$title\"\nnav: \"$navtitle\"\n---\n$content";
return $content;
}
// Delete the temporary directory if it exists
if (file_exists(TEMP_DIR)) {
$success = deleteDirectory(TEMP_DIR);
if (!$success) {
echo "Error when deleting the temporary directory\n";
return;
}
}
// Clone the repository
$cloneCommand = "git clone " . $repoURL . " " . TEMP_DIR;
exec($cloneCommand, $output, $returnCode);
if ($returnCode !== 0) {
echo "Error during repository cloning\n";
return;
}
// Copy the necessary files
if (file_exists(DOCS_DESTINATION)) {
$success = deleteDirectory(DOCS_DESTINATION);
if (!$success) {
echo "Error when deleting the destination directory\n";
return;
}
}
$success = copyDirectory(TEMP_DIR . '/' . DOCS_TO_CLONE, DOCS_DESTINATION);
if (!$success) {
echo "Error while copying the doc files\n";
return;
}
$success = copy(TEMP_DIR . "/CONTRIBUTING.md", DOCS_DESTINATION . "/contributing.md");
if (!$success) {
echo "Error when copying CONTRIBUTING.md\n";
return;
}
// Modify README.md
copy(TEMP_DIR . "/README.md", DESTINATION_DIRECTORY . "/README.md");
$content = file_get_contents(DESTINATION_DIRECTORY . "/README.md");
$content = preg_replace('/src="((?!http)[^"]*)"/', 'src="https://raw.githubusercontent.com/dunglas/frankenphp/main/$1"', $content);
file_put_contents(DESTINATION_DIRECTORY . "/README.md", $content);
rename(DESTINATION_DIRECTORY . "/README.md", DOCS_DESTINATION . "/_index.md");
// Modify .md files in the docs directory
$files = scandir(DOCS_DESTINATION);
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === "md") {
$filePath = DOCS_DESTINATION . "/" . $file;
$content = file_get_contents($filePath);
$content = addFrontmatter($content);
$content = fixLinks($content);
file_put_contents($filePath, $content);
}
}
// Extract content between "## Docs" and "##" sections of README.md
$content = file_get_contents(TEMP_DIR . "/README.md");
$start = strpos($content, "## Docs");
$end = strpos($content, "##", $start + 1);
$navContent = substr($content, $start, $end - $start);
$navContent = str_replace("##", "", $navContent);
$navContent = fixLinks($navContent);
$yamlOutput = markdownToYaml($navContent);
file_put_contents(NAV_DESTINATION, $yamlOutput);
// Delete the temporary directory
$success = deleteDirectory(TEMP_DIR);
if (!$success) {
echo "Error when deleting the temporary directory\n";
}
?>