Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Partial docfx fix - Missing some plugins #958

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/docs/.idea/.idea.DocumentationTools/.idea/.gitignore

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

1 change: 1 addition & 0 deletions src/docs/.idea/.idea.DocumentationTools/.idea/.name

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

4 changes: 4 additions & 0 deletions src/docs/.idea/.idea.DocumentationTools/.idea/encodings.xml

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

8 changes: 8 additions & 0 deletions src/docs/.idea/.idea.DocumentationTools/.idea/indexLayout.xml

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

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

22 changes: 22 additions & 0 deletions src/docs/.idea/.idea.DocumentationTools/.idea/misc.xml

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

6 changes: 6 additions & 0 deletions src/docs/.idea/.idea.DocumentationTools/.idea/vcs.xml

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

45 changes: 45 additions & 0 deletions src/docs/LuceneDocsPlugins/EnvironmentVariableProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Docfx.Common;
using Docfx.Plugins;
using System.Collections.Immutable;
using System.Composition;
using System.IO;
using System.Linq;

namespace LuceneDocsPlugins;

[Export(nameof(EnvironmentVariableProcessor), typeof(IPostProcessor))]
public class EnvironmentVariableProcessor : IPostProcessor
{
public ImmutableDictionary<string, object> PrepareMetadata(ImmutableDictionary<string, object> metadata)
{
return metadata;
}

public Manifest Process(Manifest manifest, string outputFolder)
{
foreach (var manifestItem in manifest.Files.Where(x => x.Type == "Conceptual"))
{
foreach (var manifestItemOutputFile in manifestItem.Output)
{
var outputPath = Path.Combine(outputFolder, manifestItemOutputFile.Value.RelativePath);

var content = File.ReadAllText(outputPath);

Logger.LogInfo($"Replacing environment variables in {outputPath}");

var newContent = EnvironmentVariableUtil.ReplaceEnvironmentVariables(content);

if (content == newContent)
{
continue;
}

Logger.LogInfo($"Writing new content to {outputPath}");

File.WriteAllText(outputPath, newContent);
}
}

return manifest;
}
}
41 changes: 19 additions & 22 deletions src/docs/LuceneDocsPlugins/EnvironmentVariableUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Docfx.Common;
using System;
using System.Text;
using System.Text.RegularExpressions;

Expand All @@ -21,46 +22,42 @@ namespace LuceneDocsPlugins
* limitations under the License.
*/

public class EnvironmentVariableUtil
public partial class EnvironmentVariableUtil
{
public const string EnvVarRegexString = @"[\<\[]EnvVar\:(\w+?)[\>\]]";
public static readonly Regex EnvVarRegex = new Regex("^" + EnvVarRegexString, RegexOptions.Compiled);
public static readonly Regex EnvVarRegexInline = new Regex(EnvVarRegexString, RegexOptions.Compiled | RegexOptions.Multiline);
[GeneratedRegex(@"EnvVar\:(\w+)", RegexOptions.Compiled)]
private static partial Regex EnvVarRegex();


public static string ReplaceEnvironmentVariables(string sourceText, Match match)
=> ReplaceEnvironmentVariables(sourceText, match, reuse: null);

public static string ReplaceEnvironmentVariables(string sourceText, Match match, StringBuilder reuse)
public static string ReplaceEnvironmentVariables(string sourceText)
{
if (match.Success)
var matches = EnvVarRegex().Matches(sourceText);

if (matches.Count > 0)
{
if (reuse is null)
reuse = new StringBuilder();
else
reuse.Clear();
var sb = new StringBuilder(sourceText.Length);

int lastMatchEnd = 0;

do
foreach (Match match in matches)
{
var envVar = match.Groups[1].Value;

// Append the prefix that didn't match the regex (or text since last match)
reuse.Append(sourceText.Substring(lastMatchEnd, match.Index - lastMatchEnd));
sb.Append(sourceText.AsSpan(lastMatchEnd, match.Index - lastMatchEnd));

// Do the replacement of the regex match
reuse.Append(Environment.GetEnvironmentVariable(envVar));
string envVarValue = Environment.GetEnvironmentVariable(envVar);
sb.Append(envVarValue);

Logger.LogInfo($"Replaced environment variable '{envVar}' with value '{envVarValue}' on match {match.Value}");
lastMatchEnd = match.Index + match.Length;

} while ((match = match.NextMatch()).Success);
}

// Append the suffix that didn't match the regex
reuse.Append(sourceText.Substring(lastMatchEnd));
sb.Append(sourceText.AsSpan(lastMatchEnd));

return reuse.ToString();
return sb.ToString();
}

return sourceText;
}
}
Expand Down
124 changes: 62 additions & 62 deletions src/docs/LuceneDocsPlugins/LuceneDfmEngineCustomizer.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using Microsoft.DocAsCode.Dfm;
using Microsoft.DocAsCode.MarkdownLite;
//using System;
//using System.Collections.Generic;
//using System.Collections.Immutable;
//using System.Composition;
//using Microsoft.DocAsCode.Dfm;
//using Microsoft.DocAsCode.MarkdownLite;

namespace LuceneDocsPlugins
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//namespace LuceneDocsPlugins
//{
// /*
// * Licensed to the Apache Software Foundation (ASF) under one or more
// * contributor license agreements. See the NOTICE file distributed with
// * this work for additional information regarding copyright ownership.
// * The ASF licenses this file to You under the Apache License, Version 2.0
// * (the "License"); you may not use this file except in compliance with
// * the License. You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */

/// <summary>
/// Exports our custom markdown parser via MEF to DocFx
/// </summary>
[Export(typeof(IDfmEngineCustomizer))]
public class LuceneDfmEngineCustomizer : IDfmEngineCustomizer
{
public void Customize(DfmEngineBuilder builder, IReadOnlyDictionary<string, object> parameters)
{
// insert inline rule at the top
builder.InlineRules = builder.InlineRules.Insert(0, new EnvironmentVariableInLinkInlineRule());
builder.InlineRules = builder.InlineRules.Insert(0, new EnvironmentVariableInlineRule());
// LUCENENET TODO: The inline text replacement still isn't working. Not sure why.
//builder.InlineRules = builder.InlineRules.Insert(0, new EnvironmentVariableDfmTextInlineRule());
// /// <summary>
// /// Exports our custom markdown parser via MEF to DocFx
// /// </summary>
// [Export(typeof(IDfmEngineCustomizer))]
// public class LuceneDfmEngineCustomizer : IDfmEngineCustomizer
// {
// public void Customize(DfmEngineBuilder builder, IReadOnlyDictionary<string, object> parameters)
// {
// // insert inline rule at the top
// builder.InlineRules = builder.InlineRules.Insert(0, new EnvironmentVariableInLinkInlineRule());
// builder.InlineRules = builder.InlineRules.Insert(0, new EnvironmentVariableInlineRule());
// // LUCENENET TODO: The inline text replacement still isn't working. Not sure why.
// //builder.InlineRules = builder.InlineRules.Insert(0, new EnvironmentVariableDfmTextInlineRule());

//// Find the MarkdownLinkInlineRule position, and insert rules to replace text within the link before it
//var markdownLinkInlineRuleIndex = builder.InlineRules.FindIndex(r => r is MarkdownLinkInlineRule);
//builder.InlineRules.Insert(markdownLinkInlineRuleIndex, new EnvironmentVariableInLinkInlineRule());
// //// Find the MarkdownLinkInlineRule position, and insert rules to replace text within the link before it
// //var markdownLinkInlineRuleIndex = builder.InlineRules.FindIndex(r => r is MarkdownLinkInlineRule);
// //builder.InlineRules.Insert(markdownLinkInlineRuleIndex, new EnvironmentVariableInLinkInlineRule());

//builder.InlineRules = Replace<DfmTextInlineRule, EnvironmentVariableDfmTextInlineRule>(builder.InlineRules);
// //builder.InlineRules = Replace<DfmTextInlineRule, EnvironmentVariableDfmTextInlineRule>(builder.InlineRules);

// insert block rule above header rule. Why? I dunno, that's what the docs say:
// https://dotnet.github.io/docfx/tutorial/intro_markdown_lite.html#select-token-kind
var blockIndex = builder.BlockRules.FindIndex(r => r is MarkdownHeadingBlockRule);
builder.BlockRules = builder.BlockRules.Insert(blockIndex, new LuceneNoteBlockRule());
// // insert block rule above header rule. Why? I dunno, that's what the docs say:
// // https://dotnet.github.io/docfx/tutorial/intro_markdown_lite.html#select-token-kind
// var blockIndex = builder.BlockRules.FindIndex(r => r is MarkdownHeadingBlockRule);
// builder.BlockRules = builder.BlockRules.Insert(blockIndex, new LuceneNoteBlockRule());

// LUCENENET TODO: The code replacement still isn't working (or even matching). Not sure why.
//builder.BlockRules = Replace<MarkdownCodeBlockRule, EnvironmentVariableInCodeBlockRule>(builder.BlockRules);
}
// // LUCENENET TODO: The code replacement still isn't working (or even matching). Not sure why.
// //builder.BlockRules = Replace<MarkdownCodeBlockRule, EnvironmentVariableInCodeBlockRule>(builder.BlockRules);
// }

private static ImmutableList<IMarkdownRule> Replace<TSource, TReplacement>(ImmutableList<IMarkdownRule> blockRules)
where TSource : IMarkdownRule
where TReplacement : IMarkdownRule, new()
{
var index = blockRules.FindIndex(item => item is TSource);
if (index < 0)
{
throw new ArgumentException($"{typeof(TSource).Name} should exist!");
}
blockRules = blockRules.Insert(index, new TReplacement());
return blockRules.RemoveAt(index + 1);
}
}
}
// private static ImmutableList<IMarkdownRule> Replace<TSource, TReplacement>(ImmutableList<IMarkdownRule> blockRules)
// where TSource : IMarkdownRule
// where TReplacement : IMarkdownRule, new()
// {
// var index = blockRules.FindIndex(item => item is TSource);
// if (index < 0)
// {
// throw new ArgumentException($"{typeof(TSource).Name} should exist!");
// }
// blockRules = blockRules.Insert(index, new TReplacement());
// return blockRules.RemoveAt(index + 1);
// }
// }
//}
32 changes: 8 additions & 24 deletions src/docs/LuceneDocsPlugins/LuceneDocsPlugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ under the License.
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyTitle>LuceneDocsPlugins</AssemblyTitle>
<Product>LuceneDocsPlugins</Product>
</PropertyGroup>

<PropertyGroup>
<LangVersion>7.3</LangVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup Label="Version Info">
Expand All @@ -39,32 +39,16 @@ under the License.
<PropertyGroup Label="Assembly Publishing">
<IsPublishable>true</IsPublishable>
</PropertyGroup>

<PropertyGroup Label="Assembly Signing">
<!-- Ensure this doesn't inherit the strong naming since this tool will no work with a signed assembly due to it's references -->
<SignAssembly>false</SignAssembly>
</PropertyGroup>

<ItemGroup>
<Compile Remove="packages\**" />
<EmbeddedResource Remove="packages\**" />
<None Remove="packages\**" />
</ItemGroup>

<ItemGroup>
<None Include="app.config" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Composition" Version="1.0.31" />
<PackageReference Include="Microsoft.DocAsCode.Dfm" Version="2.58.0" />
<PackageReference Include="YamlDotNet" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Web" />
<PackageReference Include="Docfx.Plugins" Version="2.76.0" />
<PackageReference Include="Docfx.Common" Version="2.76.0" />
<PackageReference Include="System.Composition" Version="8.0.0" />
</ItemGroup>

</Project>
</Project>
Loading