forked from StereoKit/StereoKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocField.cs
86 lines (72 loc) · 2.17 KB
/
DocField.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace StereoKitDocumenter
{
class DocField : IDocItem
{
public string name;
public string summary;
public string remarks;
public DocClass parent;
public List<DocExample> examples = new List<DocExample>();
public bool IsStatic { get; private set;}
public DocField(DocClass aParent, string aName)
{
parent = aParent;
name = aName;
parent.fields.Add(this);
}
public string Name => $"{parent.Name}.{name}";
public string FileName => Path.Combine(Program.options.PagesOut + "Reference/", parent.Name + "/" + name + ".md");
public string UrlName => $"{{{{site.url}}}}/Pages/Reference/{parent.Name}/{name}.html";
public Type GetFieldType(Type classType) {
Type result = classType.GetField(name)?.FieldType;
if (result == null)
result = classType.GetProperty(name)?.PropertyType;
return result;
}
public bool GetStatic(Type classType)
{
FieldInfo fInfo = classType.GetField(name);
if (fInfo != null)
return fInfo.IsStatic;
PropertyInfo pInfo = classType.GetProperty(name);
if (pInfo != null)
return pInfo.GetAccessors(true).Any(x => x.IsStatic);
return false;
}
public void AddExample(DocExample aExample) => examples.Add(aExample);
public override string ToString()
{
Type classType = parent.ClassType;
Type fieldType = GetFieldType(classType);
string exampleText = "";
if (examples.Count > 0) {
exampleText = "\n\n## Examples\n\n";
for (int i = 0; i < examples.Count; i++) {
exampleText += examples[i].data;
}
}
string signature = (GetStatic(classType) ? "static " : "") + $"{StringHelper.TypeName(fieldType.Name)} {name}";
PropertyInfo pInfo = classType.GetProperty(name);
if (pInfo != null)
signature += $"{{ {(pInfo.CanRead?"get ":"")}{(pInfo.CanWrite ? "set " : "")}}}";
return $@"---
layout: default
title: {parent.Name}.{name}
description: {StringHelper.CleanForDescription(summary)}
---
# [{parent.Name}]({parent.UrlName}).{name}
<div class='signature' markdown='1'>
{signature}
</div>
## Description
{summary}
{exampleText}
";
}
}
}