-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update packages including fix for #90
- Loading branch information
1 parent
6d91680
commit 6bcc71a
Showing
6 changed files
with
132 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace dotnet_repl; | ||
|
||
internal static class DictionaryExtensions | ||
{ | ||
public static TValue GetOrAdd<TValue>( | ||
this IDictionary<string, object> dictionary, | ||
string key, | ||
Func<string, TValue> getValue) | ||
{ | ||
if (!dictionary.TryGetValue(key, out var value)) | ||
{ | ||
value = getValue(key); | ||
dictionary.Add(key, value!); | ||
} | ||
|
||
return (TValue)value!; | ||
} | ||
|
||
public static void MergeWith<TKey, TValue>( | ||
this IDictionary<TKey, TValue> target, | ||
IDictionary<TKey, TValue> source, | ||
bool replace = false) | ||
{ | ||
foreach (var pair in source) | ||
{ | ||
if (replace || !target.ContainsKey(pair.Key)) | ||
{ | ||
target[pair.Key] = pair.Value; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.DotNet.Interactive.Documents; | ||
|
||
namespace dotnet_repl; | ||
|
||
internal static class InteractiveDocumentExtensions | ||
{ | ||
public static InteractiveDocument WithJupyterMetadata( | ||
this InteractiveDocument document, | ||
string language = "C#") | ||
{ | ||
var (kernelName, canonicalLanguageName, langVersion, fileExtension) = | ||
language.ToLowerInvariant() switch | ||
{ | ||
"c#" or "csharp" => ("csharp", "C#", "10.0", ".cs"), | ||
"f#" or "fsharp" => ("fsharp", "F#", "6.0", ".fs"), | ||
"powershell" or "pwsh" => ("powershell", "PowerShell", "7.0", ".ps1"), | ||
_ => throw new ArgumentException($"Unrecognized language: {language}") | ||
}; | ||
|
||
document.Metadata.GetOrAdd("kernelspec", _ => new Dictionary<string, object>()) | ||
.MergeWith(new Dictionary<string, object> | ||
{ | ||
["display_name"] = $".NET ({canonicalLanguageName})", | ||
["language"] = canonicalLanguageName, | ||
["name"] = $".net-{kernelName}" | ||
}); | ||
|
||
document.Metadata.GetOrAdd("language_info", _ => new Dictionary<string, object>()) | ||
.MergeWith(new Dictionary<string, object> | ||
{ | ||
["file_extension"] = fileExtension, | ||
["mimetype"] = $"text/x-{kernelName}", | ||
["name"] = canonicalLanguageName, | ||
["pygments_lexer"] = kernelName, | ||
["version"] = langVersion | ||
}); | ||
|
||
// `polyglot_notebook` is the canonical metadata key name, but we're still writing `dotnet_interactive` for backwards compatibility | ||
var kernelInfos = document.Metadata.GetOrAdd("polyglot_notebook", _ => new KernelInfoCollection()); | ||
document.Metadata["dotnet_interactive"] = kernelInfos; | ||
|
||
kernelInfos.DefaultKernelName = kernelName; | ||
kernelInfos.Add(new(kernelName)); | ||
|
||
return document; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters