-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEditorBase.cs
1868 lines (1688 loc) · 76.4 KB
/
EditorBase.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using MDSDK;
using MDSDKDerived;
using Microsoft.VisualBasic;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Metrics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime;
using System.Text.RegularExpressions;
using System.Transactions;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;
using static System.Net.Mime.MediaTypeNames;
namespace MDSDKBase
{
internal enum EditorBaseFileInfoExistenceRequirements
{
FileMustAlreadyExist,
FileMustNotAlreadyExist
}
/// <summary>
/// Abstract base class for a markdown file editor, whether the document is a topic, toc, or other.
/// Provides common query and update services, maintains a dirty flag, and saves the document on
/// request. The SDK skeleton project extends EditorBase with a class named Editor. Editor
/// is the class you add app-specific services to. Augment EditorBase only with common facilities.
/// </summary>
internal abstract class EditorBase
{
/// <summary>
/// The underlying FileInfo object representing the document on disk.
/// </summary>
public FileInfo? FileInfo = null;
/// <summary>
/// The string object representing the entire file contents.
/// </summary>
protected string? fileContents = null;
/// <summary>
/// A string collection representing each line of the file.
/// </summary>
protected List<string> fileLines = new List<string>();
/// <summary>
/// The XDocument object representing the xml document. TODO: delete this when possible; it's a holdover from the WDCML version.
/// </summary>
protected XDocument? xDocument = null;
/// <summary>
/// Represents whether the document is dirty (has unsaved changes) or not. Calling
/// <see cref="EditorBase.CheckOutAndSaveChangesIfDirty"/> only has an effect if
/// <see cref="EditorBase.IsDirty"/> is true. The <see cref="EditorBase"/> should
/// manage this value itself, but if necessary you can force the desired behavior by setting this field.
/// </summary>
public bool IsDirty = false;
public static string TBDSentenceString = "TBD";
public static string NoneSentenceString = "None.";
public static string YamlFrontmatterDelimiter = "---";
public static string CodeBlockSyntaxStartDelimiter = "```syntax";
public static string CodeBlockSyntaxXsdDelimiter = "```XSD";
public static string CodeBlockEndDelimiter = "```";
private static Regex DeconstructHyperlinkRegex = new Regex(@"\[(?<link_text>.*)\]\((?<link_url>.*)\)", RegexOptions.Compiled);
private static Regex TwoSpacesRegex = new Regex(" ", RegexOptions.Compiled);
private static Regex MsAssetIdRegex = new Regex(@"ms.assetid: (?<ms_asset_id>.*)", RegexOptions.Compiled);
private static Regex MsDescriptionRegex = new Regex(@"description: (?<description>.*)", RegexOptions.Compiled);
private static string IndexMdCallbackFunctionsH2 = "## Callback functions";
private static string IndexMdClassesH2 = "## Classes";
private static string IndexMdEnumerationsH2 = "## Enumerations";
private static string IndexMdFunctionsH2 = "## Functions";
private static string IndexMdInterfacesH2 = "## Interfaces";
private static string IndexMdIoctlsH2 = "## IOCTLs";
private static string IndexMdStructuresH2 = "## Structures";
private static string InterfaceTopicMethodsH2 = "## Methods";
private static string StructureTopicStructFieldsH2 = "## -struct-fields";
private static string LiteralAllElements = "All elements";
private static string LiteralParentElements = "Parent elements";
private static string LiteralChildElements = "Child elements";
private static string LiteralRemarks = "Remarks";
private static string LiteralExamples = "Examples";
private static string LiteralRequirements = "Requirements";
private static string TopicAllElementsH2 = $"## {EditorBase.LiteralAllElements}";
private static string TopicParentElementsH2 = $"## {EditorBase.LiteralParentElements}";
private static string TopicChildElementsH2 = $"## {EditorBase.LiteralChildElements}";
private static string TopicRemarksH2 = $"## {EditorBase.LiteralRemarks}";
private static string TopicExamplesH2 = $"## {EditorBase.LiteralExamples}";
private static string TopicRequirementsH2 = $"## {EditorBase.LiteralRequirements}";
private static string BulletPointPlusSpace = "* ";
public EditorObjectModel EditorObjectModel { get; private set; }
/// <summary>
/// Constructs a new EditorBase.
/// </summary>
/// <param name="fileInfo">The file to edit.</param>
public EditorBase(FileInfo fileInfo, EditorBaseFileInfoExistenceRequirements fileInfoExistenceRequirements = EditorBaseFileInfoExistenceRequirements.FileMustAlreadyExist)
{
this.FileInfo = fileInfo;
if (fileInfoExistenceRequirements == EditorBaseFileInfoExistenceRequirements.FileMustNotAlreadyExist)
{
if (this.FileInfo.Exists)
{
ProgramBase.ConsoleWrite(fileInfo.FullName + " already exists.", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
else
{
using (StreamWriter sw = this.FileInfo.CreateText()) { }
}
}
try
{
using (StreamReader? streamReader = this.FileInfo.OpenText())
{
this.fileContents = streamReader.ReadToEnd();
}
using (StreamReader? streamReader = this.FileInfo.OpenText())
{
while (!streamReader.EndOfStream)
{
this.fileLines.Add(streamReader.ReadLine()!.TrimEnd());
}
}
}
catch (Exception ex)
{
ProgramBase.ConsoleWrite(fileInfo.FullName + " is invalid.", ConsoleWriteStyle.Error);
ProgramBase.ConsoleWrite(ex.Message, ConsoleWriteStyle.Error);
throw new MDSDKException();
}
this.EditorObjectModel = new EditorObjectModel();
this.ParseFileContentIntoObjectModel();
}
private void ParseFileContentIntoObjectModel()
{
// TODO: this needs implementing more fully. It's currently focused on topics for xsd elements.
EditorObjectModelChildElementsH3Section? childElementsH2Section = null;
int ix = 0;
var editorBaseTopicSection = EditorObjectModelTopicSection.NothingFound;
while (ix < this.fileLines.Count)
{
string eachLine = this.fileLines[ix];
string eachLineTrimmed = eachLine.Trim();
bool moveToNextLine = true;
switch (editorBaseTopicSection)
{
case EditorObjectModelTopicSection.NothingFound:
if (eachLineTrimmed == "---")
{
editorBaseTopicSection = EditorObjectModelTopicSection.YamlFrontmatterFound;
}
else
{
ProgramBase.ConsoleWrite("The first line of a topic file must be `---`.", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
break;
case EditorObjectModelTopicSection.YamlFrontmatterFound:
if (eachLineTrimmed == "---")
{
editorBaseTopicSection = EditorObjectModelTopicSection.ContentAfterYamlFrontmatterFound;
}
break;
case EditorObjectModelTopicSection.ContentAfterYamlFrontmatterFound:
if (eachLineTrimmed.StartsWith("# "))
{
editorBaseTopicSection = EditorObjectModelTopicSection.H1Found; // H1 found, and moved to next line.
}
else if (eachLineTrimmed != string.Empty)
{
ProgramBase.ConsoleWrite("There shouldn't be anything but whitespace between the YAML frontmatter and the H1.", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
break;
case EditorObjectModelTopicSection.H1Found:
if (eachLineTrimmed.StartsWith("```"))
{
editorBaseTopicSection = EditorObjectModelTopicSection.SyntaxFound;
}
else if (eachLineTrimmed.StartsWith("#"))
{
editorBaseTopicSection = EditorObjectModelTopicSection.OtherHeadingFound;
moveToNextLine = false;
}
else
{
this.EditorObjectModel.AppendLineToDescription(eachLine);
}
break;
case EditorObjectModelTopicSection.SyntaxFound:
if (eachLineTrimmed.StartsWith("#"))
{
editorBaseTopicSection = EditorObjectModelTopicSection.OtherHeadingFound;
moveToNextLine = false;
}
break;
case EditorObjectModelTopicSection.ChildElementsH2Found:
if (eachLineTrimmed != string.Empty)
{
Table? childElementsTable = Table.GetNextTable(this.FileInfo!.Name, this.fileLines, ref ix);
this.EditorObjectModel.SetChildElementsTable(childElementsTable);
editorBaseTopicSection = EditorObjectModelTopicSection.ChildElementsH2BetweenSections;
}
break;
case EditorObjectModelTopicSection.ChildElementsH2BetweenSections:
if (eachLineTrimmed.StartsWith("### "))
{
editorBaseTopicSection = EditorObjectModelTopicSection.ChildElementH3Found;
moveToNextLine = false;
}
else if (eachLineTrimmed.StartsWith("#"))
{
editorBaseTopicSection = EditorObjectModelTopicSection.OtherHeadingFound;
moveToNextLine = false;
}
break;
case EditorObjectModelTopicSection.ChildElementH3Found:
if (eachLineTrimmed.StartsWith("### "))
{
childElementsH2Section = this.EditorObjectModel.AppendChildElementsH3Section(eachLine);
}
else if (eachLineTrimmed.StartsWith("#"))
{
editorBaseTopicSection = EditorObjectModelTopicSection.OtherHeadingFound;
moveToNextLine = false;
}
else
{
if (childElementsH2Section is not null)
{
childElementsH2Section.AppendLineToChildElementsH3Section(eachLine);
}
}
break;
case EditorObjectModelTopicSection.BetweenSections:
if (eachLineTrimmed.StartsWith("#"))
{
editorBaseTopicSection = EditorObjectModelTopicSection.OtherHeadingFound;
moveToNextLine = false;
}
break;
case EditorObjectModelTopicSection.OtherHeadingFound:
if (eachLineTrimmed == EditorBase.TopicChildElementsH2)
{
editorBaseTopicSection = EditorObjectModelTopicSection.ChildElementsH2Found; // H2 found, and moved to next line.
}
else if (eachLineTrimmed == EditorBase.TopicRemarksH2)
{
editorBaseTopicSection = EditorObjectModelTopicSection.RemarksH2Found; // H2 found, and moved to next line.
}
else if (eachLineTrimmed == EditorBase.TopicExamplesH2)
{
editorBaseTopicSection = EditorObjectModelTopicSection.ExamplesH2Found; // H2 found, and moved to next line.
}
else if (eachLineTrimmed == EditorBase.TopicRequirementsH2)
{
editorBaseTopicSection = EditorObjectModelTopicSection.RequirementsH2Found; // H2 found, and moved to next line.
}
break;
case EditorObjectModelTopicSection.RemarksH2Found:
if (eachLineTrimmed.StartsWith("#"))
{
editorBaseTopicSection = EditorObjectModelTopicSection.OtherHeadingFound;
moveToNextLine = false;
}
else
{
this.EditorObjectModel.AppendLineToRemarks(eachLine);
}
break;
case EditorObjectModelTopicSection.ExamplesH2Found:
if (eachLineTrimmed.StartsWith("#"))
{
editorBaseTopicSection = EditorObjectModelTopicSection.OtherHeadingFound;
moveToNextLine = false;
}
else
{
this.EditorObjectModel.AppendLineToExamples(eachLine);
}
break;
case EditorObjectModelTopicSection.RequirementsH2Found:
if (eachLineTrimmed != string.Empty)
{
Table? requirementsTable = Table.GetNextTable(this.FileInfo!.Name, this.fileLines, ref ix);
this.EditorObjectModel.SetRequirementsTable(requirementsTable);
editorBaseTopicSection = EditorObjectModelTopicSection.EndFound;
}
break;
default:
break;
}
if (moveToNextLine) ++ix;
}
}
public bool IsValid { get { return this.xDocument is not null; } }
#region Methods that don't modify
public static (string, string) DeconstructHyperlink(string hyperlink)
{
var matches = EditorBase.DeconstructHyperlinkRegex.Matches(hyperlink);
if (matches.Count == 1)
{
return DeconstructHyperlinkRecursive(matches[0].Groups["link_text"].Value, matches[0].Groups["link_url"].Value);
}
else
{
ProgramBase.ConsoleWrite($"Hyperlink {hyperlink} is malformed.");
throw new MDSDKException();
}
}
private static (string, string) DeconstructHyperlinkRecursive(string interface_link_text, string interface_link_url)
{
var matches = EditorBase.DeconstructHyperlinkRegex.Matches("[" + interface_link_text);
if (matches.Count == 1)
{
return DeconstructHyperlinkRecursive(matches[0].Groups["link_text"].Value, matches[0].Groups["link_url"].Value);
}
else
{
return (interface_link_text, interface_link_url);
}
}
public static MatchCollection RetrieveMatchesForTwoSpaces(string content)
{
return EditorBase.TwoSpacesRegex.Matches(content);
}
/// <summary>
/// Gets the single unique descendant with the specified name. Throws if the name is not unique.
/// </summary>
/// <param name="name">The element's name (without namespace).</param>
/// <param name="container">The scope to search in. Uses the entire document by default but you can pass another XElement to limit the seach to that element's descendants.</param>
/// <returns>The element, or null if the element does not exist.</returns>
public XElement? GetUniqueDescendant(string? name, XContainer? container = null)
{
if (container is null) container = this.xDocument;
List<XElement> elements = this.GetDescendants(name, container);
if (elements is null || elements.Count == 0) return null;
if (elements.Count == 1)
{
return elements[0];
}
else
{
ProgramBase.ConsoleWrite("You called GetUniqueDescendant(\"" + name + "\"), but \"" + name + "\" is not unique.", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
}
/// <summary>
/// Gets the first descendant with the specified name.
/// </summary>
/// <param name="name">The element's name (without namespace).</param>
/// <param name="container">The scope to search in. Uses the entire document by default but you can pass another XElement to limit the seach to that element's descendants.</param>
/// <returns>The element, or null if the element does not exist.</returns>
public XElement? GetFirstDescendant(string? name, XContainer? container = null)
{
if (container is null) container = this.xDocument;
List<XElement> elements = this.GetDescendants(name, container);
if (elements is not null && elements.Count > 0)
{
return elements[0];
}
else
{
return null;
}
}
public Table? GetFunctionsInIndexMd()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.IndexMdFunctionsH2)
{
int lineNumberToStartAtZeroBased = ix + 1;
return Table.GetNextTable(this.FileInfo!.FullName, this.fileLines, ref lineNumberToStartAtZeroBased);
}
}
return null;
}
public Table? GetEnumerationsInIndexMd()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.IndexMdEnumerationsH2)
{
int lineNumberToStartAtZeroBased = ix + 1;
return Table.GetNextTable(this.FileInfo!.FullName, this.fileLines, ref lineNumberToStartAtZeroBased);
}
}
return null;
}
public Table? GetStructuresInIndexMd()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.IndexMdStructuresH2)
{
int lineNumberToStartAtZeroBased = ix + 1;
return Table.GetNextTable(this.FileInfo!.FullName, this.fileLines, ref lineNumberToStartAtZeroBased);
}
}
return null;
}
public Table? GetInterfacesInIndexMd()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.IndexMdInterfacesH2)
{
int lineNumberToStartAtZeroBased = ix + 1;
return Table.GetNextTable(this.FileInfo!.FullName, this.fileLines, ref lineNumberToStartAtZeroBased);
}
}
return null;
}
public Table? GetCallbackFunctionsInIndexMd()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.IndexMdCallbackFunctionsH2)
{
int lineNumberToStartAtZeroBased = ix + 1;
return Table.GetNextTable(this.FileInfo!.FullName, this.fileLines, ref lineNumberToStartAtZeroBased);
}
}
return null;
}
public Table? GetClassesInIndexMd()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.IndexMdClassesH2)
{
int lineNumberToStartAtZeroBased = ix + 1;
return Table.GetNextTable(this.FileInfo!.FullName, this.fileLines, ref lineNumberToStartAtZeroBased);
}
}
return null;
}
public Table? GetIoctlsInIndexMd()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.IndexMdIoctlsH2)
{
int lineNumberToStartAtZeroBased = ix + 1;
return Table.GetNextTable(this.FileInfo!.FullName, this.fileLines, ref lineNumberToStartAtZeroBased);
}
}
return null;
}
public Table? GetMethodsInInterfaceTopic()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.InterfaceTopicMethodsH2)
{
int lineNumberToStartAtZeroBased = ix + 1;
return Table.GetNextTable(this.FileInfo!.FullName, this.fileLines, ref lineNumberToStartAtZeroBased);
}
}
return null;
}
/// <summary>
/// Retrieves the 1-based line number of the beginning of the fields section in a Win32 structure topic.
/// </summary>
/// <returns>The 1-based line number, or -1 if not found.</returns>
public int GetFieldsSection1BasedLineNumber()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed == EditorBase.StructureTopicStructFieldsH2)
{
return ix + 1;
}
}
return -1;
}
public string? GetFirstNoBreakSpaceLine()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed.Contains("\u00A0"))
{
return eachLineTrimmed;
}
}
return null;
}
public string? GetFirstAsteriskInYamlDescriptionLine()
{
for (int ix = 0; ix < this.fileLines.Count; ++ix)
{
string eachLineTrimmed = this.fileLines[ix].Trim();
if (eachLineTrimmed.StartsWith("description: *"))
{
return eachLineTrimmed;
}
}
return null;
}
/// <summary>
/// Gets all descendants, or all descendants with the specified name if one is specified.
/// </summary>
/// <param name="name">The element's name (without namespace) or null to return all descendants.</param>
/// <param name="container">The scope to search in. Uses the entire document by default but you can pass another XElement to limit the seach to that element's descendants.</param>
/// <returns>The elements, or null if the element does not exist.</returns>
public List<XElement> GetDescendants(string? name = null, XContainer? container = null)
{
if (container is null) container = this.xDocument;
if (name is null)
{
return container!.Descendants().ToList();
}
else
{
return container!.Descendants(name).ToList();
}
}
/// <summary>
/// Gets metadata@id as a string.
/// </summary>
/// <returns>The value of the id attribute if found, otherwise null.</returns>
public string? GetMetadataAtId()
{
XElement? metadata = this.GetUniqueDescendant("metadata");
if (metadata is not null)
{
XAttribute? id = metadata.Attribute("id");
if (id is not null) return id.Value;
}
return null;
}
/// <summary>
/// Gets metadata@type as a string.
/// </summary>
/// <returns>The value of the type attribute if found, otherwise null.</returns>
public string? GetMetadataAtTypeAsString()
{
XElement? metadata = this.GetUniqueDescendant("metadata");
if (metadata is not null)
{
XAttribute? type = metadata.Attribute("type");
if (type is not null) return type.Value;
}
return null;
}
/// <summary>
/// Gets metadata@type as an enum value.
/// </summary>
/// <returns>An enum value representing the value of the type attribute.</returns>
public TopicType GetMetadataAtTypeAsEnum()
{
XElement? metadata = this.GetUniqueDescendant("metadata");
if (metadata is not null)
{
XAttribute? xAttribute = metadata.Attribute("type");
if (xAttribute is not null)
{
if (xAttribute.Value == "attachedmember_winrt")
{
return TopicType.AttachedPropertyWinRT;
}
else if (xAttribute.Value == "attribute")
{
return TopicType.AttributeWinRT; // change the name a little so we're consistent.
}
else if (xAttribute.Value == "class_winrt")
{
return TopicType.ClassWinRT;
}
else if (xAttribute.Value == "delegate")
{
return TopicType.DelegateWinRT; // change the name a little so we're consistent.
}
else if (xAttribute.Value == "enum_winrt")
{
return TopicType.EnumWinRT;
}
else if (xAttribute.Value == "event_winrt")
{
return TopicType.EventWinRT;
}
else if (xAttribute.Value == "function")
{
return TopicType.MethodWinRT; // change the name so we're consistent. Some WinRT DX topics are like this.
}
//else if (xAttribute.Value == "iface")
//{
// return TopicType.InterfaceWinRT; // change the name so we're consistent. Some WinRT topics are like this (e.g. see w_net_backgrxfer).
//}
else if (xAttribute.Value == "interface_winrt")
{
return TopicType.InterfaceWinRT;
}
//else if (xAttribute.Value == "method")
//{
// return TopicType.MethodWinRT; // change the name so we're consistent. Some WinRT topics are like this (e.g. see w_net_backgrxfer).
//}
else if (xAttribute.Value == "method_overload")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "method_overload_winrt")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "method_winrt")
{
return TopicType.MethodWinRT;
}
if (xAttribute.Value == "namespace")
{
return TopicType.NamespaceWinRT; // change the name a little so we're consistent.
}
else if (xAttribute.Value == "nodepage")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "ovw")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "property_winrt")
{
return TopicType.PropertyWinRT;
}
else if (xAttribute.Value == "refpage")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "startpage")
{
return TopicType.NotYetKnown; // don't process these.
}
//else if (xAttribute.Value == "struct")
//{
// return TopicType.StructWinRT; // change the name a little so we're consistent. Some WinRT DX topics are like this.
//}
else if (xAttribute.Value == "struct_winrt")
{
return TopicType.StructWinRT;
}
else
{
return TopicType.NotYetKnown;
}
}
}
return TopicType.NotYetKnown;
}
/// <summary>
/// Gets metadata@title as a string.
/// </summary>
/// <returns>The value of the title attribute if found, otherwise null.</returns>
public string? GetMetadataAtTitle()
{
XElement? metadata = this.GetUniqueDescendant("metadata");
if (metadata is not null)
{
XElement? title = this.GetUniqueDescendant("title", metadata);
if (title is not null) return title.Value;
}
return null;
}
public string? GetYamlApiName()
{
return "ACTRL_ACCESSA";
//XElement metadata = this.GetUniqueDescendant("metadata");
//if (metadata is not null)
//{
// XAttribute xAttribute = metadata.Attribute("api_name");
// if (xAttribute is not null)
// {
// return xAttribute.Value;
// }
//}
//return null;
}
/// <summary>
/// Gets metadata@title as nodes.
/// </summary>
/// <returns>The nodes of the title attribute if found, otherwise null.</returns>
public IEnumerable<XNode>? GetMetadataAtTitleAsNodes()
{
XElement? metadata = this.GetUniqueDescendant("metadata");
if (metadata is not null)
{
XElement? title = this.GetUniqueDescendant("title", metadata);
if (title is not null) return title.Nodes();
}
return null;
}
public string? GetSyntaxName()
{
XElement? syntax = this.GetUniqueDescendant("syntax");
if (syntax is not null)
{
XElement? name = this.GetFirstDescendant("name", syntax);
if (name is not null) return name.Value;
}
return null;
}
public string? GetMetadataAtIntellisenseIdString()
{
XElement? metadata = this.GetUniqueDescendant("metadata");
if (metadata is not null)
{
XAttribute? intellisenseIdStringAttribute = metadata.Attribute("intellisense_id_string");
if (intellisenseIdStringAttribute is not null)
{
return intellisenseIdStringAttribute.Value;
}
}
return null;
}
/// <summary>
/// Gets the type name extracted from metadata@intellisense_id_string as a string.
/// </summary>
/// <returns>The type name extracted from the intellisense_id_string attribute if found, otherwise null.</returns>
public string? GetTypeNameFromMetadataAtIntellisenseIdString()
{
string? intellisenseIdString = this.GetMetadataAtIntellisenseIdString();
if (intellisenseIdString is not null)
{
string typeName = intellisenseIdString;
int startIndexOfTypeName = typeName.LastIndexOf('.') + 1;
int lengthOfTypeName = typeName.Length - startIndexOfTypeName;
typeName = typeName.Substring(startIndexOfTypeName, lengthOfTypeName);
return typeName;
}
return null;
}
public string? GetMethodWinRTParametersFromParams()
{
string parmsList = string.Empty;
XElement? paramsEl = this.GetUniqueDescendant("params");
if (paramsEl is not null)
{
foreach (XElement param in this.GetDescendants("param", paramsEl))
{
if (parmsList.Length == 0) parmsList = "(";
XElement? datatype = this.GetUniqueDescendant("datatype", param);
if (datatype is null) continue;
XElement? xref = this.GetUniqueDescendant("xref", datatype);
if (xref is null) continue;
if (parmsList.Length > 1) parmsList += ", ";
parmsList += xref.Value;
}
if (parmsList.Length > 0) parmsList += ")";
}
return parmsList;
}
/// <summary>
/// Gets applicationplatform@name as a string.
/// </summary>
/// <returns>The value of the name attribute if found, otherwise null.</returns>
public string? GetApplicationPlatformAtName()
{
XElement? applicationPlatform = this.GetUniqueDescendant("ApplicationPlatform");
if (applicationPlatform is not null)
{
XAttribute? name = applicationPlatform.Attribute("name");
if (name is not null) return name.Value;
}
return null;
}
/// <summary>
/// Gets applicationplatform@friendlyname as a string.
/// </summary>
/// <returns>The value of the friendlyname attribute if found, otherwise null.</returns>
public string? GetApplicationPlatformAtFriendlyName()
{
XElement? applicationPlatform = this.GetUniqueDescendant("ApplicationPlatform");
if (applicationPlatform is not null)
{
XAttribute? friendlyName = applicationPlatform.Attribute("friendlyName");
if (friendlyName is not null) return friendlyName.Value;
}
return null;
}
/// <summary>
/// Gets applicationplatform@version as a string.
/// </summary>
/// <returns>The value of the friendlyname attribute if found, otherwise null.</returns>
public string? GetApplicationPlatformAtVersion()
{
XElement? applicationPlatform = this.GetUniqueDescendant("ApplicationPlatform");
if (applicationPlatform is not null)
{
XAttribute? version = applicationPlatform.Attribute("version");
if (version is not null) return version.Value;
}
return null;
}
/// <summary>
/// Gets the xref element inside either applies@class or applies@iface as a string.
/// </summary>
/// <returns>The xref element if found, otherwise null.</returns>
public string? GetAppliesClassOrIfaceXrefAtRid()
{
XElement? applies = this.GetUniqueDescendant("applies");
if (applies is not null)
{
XElement? classEl = this.GetUniqueDescendant("class", applies);
if (classEl is not null)
{
return this.GetAtRidForUniqueXrefDescendant(classEl);
}
else
{
XElement? iface = this.GetUniqueDescendant("iface", applies);
if (iface is not null)
{
return this.GetAtRidForUniqueXrefDescendant(iface);
}
}
}
return null;
}
/// <summary>
/// Gets @rid for the unique xref descendant of the specified element.
/// </summary>
/// <param name="xElement">The element whose xref descendant to find.</param>
/// <returns>The value of the rid attribute if found, otherwise null.</returns>
public string? GetAtRidForUniqueXrefDescendant(XElement? xElement)
{
XElement? xref = this.GetUniqueDescendant("xref", xElement);
if (xref is not null)
{
XAttribute? rid = xref.Attribute("rid");
if (rid is not null) return rid.Value;
}
return null;
}
/// <summary>
/// Gets xref elements whose @hlink contains the specified substring.
/// </summary>
/// <param name="substring">The substring to search for</param>
/// <param name="caseSensitive">True if the comparison should be case-sensitive, otherwise false.</param>
/// <param name="container">The scope to search in. Uses the entire document by default but you can pass another XElement to limit the seach to that element's descendants.</param>
/// <returns>A list of matching xref elements.</returns>
public List<XElement> GetXrefsWhereAtHlinkContains(string substring, bool caseSensitive = false, XContainer? container = null)
{
if (container is null) container = this.xDocument;
if (!caseSensitive) substring = substring.ToLower();
List<XElement> xrefsWhereAtHlinkContains = new List<XElement>();
foreach (XElement eachXref in this.GetDescendants("xref", container))
{
XAttribute? hlink = eachXref.Attribute("hlink");
if (hlink is not null)
{
string hlinkValue = hlink.Value;
if (!caseSensitive) hlinkValue = hlinkValue.ToLower();
if (hlinkValue.Contains(substring)) xrefsWhereAtHlinkContains.Add(eachXref);
}
}
return xrefsWhereAtHlinkContains;
}
public List<XElement> GetXrefsForRid(string ridString, bool caseSensitive = false, XContainer? container = null)
{
if (container is null) container = this.xDocument;
if (!caseSensitive) ridString = ridString.ToLower();
List<XElement> xrefsForRid = new List<XElement>();
foreach (XElement eachXref in this.GetDescendants("xref", container))
{
XAttribute? ridAttribute = eachXref.Attribute("rid");
if (ridAttribute is not null)
{
string ridValue = ridAttribute.Value;
if (!caseSensitive) ridValue = ridValue.ToLower();
if (ridValue == ridString) xrefsForRid.Add(eachXref);
}
}
return xrefsForRid;
}
/// <summary>
/// Factory method that creates an Editor for the named file that is inside
/// the specified folder. Optionally throws if file not found.
/// </summary>
/// <param name="directoryInfo">The folder containing the file.</param>
/// <param name="fileName">The name of the file for which to retrieve an editor.</param>
/// <returns>An Editor object for the file in the folder.</returns>
public static Editor? GetEditorForTopicFileName(DirectoryInfo directoryInfo, string fileName, bool throwIfNotFound = true)
{
List<FileInfo> fileInfos = directoryInfo.GetFiles(fileName).ToList();
if (fileInfos.Count == 1)
{
return new Editor(fileInfos[0]);
}
ProgramBase.ConsoleWrite($"folder \"{directoryInfo.Name}\" doesn't contain \"{fileName}\" ", ConsoleWriteStyle.Error, 0);
if (throwIfNotFound) throw new MDSDKException();
return null;
}
/// <summary>
/// Factory method that creates an Editor for the xtoc file that is inside, and
/// that has the same name as, the specified project folder. Throws if not exactly
/// one xtoc file is found whose name matches the folder name.
/// </summary>
/// <param name="projectDirectoryInfo">The folder containing the project.</param>
/// <returns>An Editor object for the xtoc file.</returns>
public static Editor GetEditorForXtoc(DirectoryInfo projectDirectoryInfo)
{
List<FileInfo> xtocFiles = projectDirectoryInfo.GetFiles(projectDirectoryInfo.Name + ".xtoc").ToList();
if (xtocFiles.Count != 1)
{
ProgramBase.ConsoleWrite(string.Format($"Project folder {projectDirectoryInfo.Name} does not contain {projectDirectoryInfo.Name}.xtoc"), ConsoleWriteStyle.Error);
throw new MDSDKException();
}
return new Editor(xtocFiles[0]);
}
/// <summary>
/// Factory method that creates an Editor for each topic file inside the specified folder.
/// </summary>