Skip to content

Commit

Permalink
Release 0.7.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
VinsWorldcom committed Feb 13, 2023
2 parents 1cb71fe + c67124d commit 397ec80
Show file tree
Hide file tree
Showing 53 changed files with 1,955 additions and 350 deletions.
File renamed without changes.
50 changes: 50 additions & 0 deletions MarkdigWrapper/Markdig/SyntaxHighlighting/StyleToCssWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using ColorCode;
using ColorCode.Styling;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkdigWrapper.Markdig.SyntaxHighlighting
{
public class StyleToCssWriter
{
//Usage
//StyleToCssWriter.WriteStylesToCssFile(styleSheet.Styles, @"C:\development\NppMarkdownPanel\out.css");

private static string WriteColorToHex(Color color)
{
return string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
}

public static void WriteStylesToCssFile(StyleDictionary styles, string filePath)
{
string completeCss = "";

foreach (var s in styles)
{
string cssClass = "";
cssClass += "." + s.CssClassName + " {\n";
if (s.Background != null)
cssClass += " background-color: " + WriteColorToHex(s.Background) + ";\n";
if (s.Foreground != null)
cssClass += " color: " + WriteColorToHex(s.Foreground)+ ";\n";
if (s.Bold)
{
cssClass += " font-weight: bold;\n";
}
if (s.Italic)
{
cssClass += " font-style: italic;\n";
}
cssClass += "}\n";

completeCss += cssClass;

}
File.WriteAllText(filePath, completeCss);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
using System.IO;
using System.Text;
using ColorCode;
using ColorCode.Formatting;
using Markdig.Parsers;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Syntax;

namespace Markdig.SyntaxHighlighting {
public class SyntaxHighlightingCodeBlockRenderer : HtmlObjectRenderer<CodeBlock> {
namespace Markdig.SyntaxHighlighting
{
public class SyntaxHighlightingCodeBlockRenderer : HtmlObjectRenderer<CodeBlock>
{
private readonly CodeBlockRenderer _underlyingRenderer;
private readonly IStyleSheet _customCss;

public SyntaxHighlightingCodeBlockRenderer(CodeBlockRenderer underlyingRenderer = null, IStyleSheet customCss = null) {
public SyntaxHighlightingCodeBlockRenderer(CodeBlockRenderer underlyingRenderer = null, IStyleSheet customCss = null)
{
_underlyingRenderer = underlyingRenderer ?? new CodeBlockRenderer();
_customCss = customCss;
}

protected override void Write(HtmlRenderer renderer, CodeBlock obj) {
protected override void Write(HtmlRenderer renderer, CodeBlock obj)
{
var fencedCodeBlock = obj as FencedCodeBlock;
var parser = obj.Parser as FencedCodeBlockParser;
if (fencedCodeBlock == null || parser == null) {
if (fencedCodeBlock == null || parser == null)
{
_underlyingRenderer.Write(renderer, obj);
return;
}

var attributes = obj.TryGetAttributes() ?? new HtmlAttributes();

var languageMoniker = fencedCodeBlock.Info.Replace(parser.InfoPrefix, string.Empty);
if (string.IsNullOrEmpty(languageMoniker)) {
if (string.IsNullOrEmpty(languageMoniker))
{
_underlyingRenderer.Write(renderer, obj);
return;
}
Expand All @@ -51,36 +58,45 @@ protected override void Write(HtmlRenderer renderer, CodeBlock obj) {
renderer.WriteLine("</div>");
}

private string ApplySyntaxHighlighting(string languageMoniker, string firstLine, string code) {
private string ApplySyntaxHighlighting(string languageMoniker, string firstLine, string code)
{
var languageTypeAdapter = new LanguageTypeAdapter();
var language = languageTypeAdapter.Parse(languageMoniker, firstLine);

if (language?.Id == null) { // TODO: handle unrecognised language formats, e.g. when using mermaid diagrams
if (language?.Id == null)
{ // TODO: handle unrecognised language formats, e.g. when using mermaid diagrams
return code;
}

var codeBuilder = new StringBuilder();
var codeWriter = new StringWriter(codeBuilder);
var styleSheet = _customCss ?? StyleSheets.Default;
var colourizer = new CodeColorizer();
colourizer.Colorize(code, language, Formatters.Default, styleSheet, codeWriter);
var htmlFormatter = new HtmlClassFormatter();
colourizer.Colorize(code, language, htmlFormatter, styleSheet, codeWriter);
return codeBuilder.ToString();
}

private static string GetCode(LeafBlock obj, out string firstLine) {
private static string GetCode(LeafBlock obj, out string firstLine)
{
var code = new StringBuilder();
firstLine = null;
foreach (var line in obj.Lines.Lines) {
foreach (var line in obj.Lines.Lines)
{
var slice = line.Slice;
if (slice.Text == null) {
if (slice.Text == null)
{
continue;
}

var lineText = slice.Text.Substring(slice.Start, slice.Length);

if (firstLine == null) {
if (firstLine == null)
{
firstLine = lineText;
} else {
}
else
{
code.AppendLine();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Markdig;
using Markdig.Extensions.Yaml;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkdigWrapper.Markdig.YamlFrontMatter
{
public class YamlFrontMatterAsCodeBlockExtension : IMarkdownExtension
{
public void Setup(MarkdownPipelineBuilder pipeline) { }

public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
{
if (renderer.ObjectRenderers.Contains<YamlFrontMatterHtmlRenderer>())
{
var yamlFrontMatterRenderer = renderer.ObjectRenderers.FindExact<YamlFrontMatterHtmlRenderer>();
renderer.ObjectRenderers.Remove(yamlFrontMatterRenderer);
}

if (!renderer.ObjectRenderers.Contains<YamlFrontMatterAsCodeBlockHtmlRenderer>())
{
renderer.ObjectRenderers.InsertBefore<CodeBlockRenderer>(new YamlFrontMatterAsCodeBlockHtmlRenderer());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Markdig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkdigWrapper.Markdig.YamlFrontMatter
{
public static class YamlFrontMatterAsCodeBlockExtensions
{
public static MarkdownPipelineBuilder UseRenderYamlFrontMatterAsCodeBlock(this MarkdownPipelineBuilder pipeline)
{
pipeline.Extensions.Add(new YamlFrontMatterAsCodeBlockExtension());
return pipeline;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Markdig.Extensions.Yaml;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkdigWrapper.Markdig.YamlFrontMatter
{
public class YamlFrontMatterAsCodeBlockHtmlRenderer : HtmlObjectRenderer<YamlFrontMatterBlock>
{
private CodeBlockRenderer codeBlockRenderer;
public YamlFrontMatterAsCodeBlockHtmlRenderer()
{
codeBlockRenderer = new CodeBlockRenderer();
}
protected override void Write(HtmlRenderer renderer, YamlFrontMatterBlock obj)
{
// Render Yaml Frontmatter as simple code block
codeBlockRenderer.Write(renderer, obj);
}
}
}
23 changes: 22 additions & 1 deletion MarkdigWrapper/MarkdigMarkdownGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Markdig;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Syntax;
using Markdig.SyntaxHighlighting;
using MarkdigWrapper.Markdig.YamlFrontMatter;

namespace MarkdigWrapper
{
Expand All @@ -19,14 +21,19 @@ public MarkdigMarkdownGenerator()
{
}

public string ConvertToHtml(string markDownText, string filepath)
public string ConvertToHtml(string markDownText, string filepath, bool supportEscapeCharsInImageUris)
{
var sb = new StringBuilder();
var htmlWriter = new StringWriter(sb);
var htmlRenderer = new HtmlRenderer(htmlWriter);

var pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
// YamlFrontMatter block needs to be parsed with UseYamlFrontMatter()
// and is then rendered as code block with UseRenderYamlFrontMatterAsCodeBlock()
.UseYamlFrontMatter()
.UseRenderYamlFrontMatterAsCodeBlock()
// Syntax Highlighting
.UseSyntaxHighlighting()
.UsePreciseSourceLocation()
.Build();
Expand Down Expand Up @@ -56,6 +63,7 @@ public string ConvertToHtml(string markDownText, string filepath)

htmlWriter.Flush();
var result = sb.ToString();
if (supportEscapeCharsInImageUris) result = UnescapeImageUris(result);
return result;
}

Expand All @@ -73,5 +81,18 @@ private void SetLineNoAttributeOnAllBlocks(ContainerBlock rootBlock)
}

}

private string UnescapeImageUris(string html)
{
// Unescape URI with % characters for non - US - ASCII characters in order to workaround
// a bug under IE/Edge with local file links containing non US-ASCII chars.
// using System.Text.RegularExpressions;
//string inp = " * %25%20x : `<img src=\"file:///C:/tmp/test%20nonAscii%20path/A%C4%85C%C4%87E/A%C4%84%2520(2).png\" />`";
//outp: * %25%20x : `<img src="file:///C:/tmp/test nonAscii path/AąCćE/AĄ%20(2).png" />`
Regex regex = new Regex("src=\"file:///[^\"]+");
return regex.Replace(html, m => {
return Uri.UnescapeDataString(m.Value);
});
}
}
}
13 changes: 9 additions & 4 deletions MarkdigWrapper/MarkdigWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="LanguageTypeAdapter.cs" />
<Compile Include="Markdig\SyntaxHighlighting\StyleToCssWriter.cs" />
<Compile Include="Markdig\YamlFrontMatter\YamlFrontMatterAsCodeBlockExtensions.cs" />
<Compile Include="Markdig\YamlFrontMatter\YamlFrontMatterAsCodeBlockHtmlRenderer.cs" />
<Compile Include="Markdig\YamlFrontMatter\YamlFrontMatterAsCodeBlockExtension.cs" />
<Compile Include="Markdig\SyntaxHighlighting\LanguageTypeAdapter.cs" />
<Compile Include="MarkdigMarkdownGenerator.cs" />
<Compile Include="SyntaxHighlightingCodeBlockRenderer.cs" />
<Compile Include="SyntaxHighlightingExtension.cs" />
<Compile Include="SyntaxHighlightingExtensions.cs" />
<Compile Include="Markdig\SyntaxHighlighting\SyntaxHighlightingCodeBlockRenderer.cs" />
<Compile Include="Markdig\SyntaxHighlighting\SyntaxHighlightingExtension.cs" />
<Compile Include="Markdig\SyntaxHighlighting\SyntaxHighlightingExtensions.cs" />
<Compile Include="Wrapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -66,5 +70,6 @@
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
4 changes: 2 additions & 2 deletions MarkdigWrapper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyVersion("0.7.3.1")]
[assembly: AssemblyFileVersion("0.7.3.1")]
4 changes: 2 additions & 2 deletions MarkdigWrapper/Wrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public Wrapper()
markdigMarkdownGenerator = new MarkdigMarkdownGenerator();
}

public string ConvertToHtml(string markDownText, string filepath)
public string ConvertToHtml(string markDownText, string filepath, bool supportEscapeCharsInImageUris)
{
return markdigMarkdownGenerator.ConvertToHtml(markDownText, filepath);
return markdigMarkdownGenerator.ConvertToHtml(markDownText, filepath, supportEscapeCharsInImageUris);
}
}
}
10 changes: 9 additions & 1 deletion NppMarkdownPanel/Forms/AboutForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand All @@ -15,7 +16,14 @@ public partial class AboutForm : Form
public AboutForm()
{
InitializeComponent();
tbAbout.Text = MainResources.AboutDialogText;
var versionString = "0.X";
try
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
versionString = version.ToString();
}
catch (Exception) { }
tbAbout.Text = string.Format(MainResources.AboutDialogText, versionString);
btnOk.Focus();
this.ActiveControl = btnOk;
}
Expand Down
12 changes: 7 additions & 5 deletions NppMarkdownPanel/Forms/MainResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 397ec80

Please sign in to comment.