Merge pull request #475 from pruiz/xamarin-bug-7408
[mono.git] / mcs / class / monodoc / Monodoc / providers / ecma-provider.cs
1 //
2 // The ecmaspec provider is for ECMA specifications
3 //
4 // Authors:
5 //      John Luke (jluke@cfl.rr.com)
6 //      Ben Maurer (bmaurer@users.sourceforge.net)
7 //
8 // Use like this:
9 //   mono assembler.exe --ecmaspec DIRECTORY --out name
10 //
11
12 using System;
13 using System.Linq;
14 using System.IO;
15 using System.Text;
16 using System.Xml;
17 using System.Xml.Linq;
18 using System.Collections.Generic;
19
20 using Lucene.Net.Index;
21 using Lucene.Net.Documents;
22
23 using Monodoc.Ecma;
24 using Mono.Utilities;
25
26 namespace Monodoc.Providers
27 {
28         public class EcmaProvider : Provider
29         {
30                 HashSet<string> directories = new HashSet<string> ();
31
32                 public EcmaProvider ()
33                 {
34                 }
35
36                 public EcmaProvider (string baseDir)
37                 {
38                         AddDirectory (baseDir);
39                 }
40
41                 public void AddDirectory (string directory)
42                 {
43                         if (string.IsNullOrEmpty (directory))
44                                 throw new ArgumentNullException ("directory");
45
46                         directories.Add (directory);
47                 }
48
49                 public override void PopulateTree (Tree tree)
50                 {
51                         var storage = tree.HelpSource.Storage;
52                         var nsSummaries = new Dictionary<string, XElement> ();
53                         int resID = 0;
54
55                         foreach (var asm in directories) {
56                                 var indexFilePath = Path.Combine (asm, "index.xml");
57                                 if (!File.Exists (indexFilePath)) {
58                                         Console.Error.WriteLine ("Warning: couldn't process directory `{0}' as it has no index.xml file", asm);
59                                         continue;
60                                 }
61
62                                 EcmaDoc.PopulateTreeFromIndexFile (indexFilePath, EcmaHelpSource.EcmaPrefix, tree, storage, nsSummaries, _ => resID++.ToString ());
63                         }
64
65                         foreach (var summary in nsSummaries)
66                                 storage.Store ("xml.summary." + summary.Key, summary.Value.ToString ());
67
68                         var masterSummary = new XElement ("elements",
69                                                           directories
70                                                           .SelectMany (d => Directory.EnumerateFiles (d, "ns-*.xml"))
71                                                           .Select (ExtractNamespaceSummary));
72                         storage.Store ("mastersummary.xml", masterSummary.ToString ());
73                 }
74
75                 XElement ExtractNamespaceSummary (string nsFile)
76                 {
77                         using (var reader = XmlReader.Create (nsFile)) {
78                                 reader.ReadToFollowing ("Namespace");
79                                 var name = reader.GetAttribute ("Name");
80                                 reader.ReadToFollowing ("summary");
81                                 var summary = reader.ReadInnerXml ();
82                                 reader.ReadToFollowing ("remarks");
83                                 var remarks = reader.ReadInnerXml ();
84
85                                 return new XElement ("namespace",
86                                                      new XAttribute ("ns", name ?? string.Empty),
87                                                      new XElement ("summary", new XCData (summary)),
88                                                      new XElement ("remarks", new XCData (remarks)));
89                         }
90                 }
91
92                 public override void CloseTree (HelpSource hs, Tree tree)
93                 {
94                         AddImages (hs);
95                         AddExtensionMethods (hs);
96                 }
97
98                 void AddEcmaXml (HelpSource hs)
99                 {
100                         var xmls = directories
101                                 .SelectMany (Directory.EnumerateDirectories) // Assemblies
102                                 .SelectMany (Directory.EnumerateDirectories) // Namespaces
103                                 .SelectMany (Directory.EnumerateFiles)
104                                 .Where (f => f.EndsWith (".xml")); // Type XML files
105
106                         int resID = 0;
107                         foreach (var xml in xmls)
108                                 using (var file = File.OpenRead (xml))
109                                         hs.Storage.Store ((resID++).ToString (), file);
110                 }
111
112                 void AddImages (HelpSource hs)
113                 {
114                         var imgs = directories
115                                 .SelectMany (Directory.EnumerateDirectories)
116                                 .Select (d => Path.Combine (d, "_images"))
117                                 .Where (Directory.Exists)
118                                 .SelectMany (Directory.EnumerateFiles);
119
120                         foreach (var img in imgs)
121                                 using (var file = File.OpenRead (img))
122                                         hs.Storage.Store (Path.GetFileName (img), file);
123                 }
124
125                 void AddExtensionMethods (HelpSource hs)
126                 {
127                         var extensionMethods = directories
128                                 .SelectMany (Directory.EnumerateDirectories)
129                                 .Select (d => Path.Combine (d, "index.xml"))
130                                 .Where (File.Exists)
131                                 .Select (f => {
132                                         using (var file = File.OpenRead (f)) {
133                                                 var reader = XmlReader.Create (file);
134                                                 reader.ReadToFollowing ("ExtensionMethods");
135                                                 return reader.ReadInnerXml ();
136                                         }
137                             })
138                             .DefaultIfEmpty (string.Empty);
139
140                         hs.Storage.Store ("ExtensionMethods.xml",
141                                           "<ExtensionMethods>" + extensionMethods.Aggregate (string.Concat) + "</ExtensionMethods>");
142                 }
143
144                 IEnumerable<string> GetEcmaXmls ()
145                 {
146                         return directories
147                                 .SelectMany (Directory.EnumerateDirectories) // Assemblies
148                                 .SelectMany (Directory.EnumerateDirectories) // Namespaces
149                                 .SelectMany (Directory.EnumerateFiles)
150                                 .Where (f => f.EndsWith (".xml")); // Type XML files
151                 }
152         }
153
154         public class EcmaHelpSource : HelpSource
155         {
156                 internal const string EcmaPrefix = "ecma:";
157                 LRUCache<string, Node> cache = new LRUCache<string, Node> (4);
158
159                 public EcmaHelpSource (string base_file, bool create) : base (base_file, create)
160                 {
161                 }
162
163                 protected EcmaHelpSource () : base ()
164                 {
165                 }
166
167                 protected override string UriPrefix {
168                         get {
169                                 return EcmaPrefix;
170                         }
171                 }
172
173                 public override bool CanHandleUrl (string url)
174                 {
175                         if (url.Length > 2 && url[1] == ':') {
176                                 switch (url[0]) {
177                                 case 'T':
178                                 case 'M':
179                                 case 'C':
180                                 case 'P':
181                                 case 'E':
182                                 case 'F':
183                                 case 'N':
184                                 case 'O':
185                                         return true;
186                                 }
187                         }
188                         return base.CanHandleUrl (url);
189                 }
190
191                 // Clean the extra paramers in the id
192                 public override Stream GetHelpStream (string id)
193                 {
194                         var idParts = id.Split ('?');
195                         return base.GetHelpStream (idParts[0]);
196                 }
197
198                 public override Stream GetCachedHelpStream (string id)
199                 {
200                         var idParts = id.Split ('?');
201                         return base.GetCachedHelpStream (idParts[0]);
202                 }
203
204                 public override DocumentType GetDocumentTypeForId (string id)
205                 {
206                         return DocumentType.EcmaXml;
207                 }
208
209                 public override string GetPublicUrl (Node node)
210                 {
211                         string url = string.Empty;
212                         var type = EcmaDoc.GetNodeType (node);
213                         //Console.WriteLine ("GetPublicUrl {0} : {1} [{2}]", node.Element, node.Caption, type.ToString ());
214                         switch (type) {
215                         case EcmaNodeType.Namespace:
216                                 return node.Element; // A namespace node has already a well formated internal url
217                         case EcmaNodeType.Type:
218                                 return MakeTypeNodeUrl (node);
219                         case EcmaNodeType.Meta:
220                                 return MakeTypeNodeUrl (GetNodeTypeParent (node)) + GenerateMetaSuffix (node);
221                         case EcmaNodeType.Member:
222                                 var typeChar = EcmaDoc.GetNodeMemberTypeChar (node);
223                                 var parentNode = GetNodeTypeParent (node);
224                                 var typeNode = MakeTypeNodeUrl (parentNode).Substring (2);
225                                 return typeChar + ":" + typeNode + MakeMemberNodeUrl (typeChar, node);
226                         default:
227                                 return null;
228                         }
229                 }
230
231                 string MakeTypeNodeUrl (Node node)
232                 {
233                         // A Type node has a Element property of the form: 'ecma:{number}#{typename}/'
234                         var hashIndex = node.Element.IndexOf ('#');
235                         var typeName = node.Element.Substring (hashIndex + 1, node.Element.Length - hashIndex - 2);
236                         return "T:" + node.Parent.Caption + '.' + typeName.Replace ('.', '+');
237                 }
238
239                 string MakeMemberNodeUrl (char typeChar, Node node)
240                 {
241                         // We clean inner type ctor name which may contain the outer type name
242                         var caption = node.Caption;
243
244                         // Sanitize constructor caption of inner types
245                         if (typeChar == 'C') {
246                                 int lastDot = -1;
247                                 for (int i = 0; i < caption.Length && caption[i] != '('; i++)
248                                         lastDot = caption[i] == '.' ? i : lastDot;
249                                 return lastDot == -1 ? '.' + caption : caption.Substring (lastDot);
250                         }
251
252                         /* We handle type conversion operator by checking if the name contains " to "
253                          * (as in 'foo to bar') and we generate a corresponding conversion signature
254                          */
255                         if (typeChar == 'O' && caption.IndexOf (" to ") != -1) {
256                                 var parts = caption.Split (' ');
257                                 return "." + node.Parent.Caption + "(" + parts[0] + ", " + parts[2] + ")";
258                         }
259
260                         /* The goal here is to treat method which are explicit interface definition
261                          * such as 'void IDisposable.Dispose ()' for which the caption is a dot
262                          * expression thus colliding with the ecma parser.
263                          * If the first non-alpha character in the caption is a dot then we have an
264                          * explicit member implementation (we assume the interface has namespace)
265                          */
266                         var firstNonAlpha = caption.FirstOrDefault (c => !char.IsLetterOrDigit (c));
267                         if (firstNonAlpha == '.')
268                                 return "$" + caption;
269
270                         return "." + caption;
271                 }
272
273                 Node GetNodeTypeParent (Node node)
274                 {
275                         // Type nodes are always at level 2 so we just need to get there
276                         while (node != null && node.Parent != null && !node.Parent.Parent.Element.StartsWith ("root:/", StringComparison.OrdinalIgnoreCase))
277                                 node = node.Parent;
278                         return node;
279                 }
280
281                 string GenerateMetaSuffix (Node node)
282                 {
283                         string suffix = string.Empty;
284                         // A meta node has always a type element to begin with
285                         while (EcmaDoc.GetNodeType (node) != EcmaNodeType.Type) {
286                                 suffix = '/' + node.Element + suffix;
287                                 node = node.Parent;
288                         }
289                         return suffix;
290                 }
291
292                 public override string GetInternalIdForUrl (string url, out Node node, out Dictionary<string, string> context)
293                 {
294                         var id = string.Empty;
295                         node = null;
296                         context = null;
297
298                         if (!url.StartsWith (UriPrefix, StringComparison.OrdinalIgnoreCase)) {
299                                 node = MatchNode (url);
300                                 if (node == null)
301                                         return null;
302                                 id = node.GetInternalUrl ();
303                         }
304
305                         string hash;
306                         id = GetInternalIdForInternalUrl (id, out hash);
307                         context = EcmaDoc.GetContextForEcmaNode (hash, SourceID.ToString (), node);
308
309                         return id;
310                 }
311
312                 public string GetInternalIdForInternalUrl (string internalUrl, out string hash)
313                 {
314                         var id = internalUrl;
315                         if (id.StartsWith (UriPrefix, StringComparison.OrdinalIgnoreCase))
316                                 id = id.Substring (UriPrefix.Length);
317                         else if (id.StartsWith ("N:", StringComparison.OrdinalIgnoreCase))
318                                 id = "xml.summary." + id.Substring ("N:".Length);
319
320                         var hashIndex = id.IndexOf ('#');
321                         hash = string.Empty;
322                         if (hashIndex != -1) {
323                                 hash = id.Substring (hashIndex + 1);
324                                 id = id.Substring (0, hashIndex);
325                         }
326
327                         return id;
328                 }
329
330                 public override Node MatchNode (string url)
331                 {
332                         Node node = null;
333                         if ((node = cache.Get (url)) == null) {
334                                 node = EcmaDoc.MatchNodeWithEcmaUrl (url, Tree);
335                                 if (node != null)
336                                         cache.Put (url, node);
337                         }
338                         return node;
339                 }
340
341                 public override void PopulateIndex (IndexMaker index_maker)
342                 {
343                         foreach (Node ns_node in Tree.RootNode.Nodes){
344                                 foreach (Node type_node in ns_node.Nodes){
345                                         string typename = type_node.Caption.Substring (0, type_node.Caption.IndexOf (' '));
346                                         string full = ns_node.Caption + "." + typename;
347
348                                         string doc_tag = GetKindFromCaption (type_node.Caption);
349                                         string url = type_node.PublicUrl;
350
351                                         //
352                                         // Add MonoMac/MonoTouch [Export] attributes, those live only in classes
353                                         //
354                                         XDocument type_doc = null;
355                                         ILookup<string, XElement> prematchedMembers = null;
356                                         bool hasExports = doc_tag == "Class" && (ns_node.Caption.StartsWith ("MonoTouch") || ns_node.Caption.StartsWith ("MonoMac"));
357                                         if (hasExports) {
358                                                 try {
359                                                         string rest, hash;
360                                                         var id = GetInternalIdForInternalUrl (type_node.GetInternalUrl (), out hash);
361                                                         type_doc = XDocument.Load (GetHelpStream (id));
362                                                         prematchedMembers = type_doc.Root.Element ("Members").Elements ("Member").ToLookup (n => (string)n.Attribute ("MemberName"), n => n);
363                                                 } catch (Exception e) {
364                                                         Console.WriteLine ("Problem processing {0} for MonoTouch/MonoMac exports\n\n{0}", e);
365                                                         hasExports = false;
366                                                 }
367                                         }
368
369                                         if (doc_tag == "Class" || doc_tag == "Structure" || doc_tag == "Interface"){
370                                                 index_maker.Add (type_node.Caption, typename, url);
371                                                 index_maker.Add (full + " " + doc_tag, full, url);
372
373                                                 foreach (Node c in type_node.Nodes){
374                                                         switch (c.Caption){
375                                                         case "Constructors":
376                                                                 index_maker.Add ("  constructors", typename+"0", url + "/C");
377                                                                 break;
378                                                         case "Fields":
379                                                                 index_maker.Add ("  fields", typename+"1", url + "/F");
380                                                                 break;
381                                                         case "Events":
382                                                                 index_maker.Add ("  events", typename+"2", url + "/E");
383                                                                 break;
384                                                         case "Properties":
385                                                                 index_maker.Add ("  properties", typename+"3", url + "/P");
386                                                                 break;
387                                                         case "Methods":
388                                                                 index_maker.Add ("  methods", typename+"4", url + "/M");
389                                                                 break;
390                                                         case "Operators":
391                                                                 index_maker.Add ("  operators", typename+"5", url + "/O");
392                                                                 break;
393                                                         }
394                                                 }
395
396                                                 //
397                                                 // Now repeat, but use a different sort key, to make sure we come after
398                                                 // the summary data above, start the counter at 6
399                                                 //
400                                                 string keybase = typename + "6.";
401
402                                                 foreach (Node c in type_node.Nodes){
403                                                         var type = c.Caption[0];
404
405                                                         foreach (Node nc in c.Nodes) {
406                                                                 string res = nc.Caption;
407                                                                 string nurl = nc.PublicUrl;
408
409                                                                 // Process exports
410                                                                 if (hasExports && (type == 'C' || type == 'M' || type == 'P')) {
411                                                                         try {
412                                                                                 var member = GetMemberFromCaption (type_doc, type == 'C' ? ".ctor" : res, false, prematchedMembers);
413                                                                                 var exports = member.Descendants ("AttributeName").Where (a => a.Value.Contains ("Foundation.Export"));
414                                                                                 foreach (var exportNode in exports) {
415                                                                                         var parts = exportNode.Value.Split ('"');
416                                                                                         if (parts.Length != 3) {
417                                                                                                 Console.WriteLine ("Export attribute not found or not usable in {0}", exportNode);
418                                                                                         } else {
419                                                                                                 var export = parts[1];
420                                                                                                 index_maker.Add (export + " selector", export, nurl);
421                                                                                         }
422                                                                                 }
423                                                                         } catch (Exception e) {
424                                                                                 Console.WriteLine ("Problem processing {0}/{1} for MonoTouch/MonoMac exports\n\n{2}", nurl, res, e);
425                                                                         }
426                                                                 }
427
428                                                                 switch (type){
429                                                                 case 'C':
430                                                                         break;
431                                                                 case 'F':
432                                                                         index_maker.Add (String.Format ("{0}.{1} field", typename, res),
433                                                                                          keybase + res, nurl);
434                                                                         index_maker.Add (String.Format ("{0} field", res), res, nurl);
435                                                                         break;
436                                                                 case 'E':
437                                                                         index_maker.Add (String.Format ("{0}.{1} event", typename, res),
438                                                                                          keybase + res, nurl);
439                                                                         index_maker.Add (String.Format ("{0} event", res), res, nurl);
440                                                                         break;
441                                                                 case 'P':
442                                                                         index_maker.Add (String.Format ("{0}.{1} property", typename, res),
443                                                                                          keybase + res, nurl);
444                                                                         index_maker.Add (String.Format ("{0} property", res), res, nurl);
445                                                                         break;
446                                                                 case 'M':
447                                                                         index_maker.Add (String.Format ("{0}.{1} method", typename, res),
448                                                                                          keybase + res, nurl);
449                                                                         index_maker.Add (String.Format ("{0} method", res), res, nurl);
450                                                                         break;
451                                                                 case 'O':
452                                                                         index_maker.Add (String.Format ("{0}.{1} operator", typename, res),
453                                                                                          keybase + res, nurl);
454                                                                         break;
455                                                                 }
456                                                         }
457                                                 }
458                                         } else if (doc_tag == "Enumeration"){
459                                                 //
460                                                 // Enumerations: add the enumeration values
461                                                 //
462                                                 index_maker.Add (type_node.Caption, typename, url);
463                                                 index_maker.Add (full + " " + doc_tag, full, url);
464
465                                                 // Now, pull the values.
466                                                 string rest, hash;
467                                                 var id = GetInternalIdForInternalUrl (type_node.GetInternalUrl (), out hash);
468                                                 var xdoc = XDocument.Load (GetHelpStream (id));
469                                                 if (xdoc == null)
470                                                         continue;
471
472                                                 var members = xdoc.Root.Element ("Members").Elements ("Members");
473                                                 if (members == null)
474                                                         continue;
475
476                                                 foreach (var member_node in members){
477                                                         string enum_value = member_node.Attribute ("MemberName").Value;
478                                                         string caption = enum_value + " value";
479                                                         index_maker.Add (caption, caption, url);
480                                                 }
481                                         } else if (doc_tag == "Delegate"){
482                                                 index_maker.Add (type_node.Caption, typename, url);
483                                                 index_maker.Add (full + " " + doc_tag, full, url);
484                                         }
485                                 }
486                         }
487                 }
488
489
490                 public override void PopulateSearchableIndex (IndexWriter writer)
491                 {
492                         StringBuilder text = new StringBuilder ();
493                         SearchableDocument searchDoc = new SearchableDocument ();
494
495                         foreach (Node ns_node in Tree.RootNode.Nodes) {
496                                 foreach (Node type_node in ns_node.Nodes) {
497                                         string typename = type_node.Caption.Substring (0, type_node.Caption.IndexOf (' '));
498                                         string full = ns_node.Caption + "." + typename;
499                                         string url = type_node.PublicUrl;
500                                         string doc_tag = GetKindFromCaption (type_node.Caption);
501                                         string rest, hash;
502                                         var id = GetInternalIdForInternalUrl (type_node.GetInternalUrl (), out hash);
503                                         var xdoc = XDocument.Load (GetHelpStream (id));
504                                         if (xdoc == null)
505                                                 continue;
506                                         if (string.IsNullOrEmpty (doc_tag))
507                                                 continue;
508
509                                         // For classes, structures or interfaces add a doc for the overview and
510                                         // add a doc for every constructor, method, event, ...
511                                         // doc_tag == "Class" || doc_tag == "Structure" || doc_tag == "Interface"
512                                         if (doc_tag[0] == 'C' || doc_tag[0] == 'S' || doc_tag[0] == 'I') {
513                                                 // Adds a doc for every overview of every type
514                                                 SearchableDocument doc = searchDoc.Reset ();
515                                                 doc.Title = type_node.Caption;
516                                                 doc.HotText = typename;
517                                                 doc.Url = url;
518                                                 doc.FullTitle = full;
519
520                                                 var node_sel = xdoc.Root.Element ("Docs");
521                                                 text.Clear ();
522                                                 GetTextFromNode (node_sel, text);
523                                                 doc.Text = text.ToString ();
524
525                                                 text.Clear ();
526                                                 GetExamples (node_sel, text);
527                                                 doc.Examples = text.ToString ();
528
529                                                 writer.AddDocument (doc.LuceneDoc);
530                                                 var exportParsable = doc_tag[0] == 'C' && (ns_node.Caption.StartsWith ("MonoTouch") || ns_node.Caption.StartsWith ("MonoMac"));
531
532                                                 //Add docs for contructors, methods, etc.
533                                                 foreach (Node c in type_node.Nodes) { // c = Constructors || Fields || Events || Properties || Methods || Operators
534                                                         if (c.Element == "*")
535                                                                 continue;
536                                                         const float innerTypeBoost = 0.2f;
537
538                                                         IEnumerable<Node> ncnodes = c.Nodes;
539                                                         // The rationale is that we need to properly handle method overloads
540                                                         // so for those method node which have children, flatten them
541                                                         if (c.Caption == "Methods") {
542                                                                 ncnodes = ncnodes
543                                                                         .Where (n => n.Nodes == null || n.Nodes.Count == 0)
544                                                                         .Concat (ncnodes.Where (n => n.Nodes.Count > 0).SelectMany (n => n.Nodes));
545                                                         } else if (c.Caption == "Operators") {
546                                                                 ncnodes = ncnodes
547                                                                         .Where (n => !n.Caption.EndsWith ("Conversion"))
548                                                                         .Concat (ncnodes.Where (n => n.Caption.EndsWith ("Conversion")).SelectMany (n => n.Nodes));
549                                                         }
550
551                                                         var prematchedMembers = xdoc.Root.Element ("Members").Elements ("Member").ToLookup (n => (string)n.Attribute ("MemberName"), n => n);
552
553                                                         foreach (Node nc in ncnodes) {
554                                                                 XElement docsNode = null;
555                                                                 try {
556                                                                         docsNode = GetDocsFromCaption (xdoc, c.Caption[0] == 'C' ? ".ctor" : nc.Caption, c.Caption[0] == 'O', prematchedMembers);
557                                                                 } catch {}
558                                                                 if (docsNode == null) {
559                                                                         Console.Error.WriteLine ("Problem: {0}", nc.PublicUrl);
560                                                                         continue;
561                                                                 }
562
563                                                                 SearchableDocument doc_nod = searchDoc.Reset ();
564                                                                 doc_nod.Title = LargeName (nc) + " " + EcmaDoc.EtcKindToCaption (c.Caption[0]);
565                                                                 doc_nod.FullTitle = ns_node.Caption + '.' + typename + "::" + nc.Caption;
566                                                                 doc_nod.HotText = string.Empty;
567
568                                                                 /* Disable constructors hottext indexing as it's often "polluting" search queries
569                                                                    because it has the same hottext than standard types */
570                                                                 if (c.Caption != "Constructors") {
571                                                                         //dont add the parameters to the hottext
572                                                                         int ppos = nc.Caption.IndexOf ('(');
573                                                                         doc_nod.HotText = ppos != -1 ? nc.Caption.Substring (0, ppos) : nc.Caption;
574                                                                 }
575
576                                                                 var urlnc = nc.PublicUrl;
577                                                                 doc_nod.Url = urlnc;
578
579                                                                 text.Clear ();
580                                                                 GetTextFromNode (docsNode, text);
581                                                                 doc_nod.Text = text.ToString ();
582
583                                                                 text.Clear ();
584                                                                 GetExamples (docsNode, text);
585                                                                 doc_nod.Examples = text.ToString ();
586
587                                                                 Document lucene_doc = doc_nod.LuceneDoc;
588                                                                 lucene_doc.Boost = innerTypeBoost;
589                                                                 writer.AddDocument (lucene_doc);
590
591                                                                 // Objective-C binding specific parsing of [Export] attributes
592                                                                 if (exportParsable) {
593                                                                         try {
594                                                                                 var exports = docsNode.Parent.Elements ("Attributes").Elements ("Attribute").Elements ("AttributeName")
595                                                                                         .Select (a => (string)a).Where (txt => txt.Contains ("Foundation.Export"));
596
597                                                                                 foreach (var exportNode in exports) {
598                                                                                         var parts = exportNode.Split ('"');
599                                                                                         if (parts.Length != 3) {
600                                                                                                 Console.WriteLine ("Export attribute not found or not usable in {0}", exportNode);
601                                                                                                 continue;
602                                                                                         }
603
604                                                                                         var export = parts[1];
605                                                                                         var export_node = searchDoc.Reset ();
606                                                                                         export_node.Title = export + " Export";
607                                                                                         export_node.FullTitle = ns_node.Caption + '.' + typename + "::" + export;
608                                                                                         export_node.Url = urlnc;
609                                                                                         export_node.HotText = export;
610                                                                                         export_node.Text = string.Empty;
611                                                                                         export_node.Examples = string.Empty;
612                                                                                         lucene_doc = export_node.LuceneDoc;
613                                                                                         lucene_doc.Boost = innerTypeBoost;
614                                                                                         writer.AddDocument (lucene_doc);
615                                                                                 }
616                                                                         } catch (Exception e){
617                                                                                 Console.WriteLine ("Problem processing {0} for MonoTouch/MonoMac exports\n\n{0}", e);
618                                                                         }
619                                                                 }
620                                                         }
621                                                 }
622                                         // doc_tag == "Enumeration"
623                                         } else if (doc_tag[0] == 'E'){
624                                                 var members = xdoc.Root.Element ("Members").Elements ("Member");
625                                                 if (members == null)
626                                                         continue;
627
628                                                 text.Clear ();
629                                                 foreach (var member_node in members) {
630                                                         string enum_value = (string)member_node.Attribute ("MemberName");
631                                                         text.Append (enum_value);
632                                                         text.Append (" ");
633                                                         GetTextFromNode (member_node.Element ("Docs"), text);
634                                                         text.AppendLine ();
635                                                 }
636
637                                                 SearchableDocument doc = searchDoc.Reset ();
638
639                                                 text.Clear ();
640                                                 GetExamples (xdoc.Root.Element ("Docs"), text);
641                                                 doc.Examples = text.ToString ();
642
643                                                 doc.Title = type_node.Caption;
644                                                 doc.HotText = (string)xdoc.Root.Attribute ("Name");
645                                                 doc.FullTitle = full;
646                                                 doc.Url = url;
647                                                 doc.Text = text.ToString();
648                                                 writer.AddDocument (doc.LuceneDoc);
649                                         // doc_tag == "Delegate"
650                                         } else if (doc_tag[0] == 'D'){
651                                                 SearchableDocument doc = searchDoc.Reset ();
652                                                 doc.Title = type_node.Caption;
653                                                 doc.HotText = (string)xdoc.Root.Attribute ("Name");
654                                                 doc.FullTitle = full;
655                                                 doc.Url = url;
656
657                                                 var node_sel = xdoc.Root.Element ("Docs");
658
659                                                 text.Clear ();
660                                                 GetTextFromNode (node_sel, text);
661                                                 doc.Text = text.ToString();
662
663                                                 text.Clear ();
664                                                 GetExamples (node_sel, text);
665                                                 doc.Examples = text.ToString();
666
667                                                 writer.AddDocument (doc.LuceneDoc);
668                                         }
669                                 }
670                         }
671                 }
672
673                 string GetKindFromCaption (string s)
674                 {
675                         int p = s.LastIndexOf (' ');
676                         if (p > 0)
677                                 return s.Substring (p + 1);
678                         return null;
679                 }
680
681                 // Extract the interesting text from the docs node
682                 void GetTextFromNode (XElement n, StringBuilder sb)
683                 {
684                         // Include the text content of the docs
685                         sb.AppendLine (n.Value);
686                         foreach (var tag in n.Descendants ())
687                                 //include the url to which points the see tag and the name of the parameter
688                                 if ((tag.Name.LocalName.Equals ("see", StringComparison.Ordinal) || tag.Name.LocalName.Equals ("paramref", StringComparison.Ordinal))
689                                     && tag.HasAttributes)
690                                         sb.AppendLine ((string)tag.Attributes ().First ());
691                 }
692
693                 // Extract the code nodes from the docs
694                 void GetExamples (XElement n, StringBuilder sb)
695                 {
696                         foreach (var code in n.Descendants ("code"))
697                                 sb.Append ((string)code);
698                 }
699
700                 // Extract a large name for the Node
701                 static string LargeName (Node matched_node)
702                 {
703                         string[] parts = matched_node.GetInternalUrl ().Split('/', '#');
704                         if (parts.Length == 3 && parts[2] != String.Empty) //List of Members, properties, events, ...
705                                 return parts[1] + ": " + matched_node.Caption;
706                         else if(parts.Length >= 4) //Showing a concrete Member, property, ...
707                                 return parts[1] + "." + matched_node.Caption;
708                         else
709                                 return matched_node.Caption;
710                 }
711
712                 XElement GetMemberFromCaption (XDocument xdoc, string caption, bool isOperator, ILookup<string, XElement> prematchedMembers)
713                 {
714                         string name;
715                         IList<string> args;
716                         var doc = xdoc.Root.Element ("Members").Elements ("Member");
717
718                         if (isOperator) {
719                                 // The first case are explicit and implicit conversion operators which are grouped specifically
720                                 if (caption.IndexOf (" to ") != -1) {
721                                         var convArgs = caption.Split (new[] { " to " }, StringSplitOptions.None);
722                                         return doc
723                                                 .First (n => (AttrEq (n, "MemberName", "op_Explicit") || AttrEq (n, "MemberName", "op_Implicit"))
724                                                         && ((string)n.Element ("ReturnValue").Element ("ReturnType")).Equals (convArgs[1], StringComparison.Ordinal)
725                                                         && AttrEq (n.Element ("Parameters").Element ("Parameter"), "Type", convArgs[0]));
726                                 } else {
727                                         return doc.First (m => AttrEq (m, "MemberName", "op_" + caption));
728                                 }
729                         }
730
731                         TryParseCaption (caption, out name, out args);
732
733                         if (!string.IsNullOrEmpty (name)) { // Filter member by name
734                                 var prematched = prematchedMembers[name];
735                                 doc = prematched.Any () ? prematched : doc.Where (m => AttrEq (m, "MemberName", name));
736                         }
737                         if (args != null && args.Count > 0) // Filter member by its argument list
738                                 doc = doc.Where (m => m.Element ("Parameters").Elements ("Parameter").Attributes ("Type").Select (a => (string)a).SequenceEqual (args));
739
740                         return doc.First ();
741                 }
742
743                 XElement GetDocsFromCaption (XDocument xdoc, string caption, bool isOperator, ILookup<string, XElement> prematchedMembers)
744                 {
745                         return GetMemberFromCaption (xdoc, caption, isOperator, prematchedMembers).Element ("Docs");
746                 }
747
748                 // A simple stack-based parser to detect single type definition separated by commas
749                 IEnumerable<string> ExtractArguments (string rawArgList)
750                 {
751                         var sb = new System.Text.StringBuilder ();
752                         int genericDepth = 0;
753                         int arrayDepth = 0;
754
755                         for (int i = 0; i < rawArgList.Length; i++) {
756                                 char c = rawArgList[i];
757                                 switch (c) {
758                                 case ',':
759                                         if (genericDepth == 0 && arrayDepth == 0) {
760                                                 yield return sb.ToString ();
761                                                 sb.Clear ();
762                                                 continue;
763                                         }
764                                         break;
765                                 case '<':
766                                         genericDepth++;
767                                         break;
768                                 case '>':
769                                         genericDepth--;
770                                         break;
771                                 case '[':
772                                         arrayDepth++;
773                                         break;
774                                 case ']':
775                                         arrayDepth--;
776                                         break;
777                                 }
778                                 sb.Append (c);
779                         }
780                         if (sb.Length > 0)
781                                 yield return sb.ToString ();
782                 }
783
784                 void TryParseCaption (string caption, out string name, out IList<string> argList)
785                 {
786                         name = null;
787                         argList = null;
788                         int parenIdx = caption.IndexOf ('(');
789                         // In case of simple name, there is no need for processing
790                         if (parenIdx == -1) {
791                                 name = caption;
792                                 return;
793                         }
794                         name = caption.Substring (0, parenIdx);
795                         // Now we gather the argument list if there is any
796                         var rawArgList = caption.Substring (parenIdx + 1, caption.Length - parenIdx - 2); // Only take what's inside the parens
797                         if (string.IsNullOrEmpty (rawArgList))
798                                 return;
799
800                         argList = ExtractArguments (rawArgList).Select (arg => arg.Trim ()).ToList ();
801                 }
802
803                 bool AttrEq (XElement element, string attributeName, string expectedValue)
804                 {
805                         return ((string)element.Attribute (attributeName)).Equals (expectedValue, StringComparison.Ordinal);
806                 }
807         }
808 }