-
Notifications
You must be signed in to change notification settings - Fork 365
/
ListElement.cs
245 lines (202 loc) · 9.03 KB
/
ListElement.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//===============================================================================================================
// System : Sandcastle Tools - Sandcastle Tools Core Class Library
// File : ListElement.cs
// Author : Eric Woodruff (Eric@EWoodruff.us)
// Updated : 04/29/2023
// Note : Copyright 2022-2023, Eric Woodruff, All rights reserved
//
// This file contains the class used to handle list elements based on the topic type
//
// This code is published under the Microsoft Public License (Ms-PL). A copy of the license should be
// distributed with the code and can be found at the project website: https://GitHub.com/EWSoftware/SHFB. This
// notice, the author's name, and all copyright notices must remain intact in all applications, documentation,
// and source files.
//
// Date Who Comments
// ==============================================================================================================
// 01/14/2022 EFW Created the code
//===============================================================================================================
// Ignore Spelling: hoverable thead
using System;
using System.Xml.Linq;
namespace Sandcastle.Core.PresentationStyle.Transformation.Elements.Html
{
/// <summary>
/// This handles <c>list</c> elements based on the topic type
/// </summary>
public class ListElement : Element
{
#region Properties
//=====================================================================
/// <summary>
/// This is used to get or set the "no bullet" list style
/// </summary>
/// <value>The default if not set explicitly is <c>noBullet</c></value>
public string NoBulletStyle { get; set; } = "noBullet";
/// <summary>
/// This is used to get or set the overall table style
/// </summary>
/// <value>The default if not set explicitly is <c>table is-hoverable"</c></value>
public string TableStyle { get; set; } = "table is-hoverable";
#endregion
#region Constructor
//=====================================================================
/// <inheritdoc />
public ListElement() : base("list")
{
}
#endregion
#region Methods
//=====================================================================
/// <inheritdoc />
public override void Render(TopicTransformationCore transformation, XElement element)
{
if(transformation == null)
throw new ArgumentNullException(nameof(transformation));
if(element == null)
throw new ArgumentNullException(nameof(element));
if(transformation.IsMamlTopic)
this.RenderMamlList(transformation, element);
else
this.RenderXmlCommentsList(transformation, element);
if(transformation.SupportedFormats == HelpFileFormats.Markdown)
transformation.CurrentElement.Add("\n\n");
}
/// <summary>
/// Render a list element in a MAML topic
/// </summary>
/// <param name="transformation">The transformation in use</param>
/// <param name="element">The element to render</param>
private void RenderMamlList(TopicTransformationCore transformation, XElement element)
{
string elementName = "ul", start = null;
string styleName = null;
switch(element.Attribute("class")?.Value)
{
case "bullet":
break;
case "ordered":
elementName = "ol";
start = element.Attribute("start")?.Value;
break;
default:
styleName = this.NoBulletStyle;
break;
}
var list = new XElement(elementName);
if(styleName != null)
list.Add(new XAttribute("class", styleName));
if(!String.IsNullOrWhiteSpace(start))
list.Add(new XAttribute("start", start));
transformation.CurrentElement.Add(list);
transformation.RenderChildElements(list, element.Nodes());
}
/// <summary>
/// Render a list element in XML comments in an API topic
/// </summary>
/// <param name="transformation">The transformation in use</param>
/// <param name="element">The element to render</param>
private void RenderXmlCommentsList(TopicTransformationCore transformation, XElement element)
{
string elementName, start = null;
switch(element.Attribute("type")?.Value)
{
case "definition":
elementName = "dl";
break;
case "number":
elementName = "ol";
start = element.Attribute("start")?.Value;
break;
case "table":
elementName = "table";
break;
default: // "bullet", not specified, or unrecognized
elementName = "ul";
break;
}
var list = new XElement(elementName);
if(!String.IsNullOrWhiteSpace(start))
list.Add(new XAttribute("start", start));
transformation.CurrentElement.Add(list);
switch(elementName)
{
case "dl":
foreach(var item in element.Elements("item"))
{
XElement term = item.Element("term"), description = item.Element("description");
if(term != null && description != null)
{
var dt = new XElement("dt");
list.Add(dt);
transformation.RenderChildElements(dt, term.Nodes());
var dd = new XElement("dd");
list.Add(dd);
transformation.RenderChildElements(dd, description.Nodes());
}
}
break;
case "table":
XElement listHeader = element.Element("listheader");
if(!String.IsNullOrWhiteSpace(this.TableStyle) &&
transformation.SupportedFormats != HelpFileFormats.Markdown)
{
list.Add(new XAttribute("class", this.TableStyle));
}
if(listHeader != null)
{
var thead = new XElement("thead");
list.Add(thead);
var tr = new XElement("tr");
thead.Add(tr);
foreach(var cell in listHeader.Elements())
{
var th = new XElement("th");
tr.Add(th);
transformation.RenderChildElements(th, cell.Nodes());
}
}
foreach(var item in element.Elements("item"))
{
var tr = new XElement("tr");
list.Add(tr);
foreach(var cell in item.Elements())
{
var td = new XElement("td");
tr.Add(td);
transformation.RenderChildElements(td, cell.Nodes());
}
}
break;
default: // ol or ul
foreach(var item in element.Elements("item"))
{
XElement li = new XElement("li"), term = item.Element("term"),
description = item.Element("description");
list.Add(li);
if(term != null || description != null)
{
// If there's a term but no description, render the term as the description
if(term != null && description == null)
{
description = term;
term = null;
}
if(term != null)
{
var strong = new XElement("strong");
li.Add(strong, " \u2013 ");
transformation.RenderChildElements(strong, term.Nodes());
}
if(description != null)
transformation.RenderChildElements(li, description.Nodes());
}
else
transformation.RenderChildElements(li, item.Nodes());
}
break;
}
}
#endregion
}
}