-
Notifications
You must be signed in to change notification settings - Fork 55
/
createTOC.ps1
278 lines (244 loc) · 13.3 KB
/
createTOC.ps1
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
###Requires -RunAsAdministrator
<#
.SYNOPSIS
Create Table of Content yaml file for each Subfolder in Articles.
.DESCRIPTION
Create Table of Content yaml file for each Subfolder in Articles of current directory. This is for GitHub Pages DOCs creation.
.PARAMETER Source
Source folder location
Note:
It will check if the folder exists.
Example: "C:\AzureStackUpdate"
.EXAMPLE
Set-TableOfContent -Source "C:\Docfx\Articles\"
.EXAMPLE
Set-TableOfContent
.LINK
https://aka.ms/azurestackupdatedownload
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[ValidateScript( { If ((Test-Path $_)) { Test-Path $_ -Verbose } })]
[String]
$Source = "$PWD\articles\"
)
# Install and Import YAML Parser module: powershell-yaml
try {
Import-Module -Name powershell-yaml -ErrorAction "Stop"
}
catch {
try {
Install-Module -Name powershell-yaml -ErrorAction "Stop" -Force
}
catch {
Write-Error -Message $_.Exception.Message
break
}
}
# Declare Articles Folder based on current directory
#$ArticlesFolder = "$PWD\articles\"
$ArticlesFolder = $Source
$FoldersFromBase = Get-ChildItem -Directory -Path $ArticlesFolder
Write-Host "Found $($FoldersFromBase.count) Folders in $($ArticlesFolder)"
# For every folder in Articles folder declare BaseFolder
ForEach ($FolderB in $FoldersFromBase) {
$BaseFolder = "$PWD\articles\$FolderB"
# Filter for only MarkDown files
$FiletypeFilter = "*.md"
$MDArticles = Get-ChildItem -Path $BaseFolder -Filter $FiletypeFilter -Recurse
# Create Array Constructs to create YAML output
$TOCStruct = @()
$TOCArrayProperties = @()
$TOCArrayPropertiesCustom = @()
ForEach ($MDArticle in $MDArticles) {
$TOCProp = $MDArticle | Select-String -Pattern "toc_" | Select-Object -ExpandProperty Line
$TOCArrayProperties += $TOCProp
# Read values from the header of the .md file
$ourObject = [PSCustomObject]@{
Rootlink = ($MDArticle | Select-String -Pattern "toc_rootlink" | Select-Object -First 1 -ExpandProperty Line) -replace "toc_rootlink:\s?", ""
Sub1 = ($MDArticle | Select-String -Pattern "toc_sub1" | Select-Object -First 1 -ExpandProperty Line) -replace "toc_sub1:\s?", ""
Sub2 = ($MDArticle | Select-String -Pattern "toc_sub2" | Select-Object -First 1 -ExpandProperty Line) -replace "toc_sub2:\s?", ""
Sub3 = ($MDArticle | Select-String -Pattern "toc_sub3" | Select-Object -First 1 -ExpandProperty Line) -replace "toc_sub3:\s?", ""
Sub4 = ($MDArticle | Select-String -Pattern "toc_sub4" | Select-Object -First 1 -ExpandProperty Line) -replace "toc_sub4:\s?", ""
Title = ($MDArticle | Select-String -Pattern "toc_title" | Select-Object -First 1 -ExpandProperty Line) -replace "toc_title:\s?", ""
MDlink = ($MDArticle | Select-String -Pattern "toc_mdlink" | Select-Object -First 1 -ExpandProperty Line) -replace "toc_mdlink:\s?", ""
}
$TOCArrayPropertiesCustom += $ourObject
}
# Sort Objects so that Rootlink Users is first shown.
If ($TOCArrayPropertiesCustom.Rootlink -like "Users" -or $TOCArrayPropertiesCustom.Rootlink -like "Operators") {
$SortedCustom = $TOCArrayPropertiesCustom | Sort-Object -Property Rootlink -Descending | Sort-Object -Property Sub1, Sub2, Sub3, Sub4, Title, MDlink
}
Else {
$SortedCustom = $TOCArrayPropertiesCustom | Sort-Object -Property Rootlink -Descending | Sort-Object -Property Rootlink, Sub1, Sub2, Sub3, Sub4, Title, MDlink
}
ForEach ($File in $SortedCustom) {
# Work out if we need to add a new Top Level item
$exists = $false
ForEach ($TOCS in $TOCStruct) {
If ($TOCS.name -eq $($File.Rootlink)) {
$exists = $true
}
}
# Add new top level if necessary
If ($exists -eq $false) {
$TOCStruct += @{
name = $($File.Rootlink)
items = @()
}
}
# Work out Sub1 Level structure
ForEach ($TOCS in $TOCStruct) {
# Identify and use the correct top-level item.
If ($TOCS.name -eq $($File.Rootlink)) {
# Work out if we need to add a new Sub1 level item
$exists = $false
ForEach ($First in $TOCS.items) {
If ($First.name -eq $($File.Sub1)) {
$exists = $true
}
}
# Add new Sub1 level if necessary
If ($exists -eq $false) {
# If sub1 is empty, add this article under rootlink
If (([string]::IsNullOrEmpty($($File.Sub1)))) {
$TOCS.items += [ordered] @{
name = $($File.Title)
href = $($File.MDlink)
}
}
Else {
$TOCS.items += [ordered] @{
name = $($File.Sub1)
items = @()
}
}
}
# We now know we have Rootlink and Sub1 level nodes.
# Add Items or submenus to Sub1 Level structure
ForEach ($TOCSItem in $TOCS.items) {
If ($TOCSItem.Name -eq $($File.Sub1)) {
# If sub2 is empty, add this article under sub1
If (([string]::IsNullOrEmpty($($File.Sub2)))) {
$TOCSItem.items += [ordered] @{
name = $($File.Title)
href = $($File.MDlink)
}
}
# sub2 isn't empty: article sits under sub2 or lower
Else {
# Work out if we need to add a new Sub2 level item
$s2exists = $false
ForEach ($Second in $TOCSItem.items) {
if ($Second.name -eq $($File.Sub2)) {
$s2exists = $true
}
}
# Add new Sub2 level if necessary
If ($s2exists -eq $false) {
$TOCSItem.items += [ordered] @{
name = $($File.Sub2)
items = @()
}
}
# We now know we have Rootlink, Sub1 and Sub2 level nodes.
# Add Items or submenus to Sub2 Level Structure
ForEach ($Lev2Item in $TOCSItem.items) {
If ($Lev2Item.Name -eq $($File.Sub2)) {
# If Sub3 is empty, add this article under Sub2
If (([string]::IsNullOrEmpty($($File.Sub3)))) {
$Lev2Item.items += [ordered] @{
name = $($File.Title)
href = $($File.MDlink)
}
#Write-Host "Sub3 Empty $($File.MDlink)"
}
# Sub3 isn't empty: article sits under Sub3 or lower
Else {
# Work out if we need to add a new Sub3 level item
$s3exists = $false
ForEach ($Third in $Lev2Item.items) {
#ForEach ($Third in $Lev2Item.items.items) {
if ($Third.name -eq $($File.Sub3)) {
#Write-Host "$($Third.Name) vs $($File.Sub3)"
$s3exists = $true
}
}
# Add new Sub3 level if necessary
If ($s3exists -eq $false) {
$Lev2Item.items += [ordered] @{ # !!!!!
#$Lev2Item.items.items += [ordered] @{ # !!!!!
name = $($File.Sub3)
items = @()
}
}
# We now know we have Rootlink, Sub1 and Sub2 and Sub3 level nodes.
# Add Items or submenus to Sub3 Level Structure
#ForEach ($Lev3Item in $Lev2Item.items.items) {
ForEach ($Lev3Item in $Lev2Item.items) {
If ($Lev3Item.Name -eq $($File.Sub3)) {
# If Sub4 is empty, add this article under Sub3
If (([string]::IsNullOrEmpty($($File.Sub4)))) {
$Lev3Item.items += [ordered] @{
name = $($File.Title)
href = $($File.MDlink)
}
#Write-Host "Sub4 Empty $($File.MDlink)"
}
Else {
# Work out if we need to add a new Sub4 level item
$s4exists = $false
#ForEach ($Fourth in $Lev3Item.items.items) {
ForEach ($Fourth in $Lev3Item.items) {
if ($Fourth.name -eq $($File.Sub4)) {
#Write-Host "$($Third.Name) vs $($File.Sub3)"
$s4exists = $true
}
}
# Add new Sub4 level if necessary
If ($s4exists -eq $false) {
#$Lev3Item.items.items += [ordered] @{ # !!!!!
$Lev3Item.items += [ordered] @{ # !!!!!
name = $($File.Sub4)
items = @()
}
}
}
# We now know we have Rootlink, Sub1 and Sub2 and Sub3 level nodes.
# Add Items or submenus to Sub3 Level Structure
#ForEach ($Lev4Item in $Lev3Item.items.items) {
ForEach ($Lev4Item in $Lev3Item.items) {
If ($Lev4Item.Name -eq $($File.Sub4)) {
# If Sub4 is NOT empty, add this article under Sub4
If (-not([string]::IsNullOrEmpty($($File.Sub4)))) {
$Lev4Item.items += [ordered] @{
name = $($File.Title)
href = $($File.MDlink)
}
#Write-Host "Sub4 Empty $($File.MDlink)"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
<#
Bug with Single Item Arrays
https://stackoverflow.com/questions/18662967/convertto-json-an-array-with-a-single-item
We cannot use pipe -> $TOCStruct | ConvertTo-Yaml -OutFile $BaseFolder\toc.yml -Force
We have to use -> ConvertTo-Yaml -Data $TOCStruct -OutFile $BaseFolder\toc.yml -Force
#>
$CaptureYamlOut = ConvertTo-Yaml -Data $TOCStruct
ConvertTo-Yaml -Data $TOCStruct -OutFile $BaseFolder\toc.yml -Force
Write-Host "Created new toc.yml file under $BaseFolder"
Write-Verbose -Message $CaptureYamlOut
}