代码拉取完成,页面将自动刷新
using CsvHelper.DocsGenerator.Generators;
using CsvHelper.DocsGenerator.Infos;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace CsvHelper.DocsGenerator
{
public class Startup
{
public Startup Configure()
{
return this;
}
public Startup Run()
{
var outputDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "Output");
if (Directory.Exists(outputDirectoryPath))
{
Directory.Delete(outputDirectoryPath, true);
}
Directory.CreateDirectory(outputDirectoryPath);
var xmlDocs = XElement.Load("CsvHelper.xml");
var assemblyInfo = new AssemblyInfo(typeof(CsvHelperException).Assembly, xmlDocs);
GenerateMarkdownFiles(outputDirectoryPath, assemblyInfo);
GenerateToc(outputDirectoryPath, assemblyInfo);
return this;
}
private void GenerateMarkdownFiles(string outputDirectoryPath, AssemblyInfo assemblyInfo)
{
var documentGeneratorFactory = new DocumentGeneratorFactory();
// Write assembly file.
var documentGenerator = documentGeneratorFactory.Create(assemblyInfo);
var content = documentGenerator.Generate();
var filePath = Path.Combine(outputDirectoryPath, "api.md");
File.WriteAllText(filePath, content);
outputDirectoryPath = Path.Combine(outputDirectoryPath, "api");
Directory.CreateDirectory(outputDirectoryPath);
// Write namespace files and directories.
foreach (var @namespace in assemblyInfo.Namespaces)
{
var directoryPath = Path.Combine(outputDirectoryPath, @namespace.Namespace);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
documentGenerator = documentGeneratorFactory.Create(@namespace);
content = documentGenerator.Generate();
filePath = Path.Join(outputDirectoryPath, $"{@namespace.Namespace}.md");
File.WriteAllText(filePath, content);
}
// Write type files.
foreach (var @namespace in assemblyInfo.Namespaces)
{
var directoryPath = Path.Combine(outputDirectoryPath, @namespace.Namespace);
foreach (var typeInfo in @namespace.Types)
{
documentGenerator = documentGeneratorFactory.Create(typeInfo);
content = documentGenerator.Generate();
filePath = Path.Combine(directoryPath, $"{typeInfo.Type.Name}.md");
File.WriteAllText(filePath, content);
}
}
}
private void GenerateToc(string outputDirectoryPath, AssemblyInfo assemblyInfo)
{
var toc = new JObject
(
new JProperty("api", new JObject
(
new JProperty("title", "CsvHelper Namespaces"),
new JProperty("path", "api"),
new JProperty("children", new JArray
(
assemblyInfo.Namespaces.Select(namespaceInfo => new JObject(
new JProperty("title", namespaceInfo.Namespace),
new JProperty("path", $"api/{namespaceInfo.Namespace}"),
new JProperty("children", new JArray
(
namespaceInfo.Types.Select(typeInfo => new JObject
(
new JProperty("title", typeInfo.Type.Name),
new JProperty("path", $"api/{namespaceInfo.Namespace}/{typeInfo.Type.Name}")
))
))
))
))
))
);
var filePath = Path.Combine(outputDirectoryPath, "api.json");
File.WriteAllText(filePath, JsonConvert.SerializeObject(toc, Formatting.Indented));
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。