-
Notifications
You must be signed in to change notification settings - Fork 214
/
generate-readme.php
176 lines (136 loc) · 5.53 KB
/
generate-readme.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
<?php
/**
* Generates a tree structure of directories and subdirectories.The createTree function takes two arguments: $directoryPath and $directoryRegex.
* @param string $directoryPath represents the path of the directory for which the tree structure is generated.
* @param string $directoryRegex is an optional argument that defines the regular expression pattern used to include directories in the tree structure (default value is /^task-/)
* @return string The tree structure as a string.
*/
function createTree_v1($directoryPath = './', $directoryRegex = '/^task_/')
{
$tree = [];
$directoriesArray = array_filter(
glob($directoryPath . '/*'),
'is_dir'
);
sort($directoriesArray);
foreach ($directoriesArray as $directory) {
$dirName = trim(
basename($directory)
);
if (preg_match($directoryRegex, $dirName)) {
$realPath = realpath($directory);
$relativePath = substr($realPath, strpos($realPath, 'home'));
$pathParts = explode(DIRECTORY_SEPARATOR, $relativePath);
$parentDir = $pathParts[count($pathParts) - 2];
if (!isset($tree[$parentDir])) {
$tree[$parentDir] = [];
}
$tree[$parentDir][] = "$dirName $relativePath";
}
if (!empty(glob("$directory/*"))) {
$tree = array_merge($tree, createTree_v1($directory));
}
}
return $tree;
}
function createIndividualSectionsMarkdown($tree)
{
foreach ($tree as $key => $value) {
$markdown = '';
preg_match('/home\/.*?(?=\/task_)/', $value[0], $matches);
$readMeFileDir = $matches[0];
$readMeFileDir = substr($readMeFileDir, 0, strrpos($readMeFileDir, '/'));
$markdown .= "## $key\n\n> Auto generated ReadMe\n\n";
$markdown .= "| Task | Description |\n";
$markdown .= "| --- | --- |\n";
foreach ($value as $task) {
$taskNumber = substr($task, strpos($task, 'task_'), 8);
$taskNumber = str_replace('-', '', $taskNumber);
$columnLink=explode(' ', $task);
$columnText=$columnLink[0];
$columnLink = explode('/', $columnLink[1]);
$lastElement=end($columnLink);
$secondLastElement = $columnLink[count($columnLink) - 2];
$columnLink = "$secondLastElement/$lastElement";
$markdown .= "| $taskNumber | [$columnText]($columnLink) |\n";
}
$markdown .= "\n";
file_put_contents($readMeFileDir . '/ReadMe.md', $markdown);
}
}
function createGlobalMarkdownTable($tree) {
$markdown = "# Home \n\n> Auto generated ReadMe\n\n";
// ls -ltrh home | egrep -v "total" | awk '{print "\"" $9 "\","}' | sort
$topics = array(
"cloud_certifications",
"cloud_providers",
"containers",
"databases",
"infrastructure_as_code",
"interview",
"observability",
"os_and_concepts",
"productivity_tools",
"web_servers",
);
foreach ($topics as $topic) {
$view_text=$topic;
$view_text = str_replace('_', ' ', $view_text);
$view_text = ucwords($view_text);
$topic = ucwords($topic);
$markdown .= "- [$view_text](#$topic)\n";
}
$markdown .= "\n";
foreach ($topics as $topic) {
$matchingKeys = array_filter(
array_keys($tree),
function ($key) use ($topic) {
return strpos($key, $topic) !== false;
}
);
if (empty($matchingKeys)) {
continue;
}
$topic = ucwords($topic);
$markdown .= "## $topic\n\n";
$markdown .= "|";
foreach ($matchingKeys as $matchingKey) {
$matchingKey = explode('_', $matchingKey);
$matchingKey = array_slice($matchingKey, 1);
$matchingKey = implode('_', $matchingKey);
// remove _$topic from $matchingKey, make $topc lowercase before replacing
$topic = strtolower($topic);
$matchingKey = str_replace("_$topic", '', $matchingKey);
$matchingKey = ucwords($matchingKey);
$markdown .= " $matchingKey |";
}
$markdown .= "\n";
$markdown .= "|";
foreach ($matchingKeys as $matchingKey) {
$markdown .= " --- |";
}
$markdown .= "\n";
$markdown .= "|";
foreach ($matchingKeys as $matchingKey) {
preg_match('/home\/.*?(?=\/task_)/', $tree[$matchingKey][0], $matches);
$readMeFileDir = $matches[0];
$readMeFileDir = substr($readMeFileDir, 0, strrpos($readMeFileDir, '/'));
$linkToDir = $readMeFileDir;
$markdown .= " [Practice Tasks]($linkToDir) |";
}
$markdown .= "\n\n";
// All caps certain words in markdown like AWS, GCP, OS etc
$markdown = str_replace('Aws', 'AWS', $markdown);
$markdown = str_replace('Oci', 'OCI', $markdown);
$markdown = str_replace('Gcp', 'GCP', $markdown);
$markdown = str_replace('Os', 'OS', $markdown);
}
return $markdown;
}
$tree = createTree_v1('.', '/^task_/'); // if first call is for ".", second call is for "./home" and so on as the function is recursive
// print_r($tree);
createIndividualSectionsMarkdown($tree);
$markdown=createGlobalMarkdownTable($tree);
// Put markdown in README-test.md file
file_put_contents('ReadMe.md', $markdown);
?>