-
Notifications
You must be signed in to change notification settings - Fork 122
/
FromYmlToTOC-INDEX-MAP.ps1
476 lines (391 loc) · 15.2 KB
/
FromYmlToTOC-INDEX-MAP.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
[cmdletbinding()]
param (
[string]$directory = (Get-Location)
)
function Get-DocumentMetadata {
param (
[System.IO.FileInfo] $file,
[string] $relativepath
)
$properties = @{
'Title' = ''
'UID' = ''
'RelativePath' = $relativepath.TrimStart('/')
'Parent' = $file.DirectoryName
'FileName' = $file.Name
'tagline' = ''
'imageAlt' = ''
'imageSrc' = ''
'twitter' = ''
'location' = ''
'display' = ''
'lat' = ''
'long' = ''
}
$metadata = New-Object -TypeName PSObject -Prop $properties
$metadata.PSObject.TypeNames.Insert(0,'DocFX.DocumentMetadata')
if ($file.Extension -eq '.yml') {
if($file.Name -eq 'index.yml')
{
$script:indexTitle = Get-IndexTitle -file $file
}
$title = Get-YamlProp -file $file -propName 'name'
$metadata.Title = if ($title) { $title }
$uid = Get-YamlProp -file $file -propName 'uid'
$metadata.UID = if ($uid) { $uid }
$metadata.tagline = Get-YamlProp -file $file -propName 'tagline'
#$metadata.twitter = Get-YamlProp -file $file -propName 'twitter'
#$metadata.location = Get-YamlProp -file $file -propName 'location'
$metadata.imageAlt = Get-YamlProp -file $file -propName ' alt'
$metadata.imageSrc = Get-YamlProp -file $file -propName ' src'
$metadata.display = Get-YamlProp -file $file -propName ' display'
$metadata.lat = Get-YamlProp -file $file -propName ' lat'
$metadata.long = Get-YamlProp -file $file -propName ' long'
return $metadata
}
if ($file.Extension -eq '.md') {
#$title = Get-MarkdownMetadata -file $file
#$metadata.Title = if ($title) { $title } else { $file.Name }
if($file.Name -eq 'index.md')
{
$script:indexTitle = Get-IndexTitle -file $file
}
$title = Get-YamlProp -file $file -propName 'name'
$metadata.Title = if ($title) { $title }
return $metadata
}
}
function Get-IndexTitle {
param (
[System.IO.FileInfo] $file
)
$title = ([regex]'^title\:.+')
# Look for the metadata.name property.
foreach ($linegroup in (Get-Content $file.FullName -ReadCount 1000)) {
if ($title.Match($linegroup).Success) {
return $title.Match($linegroup).Groups[0].Value.Replace('title:', '').TrimStart(' ')
}
}
}
function Get-YamlName {
param (
[System.IO.FileInfo] $file
)
$name = ([regex]'^name\:.+')
# Look for the metadata.name property.
foreach ($linegroup in (Get-Content $file.FullName -ReadCount 1000)) {
if ($name.Match($linegroup).Success) {
return $name.Match($linegroup).Groups[0].Value.Replace('name:', '').TrimStart(' ')
}
}
}
function Get-YamlUID {
param (
[System.IO.FileInfo] $file
)
$uid = ([regex]'^uid\:.+')
# Look for the metadata.title property.
foreach ($linegroup in (Get-Content $file.FullName -ReadCount 1000)) {
if ($uid.Match($linegroup).Success) {
return $linegroup.Replace('uid:', '').TrimStart(' ')
}
}
}
function Get-YamlProp {
param (
[System.IO.FileInfo] $file,
[System.String] $propName
)
$propRegex = ([regex]'^{$propName}\:.+')
# Look for the metadata.title property.
foreach ($linegroup in (Get-Content -Encoding UTF8 $file.FullName -ReadCount 1000)) {
#if ($propRegex.Match($linegroup).Success) {
if ($linegroup -match '^'+$propName+'\:.+')
{
return $linegroup.Replace($propName+':', '').TrimStart(' ')
}
}
return "";
}
function Remove-RootPath {
param (
[string] $rootpath,
[string] $fullpath
)
return $fullpath.Replace($rootpath, '').Replace([IO.Path]::DirectorySeparatorChar, '/')
}
function Check-GlobMatch {
param (
[string[]] $patterns,
[string] $matchpath
)
foreach ($pattern in $patterns) {
$pattern_regex = ($pattern -split '\*\*' | ForEach-Object { $_.Replace('*', '[^\/]+') }) -join '.*'
$match = $matchpath -match $pattern_regex
Write-Verbose "Path $matchpath matches $pattern_regex : $match"
if($match -eq $true)
{
return $match
}
}
return $false
}
function Format-Yaml {
param (
$object
)
foreach ($item in $object) {
$depth = $item.Name.Split('/').Length
$index = $item.Group | Where-Object { $_.FileName -match "index.*" } | Select-Object -First 1
$children = $item.Group | Where-Object { -Not ($_.FileName -match "index.*") }
$indent = ' ' * $depth
$startobject = (' ' * ($depth - 1)) + '- '
$uid = if ($index) { $index.UID } else { $item.Name.Split('/') | Select-Object -Last 1 }
$name = $startobject + 'name: Regional Directors' #+ $indexTitle
$href = if ($index.RelativePath) { $indent, 'href: ', $index.RelativePath -join '' } else { "" }
$expanded = $indent + 'expanded: true'
$items = $indent + 'items: '
if (([array]$children).Length -gt 0) {
$contents = @(
($name, $href, $expanded, $items | Where-Object { $_.Length -gt 0 }) -join [Environment]::NewLine
)
$indent = ' ' * ($depth + 1)
$startobject = (' ' * ($depth)) + '- '
$contents += $children `
| ForEach-Object {
$name = $startobject + 'name: ' + $_.Title
$uid = $indent + 'uid: ' + $_.UID
return $name, $uid -join [Environment]::NewLine
}
} else {
$contents = @(
($name, $uid | Where-Object { $_.Length -gt 0 }) -join [Environment]::NewLine
)
}
$contents += '# Add more to the list. Be careful to preserve spaces and hyphen format.'
return $contents
}
}
function Format-Index-Yaml {
param (
$object
)
foreach ($item in $object) {
$depth = $item.Name.Split('/').Length
$index = $item.Group | Where-Object { $_.FileName -match "index.*" } | Select-Object -First 1
$children = $item.Group | Where-Object { -Not ($_.FileName -match "index.*") }
$indent = ' ' * $depth
$startobject = (' ' * ($depth - 1)) + '- '
$uid = if ($index) { $index.UID } else { $item.Name.Split('/') | Select-Object -Last 1 }
if (([array]$children).Length -gt 0) {
$indent = ' ' * ($depth + 1)
$startobject = ('' * ($depth)) + '- '
$IndexFilecontent = $children `
| ForEach-Object {
$uidVal = $_.UID
IF([string]::IsNullOrEmpty($uidVal))
{
$uid = ''
}
else
{
$uid = $startobject + 'uid: ' + $_.UID
}
$nameVal = $_.Title
IF([string]::IsNullOrEmpty($nameVal))
{
$name = ''
}
else
{
$name = $indent + 'name: ' + $_.Title
}
$taglineVal = $_.tagline
IF([string]::IsNullOrEmpty($taglineVal))
{
$tagline = ''
}
else
{
$tagline = $indent + 'tagline: ' + $_.tagline
}
### Image properties
$imageSrcVal = $_.imageSrc
IF([string]::IsNullOrEmpty($imageSrcVal))
{
$imageSrc = ''
}
else
{
$imageSrc = $indent + ' src: ' + $_.imageSrc
}
$imageAltVal = $_.imageAlt
IF([string]::IsNullOrEmpty($imageAltVal))
{
$imageAlt = ''
}
else
{
$imageAlt = $indent + ' alt: ' + $_.imageAlt
}
IF([string]::IsNullOrEmpty($imageAltVal) -and [string]::IsNullOrEmpty($imageSrcVal))
{
$image = ''
}
else
{
$image = $indent + 'image:'
}
### Location properties
$displayVal = $_.display
IF([string]::IsNullOrEmpty($displayVal))
{
$display = ''
}
else
{
$display = $indent + ' display: ' + $_.display
}
$latVal = $_.lat
IF([string]::IsNullOrEmpty($latVal))
{
$lat = ''
}
else
{
$lat = $indent + ' lat: ' + $_.lat
}
$longVal = $_.long
IF([string]::IsNullOrEmpty($longVal))
{
$long = ''
}
else
{
$long = $indent + ' long: ' + $_.long
}
IF([string]::IsNullOrEmpty($displayVal) -and [string]::IsNullOrEmpty($latVal) -and [string]::IsNullOrEmpty($longVal))
{
$location = $null
}
else
{
$location = $indent + 'location: '
}
#$twitterval = $_.twitter
#IF([string]::IsNullOrEmpty($twitterval))
#{
# $twitter = ''
#}
#else
#{
# $twitter = $indent + 'twitter: ' + $_.twitter.Replace('https://twitter.com/', '')
#}
return ($uid , $name, $tagline, $image, $imageSrc, $imageAlt, $location, $display, $lat, $long | Where-Object { $_.Length -gt 0 } )-join [Environment]::NewLine
}
} else {
Write-Host "No children found"
}
return $IndexFilecontent
}
}
function Format-Markdown {
param (
$object
)
$object.
$heading = '#' * $object.RelativePath.Split('/').Length
return $heading + " [" + $object.Title + "](" + $object.RelativePath + ")"
}
# Set location to directory path
$directory = [System.IO.Path]::GetFullPath(($directory))
$opc_path = [System.IO.Path]::GetFullPath((Join-Path $directory '.openpublishing.publish.config.json'))
Write-Verbose "Working directory is $opc_path"
# Read .openpublishing.publish.config.json (opconfig)
# Look at opconfig's "docsets_to_publish" array. Each item's "build_source_folder" tells us where a docfx.json, toc.md/yml and content is located.
$opc_json = Get-Content -Raw $opc_path | Out-String | ConvertFrom-Json
$source_folders = $opc_json.docsets_to_publish.build_source_folder
$indexTitle = ''
# Read the docfx.json file's "build.content" array. This tells us the glob patterns to use to locate content, and which content to exclude.
foreach ($source_folder in $source_folders) {
Write-Verbose "Finding docfx.json in source folder: $source_folder"
$docfx_dir = [System.IO.Path]::GetFullPath((Join-Path $directory $source_folder))
$docfx_path = [System.IO.Path]::GetFullPath((Join-Path $docfx_dir 'docfx.json'))
$toc_path = [System.IO.Path]::GetFullPath((Join-Path $docfx_dir 'toc.yml'))
$index_path = [System.IO.Path]::GetFullPath((Join-Path $docfx_dir 'index.yml'))
$map_path = [System.IO.Path]::GetFullPath((Join-Path $docfx_dir 'map.yml'))
Write-Verbose "Found docfx json: $docfx_path"
# Use the globs to locate the files on disk, and build the TOC structure, serializing to md or yml.
# TOC should go in top-level output folder from 'docsets_to_publish' array, it seems.
$docfx_json = Get-Content -Raw $docfx_path | Out-String | ConvertFrom-Json
$includes = $docfx_json.build.content.files
$excludes = $docfx_json.build.content.exclude
$excludes = "**/toc.*", "**/map.*", "**/tweets.*" # Exclude TOC, map and tweets files.
#using @' '@ to avoid new line formatting notation
$content = @'
metadata:
name: advocates_toc
items:
'@
$IndexFilecontent = @'
### YamlMime:ProfileList
title: Microsoft Regional Directors
description:
Trusted advisors to the developer and IT professional audiences and Microsoft.
focalImage:
src: ./media/RD-header-image.png
alt: ""
metadata:
title: Microsoft Regional Directors
description: Trusted advisors to the developer and IT professional audiences and Microsoft.
hide_bc: true
bannerLinks:
- text: About
url: ./about/index.md
- text: Code of Conduct
url: ./CodeOfConduct/index.md
filterText: Regional Directors
profiles:
'@
$MapFilecontent = @'
### YamlMime:ProfileList
title: Microsoft Regional Directors
description:
Trusted advisors to the developer and IT professional audiences and Microsoft.
focalImage:
src: ./media/RD-header-image.png
alt: ""
metadata:
title: Microsoft Regional Directors
description: Trusted advisors to the developer and IT professional audiences and Microsoft.
hide_bc: true
mode: map
bannerLinks:
- text: Map View
url: ./map.yml
- text: About
url: ./about/index.md
- text: Code of Conduct
url: ./CodeOfConduct/index.md
filterText: Regional Directors
profiles:
'@
$objects = Get-ChildItem -Path $docfx_dir -Recurse -File `
| Where-Object { (-Not (Check-GlobMatch -patterns $excludes -matchpath ( Remove-RootPath -rootpath $docfx_dir -fullpath $_.FullName ))) -and (Check-GlobMatch -patterns $includes -matchpath ( Remove-RootPath -rootpath $docfx_dir -fullpath $_.FullName )) } `
| Sort-Object FullName `
| ForEach-Object { Get-DocumentMetadata -file $_ -relativepath ( Remove-RootPath -rootpath $docfx_dir -fullpath $_.FullName ) } `
| Group-Object { Remove-RootPath -rootpath $docfx_dir -fullpath $_.Parent } `
| Sort-Object Name `
# writing to TOC file
ForEach-Object -Begin { return $content } -Process { Format-Yaml -object $objects } `
| Out-File -filepath $toc_path
# writing to Index file
ForEach-Object -Begin { return $IndexFilecontent } -Process { Format-Index-Yaml -object $objects } `
| Out-File -filepath $index_path
# writing to map file
# ForEach-Object -Begin { return $MapFilecontent } -Process { Format-Index-Yaml -object $objects } `
# | Out-File -filepath $map_path
Write-Verbose "Generated table of contents at $toc_path"
Write-Verbose "Generated Index file at $index_path"
Write-Verbose "Generated map file at $map_path"
}