-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples-to-md.ps1
79 lines (53 loc) · 2.04 KB
/
examples-to-md.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
$ROOT = if ($env:ROOT) { $env:ROOT } else { $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.\') }
$NewLine = [Environment]::NewLine
Get-ChildItem $ROOT\Steep.Examples\ -Filter *.Example.cs -Recurse |
Foreach-Object {
$exampleName = $_.BaseName.substring(0,$_.BaseName.IndexOf('.'))
$originalContent = [IO.File]::ReadAllText($_.FullName)
$exampleBegin = $originalContent.IndexOf("//!example-begin")
$exampleEnd = $originalContent.IndexOf("//!example-end")
$newContent = $originalContent
if($exampleBegin -ge 0 -And $exampleEnd -ge 0) {
$newLineIndex = $originalContent.IndexOf("`n", $exampleBegin) + 1
$newContent = $originalContent.Substring($newLineIndex, $exampleEnd - $newLineIndex)
}
$newPath = ($ROOT + '\docs\examples\' + $exampleName + '.md')
$newContent | Set-Content $newPath
$lines = [IO.File]::ReadAllLines($newPath)
# Clear extra indentation
$indent = 100
foreach ($line in $lines){ # TODO: only shows 2 lines...
if($line -eq '') {
continue
}
$currIndent = 0
foreach ($char in $line.GetEnumerator()){
if($char -eq ' ' -or $char -eq '`n') {
$currIndent++
continue
}
break
}
if($currIndent -lt $indent) {
$indent = $currIndent
}
}
Write-Host $indent
$indentedNewContent = New-Object -TypeName "System.Text.StringBuilder"
foreach ($line in $lines){
if($line -eq '') {
$indentedNewContent.AppendLine()
continue
}
$indentedNewContent.AppendLine($line.substring($indent))
}
$s = $indentedNewContent.ToString()
$content = '# ' + $exampleName + $NewLine + $NewLine + '```csharp' + $NewLine + $s.Trim() + $NewLine + '```'
#filter and save content to the original file
# $content | Where-Object {$_ -match 'step[49]'} | Set-Content $_.FullName
#filter and save content to a new file
# $content | Where-Object {$_ -match 'step[49]'} | Set-Content ($_.BaseName + '_out.log')
mkdir -Force $ROOT\docs\examples\
$content | Set-Content $newPath
}
"Examples To MD"