Skip to content

Commit

Permalink
First pass - singleton version
Browse files Browse the repository at this point in the history
  • Loading branch information
sm6srw committed Nov 17, 2020
1 parent 71509da commit bbefc36
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
<Project>{47533B7C-0E1A-44A4-8511-B438645F052A}</Project>
<Name>DynamoPackages</Name>
</ProjectReference>
<ProjectReference Include="..\DynamoUtilities\DynamoUtilities.csproj">
<Project>{B5F435CB-0D8A-40B1-A4F7-5ECB3CE792A9}</Project>
<Name>DynamoUtilities</Name>
</ProjectReference>
<ProjectReference Include="..\NodeServices\DynamoServices.csproj">
<Project>{ef879a10-041d-4c68-83e7-3192685f1bae}</Project>
<Name>DynamoServices</Name>
Expand Down
49 changes: 3 additions & 46 deletions src/DocumentationBrowserViewExtension/MarkdownHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System;
using System.IO;
using System.Linq;
using Dynamo.PackageManager;
using Markdig;
using Markdig.Parsers;
using Markdig.Renderers;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using Dynamo.Utilities;

namespace Dynamo.DocumentationBrowser
{
Expand All @@ -17,7 +11,7 @@ internal class MarkdownHandler
{
private const string NODE_ANNOTATION_NOT_FOUND = "Dynamo.DocumentationBrowser.Docs.NodeAnnotationNotFound.md";
private const string SYNTAX_HIGHLIGHTING = "Dynamo.DocumentationBrowser.Docs.syntaxHighlight.html";
private readonly MarkdownPipeline pipeline;
private readonly MD2HTML converter = new MD2HTML();


private static MarkdownHandler instance;
Expand All @@ -32,10 +26,6 @@ internal static MarkdownHandler Instance

private MarkdownHandler()
{
var pipelineBuilder = new MarkdownPipelineBuilder();
pipeline = pipelineBuilder
.UseAdvancedExtensions()
.Build();
}

/// <summary>
Expand Down Expand Up @@ -86,17 +76,11 @@ internal bool ParseToHtml(ref StringWriter writer, string nodeNamespace)
}
scriptTagsRemoved = false;

var renderer = new HtmlRenderer(writer);
pipeline.Setup(renderer);
converter.ParseToHtml(ref writer, mdString, mdFilePath);

var document = MarkdownParser.Parse(mdString, pipeline);
ConvertRelativeLocalImagePathsToAbsolute(mdFilePath, document);

renderer.Render(document);
// inject the syntax highlighting script at the bottom at the document.
writer.WriteLine(DocumentationBrowserUtils.GetDPIScript());
writer.WriteLine(GetSyntaxHighlighting());

return scriptTagsRemoved;
}

Expand All @@ -112,32 +96,5 @@ private static string GetSyntaxHighlighting()

return syntaxHighlightingContent;
}

/// <summary>
/// For markdown local images needs to be in the same folder as the md file
/// referencing it with a relative path "./image.png", when we convert to html
/// we need the full path. This method finds relative image paths and converts them to absolute paths.
/// </summary>
private static void ConvertRelativeLocalImagePathsToAbsolute(string mdFilePath, MarkdownDocument document)
{
var imageLinks = document.Descendants<ParagraphBlock>()
.SelectMany(x => x.Inline.Descendants<LinkInline>())
.Where(x => x.IsImage)
.Select(x => x).ToList();

foreach (var image in imageLinks)
{
if (!image.Url.StartsWith("./"))
continue;

var imageName = image.Url.Split(new string[] { "./" }, StringSplitOptions.None);
var dir = Path.GetDirectoryName(mdFilePath);

var htmlImagePathPrefix = @"file:///";
var absoluteImagePath = Path.Combine(dir, imageName.Last());

image.Url = $"{htmlImagePathPrefix}{absoluteImagePath}";
}
}
}
}
3 changes: 2 additions & 1 deletion src/DynamoUtilities/DynamoUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="DataMarshaler.cs" />
<Compile Include="EnumerableExtensions.cs" />
<Compile Include="Guid.cs" />
<Compile Include="MD2HTML.cs" />
<Compile Include="ModifierKeys.cs" />
<Compile Include="Option.cs" />
<Compile Include="OrderedSet.cs" />
Expand Down Expand Up @@ -89,4 +90,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
78 changes: 78 additions & 0 deletions src/DynamoUtilities/MD2HTML.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace Dynamo.Utilities
{
public class MD2HTML
{
private readonly Process process = new Process();
public MD2HTML()
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(ProcessExit);

ProcessStartInfo startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,

UseShellExecute = false,
Arguments = @"",
FileName = ToolPath()
};

process.StartInfo = startInfo;
process.Start();
}

~MD2HTML()
{
KillProcess();
}

public void ParseToHtml(ref StringWriter writer, string mdString, string mdPath)
{
process.StandardInput.WriteLine("<<<<<Convert>>>>>");
process.StandardInput.WriteLine(mdPath);
process.StandardInput.WriteLine(mdString);
process.StandardInput.WriteLine("<<<<<Eod>>>>>");
var done = false;
while(!done)
{
var line = process.StandardOutput.ReadLine();
if (line == null || line == "<<<<<Eod>>>>>")
{
done = true;
}
else
{
if (!string.IsNullOrWhiteSpace(line))
{
writer.WriteLine(line);
}
}
}
}

private static string ToolPath ()
{
var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new ArgumentNullException("Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)");
var toolPath = Path.Combine(rootPath, @"MD2HTML\MD2HTML.exe");
return toolPath;
}

private void KillProcess()
{
if (!process.HasExited)
{
process.Kill();
}
}
private void ProcessExit(object sender, EventArgs e)
{
KillProcess();
}
}
}

0 comments on commit bbefc36

Please sign in to comment.