Skip to content

Commit

Permalink
Attempting to modify the markdig markdown engine to process special t…
Browse files Browse the repository at this point in the history
…ags inside the triple-slash comments ... but can't get it to work so will revert back to dfm, just keeping this here for history
  • Loading branch information
Shazwazza committed Aug 21, 2018
1 parent 7ca803a commit efb0b00
Show file tree
Hide file tree
Showing 21 changed files with 386 additions and 142 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>..\..\..\..\websites\apidocs\lucenetemplate\plugins\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand All @@ -37,20 +37,32 @@
<Reference Include="HtmlAgilityPack, Version=1.4.9.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.4.9\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.Common, Version=2.24.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.Common.2.24.0\lib\net461\Microsoft.DocAsCode.Common.dll</HintPath>
<Reference Include="Markdig, Version=0.15.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Markdig.0.15.1\lib\net40\Markdig.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.Dfm, Version=2.24.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.Dfm.2.24.0\lib\net461\Microsoft.DocAsCode.Dfm.dll</HintPath>
<Reference Include="Microsoft.DocAsCode.Common, Version=2.38.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.Common.2.38.1\lib\net461\Microsoft.DocAsCode.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.MarkdownLite, Version=2.24.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.MarkdownLite.2.24.0\lib\net461\Microsoft.DocAsCode.MarkdownLite.dll</HintPath>
<Reference Include="Microsoft.DocAsCode.Dfm, Version=2.38.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.Dfm.2.38.1\lib\net461\Microsoft.DocAsCode.Dfm.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.Plugins, Version=2.24.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.Plugins.2.24.0\lib\net461\Microsoft.DocAsCode.Plugins.dll</HintPath>
<Reference Include="Microsoft.DocAsCode.MarkdigEngine, Version=2.38.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.MarkdigEngine.2.38.1\lib\net461\Microsoft.DocAsCode.MarkdigEngine.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.YamlSerialization, Version=2.24.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.YamlSerialization.2.24.0\lib\net461\Microsoft.DocAsCode.YamlSerialization.dll</HintPath>
<Reference Include="Microsoft.DocAsCode.MarkdigEngine.Extensions, Version=2.38.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.MarkdigEngine.Extensions.2.38.1\lib\net461\Microsoft.DocAsCode.MarkdigEngine.Extensions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.MarkdigEngine.Validators, Version=2.38.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.MarkdigEngine.Validators.2.38.1\lib\net461\Microsoft.DocAsCode.MarkdigEngine.Validators.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.MarkdownLite, Version=2.38.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.MarkdownLite.2.38.1\lib\net461\Microsoft.DocAsCode.MarkdownLite.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.Plugins, Version=2.38.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.Plugins.2.38.1\lib\net461\Microsoft.DocAsCode.Plugins.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DocAsCode.YamlSerialization, Version=2.38.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DocAsCode.YamlSerialization.2.38.1\lib\net461\Microsoft.DocAsCode.YamlSerialization.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand All @@ -76,23 +88,34 @@
<HintPath>..\packages\System.Composition.TypedParts.1.0.31\lib\portable-net45+win8+wp8+wpa81\System.Composition.TypedParts.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="YamlDotNet, Version=4.1.0.0, Culture=neutral, PublicKeyToken=ec19458f3c15af5e, processorArchitecture=MSIL">
<HintPath>..\packages\YamlDotNet.Signed.4.1.0\lib\net35\YamlDotNet.dll</HintPath>
<Reference Include="YamlDotNet, Version=4.2.2.0, Culture=neutral, PublicKeyToken=ec19458f3c15af5e, processorArchitecture=MSIL">
<HintPath>..\packages\YamlDotNet.Signed.4.2.2\lib\net35\YamlDotNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="LuceneDfmEngineCustomizer.cs" />
<Compile Include="LuceneNoteBlockRule.cs" />
<Compile Include="LuceneNoteBlockToken.cs" />
<Compile Include="LuceneRendererPartProvider.cs" />
<Compile Include="LuceneTokenRendererPart.cs" />
<Compile Include="LuceneExperimentalBlock.cs" />
<Compile Include="LuceneExperimentalBlockParser.cs" />
<Compile Include="LuceneExperimentalBlockRenderer.cs" />
<Compile Include="LuceneExperimentalInline.cs" />
<Compile Include="LuceneExperimentalInlineParser.cs" />
<Compile Include="LuceneExperimentalInlineRenderer.cs" />
<Compile Include="LuceneExperimentalMarkdownExtension.cs" />
<Compile Include="LuceneMarkdigExtensions.cs" />
<Compile Include="LuceneMarkdownService.cs" />
<Compile Include="LuceneMarkdigServiceProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionHelper.cs" />
<Compile Include="TagMatcher.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Markdig.Parsers;
using Markdig.Syntax;

namespace LuceneDocsPlugins
{
public class LuceneExperimentalBlock : LeafBlock
{
public string MatchType { get; }

public LuceneExperimentalBlock(BlockParser parser, string matchType) : base(parser)
{
MatchType = matchType;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Markdig.Parsers;
using Markdig.Syntax.Inlines;

namespace LuceneDocsPlugins
{
//TODO: Make an inline parser and see if that works

public class LuceneExperimentalBlockParser : BlockParser
{

public LuceneExperimentalBlockParser()
{
OpeningCharacters = new[] { '@' };
}

public override BlockState TryOpen(BlockProcessor processor)
{
if (processor.IsCodeIndent)
{
return BlockState.None;
}

var line = processor.Line;

var matchType = TagMatcher.GetMatch(line);

if (matchType == null)
return BlockState.None;

var luceneExperimentalBlock = new LuceneExperimentalBlock(this, matchType);

processor.NewBlocks.Push(luceneExperimentalBlock);

return BlockState.BreakDiscard;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Markdig.Renderers;
using Markdig.Renderers.Html;

namespace LuceneDocsPlugins
{
public class LuceneExperimentalBlockRenderer : HtmlObjectRenderer<LuceneExperimentalBlock>
{
private const string Message = "This is a Lucene.NET {0} API, use at your own risk";

protected override void Write(HtmlRenderer renderer, LuceneExperimentalBlock inclusion)
{
renderer.Write("<div class=\"lucene-block lucene-" + inclusion.MatchType.ToLower() + "\">" + string.Format(Message, inclusion.MatchType.ToUpper()) + "</div>");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Markdig.Syntax.Inlines;

namespace LuceneDocsPlugins
{
public class LuceneExperimentalInline : LeafInline
{
public string MatchType { get; }

public LuceneExperimentalInline(string matchType)
{
MatchType = matchType;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Markdig.Helpers;
using Markdig.Parsers;

namespace LuceneDocsPlugins
{
public class LuceneExperimentalInlineParser : InlineParser
{
public LuceneExperimentalInlineParser()
{
OpeningCharacters = new[] { '@' };
}

public override bool Match(InlineProcessor processor, ref StringSlice slice)
{
var matchType = TagMatcher.GetMatch(slice);

if (matchType == null)
return false;

var luceneExperimentalInline = new LuceneExperimentalInline(matchType);

processor.Inline = luceneExperimentalInline;
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Markdig.Renderers;
using Markdig.Renderers.Html;

namespace LuceneDocsPlugins
{
public class LuceneExperimentalInlineRenderer : HtmlObjectRenderer<LuceneExperimentalInline>
{
private const string Message = "This is a Lucene.NET {0} API, use at your own risk";

protected override void Write(HtmlRenderer renderer, LuceneExperimentalInline inclusion)
{
renderer.Write("<div class=\"lucene-block lucene-" + inclusion.MatchType.ToLower() + "\">" + string.Format(Message, inclusion.MatchType.ToUpper()) + "</div>");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Markdig;
using Markdig.Renderers;
using Microsoft.DocAsCode.MarkdigEngine.Extensions;

namespace LuceneDocsPlugins
{
public class LuceneExperimentalMarkdownExtension : IMarkdownExtension
{
public void Setup(MarkdownPipelineBuilder pipeline)
{
pipeline.BlockParsers.Insert(0, new LuceneExperimentalBlockParser());
pipeline.InlineParsers.Insert(0, new LuceneExperimentalInlineParser());

//pipeline.BlockParsers.AddIfNotAlready<LuceneExperimentalBlockParser>();
//pipeline.InlineParsers.AddIfNotAlready<LuceneExperimentalInlineParser>();
}

public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
{
if (!(renderer is HtmlRenderer htmlRenderer)) return;

if (!htmlRenderer.ObjectRenderers.Contains<LuceneExperimentalBlockRenderer>())
{
htmlRenderer.ObjectRenderers.Insert(0, new LuceneExperimentalBlockRenderer());
}

if (!htmlRenderer.ObjectRenderers.Contains<LuceneExperimentalInlineRenderer>())
{
htmlRenderer.ObjectRenderers.Insert(0, new LuceneExperimentalInlineRenderer());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Markdig;

namespace LuceneDocsPlugins
{
public static class LuceneMarkdigExtensions
{
public static MarkdownPipelineBuilder UseLuceneExperimental(this MarkdownPipelineBuilder pipeline)
{
var extensions = pipeline.Extensions;
if (!extensions.Contains<LuceneExperimentalMarkdownExtension>())
{
extensions.Insert(0, new LuceneExperimentalMarkdownExtension());
}
return pipeline;
}

public static MarkdownPipeline UseLuceneExperimental(this MarkdownPipeline pipeline)
{
var extensions = pipeline.Extensions;
if (!extensions.Contains<LuceneExperimentalMarkdownExtension>())
{
extensions.Insert(0, new LuceneExperimentalMarkdownExtension());
}
return pipeline;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Composition;
using Microsoft.DocAsCode.MarkdigEngine;
using Microsoft.DocAsCode.Plugins;

namespace LuceneDocsPlugins
{
[Export("markdig-custom", typeof(IMarkdownServiceProvider))]
public class LuceneMarkdigServiceProvider : IMarkdownServiceProvider
{
[Import]
public ICompositionContainer Container { get; set; }

public IMarkdownService CreateMarkdownService(MarkdownServiceParameters parameters)
{
return new LuceneMarkdownService(parameters, this.Container);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using Markdig;
using Microsoft.DocAsCode.Common;
using Microsoft.DocAsCode.MarkdigEngine;
using Microsoft.DocAsCode.MarkdigEngine.Extensions;
using Microsoft.DocAsCode.Plugins;

namespace LuceneDocsPlugins
{
public class LuceneMarkdownService : MarkdigMarkdownService, IMarkdownService
{
public LuceneMarkdownService(MarkdownServiceParameters parameters, ICompositionContainer container = null)
: base(parameters, container)
{
}

string IMarkdownService.Name => "markdig-custom";

MarkupResult IMarkdownService.Markup(string content, string filePath)
{
return this.Markup(content, filePath, false);
}

MarkupResult IMarkdownService.Markup(string content, string filePath, bool enableValidation)
{
if (content == null)
throw new ArgumentNullException(nameof(content));
if (filePath == null)
throw new ArgumentException("file path can't be null or empty.");

//TODO: Need to use reflection here because of how docfx is structured right now
var markdownPipeline = (MarkdownPipeline)ReflectionHelper.CallMethod(this, "CreateMarkdownPipeline", false, enableValidation);
//Add our extesions directly to the MarkdownPipeline (they don't need to be added to the MarkdownPipelineBuilder
markdownPipeline.UseLuceneExperimental();

using (InclusionContext.PushFile((RelativePath)filePath))
return new MarkupResult()
{
Html = Markdown.ToHtml(content, markdownPipeline),
Dependency = InclusionContext.Dependencies.Select(file => (string)(RelativePath)file).ToImmutableArray()
};
}
}
}
Loading

0 comments on commit efb0b00

Please sign in to comment.