forked from Claymore/SharpMediaWiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WikiPageSection.cs
111 lines (98 loc) · 2.83 KB
/
WikiPageSection.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections.Generic;
namespace Claymore.SharpMediaWiki
{
public class WikiPageSection
{
private string _text;
private readonly List<WikiPageSection> _subSections;
private string _rawTitle;
private string _title;
public string Title
{
get
{
return _title;
}
set
{
string filler = "";
for (int i = 0; i < Level; ++i)
{
filler += "=";
}
_title = value;
_rawTitle = filler + Title + filler;
}
}
public int Level { get; private set; }
public WikiPageSection(string title, int level, string text)
{
Level = level;
_text = text;
Title = title;
_subSections = new List<WikiPageSection>();
}
public WikiPageSection(string title, string rawTitle, int level, string text)
{
_rawTitle = rawTitle;
_title = title;
Level = level;
_text = text;
_subSections = new List<WikiPageSection>();
}
public string SectionText
{
get { return _text; }
set { _text = value; }
}
public IList<WikiPageSection> Subsections
{
get { return _subSections; }
}
public string Text
{
get
{
return string.Format("{0}\n{1}{2}",
_rawTitle,
_text,
string.Concat(_subSections.ConvertAll(s => s.Text).ToArray()));
}
}
public override string ToString()
{
return Title.Trim();
}
public void AddSubsection(WikiPageSection section)
{
int index = _subSections.Count - 1;
if (index >= 0 && index < _subSections.Count &&
_subSections[index].Level < section.Level)
{
_subSections[index].AddSubsection(section);
}
else
{
_subSections.Add(section);
}
}
public void ForEach(Action action)
{
foreach (WikiPageSection section in Subsections)
{
action(section);
}
}
public T Reduce<T>(T aggregator, ReduceAction<T> action)
{
T result = aggregator;
foreach (WikiPageSection section in Subsections)
{
result = action(section, result);
}
return result;
}
public delegate T ReduceAction<T>(WikiPageSection section, T aggregator);
public delegate void Action(WikiPageSection section);
}
}