-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,141 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using MarkdownWikiGenerator; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ConsoleApp1; | ||
|
||
public static class Dump | ||
{ | ||
public static void Factory() | ||
{ | ||
var emptyCommentTable = Enumerable.Empty<string>().ToLookup(x => x, _ => new XmlDocumentComment()); | ||
var t = new MarkdownableType(typeof(R3.Observable), emptyCommentTable); | ||
Console.WriteLine(t.ToString()); | ||
} | ||
|
||
public static void Operator() | ||
{ | ||
var emptyCommentTable = Enumerable.Empty<string>().ToLookup(x => x, _ => new XmlDocumentComment()); | ||
var t = new MarkdownableType(typeof(R3.ObservableExtensions), emptyCommentTable); | ||
Console.WriteLine(t.ToString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#nullable disable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Metadata; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace MarkdownWikiGenerator | ||
{ | ||
public static class Beautifier | ||
{ | ||
public static string BeautifyType(Type t, bool isFull = false) | ||
{ | ||
if (t == null) return ""; | ||
if (t == typeof(void)) return "void"; | ||
if (t.IsArray) | ||
{ | ||
var innerFormat = BeautifyType(t.GetElementType(), isFull); | ||
return innerFormat + "[]"; | ||
} | ||
|
||
if (!t.IsGenericType) return (isFull) ? t.FullName : t.Name; | ||
|
||
{ | ||
var innerFormat = string.Join(", ", t.GetGenericArguments().Select(x => BeautifyType(x))); | ||
return Regex.Replace(isFull ? t.GetGenericTypeDefinition().FullName : t.GetGenericTypeDefinition().Name, @"`.+$", "") + "<" + innerFormat + ">"; | ||
} | ||
} | ||
|
||
public static string ToMarkdownMethodInfo(MethodInfo methodInfo) | ||
{ | ||
var isExtension = IsExtensionMethod(methodInfo); | ||
|
||
var seq = methodInfo.GetParameters().Select(x => | ||
{ | ||
var isParams = IsParamsParameter(x); | ||
var refKind = IsOutParameter(x) ? "out " : IsRefParameter(x) ? "ref " : IsInParameter(x) ? "in " : ""; | ||
var prefix = isParams ? "params " : refKind; | ||
var suffix = x.HasDefaultValue ? (" = " + (x.DefaultValue ?? $"default")) : ""; | ||
return prefix + "`" + BeautifyType(x.ParameterType) + "` " + x.Name + suffix; | ||
}); | ||
|
||
// NOTE: modify ** | ||
return "**" + methodInfo.Name + "**" + "(" + (isExtension ? "this " : "") + string.Join(", ", seq) + ")"; | ||
} | ||
|
||
static bool IsExtensionMethod(MethodInfo method) | ||
{ | ||
return method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false); | ||
} | ||
|
||
static bool IsParamsParameter(ParameterInfo parameter) | ||
{ | ||
return parameter.IsDefined(typeof(ParamArrayAttribute), false); | ||
} | ||
|
||
static bool IsOutParameter(ParameterInfo parameter) | ||
{ | ||
return parameter.IsOut; | ||
} | ||
|
||
static bool IsRefParameter(ParameterInfo parameter) | ||
{ | ||
return parameter.ParameterType.IsByRef && !parameter.IsOut; | ||
} | ||
|
||
static bool IsInParameter(ParameterInfo parameter) | ||
{ | ||
return parameter.IsIn && parameter.ParameterType.IsByRef; | ||
} | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
sandbox/ConsoleApp1/MarkdownGenerator/MarkdownBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#nullable disable | ||
|
||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace MarkdownWikiGenerator | ||
{ | ||
public class MarkdownBuilder | ||
{ | ||
public static string MarkdownCodeQuote(string code) | ||
{ | ||
return "`" + code + "`"; | ||
} | ||
|
||
|
||
StringBuilder sb = new StringBuilder(); | ||
|
||
public void Append(string text) | ||
{ | ||
sb.Append(text); | ||
} | ||
|
||
public void AppendLine() | ||
{ | ||
sb.AppendLine(); | ||
} | ||
|
||
public void AppendLine(string text) | ||
{ | ||
sb.AppendLine(text); | ||
} | ||
|
||
public void Header(int level, string text) | ||
{ | ||
for (int i = 0; i < level; i++) | ||
{ | ||
sb.Append("#"); | ||
} | ||
sb.Append(" "); | ||
sb.AppendLine(text); | ||
} | ||
|
||
public void HeaderWithCode(int level, string code) | ||
{ | ||
for (int i = 0; i < level; i++) | ||
{ | ||
sb.Append("#"); | ||
} | ||
sb.Append(" "); | ||
CodeQuote(code); | ||
sb.AppendLine(); | ||
} | ||
|
||
public void HeaderWithLink(int level, string text, string url) | ||
{ | ||
for (int i = 0; i < level; i++) | ||
{ | ||
sb.Append("#"); | ||
} | ||
sb.Append(" "); | ||
Link(text, url); | ||
sb.AppendLine(); | ||
} | ||
|
||
public void Link(string text, string url) | ||
{ | ||
sb.Append("["); | ||
sb.Append(text); | ||
sb.Append("]"); | ||
sb.Append("("); | ||
sb.Append(url); | ||
sb.Append(")"); | ||
} | ||
|
||
public void Image(string altText, string imageUrl) | ||
{ | ||
sb.Append("!"); | ||
Link(altText, imageUrl); | ||
} | ||
|
||
public void Code(string language, string code) | ||
{ | ||
sb.Append("```"); | ||
sb.AppendLine(language); | ||
sb.AppendLine(code); | ||
sb.AppendLine("```"); | ||
} | ||
|
||
public void CodeQuote(string code) | ||
{ | ||
sb.Append("`"); | ||
sb.Append(code); | ||
sb.Append("`"); | ||
} | ||
|
||
public void Table(string[] headers, IEnumerable<string[]> items) | ||
{ | ||
sb.Append("| "); | ||
foreach (var item in headers) | ||
{ | ||
sb.Append(item); | ||
sb.Append(" | "); | ||
} | ||
sb.AppendLine(); | ||
|
||
sb.Append("| "); | ||
foreach (var item in headers) | ||
{ | ||
sb.Append("---"); | ||
sb.Append(" | "); | ||
} | ||
sb.AppendLine(); | ||
|
||
|
||
foreach (var item in items) | ||
{ | ||
sb.Append("| "); | ||
foreach (var item2 in item) | ||
{ | ||
sb.Append(item2); | ||
sb.Append(" | "); | ||
} | ||
sb.AppendLine(); | ||
} | ||
sb.AppendLine(); | ||
} | ||
|
||
public void List(string text) // nest zero | ||
{ | ||
sb.Append("- "); | ||
sb.AppendLine(text); | ||
} | ||
|
||
public void ListLink(string text, string url) // nest zero | ||
{ | ||
sb.Append("- "); | ||
Link(text, url); | ||
sb.AppendLine(); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return sb.ToString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.