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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using Microsoft.Ddue.Tools;
namespace Microsoft.Ddue.Tools
{
/// <summary>
/// Sandcastle component converting Microsoft Help 2.0 output to Microsoft Help System output.
/// </summary>
public class MSHCComponent : BuildComponent
{
// component tag names in the configuration file
private class ConfigurationTag
{
public const string Data = "data";
}
// component attribute names in the configuration file
private class ConfigurationAttr
{
public const string Locale = "locale";
public const string SelfBranded = "self-branded";
public const string TopicVersion = "topic-version";
public const string TocFile = "toc-file";
public const string TocParent = "toc-parent";
public const string TocParentVersion = "toc-parent-version";
}
// XPath expressions to navigate the TOC file
private class TocXPath
{
public const string Topics = "/topics";
public const string Topic = "topic";
}
// attribute names in the TOC file
private class TocAttr
{
public const string Id = "id";
}
// Microsoft Help 2.0 namespace info
private class Help2Namespace
{
public const string Prefix = "MSHelp";
public const string Uri = "http://msdn.microsoft.com/mshelp";
}
// XPath expressions to navigate Microsoft Help 2.0 data in the document
private class Help2XPath
{
public const string Head = "head";
public const string Xml = "xml";
public const string TocTitle = "MSHelp:TOCTitle";
public const string Attr = "MSHelp:Attr[@Name='{0}']";
public const string Keyword = "MSHelp:Keyword[@Index='{0}']";
}
// Microsoft Help 2.0 tag attributes in the document
private class Help2Attr
{
public const string Value = "Value";
public const string Term = "Term";
public const string Title = "Title";
}
// Microsoft Help 2.0 attribute values in the document
private class Help2Value
{
public const string K = "K";
public const string F = "F";
public const string Locale = "Locale";
public const string AssetID = "AssetID";
public const string DevLang = "DevLang";
public const string Abstract = "Abstract";
}
// Microsoft Help System tags
private class MHSTag
{
public const string Meta = "meta";
}
// Microsoft Help System meta tag attributes
private class MHSMetaAttr
{
public const string Name = "name";
public const string Content = "content";
}
// Microsoft Help System meta names
private class MHSMetaName
{
public const string SelfBranded = "SelfBranded";
public const string ContentType = "ContentType";
public const string Locale = "Microsoft.Help.Locale";
public const string TopicLocale = "Microsoft.Help.TopicLocale";
public const string Id = "Microsoft.Help.Id";
public const string TopicVersion = "Microsoft.Help.TopicVersion";
public const string TocParent = "Microsoft.Help.TocParent";
public const string TocParentVersion = "Microsoft.Help.TOCParentTopicVersion";
public const string TocOrder = "Microsoft.Help.TocOrder";
public const string Title = "Title";
public const string Keywords = "Microsoft.Help.Keywords";
public const string F1 = "Microsoft.Help.F1";
public const string Category = "Microsoft.Help.Category";
public const string Description = "Description";
}
// Microsoft Help System meta default values
private class MHSDefault
{
public const bool SelfBranded = true;
public const string Locale = "en-us";
public const string Reference = "Reference";
public const string TopicVersion = "100";
public const string TocParent = "-1";
public const string TocParentVersion = "100";
public const string TocFile = "./toc.xml";
public const string ShortName = "MHS";
}
// TOC information of a document
private class TocInfo
{
private string _parent;
private string _parentVersion;
private int _order;
public TocInfo(string parent, string parentVersion, int order)
{
_parent = parent;
_parentVersion = parentVersion;
_order = order;
}
public string Parent { get { return _parent; }}
public string ParentVersion { get { return _parentVersion; } }
public int Order { get { return _order; } }
}
private XmlDocument _document;
private XmlNode _head;
private XmlNode _xml;
private string _locale = string.Empty;
private bool _selfBranded = MHSDefault.SelfBranded;
private string _topicVersion = MHSDefault.TopicVersion;
private string _tocParent = MHSDefault.TocParent;
private string _tocParentVersion = MHSDefault.TocParentVersion;
private Dictionary<string, TocInfo> _toc = new Dictionary<string, TocInfo>();
/// <summary>
/// Creates a new instance of the <see cref="MHSComponent"/> class.
/// </summary>
/// <param name="assembler">The active <see cref="BuildAssembler"/>.</param>
/// <param name="configuration">The current <see cref="XPathNavigator"/> of the configuration.</param>
public MSHCComponent(BuildAssembler assembler, XPathNavigator configuration)
: base(assembler, configuration)
{
string tocFile = MHSDefault.TocFile;
XPathNavigator data = configuration.SelectSingleNode(ConfigurationTag.Data);
if (data != null)
{
string value = data.GetAttribute(ConfigurationAttr.Locale, string.Empty);
if (!string.IsNullOrEmpty(value))
_locale = value;
value = data.GetAttribute(ConfigurationAttr.SelfBranded, string.Empty);
if (!string.IsNullOrEmpty(value))
_selfBranded = bool.Parse(value);
value = data.GetAttribute(ConfigurationAttr.TopicVersion, string.Empty);
if (!string.IsNullOrEmpty(value))
_topicVersion = value;
value = data.GetAttribute(ConfigurationAttr.TocParent, string.Empty);
if (!string.IsNullOrEmpty(value))
_tocParent = value;
value = data.GetAttribute(ConfigurationAttr.TocParentVersion, string.Empty);
if (!string.IsNullOrEmpty(value))
_tocParentVersion = value;
value = data.GetAttribute(ConfigurationAttr.TocFile, string.Empty);
if (!string.IsNullOrEmpty(value))
tocFile = value;
}
LoadToc(Path.GetFullPath(Environment.ExpandEnvironmentVariables(tocFile)));
}
#region Public
/// <summary>
/// Applies Microsoft Help System transformation to the output document.
/// </summary>
/// <param name="document">The <see cref="XmlDocument"/> to apply transformation to.</param>
/// <param name="key">Topic key of the output document.</param>
public override void Apply(XmlDocument document, string key)
{
_document = document;
ModifyAttribute("id", "mainSection");
ModifyAttribute("class", "members");
FixHeaderBottomBackground("nsrBottom", "headerBottom");
XmlElement html = _document.DocumentElement;
_head = html.SelectSingleNode(Help2XPath.Head);
if (_head == null)
{
_head = document.CreateElement(Help2XPath.Head);
if (!html.HasChildNodes)
html.AppendChild(_head);
else
html.InsertBefore(_head, html.FirstChild);
}
AddMHSMeta(MHSMetaName.SelfBranded, _selfBranded.ToString().ToLower());
AddMHSMeta(MHSMetaName.ContentType, MHSDefault.Reference);
AddMHSMeta(MHSMetaName.TopicVersion, _topicVersion);
string locale = _locale;
string id = Guid.NewGuid().ToString();
_xml = _head.SelectSingleNode(Help2XPath.Xml);
if (_xml != null)
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(_document.NameTable);
if (!nsmgr.HasNamespace(Help2Namespace.Prefix))
nsmgr.AddNamespace(Help2Namespace.Prefix, Help2Namespace.Uri);
XmlElement elem = _xml.SelectSingleNode(Help2XPath.TocTitle, nsmgr) as XmlElement;
if (elem != null)
AddMHSMeta(MHSMetaName.Title, elem.GetAttribute(Help2Attr.Title));
foreach (XmlElement keyword in _xml.SelectNodes(string.Format(Help2XPath.Keyword, Help2Value.K), nsmgr))
AddMHSMeta(MHSMetaName.Keywords, keyword.GetAttribute(Help2Attr.Term), true);
foreach (XmlElement keyword in _xml.SelectNodes(string.Format(Help2XPath.Keyword, Help2Value.F), nsmgr))
AddMHSMeta(MHSMetaName.F1, keyword.GetAttribute(Help2Attr.Term), true);
foreach (XmlElement lang in _xml.SelectNodes(string.Format(Help2XPath.Attr, Help2Value.DevLang), nsmgr))
AddMHSMeta(MHSMetaName.Category, Help2Value.DevLang + ":" + lang.GetAttribute(Help2Attr.Value), true);
elem = _xml.SelectSingleNode(string.Format(Help2XPath.Attr, Help2Value.Abstract), nsmgr) as XmlElement;
if (elem != null)
AddMHSMeta(MHSMetaName.Description, elem.GetAttribute(Help2Attr.Value));
elem = _xml.SelectSingleNode(string.Format(Help2XPath.Attr, Help2Value.AssetID), nsmgr) as XmlElement;
if (elem != null)
id = elem.GetAttribute(Help2Attr.Value);
if (string.IsNullOrEmpty(locale))
{
elem = _xml.SelectSingleNode(string.Format(Help2XPath.Attr, Help2Value.Locale), nsmgr) as XmlElement;
if (elem != null)
locale = elem.GetAttribute(Help2Attr.Value);
}
}
if (string.IsNullOrEmpty(locale))
locale = MHSDefault.Locale;
AddMHSMeta(MHSMetaName.Locale, locale);
AddMHSMeta(MHSMetaName.TopicLocale, locale);
AddMHSMeta(MHSMetaName.Id, id);
if (_toc.ContainsKey(id))
{
TocInfo tocInfo = _toc[id];
AddMHSMeta(MHSMetaName.TocParent, tocInfo.Parent);
if (tocInfo.Parent != MHSDefault.TocParent)
AddMHSMeta(MHSMetaName.TocParentVersion, tocInfo.ParentVersion);
AddMHSMeta(MHSMetaName.TocOrder, tocInfo.Order.ToString());
}
}
#endregion
#region Private
// loads TOC structure from a file
private void LoadToc(string path)
{
_toc.Clear();
using (Stream stream = File.OpenRead(path))
{
XPathDocument document = new XPathDocument(stream);
XPathNavigator navigator = document.CreateNavigator();
LoadToc(navigator.SelectSingleNode(TocXPath.Topics), _tocParent, _tocParentVersion);
}
}
// loads TOC structure from an XPathNavigator
private void LoadToc(XPathNavigator navigator, string parent, string parentVersion)
{
int i = -1;
XPathNodeIterator interator = navigator.SelectChildren(TocXPath.Topic, string.Empty);
while (interator.MoveNext())
{
XPathNavigator current = interator.Current;
string id = current.GetAttribute(TocAttr.Id, string.Empty);
if (!string.IsNullOrEmpty(id))
{
TocInfo info = new TocInfo(parent, parentVersion, ++i);
_toc.Add(id, info);
LoadToc(current, id, _topicVersion);
}
}
}
// Adds Microsoft Help System meta data to the output document
private XmlElement AddMHSMeta(string name, string content)
{
return AddMHSMeta(name, content, false);
}
// Adds Microsoft Help System meta data to the output document
private XmlElement AddMHSMeta(string name, string content, bool multiple)
{
if (string.IsNullOrEmpty(content))
return null;
XmlElement elem = null;
if (!multiple)
elem = _document.SelectSingleNode(string.Format(@"//meta[@{0}]", name)) as XmlElement;
if (elem == null)
{
elem = _document.CreateElement(MHSTag.Meta);
elem.SetAttribute(MHSMetaAttr.Name, name);
elem.SetAttribute(MHSMetaAttr.Content, content);
_head.AppendChild(elem);
}
return elem;
}
// Modifies an attribute value to prevent conflicts with Microsoft Help System branding
private void ModifyAttribute(string name, string value)
{
XmlNodeList list = _document.SelectNodes(string.Format(@"//*[@{0}='{1}']", name, value));
foreach (XmlElement elem in list)
elem.SetAttribute(name, value + MHSDefault.ShortName);
}
// Works around a Microsoft Help System issue ('background' attribute isn't supported):
// adds a hidden image so that its path will be transformed by MHS runtime handler,
// sets the 'background' attribute to the transformed path on page load
private void FixHeaderBottomBackground(string className, string newId)
{
XmlElement elem = _document.SelectSingleNode(string.Format(@"//*[@class='{0}']", className)) as XmlElement;
if (elem == null)
return;
string src = elem.GetAttribute("background");
if (string.IsNullOrEmpty(src))
return;
elem.SetAttribute("id", newId);
XmlElement img = _document.CreateElement("img");
img.SetAttribute("src", src);
img.SetAttribute("id", newId + "Image");
img.SetAttribute("style", "display: none");
elem.AppendChild(img);
}
#endregion
}
}
|