forked from dfinke/PowerShellNotebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertToPowerShellNoteBook.ps1
More file actions
101 lines (85 loc) · 4.19 KB
/
Copy pathConvertToPowerShellNoteBook.ps1
File metadata and controls
101 lines (85 loc) · 4.19 KB
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
function ConvertTo-PowerShellNoteBook {
<#
.Example
ConvertTo-PowerShellNoteBook -InputFileName c:\Temp\demo.txt -OutputNotebookName c:\Temp\demo.ipynb
#>
param(
$InputFileName,
$OutputNotebookName
)
<#################################################################################################
Parsing section.
#################################################################################################>
try{$CommentRanges = [System.Management.Automation.PSParser]::Tokenize((Get-Content $InputFileName), [ref]$null).Where({$_.Type -eq 'Comment'}) |
Select-Object -Property Start, Length, Type, Content }
Catch{"This is not a valid PowerShell file"}
$s = Get-Content -Raw ( Resolve-Path $InputFileName )
$BlocksWitGaps = @()
$Previous = $null
foreach($CommentBlock in $CommentRanges ) {
$BlockOffsets=[ordered]@{
Start = $CommentBlock.Start;
StopOffset = $CommentBlock.Start+$CommentBlock.Length;
Length = $CommentBlock.Length;
GapLength = [int] $CommentBlock.Start-$Previous.StopOffset;
PreviousStart = $Previous.Start;
PreviousStopOffset = $Previous.StopOffset;
Type = 'Code';
GapText = IF($CommentBlock.Start-$Previous.StopOffset -gt 1){[string] $s.Substring($Previous.StopOffset, ($CommentBlock.Start-$Previous.StopOffset)).trimstart()}else {[string] ''};
Content = $CommentBlock.Content
}
$Previous=$BlockOffsets
$BlocksWitGaps+=[pscustomobject] $BlockOffsets
}
$AllBlocks = @()
$Previous = $null
$AllBlocks = $CommentRanges
<# Catch anything missed from the tail of the file, add it to $AllBlocks #>
if($BlocksWitGaps.Count -eq 1){
<# This step handles ading the psobjects together if $CommentBlock isn't an array. #>
$AllBlocks = @($CommentBlock;[pscustomobject][Ordered]@{
Start = ($CommentBlock.Start+$CommentBlock.Length);
Length = $s.Length-($CommentBlock.Start+$CommentBlock.Length);
Type = 'Gap';
Content = $s.Substring(($CommentBlock.Start+$CommentBlock.Length), ($s.Length-($CommentBlock.Start+$CommentBlock.Length))).trimstart()})
}
else {
if(($CommentBlock.Start+$CommentBlock.Length) -lt $s.Length){
$AllBlocks+=[pscustomobject][ordered]@{
Start = ($CommentBlock.Start+$CommentBlock.Length);
Length = $s.Length-($CommentBlock.Start+$CommentBlock.Length);
Type = 'Gap';
Content = $s.Substring(($CommentBlock.Start+$CommentBlock.Length), ($s.Length-($CommentBlock.Start+$CommentBlock.Length))).trimstart()}
}
}
foreach($GapBlock in $BlocksWitGaps ) {
$GapOffsets=[ordered]@{
Start = $GapBlock.PreviousStopOffset;
StopOffset = $GapBlock.Start;
Length = $GapBlock.GapLength;
Type = 'Gap';
Content = $GapBlock.GapText.trimstart()
}
$Previous=$GapOffsets
$AllBlocks+=if($GapOffsets.Length -gt 0){[pscustomobject] $GapOffsets}
}
<#################################################################################################
Notebook creation section.
#################################################################################################>
New-PSNotebook -NoteBookName $OutputNotebookName {
foreach($Block in $AllBlocks | Sort-Object Start ) {
switch ($Block.Type) {
'Comment' {$TextBlock = $s.Substring($Block.Start, $Block.Length) -replace ("\n", " `n ")
if($TextBlock.Trim().length -gt 0){
Add-NotebookMarkdown -markdown (-join $TextBlock)
}
}
'Gap' {$GapBlock = $s.Substring($Block.Start, $Block.Length)
if($GapBlock.Trim().length -gt 0){
Add-NotebookCode -code (-join $GapBlock.trimstart().trimend()) -replace ("\n", " `n ")
}
}
}
}
}
}