Codefactor style fixes

This commit is contained in:
Aditya Patwardhan 2018-07-11 12:52:22 -07:00
parent 481d3b1ebf
commit 7bf1604f97
18 changed files with 228 additions and 193 deletions

View file

@ -2,12 +2,12 @@
// Licensed under the MIT License.
using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Threading.Tasks;
using Microsoft.PowerShell.MarkdownRender;
using Dbg = System.Management.Automation;
@ -20,36 +20,35 @@ namespace Microsoft.PowerShell.Commands
[Cmdlet(
VerbsData.ConvertFrom, "Markdown",
DefaultParameterSetName = PathParameterSet,
HelpUri = "TBD"
)]
HelpUri = "TBD")]
[OutputType(typeof(Microsoft.PowerShell.MarkdownRender.MarkdownInfo))]
public class ConvertFromMarkdownCommand : PSCmdlet
{
/// <summary>
/// Path to the file to convert from Markdown to MarkdownInfo.
/// Gets or sets path to the file to convert from markdown to MarkdownInfo.
/// </summary>
[ValidateNotNullOrEmpty]
[Parameter(ParameterSetName = PathParameterSet, Mandatory = true)]
public string[] Path { get; set; }
/// <summary>
/// Path to the file to convert from Markdown to MarkdownInfo.
/// Gets or sets the path to the file to convert from markdown to MarkdownInfo.
/// </summary>
[ValidateNotNullOrEmpty]
[Parameter(ParameterSetName = LiteralPathParameterSet, Mandatory = true)]
public string[] LiteralPath { get; set; }
/// <summary>
/// InputObject of type System.IO.FileInfo or string with content to convert from Markdown to MarkdownInfo.
/// 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>
/// The Markdown document should be converted to a VT100 encoded string.
/// Gets or sets if the markdown document should be converted to a VT100 encoded string.
/// </summary>
[Parameter()]
[Parameter]
public SwitchParameter AsVT100EncodedString { get; set; }
private const string PathParameterSet = "PathParamSet";
@ -59,13 +58,13 @@ namespace Microsoft.PowerShell.Commands
private MarkdownOptionInfo mdOption = null;
/// <summary>
/// Override BeginProcess.
/// Override BeginProcessing.
/// </summary>
protected override void BeginProcessing()
{
mdOption = (SessionState.PSVariable.GetValue("MarkdownOptionInfo", new MarkdownOptionInfo())) as MarkdownOptionInfo;
mdOption = SessionState.PSVariable.GetValue("MarkdownOptionInfo", new MarkdownOptionInfo()) as MarkdownOptionInfo;
if(mdOption == null)
if (mdOption == null)
{
throw new InvalidOperationException();
}
@ -84,18 +83,15 @@ namespace Microsoft.PowerShell.Commands
switch (ParameterSetName)
{
case InputObjParamSet:
Object baseObj = InputObject.BaseObject;
object baseObj = InputObject.BaseObject;
//var fileInfo = baseObj as FileInfo;
if (baseObj is FileInfo fileInfo)
{
WriteObject(
MarkdownConverter.Convert(
ReadContentFromFile(fileInfo.FullName).Result,
conversionType,
mdOption
)
);
mdOption));
}
else if (baseObj is string inpObj)
{
@ -112,6 +108,7 @@ namespace Microsoft.PowerShell.Commands
WriteError(errorRecord);
}
break;
case PathParameterSet:
@ -136,8 +133,7 @@ namespace Microsoft.PowerShell.Commands
MarkdownConverter.Convert(
ReadContentFromFile(resolvedPath).Result,
conversionType,
optionInfo)
);
optionInfo));
}
}
}
@ -151,7 +147,6 @@ namespace Microsoft.PowerShell.Commands
string mdContent = await reader.ReadToEndAsync();
return mdContent;
}
}
private List<string> ResolvePath(string path, bool isLiteral)
@ -186,10 +181,12 @@ namespace Microsoft.PowerShell.Commands
if (!provider.Name.Equals("FileSystem", StringComparison.OrdinalIgnoreCase))
{
string errorMessage = StringUtil.Format(ConvertMarkdownStrings.FileSystemPathsOnly, path);
ErrorRecord errorRecord = new ErrorRecord(new ArgumentException(),
"OnlyFileSystemPathsSupported",
ErrorCategory.InvalidArgument,
path);
ErrorRecord errorRecord = new ErrorRecord(
new ArgumentException(),
"OnlyFileSystemPathsSupported",
ErrorCategory.InvalidArgument,
path);
WriteError(errorRecord);
return null;

View file

@ -2,22 +2,22 @@
// Licensed under the MIT License.
using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
using System.Threading.Tasks;
using Microsoft.PowerShell.MarkdownRender;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Class for implementing Set-MarkdownOption cmdlet.
/// </summary>
[Cmdlet(
VerbsCommon.Set, "MarkdownOption",
DefaultParameterSetName = IndividualSetting,
HelpUri = "TBD"
)]
HelpUri = "TBD")]
[OutputType(typeof(Microsoft.PowerShell.MarkdownRender.MarkdownOptionInfo))]
public class SetMarkdownOptionCommand : PSCmdlet
{
@ -26,83 +26,83 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string Header1Color { get; set;}
public string Header1Color { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for Header Level 2.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string Header2Color { get; set;}
public string Header2Color { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for Header Level 3.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string Header3Color { get; set;}
public string Header3Color { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for Header Level 4.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string Header4Color { get; set;}
public string Header4Color { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for Header Level 5.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string Header5Color { get; set;}
public string Header5Color { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for Header Level 6.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string Header6Color { get; set;}
public string Header6Color { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for code block background.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string Code { get; set;}
public string Code { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for image alt text foreground.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string ImageAltTextForegroundColor { get; set;}
public string ImageAltTextForegroundColor { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for link foreground.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string LinkForegroundColor { get; set;}
public string LinkForegroundColor { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for italics text foreground.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string ItalicsForegroundColor { get; set;}
public string ItalicsForegroundColor { get; set; }
/// <summary>
/// Gets or sets the VT100 escape sequence for bold text foreground.
/// </summary>
[ValidatePattern(@"^\[*[0-9;]*?m{1}")]
[Parameter(ParameterSetName = IndividualSetting)]
public string BoldForegroundColor { get; set;}
public string BoldForegroundColor { get; set; }
/// <summary>
/// Gets or sets the switch to PassThru the values set.
/// </summary>
[Parameter()]
public SwitchParameter PassThru { get; set;}
[Parameter]
public SwitchParameter PassThru { get; set; }
/// <summary>
/// Gets or sets the Theme.
@ -110,14 +110,14 @@ namespace Microsoft.PowerShell.Commands
[ValidateNotNullOrEmpty]
[Parameter(ParameterSetName = ThemeParamSet, Mandatory = true)]
[ValidateSet(DarkThemeName, LightThemeName)]
public string Theme { get; set;}
public string Theme { get; set; }
/// <summary>
/// Gets or sets InputObject.
/// </summary>
[ValidateNotNullOrEmpty]
[Parameter(ParameterSetName = InputObjectParamSet, Mandatory = true, ValueFromPipeline = true)]
public PSObject InputObject { get; set;}
public PSObject InputObject { get; set; }
private const string IndividualSetting = "IndividualSetting";
private const string InputObjectParamSet = "InputObject";
@ -133,28 +133,30 @@ namespace Microsoft.PowerShell.Commands
{
MarkdownOptionInfo mdOptionInfo = null;
switch(ParameterSetName)
switch (ParameterSetName)
{
case ThemeParamSet:
mdOptionInfo = new MarkdownOptionInfo();
if(string.Equals(Theme, LightThemeName, StringComparison.OrdinalIgnoreCase))
if (string.Equals(Theme, LightThemeName, StringComparison.OrdinalIgnoreCase))
{
mdOptionInfo.SetLightTheme();
}
else if(string.Equals(Theme, DarkThemeName, StringComparison.OrdinalIgnoreCase))
else if (string.Equals(Theme, DarkThemeName, StringComparison.OrdinalIgnoreCase))
{
mdOptionInfo.SetDarkTheme();
}
break;
case InputObjectParamSet:
Object baseObj = InputObject.BaseObject;
object baseObj = InputObject.BaseObject;
mdOptionInfo = baseObj as MarkdownOptionInfo;
if(mdOptionInfo == null)
if (mdOptionInfo == null)
{
throw new ArgumentException();
}
break;
case IndividualSetting:
@ -174,57 +176,57 @@ namespace Microsoft.PowerShell.Commands
private void SetOptions(MarkdownOptionInfo mdOptionInfo)
{
if (!String.IsNullOrEmpty(Header1Color))
if (!string.IsNullOrEmpty(Header1Color))
{
mdOptionInfo.Header1 = Header1Color;
}
if (!String.IsNullOrEmpty(Header2Color))
if (!string.IsNullOrEmpty(Header2Color))
{
mdOptionInfo.Header2 = Header2Color;
}
if (!String.IsNullOrEmpty(Header3Color))
if (!string.IsNullOrEmpty(Header3Color))
{
mdOptionInfo.Header3 = Header3Color;
}
if (!String.IsNullOrEmpty(Header4Color))
if (!string.IsNullOrEmpty(Header4Color))
{
mdOptionInfo.Header4 = Header4Color;
}
if (!String.IsNullOrEmpty(Header5Color))
if (!string.IsNullOrEmpty(Header5Color))
{
mdOptionInfo.Header5 = Header5Color;
}
if (!String.IsNullOrEmpty(Header6Color))
if (!string.IsNullOrEmpty(Header6Color))
{
mdOptionInfo.Header6 = Header6Color;
}
if (!String.IsNullOrEmpty(Code))
if (!string.IsNullOrEmpty(Code))
{
mdOptionInfo.Code = Code;
}
if (!String.IsNullOrEmpty(ImageAltTextForegroundColor))
if (!string.IsNullOrEmpty(ImageAltTextForegroundColor))
{
mdOptionInfo.Image = ImageAltTextForegroundColor;
}
if (!String.IsNullOrEmpty(LinkForegroundColor))
if (!string.IsNullOrEmpty(LinkForegroundColor))
{
mdOptionInfo.Link = LinkForegroundColor;
}
if (!String.IsNullOrEmpty(ItalicsForegroundColor))
if (!string.IsNullOrEmpty(ItalicsForegroundColor))
{
mdOptionInfo.EmphasisItalics = ItalicsForegroundColor;
}
if (!String.IsNullOrEmpty(BoldForegroundColor))
if (!string.IsNullOrEmpty(BoldForegroundColor))
{
mdOptionInfo.EmphasisBold = BoldForegroundColor;
}
@ -236,15 +238,14 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
[Cmdlet(
VerbsCommon.Get, "MarkdownOption",
HelpUri = "TBD"
)]
HelpUri = "TBD")]
[OutputType(typeof(Microsoft.PowerShell.MarkdownRender.MarkdownOptionInfo))]
public class GetMarkdownOptionCommand : PSCmdlet
{
private const string MarkdownOptionInfoVariableName = "MarkdownOptionInfo";
/// <summary>
/// Override endproessing.
/// Override EndProcessing.
/// </summary>
protected override void EndProcessing()
{

View file

@ -2,11 +2,11 @@
// Licensed under the MIT License.
using System;
using System.IO;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using Microsoft.PowerShell.MarkdownRender;
@ -19,31 +19,31 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
[Cmdlet(
VerbsCommon.Show, "Markdown",
HelpUri = "TBD"
)]
HelpUri = "TBD")]
[OutputType(typeof(string))]
public class ShowMarkdownCommand : PSCmdlet
{
/// <summary>
/// InputObject of type Microsoft.PowerShell.MarkdownRender.MarkdownInfo to display
/// Gets or sets InputObject of type Microsoft.PowerShell.MarkdownRender.MarkdownInfo to display.
/// </summary>
[ValidateNotNullOrEmpty]
[Parameter(Mandatory = true, ValueFromPipeline = true)]
public PSObject InputObject { get; set; }
/// <summary>
/// Switch to view Html in default browser.
/// Gets or sets the switch to view Html in default browser.
/// </summary>
[Parameter()]
[Parameter]
public SwitchParameter UseBrowser { get; set; }
private SteppablePipeline stepPipe;
/// <summary>
/// Override BeginProcessing.
/// </summary>
protected override void BeginProcessing()
{
if(! this.MyInvocation.BoundParameters.ContainsKey("UseBrowser"))
if (!this.MyInvocation.BoundParameters.ContainsKey("UseBrowser"))
{
// Since UseBrowser is not bound, we use proxy to Out-Default
stepPipe = ScriptBlock.Create(@"Microsoft.PowerShell.Core\Out-Default @PSBoundParameters").GetSteppablePipeline(this.MyInvocation.CommandOrigin);
@ -52,11 +52,11 @@ namespace Microsoft.PowerShell.Commands
}
/// <summary>
/// Override ProcessRecord
/// Override ProcessRecord.
/// </summary>
protected override void ProcessRecord()
{
Object inpObj = InputObject.BaseObject;
object inpObj = InputObject.BaseObject;
var markdownInfo = inpObj as MarkdownInfo;
if (markdownInfo == null)
@ -75,15 +75,15 @@ namespace Microsoft.PowerShell.Commands
{
var html = markdownInfo.Html;
if (!String.IsNullOrEmpty(html))
if (!string.IsNullOrEmpty(html))
{
string tmpFilePath = Path.Combine(Path.GetTempPath(), (Guid.NewGuid().ToString() + ".html"));
string tmpFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".html");
using (var writer = new StreamWriter(new FileStream(tmpFilePath, FileMode.Create, FileAccess.Write, FileShare.Write)))
{
writer.Write(html);
}
if(OutputBypassTestHook)
if (outputBypassTestHook)
{
WriteObject(html);
return;
@ -116,15 +116,15 @@ namespace Microsoft.PowerShell.Commands
{
var vt100String = markdownInfo.VT100EncodedString;
if(!String.IsNullOrEmpty(vt100String))
if (!string.IsNullOrEmpty(vt100String))
{
if(OutputBypassTestHook)
if (outputBypassTestHook)
{
WriteObject(vt100String);
return;
}
if(stepPipe != null)
if (stepPipe != null)
{
stepPipe.Process(vt100String);
}
@ -148,21 +148,22 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
protected override void EndProcessing()
{
if(stepPipe != null)
if (stepPipe != null)
{
stepPipe.End();
}
}
private static bool OutputBypassTestHook = false;
private static bool outputBypassTestHook = false;
/// <summary>
/// Test hook to enable or disable launching of browser.
/// When set, the converted output is returned.
/// </summary>
/// <param name="value">True to enable test hook, false to disable.</param>
public static void SetOutputBypassTestHook(bool value)
{
OutputBypassTestHook = value;
outputBypassTestHook = value;
}
}
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax.Inlines;
using Markdig.Renderers;
using Markdig.Syntax.Inlines;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -16,7 +16,7 @@ namespace Microsoft.PowerShell.MarkdownRender
{
protected override void Write(VT100Renderer renderer, CodeInline obj)
{
renderer.Write(renderer.EscapeSequences.FormatCode(obj.Content , isInline: true));
renderer.Write(renderer.EscapeSequences.FormatCode(obj.Content, isInline : true));
}
}
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax.Inlines;
using Markdig.Renderers;
using Markdig.Syntax.Inlines;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -16,7 +16,7 @@ namespace Microsoft.PowerShell.MarkdownRender
{
protected override void Write(VT100Renderer renderer, EmphasisInline obj)
{
renderer.Write(renderer.EscapeSequences.FormatEmphasis(obj.FirstChild.ToString() , isBold: obj.IsDouble ? true : false ));
renderer.Write(renderer.EscapeSequences.FormatEmphasis(obj.FirstChild.ToString(), isBold : obj.IsDouble ? true : false));
}
}
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -18,11 +18,11 @@ namespace Microsoft.PowerShell.MarkdownRender
{
foreach (var codeLine in obj.Lines.Lines)
{
if (!String.IsNullOrWhiteSpace(codeLine.ToString()))
if (!string.IsNullOrWhiteSpace(codeLine.ToString()))
{
// If the code block is of type YAML, then tab to right to improve readability.
// This specifically helps for parameters help content.
if (String.Equals(obj.Info, "yaml", StringComparison.OrdinalIgnoreCase))
if (string.Equals(obj.Info, "yaml", StringComparison.OrdinalIgnoreCase))
{
renderer.WriteLine("\t" + codeLine.ToString());
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -17,7 +17,7 @@ namespace Microsoft.PowerShell.MarkdownRender
protected override void Write(VT100Renderer renderer, HeadingBlock obj)
{
// Format header and then add blank line to improve readability.
switch(obj.Level)
switch (obj.Level)
{
case 1:
renderer.WriteLine(renderer.EscapeSequences.FormatHeader1(obj.Inline.FirstChild.ToString()));

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax.Inlines;
using Markdig.Renderers;
using Markdig.Syntax.Inlines;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -19,7 +19,7 @@ namespace Microsoft.PowerShell.MarkdownRender
// If the next sibling is null, then this is the last line in the paragraph.
// Add new line character at the end.
// Else just write without newline at the end.
if(obj.NextSibling == null)
if (obj.NextSibling == null)
{
renderer.WriteLine(obj.ToString());
}

View file

@ -4,12 +4,11 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax.Inlines;
using Markdig.Renderers;
using Markdig.Syntax.Inlines;
namespace Microsoft.PowerShell.MarkdownRender
{
/// <summary>
/// Renderer for adding VT100 escape sequences for line breaks.
/// </summary>
@ -19,7 +18,7 @@ namespace Microsoft.PowerShell.MarkdownRender
{
// If it is a hard line break add new line at the end.
// Else, add a space for after the last character to improve readability.
if(obj.IsHard)
if (obj.IsHard)
{
renderer.WriteLine();
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax.Inlines;
using Markdig.Renderers;
using Markdig.Syntax.Inlines;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -17,7 +17,7 @@ namespace Microsoft.PowerShell.MarkdownRender
protected override void Write(VT100Renderer renderer, LinkInline obj)
{
// Format link as image or link.
if(obj.IsImage)
if (obj.IsImage)
{
renderer.Write(renderer.EscapeSequences.FormatImage(obj.FirstChild.ToString()));
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -42,7 +42,7 @@ namespace Microsoft.PowerShell.MarkdownRender
// For a numbered list, we need to make sure the index is incremented.
foreach (var line in block)
{
if(line is ParagraphBlock paragraphBlock)
if (line is ParagraphBlock paragraphBlock)
{
renderer.Write(index.ToString()).Write(". ").Write(paragraphBlock.Inline);
}

View file

@ -5,8 +5,8 @@ using System;
using System.IO;
using System.Threading;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -38,20 +38,21 @@ namespace Microsoft.PowerShell.MarkdownRender
var paragraphBlock = block as ParagraphBlock;
if(paragraphBlock != null)
if (paragraphBlock != null)
{
renderer.Write(indent).Write(listBullet).Write(" ").Write(paragraphBlock.Inline);
}
else //If there is a sublist, the block is a ListBlock instead of ParagraphBlock.
else
{
// If there is a sublist, the block is a ListBlock instead of ParagraphBlock.
var subList = block as ListBlock;
if (subList != null)
{
foreach(var subListItem in subList)
foreach (var subListItem in subList)
{
var subListItemBlock = subListItem as ListItemBlock;
if(subListItemBlock != null)
if (subListItemBlock != null)
{
foreach (var line in subListItemBlock)
{
@ -67,16 +68,19 @@ namespace Microsoft.PowerShell.MarkdownRender
// Typical padding is at most a screen's width, any more than that and we won't bother caching.
private const int IndentCacheMax = 120;
private static readonly string[] IndentCache = new string[IndentCacheMax];
internal static string Padding(int countOfSpaces)
{
if (countOfSpaces >= IndentCacheMax)
{
return new string(' ', countOfSpaces);
}
var result = IndentCache[countOfSpaces];
if (result == null)
{
Interlocked.CompareExchange(ref IndentCache[countOfSpaces], new string(' ', countOfSpaces), comparand:null);
Interlocked.CompareExchange(ref IndentCache[countOfSpaces], new string(' ', countOfSpaces), comparand : null);
result = IndentCache[countOfSpaces];
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -32,17 +32,17 @@ namespace Microsoft.PowerShell.MarkdownRender
public class MarkdownInfo
{
/// <summary>
/// Html content after conversion.
/// Gets the Html content after conversion.
/// </summary>
public string Html { get; internal set;}
public string Html { get; internal set; }
/// <summary>
/// VT100 encoded string after conversion.
/// Gets the VT100 encoded string after conversion.
/// </summary>
public string VT100EncodedString { get; internal set;}
public string VT100EncodedString { get; internal set; }
/// <summary>
/// AST of the markdown string.
/// Gets the AST of the markdown string.
/// </summary>
public Markdig.Syntax.MarkdownDocument Tokens { get; internal set; }
}
@ -55,25 +55,27 @@ namespace Microsoft.PowerShell.MarkdownRender
/// <summary>
/// Convert from markdown string to VT100 encoded string or HTML. Returns MarkdownInfo object.
/// </summary>
/// <param name="markdownString">string with markdown content to be converted</param>
/// <param name="conversionType">specifies type of conversion, either VT100 or HTML</param>
/// <param name="optionInfo">specifies the rendering options for VT100 rendering</param>
/// <param name="markdownString">String with markdown content to be converted.</param>
/// <param name="conversionType">Specifies type of conversion, either VT100 or HTML.</param>
/// <param name="optionInfo">Specifies the rendering options for VT100 rendering.</param>
/// <returns>MarkdownInfo object with the converted output.</returns>
public static MarkdownInfo Convert(string markdownString, MarkdownConversionType conversionType, MarkdownOptionInfo optionInfo)
{
var renderInfo = new MarkdownInfo();
var writer = new StringWriter();
MarkdownPipeline pipeline = null;
if(conversionType.HasFlag(MarkdownConversionType.HTML))
if (conversionType.HasFlag(MarkdownConversionType.HTML))
{
pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
var renderer = new Markdig.Renderers.HtmlRenderer(writer);
renderInfo.Html = Markdig.Markdown.Convert(markdownString, renderer, pipeline).ToString();
}
if(conversionType.HasFlag(MarkdownConversionType.VT100))
if (conversionType.HasFlag(MarkdownConversionType.VT100))
{
pipeline = new MarkdownPipelineBuilder().Build();
// Use the VT100 renderer.
var renderer = new VT100Renderer(writer, optionInfo);
renderInfo.VT100EncodedString = Markdig.Markdown.Convert(markdownString, renderer, pipeline).ToString();

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -18,6 +18,7 @@ namespace Microsoft.PowerShell.MarkdownRender
{
// Call the renderer for children, leaf inline or line breaks.
renderer.WriteChildren(obj.Inline);
// Add new line at the end of the paragraph.
renderer.WriteLine();
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -17,7 +17,7 @@ namespace Microsoft.PowerShell.MarkdownRender
protected override void Write(VT100Renderer renderer, QuoteBlock obj)
{
// Iterate through each item and add the quote character before the content.
foreach(var item in obj)
foreach (var item in obj)
{
renderer.Write(obj.QuoteChar).Write(" ").Write(item);
}

View file

@ -4,8 +4,8 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
@ -14,60 +14,60 @@ namespace Microsoft.PowerShell.MarkdownRender
/// </summary>
public sealed class MarkdownOptionInfo
{
private const char Esc = (char) 0x1b;
private const char Esc = (char)0x1b;
/// <summary>
/// Current VT100 escape sequence for header 1.
/// Gets or sets current VT100 escape sequence for header 1.
/// </summary>
public string Header1 { get; set; }
/// <summary>
/// Current VT100 escape sequence for header 2.
/// Gets or sets current VT100 escape sequence for header 2.
/// </summary>
public string Header2 { get; set; }
/// <summary>
/// Current VT100 escape sequence for header 3.
/// Gets or sets current VT100 escape sequence for header 3.
/// </summary>
public string Header3 { get; set; }
/// <summary>
/// Current VT100 escape sequence for header 4.
/// Gets or sets current VT100 escape sequence for header 4.
/// </summary>
public string Header4 { get; set; }
/// <summary>
/// Current VT100 escape sequence for header 5.
/// Gets or sets current VT100 escape sequence for header 5.
/// </summary>
public string Header5 { get; set; }
/// <summary>
/// Current VT100 escape sequence for header 6.
/// Gets or sets current VT100 escape sequence for header 6.
/// </summary>
public string Header6 { get; set; }
/// <summary>
/// Current VT100 escape sequence for code inline and code blocks.
/// Gets or sets current VT100 escape sequence for code inline and code blocks.
/// </summary>
public string Code { get; set; }
/// <summary>
/// Current VT100 escape sequence for links.
/// Gets or sets current VT100 escape sequence for links.
/// </summary>
public string Link { get; set; }
/// <summary>
/// Current VT100 escape sequence for images.
/// Gets or sets current VT100 escape sequence for images.
/// </summary>
public string Image { get; set; }
/// <summary>
/// Current VT100 escape sequence for bold text.
/// Gets or sets current VT100 escape sequence for bold text.
/// </summary>
public string EmphasisBold { get; set; }
/// <summary>
/// Current VT100 escape sequence for italics text.
/// Gets or sets current VT100 escape sequence for italics text.
/// </summary>
public string EmphasisItalics { get; set; }
@ -75,11 +75,13 @@ namespace Microsoft.PowerShell.MarkdownRender
/// Get the property as an rendered escape sequence.
/// This is used for typesps1xml for displaying.
/// </summary>
/// <param name="propertyName">Name of the property to get as escape sequence.</param>
/// <returns>Specified property name as escape sequence.</returns>
public string AsEscapeSequence(string propertyName)
{
var propertyValue = this.GetType().GetProperty(propertyName)?.GetValue(this) as string;
if(!String.IsNullOrEmpty(propertyValue))
if (!string.IsNullOrEmpty(propertyValue))
{
return string.Concat(Esc, propertyValue, propertyValue, Esc, "[0m");
}
@ -90,7 +92,7 @@ namespace Microsoft.PowerShell.MarkdownRender
}
/// <summary>
/// Set dark as the default theme.
/// Initializes a new instance of <see cref="MarkdownOptionInfo"/> class and sets dark as the default theme.
/// </summary>
public MarkdownOptionInfo()
{
@ -134,23 +136,24 @@ namespace Microsoft.PowerShell.MarkdownRender
}
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
public class VT100EscapeSequences
{
private const char Esc = (char) 0x1B;
private const char Esc = (char)0x1B;
private string EndSequence = Esc + "[0m";
private string endSequence = Esc + "[0m";
private MarkdownOptionInfo options;
/// <summary>
/// Set the options as per <param name="optionInfo"/>
/// Initializes a new instance of the <see cref="VT100EscapeSequences"/> class.
/// </summary>
/// <param name="optionInfo">MarkdownOptionInfo object to initialize with.</param>
public VT100EscapeSequences(MarkdownOptionInfo optionInfo)
{
if(optionInfo == null)
if (optionInfo == null)
{
throw new ArgumentNullException("optionInfo");
}
@ -158,100 +161,124 @@ namespace Microsoft.PowerShell.MarkdownRender
options = optionInfo;
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="headerText">Text of the header to format.</param>
/// <returns>Formatted Header 1 string.</returns>
public string FormatHeader1(string headerText)
{
return String.Concat(Esc, options.Header1, headerText, EndSequence);
return string.Concat(Esc, options.Header1, headerText, endSequence);
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="headerText">Text of the header to format.</param>
/// <returns>Formatted Header 2 string.</returns>
public string FormatHeader2(string headerText)
{
return String.Concat(Esc, options.Header2, headerText, EndSequence);
return string.Concat(Esc, options.Header2, headerText, endSequence);
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="headerText">Text of the header to format.</param>
/// <returns>Formatted Header 3 string.</returns>
public string FormatHeader3(string headerText)
{
return String.Concat(Esc, options.Header3, headerText, EndSequence);
return string.Concat(Esc, options.Header3, headerText, endSequence);
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="headerText">Text of the header to format.</param>
/// <returns>Formatted Header 4 string.</returns>
public string FormatHeader4(string headerText)
{
return String.Concat(Esc, options.Header4, headerText, EndSequence);
return string.Concat(Esc, options.Header4, headerText, endSequence);
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="headerText">Text of the header to format.</param>
/// <returns>Formatted Header 5 string.</returns>
public string FormatHeader5(string headerText)
{
return String.Concat(Esc, options.Header5, headerText, EndSequence);
return string.Concat(Esc, options.Header5, headerText, endSequence);
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="headerText">Text of the header to format.</param>
/// <returns>Formatted Header 6 string.</returns>
public string FormatHeader6(string headerText)
{
return String.Concat(Esc, options.Header6, headerText, EndSequence);
return string.Concat(Esc, options.Header6, headerText, endSequence);
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="codeText">Text of the code block to format.</param>
/// <param name="isInline">True if it is a inline code block, false otherwise.</param>
/// <returns>Formatted code block string.</returns>
public string FormatCode(string codeText, bool isInline)
{
if(isInline)
if (isInline)
{
return String.Concat(Esc, options.Code, codeText, EndSequence);
return string.Concat(Esc, options.Code, codeText, endSequence);
}
else
{
// For code blocks, [500@ make sure that the whole line has background color.
return String.Concat(Esc, options.Code, codeText, Esc, "[500@", EndSequence);
return string.Concat(Esc, options.Code, codeText, Esc, "[500@", endSequence);
}
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="linkText">Text of the link to format.</param>
/// <param name="url">URL of the link.</param>
/// <param name="hideUrl">True url should be hidden, false otherwise. Default is true.</param>
/// <returns>Formatted link string.</returns>
public string FormatLink(string linkText, string url, bool hideUrl = true)
{
if(hideUrl)
if (hideUrl)
{
return String.Concat(Esc, options.Link, "\"", linkText, "\"", EndSequence);
return string.Concat(Esc, options.Link, "\"", linkText, "\"", endSequence);
}
else
{
return String.Concat("\"", linkText, "\" (", Esc, options.Link, url, EndSequence, ")");
return string.Concat("\"", linkText, "\" (", Esc, options.Link, url, endSequence, ")");
}
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="emphasisText">Text to format as emphasis.</param>
/// <param name="isBold">True if it is to be formatted as bold, false to format it as italics.</param>
/// <returns>Formatted emphasis string.</returns>
public string FormatEmphasis(string emphasisText, bool isBold)
{
var sequence = isBold ? options.EmphasisBold : options.EmphasisItalics;
return String.Concat(Esc, sequence, emphasisText, EndSequence);
return string.Concat(Esc, sequence, emphasisText, endSequence);
}
///<summary>
/// Class to represent default VT100 escape sequences
///</summary>
/// <summary>
/// Class to represent default VT100 escape sequences.
/// </summary>
/// <param name="altText">Text of the image to format.</param>
/// <returns>Formatted image string.</returns>
public string FormatImage(string altText)
{
return String.Concat(Esc, options.Image, "[", altText, "]", EndSequence);
return string.Concat(Esc, options.Image, "[", altText, "]", endSequence);
}
}
}

View file

@ -4,14 +4,15 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
/// <summary>
/// Implement the MarkdownObjectRenderer with VT100Renderer.
/// </summary>
/// <typeparam name="T">The element type of the renderer.</typeparam>
public abstract class VT100ObjectRenderer<T> : MarkdownObjectRenderer<VT100Renderer, T> where T : MarkdownObject
{
}

View file

@ -4,19 +4,21 @@
using System;
using System.IO;
using Markdig;
using Markdig.Syntax;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
/// <summary>
/// Implementation of the VT100 renderer.
/// Initializes an instance of the VT100 renderer.
/// </summary>
public sealed class VT100Renderer : TextRendererBase<VT100Renderer>
{
/// <summary>
/// Initialize the VT100 renderer with <param name="optionInfo"/> and write the output <param name="writer"/>.
/// Initializes a new instance of the <see cref="VT100Renderer"/> class.
/// </summary>
/// <param name="writer">TextWriter to write to.</param>
/// <param name="optionInfo">MarkdownOptionInfo object with options.</param>
public VT100Renderer(TextWriter writer, MarkdownOptionInfo optionInfo) : base(writer)
{
EscapeSequences = new VT100EscapeSequences(optionInfo);
@ -36,8 +38,8 @@ namespace Microsoft.PowerShell.MarkdownRender
}
/// <summary>
/// Get the current escape sequences.
/// Gets the current escape sequences.
/// </summary>
public VT100EscapeSequences EscapeSequences { get; private set;}
public VT100EscapeSequences EscapeSequences { get; private set; }
}
}