Skip to content

Commit

Permalink
added overrides for AppendUrl & AppendLink IWriter extension methods (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Doraku committed Jun 20, 2022
1 parent 6d26601 commit 3149b89
Show file tree
Hide file tree
Showing 31 changed files with 108 additions and 43 deletions.
5 changes: 5 additions & 0 deletions source/DefaultDocumentation.Api/DefaultDocumentation.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
],
"LinksOutputFilePath": "..\\..\\documentation\\DefaultDocumentation.Api.txt",
"LinksBaseUrl": "https://github.com/Doraku/DefaultDocumentation/blob/master/documentation/api/",
"Sections": [
"FolderFileNameOverrides",
"Default"
],
"NamespaceDocItem": {
"Sections": [
"FolderFileNameOverrides",
"Title",
"summary",
"TableOfContents"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"DefaultDocumentation.Console": {
"commandName": "Project",
"commandLineArgs": "--ConfigurationFilePath D:\\Projects\\DefaultDocumentation\\source\\DefaultDocumentation.Api\\DefaultDocumentation.json",
"commandLineArgs": "--ConfigurationFilePath D:\\Projects\\DefaultDocumentation\\source\\DefaultDocumentation.Api\\DefaultDocumentation.json\r\n--Plugins \"D:\\Projects\\DefaultDocumentation\\source\\Sample\\DefaultDocumentation.PluginExample\\bin\\Debug\\netstandard2.0\\DefaultDocumentation.PluginExample.dll\"\r\n--FileNameFactory Folder",
"workingDirectory": "D:\\Projects\\DefaultDocumentation"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.IO;
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Markdown.Internal;

namespace DefaultDocumentation.Markdown.Elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Elements
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Globalization;
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Elements
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Elements
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;
using DefaultDocumentation.Models.Parameters;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Elements
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;
using DefaultDocumentation.Models.Parameters;

Expand Down
70 changes: 55 additions & 15 deletions source/DefaultDocumentation.Markdown/Extensions/IWriterExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation;

namespace DefaultDocumentation.Markdown.Extensions
namespace DefaultDocumentation.Api
{
/// <summary>
/// Provides extension methods on the <see cref="IWriter"/> type.
Expand All @@ -27,6 +27,8 @@ public static class IWriterExtension
private const string CurrentItemKey = "Markdown.CurrentItem";
private const string DisplayAsSingleLineKey = "Markdown.DisplayAsSingleLine";
private const string IgnoreLineBreakLineKey = "Markdown.IgnoreLineBreak";
private const string AppendLinkOverride = "Markdown.AppendLink(DocItem,string)";
private const string AppendUrlOverride = "Markdown.AppendUrl(string,string,string)";

/// <summary>
/// Gets the current item that is being processed by this <see cref="IWriter"/>.
Expand Down Expand Up @@ -98,6 +100,32 @@ public static IWriter SetIgnoreLineBreakLine(this IWriter writer, bool? value)
return writer;
}

/// <summary>
/// Sets an override of the default action for <see cref="AppendLink(IWriter, DocItem, string?)"/>.
/// </summary>
/// <param name="writer">The <see cref="IWriter"/> for which to set this override.</param>
/// <param name="value">The action to use.</param>
/// <returns>The given <see cref="IWriter"/>.</returns>
public static IWriter SetAppendLinkOverride(this IWriter writer, Action<IWriter, DocItem, string?>? value)
{
writer[AppendLinkOverride] = value;

return writer;
}

/// <summary>
/// Sets an override of the default action for <see cref="AppendUrl(IWriter, string, string?, string?)"/>.
/// </summary>
/// <param name="writer">The <see cref="IWriter"/> for which to set this override.</param>
/// <param name="value">The action to use.</param>
/// <returns>The given <see cref="IWriter"/>.</returns>
public static IWriter SetAppendUrlOverride(this IWriter writer, Action<IWriter, string, string?, string?>? value)
{
writer[AppendUrlOverride] = value;

return writer;
}

/// <summary>
/// Append an url in the markdown format.
/// </summary>
Expand All @@ -108,22 +136,27 @@ public static IWriter SetIgnoreLineBreakLine(this IWriter writer, bool? value)
/// <returns>The given <see cref="IWriter"/>.</returns>
public static IWriter AppendUrl(this IWriter writer, string url, string? displayedName = null, string? tooltip = null)
{
if (string.IsNullOrEmpty(url))
{
writer.Append((displayedName ?? "").Prettify());
}
else
static void Default(IWriter writer, string url, string? displayedName, string? tooltip)
{
writer
.Append("[")
.Append((displayedName ?? url).Prettify())
.Append("](")
.Append(url)
.Append(" '")
.Append(tooltip ?? url)
.Append("')");
if (string.IsNullOrEmpty(url))
{
writer.Append((displayedName ?? "").Prettify());
}
else
{
writer
.Append("[")
.Append((displayedName ?? url).Prettify())
.Append("](")
.Append(url)
.Append(" '")
.Append(tooltip ?? url)
.Append("')");
}
}

((writer[AppendUrlOverride] as Action<IWriter, string, string?, string?>) ?? Default)(writer, url, displayedName, tooltip);

return writer;
}

Expand All @@ -134,7 +167,14 @@ public static IWriter AppendUrl(this IWriter writer, string url, string? display
/// <param name="item">The <see cref="DocItem"/> to link to.</param>
/// <param name="displayedName">The displayed name of the link.</param>
/// <returns>The given <see cref="IWriter"/>.</returns>
public static IWriter AppendLink(this IWriter writer, DocItem item, string? displayedName = null) => writer.AppendUrl(writer.Context.GetUrl(item), displayedName ?? item.Name, item.FullName);
public static IWriter AppendLink(this IWriter writer, DocItem item, string? displayedName = null)
{
static void Default(IWriter writer, DocItem item, string? displayedName) => writer.AppendUrl(writer.Context.GetUrl(item), displayedName ?? item.Name, item.FullName);

((writer[AppendLinkOverride] as Action<IWriter, DocItem, string?>) ?? Default)(writer, item, displayedName);

return writer;
}

/// <summary>
/// Append an link to an id using <see cref="IGeneralContext.GetUrl(string)"/> to resolve the url in the markdown format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;
using DefaultDocumentation.Models.Members;
using DefaultDocumentation.Models.Parameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;
using DefaultDocumentation.Models.Members;
using DefaultDocumentation.Models.Parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models.Types;

namespace DefaultDocumentation.Markdown.Sections
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models.Members;

namespace DefaultDocumentation.Markdown.Sections
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Sections
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Linq;
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Sections
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models.Members;

namespace DefaultDocumentation.Markdown.Sections
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Sections
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;

namespace DefaultDocumentation.Markdown.Sections
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models.Members;
using DefaultDocumentation.Models.Types;
using ICSharpCode.Decompiler.Documentation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models.Types;
using ICSharpCode.Decompiler.TypeSystem;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Sections
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models.Members;
using DefaultDocumentation.Models.Types;
using ICSharpCode.Decompiler.TypeSystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Xml.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;

namespace DefaultDocumentation.Markdown.Sections
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;
using DefaultDocumentation.Models.Parameters;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;
using DefaultDocumentation.Models.Members;
using DefaultDocumentation.Models.Parameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;
using DefaultDocumentation.Models.Members;
using DefaultDocumentation.Models.Parameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models.Members;

namespace DefaultDocumentation.Markdown.Sections
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using DefaultDocumentation.Api;
using DefaultDocumentation.Markdown.Extensions;
using DefaultDocumentation.Models;

namespace DefaultDocumentation.Markdown.Writers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="DefaultDocumentation.Api" Version="0.8.1" />
<!--<PackageReference Include="DefaultDocumentation.Api" Version="0.8.1" />-->
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\DefaultDocumentation.Markdown\DefaultDocumentation.Markdown.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DefaultDocumentation.Api;
using DefaultDocumentation.Models;

namespace DefaultDocumentation.PluginExample
{
public sealed class FolderFileNameOverrides : ISection
{
private void AppendLink(IWriter writer, DocItem item, string displayedName)
{
if (item is ExternDocItem)
{
writer.AppendUrl(writer.Context.GetUrl(item), displayedName ?? item.Name, item.FullName);
}
else
{
string[] url = writer.Context.GetUrl(item).Split('/');
string[] pageUrl = writer.Context.GetUrl(writer.DocItem).Split('/');

List<string> relativeUrl = new();

int pathCount;
for (pathCount = 0; pathCount < pageUrl.Length && pathCount < url.Length && pageUrl[pathCount] == url[pathCount]; pathCount++)
{ }

relativeUrl.AddRange(Enumerable.Repeat("..", Math.Max(0, pageUrl.Length - pathCount - 1)));
relativeUrl.AddRange(url.Skip(pathCount));

writer.AppendUrl(string.Join("/", relativeUrl), displayedName ?? item.Name, item.FullName);
}
}

public string Name => nameof(FolderFileNameOverrides);

public void Write(IWriter writer)
{
writer.SetAppendLinkOverride(AppendLink);
}
}
}

0 comments on commit 3149b89

Please sign in to comment.