File size: 8,406 Bytes
8c763fb | 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Security;
using System.Threading.Tasks;
using Microsoft.PowerShell.MarkdownRender;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Converts a Markdown string to a MarkdownInfo object.
/// The conversion can be done into a HTML text or VT100 encoding string.
/// </summary>
[Cmdlet(
VerbsData.ConvertFrom, "Markdown",
DefaultParameterSetName = PathParameterSet,
HelpUri = "https://go.microsoft.com/fwlink/?linkid=2006503")]
[OutputType(typeof(Microsoft.PowerShell.MarkdownRender.MarkdownInfo))]
public class ConvertFromMarkdownCommand : PSCmdlet
{
/// <summary>
/// Gets or sets path to the file to convert from Markdown to MarkdownInfo.
/// </summary>
[ValidateNotNullOrEmpty]
[Parameter(ParameterSetName = PathParameterSet, Mandatory = true, Position = 0)]
public string[] Path { get; set; }
/// <summary>
/// Gets or sets the path to the file to convert from Markdown to MarkdownInfo.
/// </summary>
[ValidateNotNullOrEmpty]
[Alias("PSPath", "LP")]
[Parameter(ParameterSetName = LiteralPathParameterSet, Mandatory = true)]
public string[] LiteralPath { get; set; }
/// <summary>
/// Gets or sets the InputObject of type System.IO.FileInfo or string with content to convert from Markdown to MarkdownInfo.
/// </summary>
[ValidateNotNullOrEmpty]
[Parameter(ParameterSetName = InputObjParamSet, Mandatory = true, ValueFromPipeline = true)]
public PSObject InputObject { get; set; }
/// <summary>
/// Gets or sets if the Markdown document should be converted to a VT100 encoded string.
/// </summary>
[Parameter]
public SwitchParameter AsVT100EncodedString { get; set; }
private const string PathParameterSet = "PathParamSet";
private const string LiteralPathParameterSet = "LiteralParamSet";
private const string InputObjParamSet = "InputObjParamSet";
private MarkdownConversionType _conversionType = MarkdownConversionType.HTML;
private PSMarkdownOptionInfo _mdOption = null;
/// <summary>
/// Read the PSMarkdownOptionInfo set in SessionState.
/// </summary>
protected override void BeginProcessing()
{
_mdOption = PSMarkdownOptionInfoCache.Get(this.CommandInfo);
bool? supportsVT100 = this.Host?.UI.SupportsVirtualTerminal;
// supportsVT100 == null if the host is null.
// supportsVT100 == false if host does not support VT100.
if (supportsVT100 != true)
{
_mdOption.EnableVT100Encoding = false;
}
if (AsVT100EncodedString)
{
_conversionType = MarkdownConversionType.VT100;
}
}
/// <summary>
/// Override ProcessRecord.
/// </summary>
protected override void ProcessRecord()
{
switch (ParameterSetName)
{
case InputObjParamSet:
object baseObj = InputObject.BaseObject;
if (baseObj is FileInfo fileInfo)
{
WriteObject(
MarkdownConverter.Convert(
ReadContentFromFile(fileInfo.FullName)?.Result,
_conversionType,
_mdOption));
}
else if (baseObj is string inpObj)
{
WriteObject(MarkdownConverter.Convert(inpObj, _conversionType, _mdOption));
}
else
{
string errorMessage = StringUtil.Format(ConvertMarkdownStrings.InvalidInputObjectType, baseObj.GetType());
ErrorRecord errorRecord = new(
new InvalidDataException(errorMessage),
"InvalidInputObject",
ErrorCategory.InvalidData,
InputObject);
WriteError(errorRecord);
}
break;
case PathParameterSet:
ConvertEachFile(Path, _conversionType, isLiteral: false, optionInfo: _mdOption);
break;
case LiteralPathParameterSet:
ConvertEachFile(LiteralPath, _conversionType, isLiteral: true, optionInfo: _mdOption);
break;
}
}
private void ConvertEachFile(IEnumerable<string> paths, MarkdownConversionType conversionType, bool isLiteral, PSMarkdownOptionInfo optionInfo)
{
foreach (var path in paths)
{
var resolvedPaths = ResolvePath(path, isLiteral);
foreach (var resolvedPath in resolvedPaths)
{
WriteObject(
MarkdownConverter.Convert(
ReadContentFromFile(resolvedPath)?.Result,
conversionType,
optionInfo));
}
}
}
private async Task<string> ReadContentFromFile(string filePath)
{
ErrorRecord errorRecord = null;
try
{
using (StreamReader reader = new(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
string mdContent = await reader.ReadToEndAsync();
return mdContent;
}
}
catch (FileNotFoundException fnfe)
{
errorRecord = new ErrorRecord(
fnfe,
"FileNotFound",
ErrorCategory.ResourceUnavailable,
filePath);
}
catch (SecurityException se)
{
errorRecord = new ErrorRecord(
se,
"FileSecurityError",
ErrorCategory.SecurityError,
filePath);
}
catch (UnauthorizedAccessException uae)
{
errorRecord = new ErrorRecord(
uae,
"FileUnauthorizedAccess",
ErrorCategory.SecurityError,
filePath);
}
WriteError(errorRecord);
return null;
}
private List<string> ResolvePath(string path, bool isLiteral)
{
ProviderInfo provider = null;
PSDriveInfo drive = null;
List<string> resolvedPaths = new();
try
{
if (isLiteral)
{
resolvedPaths.Add(Context.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path, out provider, out drive));
}
else
{
resolvedPaths.AddRange(Context.SessionState.Path.GetResolvedProviderPathFromPSPath(path, out provider));
}
}
catch (ItemNotFoundException infe)
{
var errorRecord = new ErrorRecord(
infe,
"FileNotFound",
ErrorCategory.ResourceUnavailable,
path);
WriteError(errorRecord);
}
if (!provider.Name.Equals("FileSystem", StringComparison.OrdinalIgnoreCase))
{
string errorMessage = StringUtil.Format(ConvertMarkdownStrings.FileSystemPathsOnly, path);
ErrorRecord errorRecord = new(
new ArgumentException(),
"OnlyFileSystemPathsSupported",
ErrorCategory.InvalidArgument,
path);
WriteError(errorRecord);
return null;
}
return resolvedPaths;
}
}
}
|