mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-08-02 10:15:58 +08:00
Add initial docs
This commit is contained in:

committed by
Patrik Svensson

parent
decb887b0a
commit
f1912b1d44
38
docs/src/Constants.cs
Normal file
38
docs/src/Constants.cs
Normal file
@ -0,0 +1,38 @@
|
||||
namespace Docs
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
public const string NoContainer = nameof(NoContainer);
|
||||
public const string NoSidebar = nameof(NoSidebar);
|
||||
public const string Topic = nameof(Topic);
|
||||
public const string EditLink = nameof(EditLink);
|
||||
public const string Description = nameof(Description);
|
||||
public const string Hidden = nameof(Hidden);
|
||||
|
||||
public static class Colors
|
||||
{
|
||||
public const string Url = "https://raw.githubusercontent.com/spectresystems/spectre.console/main/resources/scripts/Generator/Data/colors.json";
|
||||
public const string Root = "COLORS_ROOT";
|
||||
}
|
||||
|
||||
public static class Site
|
||||
{
|
||||
public const string Owner = "SITE_OWNER";
|
||||
public const string Repository = "SITE_REPOSITORY";
|
||||
public const string Branch = "SITE_BRANCH";
|
||||
}
|
||||
|
||||
public static class Deployment
|
||||
{
|
||||
public const string GitHubToken = "GITHUB_TOKEN";
|
||||
public const string TargetBranch = "DEPLOYMENT_TARGET_BRANCH";
|
||||
}
|
||||
|
||||
public static class Sections
|
||||
{
|
||||
public const string Splash = nameof(Splash);
|
||||
public const string Sidebar = nameof(Sidebar);
|
||||
public const string Subtitle = nameof(Subtitle);
|
||||
}
|
||||
}
|
||||
}
|
30
docs/src/Extensions/BootstrapperExtensions.cs
Normal file
30
docs/src/Extensions/BootstrapperExtensions.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using Statiq.App;
|
||||
using Statiq.Common;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Docs
|
||||
{
|
||||
public static class BootstrapperExtensions
|
||||
{
|
||||
public static Bootstrapper ConfigureSite(this Bootstrapper bootstrapper, string owner, string repo, string branch)
|
||||
{
|
||||
if (bootstrapper != null)
|
||||
{
|
||||
bootstrapper.AddSetting(Constants.Site.Owner, owner);
|
||||
bootstrapper.AddSetting(Constants.Site.Repository, repo);
|
||||
bootstrapper.AddSetting(Constants.Site.Branch, branch);
|
||||
}
|
||||
return bootstrapper;
|
||||
}
|
||||
|
||||
public static Bootstrapper ConfigureDeployment(this Bootstrapper bootstrapper, string deployBranch)
|
||||
{
|
||||
if (bootstrapper != null)
|
||||
{
|
||||
bootstrapper.AddSetting(Constants.Deployment.TargetBranch, deployBranch);
|
||||
}
|
||||
return bootstrapper;
|
||||
}
|
||||
}
|
||||
}
|
33
docs/src/Extensions/DocumentExtensions.cs
Normal file
33
docs/src/Extensions/DocumentExtensions.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using Statiq.Common;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Docs
|
||||
{
|
||||
public static class DocumentExtensions
|
||||
{
|
||||
public static string GetDescription(this IDocument document)
|
||||
{
|
||||
return document?.GetString(Constants.Description, string.Empty) ?? string.Empty;
|
||||
}
|
||||
|
||||
public static bool HasVisibleChildren(this IDocument document)
|
||||
{
|
||||
if (document != null)
|
||||
{
|
||||
return document.HasChildren() && document.GetChildren().Any(x => x.IsVisible());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsVisible(this IDocument document)
|
||||
{
|
||||
return !document.GetBool(Constants.Hidden, false);
|
||||
}
|
||||
|
||||
public static IEnumerable<IDocument> OnlyVisible(this IEnumerable<IDocument> source)
|
||||
{
|
||||
return source.Where(x => x.IsVisible());
|
||||
}
|
||||
}
|
||||
}
|
10
docs/src/Extensions/StringExtensions.cs
Normal file
10
docs/src/Extensions/StringExtensions.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace Docs
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static bool IsNotEmpty(this string source)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(source);
|
||||
}
|
||||
}
|
||||
}
|
92
docs/src/Models/Color.cs
Normal file
92
docs/src/Models/Color.cs
Normal file
@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Docs.Models
|
||||
{
|
||||
public sealed class Color
|
||||
{
|
||||
public int Number { get; set; }
|
||||
public string Hex { get; set; }
|
||||
public string Name { get; set; }
|
||||
public Rgb Rgb { get; set; }
|
||||
public string ClrName { get; set; }
|
||||
|
||||
public int R => Rgb.R;
|
||||
public int G => Rgb.G;
|
||||
public int B => Rgb.B;
|
||||
|
||||
private static Dictionary<int, string> ClrNames { get; } = new Dictionary<int, string>
|
||||
{
|
||||
{ 0, "Black" },
|
||||
{ 1, "DarkRed" },
|
||||
{ 2, "DarkGreen" },
|
||||
{ 3, "DarkYellow" },
|
||||
{ 4, "DarkBlue" },
|
||||
{ 5, "DarkMagenta" },
|
||||
{ 6, "DarkCyan" },
|
||||
{ 7, "Gray" },
|
||||
{ 8, "DarkGray" },
|
||||
{ 9, "Red" },
|
||||
{ 10, "Green" },
|
||||
{ 11, "Yellow" },
|
||||
{ 12, "Blue" },
|
||||
{ 13, "Magenta" },
|
||||
{ 14, "Cyan" },
|
||||
{ 15, "White" },
|
||||
};
|
||||
|
||||
public static IEnumerable<Color> Parse(string json)
|
||||
{
|
||||
var source = JsonConvert.DeserializeObject<List<Color>>(json);
|
||||
|
||||
var check = new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var color in source.OrderBy(c => c.Number))
|
||||
{
|
||||
if (ClrNames.TryGetValue(color.Number, out var clrName))
|
||||
{
|
||||
color.ClrName = clrName;
|
||||
}
|
||||
else
|
||||
{
|
||||
color.ClrName = string.Empty;
|
||||
}
|
||||
|
||||
if (!check.ContainsKey(color.Name))
|
||||
{
|
||||
check.Add(color.Name, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
var newName = (string)null;
|
||||
for (int i = 1; i < 100; i++)
|
||||
{
|
||||
if (!check.ContainsKey($"{color.Name}_{i}"))
|
||||
{
|
||||
newName = $"{color.Name}_{i}";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (newName == null)
|
||||
{
|
||||
throw new InvalidOperationException("Impossible!");
|
||||
}
|
||||
|
||||
check.Add(newName, color);
|
||||
color.Name = newName;
|
||||
}
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Rgb
|
||||
{
|
||||
public int R { get; set; }
|
||||
public int G { get; set; }
|
||||
public int B { get; set; }
|
||||
}
|
||||
}
|
14
docs/src/Models/ColorModel.cs
Normal file
14
docs/src/Models/ColorModel.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Docs.Models
|
||||
{
|
||||
public sealed class ColorModel
|
||||
{
|
||||
public List<Color> Colors { get; set; }
|
||||
|
||||
public ColorModel(IEnumerable<Color> colors)
|
||||
{
|
||||
Colors = new List<Color>(colors);
|
||||
}
|
||||
}
|
||||
}
|
41
docs/src/Pipelines/ColorsPipeline.cs
Normal file
41
docs/src/Pipelines/ColorsPipeline.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Docs.Models;
|
||||
using NJsonSchema;
|
||||
using Statiq.Common;
|
||||
using Statiq.Core;
|
||||
|
||||
namespace Docs.Pipelines
|
||||
{
|
||||
public class ColorsPipeline : Pipeline
|
||||
{
|
||||
public const string Url = "https://raw.githubusercontent.com/spectresystems/spectre.console/main/resources/scripts/Generator/Data/colors.json";
|
||||
|
||||
public ColorsPipeline()
|
||||
{
|
||||
InputModules = new ModuleList
|
||||
{
|
||||
new ExecuteConfig(
|
||||
Config.FromContext(ctx => {
|
||||
return new ReadWeb(Url);
|
||||
}))
|
||||
};
|
||||
|
||||
ProcessModules = new ModuleList
|
||||
{
|
||||
new ExecuteConfig(
|
||||
Config.FromDocument(async (doc, ctx) =>
|
||||
{
|
||||
var colors = Color.Parse(await doc.GetContentStringAsync()).ToList();
|
||||
var definitions = new List<IDocument> { colors.ToDocument(Constants.Colors.Root) };
|
||||
|
||||
return doc.Clone(new MetadataDictionary
|
||||
{
|
||||
[Constants.Colors.Root] = definitions
|
||||
});
|
||||
}))
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
22
docs/src/Pipelines/DeploymentPipeline.cs
Normal file
22
docs/src/Pipelines/DeploymentPipeline.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Statiq.Common;
|
||||
using Statiq.Core;
|
||||
using Statiq.Web.GitHub;
|
||||
|
||||
namespace Docs.Pipelines
|
||||
{
|
||||
public class DeploymentPipeline : Pipeline
|
||||
{
|
||||
public DeploymentPipeline()
|
||||
{
|
||||
Deployment = true;
|
||||
OutputModules = new ModuleList
|
||||
{
|
||||
new DeployGitHubPages(
|
||||
Config.FromSetting<string>(Constants.Site.Owner),
|
||||
Config.FromSetting<string>(Constants.Site.Repository),
|
||||
Config.FromSetting<string>(Constants.Deployment.GitHubToken))
|
||||
.ToBranch(Config.FromSetting<string>(Constants.Deployment.TargetBranch))
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
34
docs/src/Shortcodes/ChildrenShortcode.cs
Normal file
34
docs/src/Shortcodes/ChildrenShortcode.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using Statiq.Common;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Docs.Shortcodes
|
||||
{
|
||||
public class ChildrenShortcode : SyncShortcode
|
||||
{
|
||||
public override ShortcodeResult Execute(KeyValuePair<string, string>[] args, string content, IDocument document, IExecutionContext context)
|
||||
{
|
||||
var ul = new XElement("ul", new XAttribute("class", "list-group"));
|
||||
|
||||
foreach (var child in document.GetChildren().OnlyVisible())
|
||||
{
|
||||
var li = new XElement("li", new XAttribute("class", "list-group-item"));
|
||||
|
||||
var link = new XElement("a", new XAttribute("href", child.GetLink()));
|
||||
link.Add(child.GetTitle());
|
||||
li.Add(link);
|
||||
|
||||
var description = child.GetDescription();
|
||||
if (description.IsNotEmpty())
|
||||
{
|
||||
li.Add(new XElement("br"));
|
||||
li.Add(new XElement("i", description));
|
||||
}
|
||||
|
||||
ul.Add(li);
|
||||
}
|
||||
|
||||
return ul.ToString();
|
||||
}
|
||||
}
|
||||
}
|
59
docs/src/Shortcodes/ColorTableShortcode.cs
Normal file
59
docs/src/Shortcodes/ColorTableShortcode.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NJsonSchema;
|
||||
using Statiq.Common;
|
||||
using System.Xml.Linq;
|
||||
using Docs.Pipelines;
|
||||
using Docs.Models;
|
||||
|
||||
namespace Docs.Shortcodes
|
||||
{
|
||||
public class ColorTableShortcode : SyncShortcode
|
||||
{
|
||||
private const string ColorStyle = "display: inline-block;width: 60px; height: 15px;";
|
||||
|
||||
public override ShortcodeResult Execute(KeyValuePair<string, string>[] args, string content, IDocument document, IExecutionContext context)
|
||||
{
|
||||
// Get the definition.
|
||||
var colors = context.Outputs
|
||||
.FromPipeline(nameof(ColorsPipeline))
|
||||
.First()
|
||||
.GetChildren(Constants.Colors.Root)
|
||||
.OfType<ObjectDocument<List<Color>>>()
|
||||
.First().Object;
|
||||
|
||||
var table = new XElement("table", new XAttribute("class", "table"));
|
||||
var header = new XElement("tr", new XAttribute("class", "color-row"));
|
||||
header.Add(new XElement("th", ""));
|
||||
header.Add(new XElement("th", "Name"));
|
||||
header.Add(new XElement("th", "RGB"));
|
||||
header.Add(new XElement("th", "Hex"));
|
||||
header.Add(new XElement("th", "System.ConsoleColor"));
|
||||
table.Add(header);
|
||||
|
||||
foreach (var color in colors)
|
||||
{
|
||||
var rep = new XElement("td",
|
||||
new XElement("span",
|
||||
new XAttribute("class", "color-representation"),
|
||||
new XAttribute("style", $"background-color:{color.Hex};")));
|
||||
var name = new XElement("td", new XElement("code", color.Name.ToLower()));
|
||||
var rgb = new XElement("td", new XElement("code", $"{color.R},{color.G},{color.B}"));
|
||||
var hex = new XElement("td", new XElement("code", color.Hex));
|
||||
var clr = new XElement("td", new XElement("code", color.ClrName));
|
||||
|
||||
// Create row
|
||||
var row = new XElement("tr");
|
||||
row.Add(rep);
|
||||
row.Add(name);
|
||||
row.Add(rgb);
|
||||
row.Add(hex);
|
||||
row.Add(clr);
|
||||
table.Add(row);
|
||||
}
|
||||
|
||||
return table.ToString();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user