forked from dfinke/PowerShellNotebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetNotebookContent.ps1
More file actions
57 lines (47 loc) · 1.98 KB
/
Copy pathGetNotebookContent.ps1
File metadata and controls
57 lines (47 loc) · 1.98 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
function Get-NotebookContent {
<#
.SYNOPSIS
Get-NotebookContents reads the contents of a Jupyter Notebooks
.Example
Get-NotebookContent .\samplenotebook\Chapter01code.ipynb
NoteBookName Type Source
------------ ---- ------
Chapter01code.ipynb markdown ## Code for chapter 1 PowerShell in Action third edition
Chapter01code.ipynb markdown ## Introduction
Chapter01code.ipynb code 'Hello world.'
Chapter01code.ipynb code Get-ChildItem -Path $env:windir\*.log | Select-String -List error | Format-Table Path,L...
Chapter01code.ipynb code ([xml] [System.Net.WebClient]::new().DownloadString('http://blogs.msdn.com/powershell/r...
Chapter01code.ipynb markdown ## 1.2 PowerShell example code
Chapter01code.ipynb code Get-ChildItem -Path C:\somefile.txt
.Example
Get-Notebook .\samplenotebook\*sharp*|Get-NotebookContent
NoteBookName Type Source
------------ ---- ------
csharp.ipynb code {Console.Write("hello world")}
fsharp.ipynb code {printfn "hello world"}
#>
param(
[Parameter(ValueFromPipelineByPropertyName)]
$NoteBookFullName,
[Switch]$JustCode,
[Switch]$JustMarkdown
)
Process {
if ([System.Uri]::IsWellFormedUriString($NoteBookFullName, [System.UriKind]::Absolute)) {
$r = Invoke-RestMethod $NoteBookFullName
}
elseif (Test-Path $NoteBookFullName -ErrorAction SilentlyContinue) {
$r = Get-Content $NoteBookFullName | ConvertFrom-Json
}
if ($JustCode) { $cellType = "code" }
if ($JustMarkdown) { $cellType = "markdown" }
if ($JustCode -and $JustMarkdown) { $cellType = $null }
$r.cells | Where-Object { $_.cell_type -match $cellType } | ForEach-Object {
[PSCustomObject][Ordered]@{
NoteBookName = Split-Path -Leaf $NoteBookFullName
Type = $_.'cell_type'
Source = -join $_.source
}
}
}
}