2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Collections.Specialized;
5 using System.Configuration;
8 using System.Reflection;
9 using System.Runtime.InteropServices;
12 using Monodoc.Providers;
13 using Lucene.Net.Analysis.Standard;
14 using Lucene.Net.Index;
24 public const int MonodocVersion = 2;
25 const string RootNamespace = "root:/";
27 static List<string> uncompiledHelpSourcePaths = new List<string>();
28 HashSet<string> loadedSourceFiles = new HashSet<string>();
29 List<HelpSource> helpSources = new List<HelpSource>();
30 Dictionary<string, Node> nameToNode = new Dictionary<string, Node>();
31 Dictionary<string, HelpSource> nameToHelpSource = new Dictionary<string, HelpSource>();
33 public IList<HelpSource> HelpSources {
35 return this.helpSources.AsReadOnly();
39 public DateTime LastHelpSourceTime {
46 int platform = (int)Environment.OSVersion.Platform;
47 return platform == 4 || platform == 128 || platform == 6;
51 RootTree () : base (null, "Mono Documentation", "root:")
53 base.RootNode.EnsureNodes();
54 this.LastHelpSourceTime = DateTime.Now;
57 public static void AddUncompiledSource (string path)
59 uncompiledHelpSourcePaths.Add (path);
62 public static RootTree LoadTree ()
64 return RootTree.LoadTree (RootTree.ProbeBaseDirectories ());
67 static string ProbeBaseDirectories ()
71 result = Settings.Get ("docPath") ?? ".";
77 public static RootTree LoadTree (string basedir, bool includeExternal = true)
79 if (string.IsNullOrEmpty (basedir))
80 throw new ArgumentNullException ("basedir");
81 if (!Directory.Exists (basedir))
82 throw new ArgumentException ("basedir", string.Format ("Base documentation directory at '{0}' doesn't exist", basedir));
84 XmlDocument xmlDocument = new XmlDocument ();
85 string filename = Path.Combine (basedir, "monodoc.xml");
86 xmlDocument.Load (filename);
87 IEnumerable<string> sourceFiles = Directory.EnumerateFiles (Path.Combine (basedir, "sources"), "*.source");
89 sourceFiles = sourceFiles.Concat (RootTree.ProbeExternalDirectorySources ());
90 return RootTree.LoadTree (basedir, xmlDocument, sourceFiles);
93 static IEnumerable<string> ProbeExternalDirectorySources ()
95 IEnumerable<string> enumerable = Enumerable.Empty<string> ();
97 string path = Settings.Get ("docExternalPath");
98 enumerable = enumerable.Concat (System.IO.Directory.EnumerateFiles (path, "*.source"));
102 if (Directory.Exists ("/Library/Frameworks/Mono.framework/External/monodoc"))
103 enumerable = enumerable.Concat (Directory.EnumerateFiles ("/Library/Frameworks/Mono.framework/External/monodoc", "*.source"));
105 var windowsPath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "monodoc");
106 if (Directory.Exists (windowsPath))
107 enumerable = enumerable.Concat (Directory.EnumerateFiles (windowsPath, "*.source"));
112 public static RootTree LoadTree (string indexDir, XmlDocument docTree, IEnumerable<string> sourceFiles)
114 if (docTree == null) {
115 docTree = new XmlDocument ();
116 using (Stream manifestResourceStream = typeof (RootTree).Assembly.GetManifestResourceStream ("monodoc.xml")) {
117 docTree.Load (manifestResourceStream);
121 sourceFiles = (sourceFiles ?? new string[0]);
122 RootTree rootTree = new RootTree ();
123 rootTree.basedir = indexDir;
124 XmlNodeList xml_node_list = docTree.SelectNodes ("/node/node");
125 rootTree.nameToNode["root"] = rootTree.RootNode;
126 rootTree.nameToNode["libraries"] = rootTree.RootNode;
127 rootTree.Populate (rootTree.RootNode, xml_node_list);
129 if (rootTree.LookupEntryPoint ("various") == null) {
130 Console.Error.WriteLine ("No 'various' doc node! Check monodoc.xml!");
131 Node rootNode = rootTree.RootNode;
134 foreach (string current in sourceFiles)
135 rootTree.AddSourceFile (current);
137 foreach (string path in uncompiledHelpSourcePaths) {
138 var hs = new Providers.EcmaUncompiledHelpSource (path);
139 hs.RootTree = rootTree;
140 rootTree.helpSources.Add (hs);
141 string epath = "extra-help-source-" + hs.Name;
142 Node hsn = rootTree.RootNode.CreateNode (hs.Name, "root:/" + epath);
143 rootTree.nameToHelpSource [epath] = hs;
145 foreach (Node n in hs.Tree.RootNode.Nodes)
149 RootTree.PurgeNode (rootTree.RootNode);
150 rootTree.RootNode.Sort ();
154 public void AddSource (string sourcesDir)
156 IEnumerable<string> enumerable = Directory.EnumerateFiles (sourcesDir, "*.source");
157 foreach (string current in enumerable)
158 if (!this.AddSourceFile (current))
159 Console.Error.WriteLine ("Error: Could not load source file {0}", current);
162 public bool AddSourceFile (string sourceFile)
164 if (this.loadedSourceFiles.Contains (sourceFile))
167 Node node = this.LookupEntryPoint ("various") ?? base.RootNode;
168 XmlDocument xmlDocument = new XmlDocument ();
170 xmlDocument.Load (sourceFile);
176 XmlNodeList extra_nodes = xmlDocument.SelectNodes ("/monodoc/node");
177 if (extra_nodes.Count > 0)
178 this.Populate (node, extra_nodes);
180 XmlNodeList sources = xmlDocument.SelectNodes ("/monodoc/source");
181 if (sources == null) {
182 Console.Error.WriteLine ("Error: No <source> section found in the {0} file", sourceFile);
186 loadedSourceFiles.Add (sourceFile);
187 foreach (XmlNode xmlNode in sources) {
188 XmlAttribute a = xmlNode.Attributes["provider"];
190 Console.Error.WriteLine ("Error: no provider in <source>");
193 string provider = a.InnerText;
194 a = xmlNode.Attributes["basefile"];
196 Console.Error.WriteLine ("Error: no basefile in <source>");
199 string basefile = a.InnerText;
200 a = xmlNode.Attributes["path"];
202 Console.Error.WriteLine ("Error: no path in <source>");
205 string path = a.InnerText;
206 string basefilepath = Path.Combine (Path.GetDirectoryName (sourceFile), basefile);
207 HelpSource helpSource = RootTree.GetHelpSource (provider, basefilepath);
208 if (helpSource != null) {
209 helpSource.RootTree = this;
210 this.helpSources.Add (helpSource);
211 this.nameToHelpSource[path] = helpSource;
212 Node node2 = this.LookupEntryPoint (path);
214 Console.Error.WriteLine ("node `{0}' is not defined on the documentation map", path);
217 foreach (Node current in helpSource.Tree.RootNode.Nodes) {
218 node2.AddNode (current);
226 static bool PurgeNode (Node node)
229 if (!node.Documented)
231 List<Node> list = new List<Node> ();
232 foreach (Node current in node.Nodes)
234 bool flag = RootTree.PurgeNode (current);
240 result = (node.Nodes.Count == list.Count);
241 foreach (Node current2 in list)
243 node.DeleteNode (current2);
249 public static string[] GetSupportedFormats ()
261 public static HelpSource GetHelpSource (string provider, string basefilepath)
268 result = new XhtmlHelpSource (basefilepath, false);
271 result = new ManHelpSource (basefilepath, false);
274 result = new ErrorHelpSource (basefilepath, false);
277 result = new EcmaSpecHelpSource (basefilepath, false);
280 result = new EcmaHelpSource (basefilepath, false);
283 Console.Error.WriteLine ("Error: Unknown provider specified: {0}", provider);
287 } catch (FileNotFoundException) {
288 Console.Error.WriteLine ("Error: did not find one of the files in sources/" + basefilepath);
294 public static Provider GetProvider (string provider, params string[] basefilepaths)
298 return new EcmaProvider (basefilepaths[0]);
300 return new EcmaSpecProvider (basefilepaths[0]);
302 return new ErrorProvider (basefilepaths[0]);
304 return new ManProvider (basefilepaths);
307 return new XhtmlProvider (basefilepaths[0]);
310 throw new NotSupportedException (provider);
313 void Populate (Node parent, XmlNodeList xml_node_list)
315 foreach (XmlNode xmlNode in xml_node_list) {
316 XmlAttribute e = xmlNode.Attributes["parent"];
318 if (e != null && this.nameToNode.TryGetValue (e.InnerText, out parent2)) {
319 xmlNode.Attributes.Remove (e);
320 Populate (parent2, xmlNode.SelectNodes ("."));
323 e = xmlNode.Attributes["label"];
325 Console.Error.WriteLine ("`label' attribute missing in <node>");
328 string label = e.InnerText;
329 e = xmlNode.Attributes["name"];
331 Console.Error.WriteLine ("`name' attribute missing in <node>");
334 string name = e.InnerText;
335 Node orCreateNode = parent.GetOrCreateNode (label, "root:/" + name);
336 orCreateNode.EnsureNodes ();
337 this.nameToNode[name] = orCreateNode;
338 XmlNodeList xmlNodeList = xmlNode.SelectNodes ("./node");
339 if (xmlNodeList != null) {
340 this.Populate (orCreateNode, xmlNodeList);
345 public Node LookupEntryPoint (string name)
348 if (!this.nameToNode.TryGetValue (name, out result)) {
354 public TOutput RenderUrl<TOutput> (string url, IDocGenerator<TOutput> generator, HelpSource hintSource = null)
357 return RenderUrl<TOutput> (url, generator, out dummy, hintSource);
360 public TOutput RenderUrl<TOutput> (string url, IDocGenerator<TOutput> generator, out Node node, HelpSource hintSource = null)
363 string internalId = null;
364 Dictionary<string, string> context = null;
365 HelpSource hs = GetHelpSourceAndIdForUrl (url, hintSource, out internalId, out context, out node);
366 return generator.Generate (hs, internalId, context);
369 public HelpSource GetHelpSourceAndIdForUrl (string url, out string internalId, out Dictionary<string, string> context)
372 return GetHelpSourceAndIdForUrl (url, out internalId, out context, out dummy);
375 public HelpSource GetHelpSourceAndIdForUrl (string url, out string internalId, out Dictionary<string, string> context, out Node node)
377 return GetHelpSourceAndIdForUrl (url, null, out internalId, out context, out node);
380 public HelpSource GetHelpSourceAndIdForUrl (string url, HelpSource hintSource, out string internalId, out Dictionary<string, string> context, out Node node)
386 if (url.StartsWith ("root:/", StringComparison.OrdinalIgnoreCase))
387 return this.GetHelpSourceAndIdFromName (url.Substring ("root:/".Length), out internalId, out node);
389 HelpSource helpSource = hintSource;
390 if (helpSource == null || string.IsNullOrEmpty (internalId = helpSource.GetInternalIdForUrl (url, out node, out context))) {
392 foreach (var hs in helpSources.Where (h => h.CanHandleUrl (url))) {
393 if (!string.IsNullOrEmpty (internalId = hs.GetInternalIdForUrl (url, out node, out context))) {
403 public HelpSource GetHelpSourceAndIdFromName (string name, out string internalId, out Node node)
405 internalId = "root:";
406 node = this.LookupEntryPoint (name);
408 return node == null ? null : node.Nodes.Select (n => n.Tree.HelpSource).Where (hs => hs != null).Distinct ().FirstOrDefault ();
411 public HelpSource GetHelpSourceFromId (int id)
413 return (id < 0 || id >= this.helpSources.Count) ? null : this.helpSources[id];
416 public Stream GetImage (string url)
418 if (url.StartsWith ("source-id:", StringComparison.OrdinalIgnoreCase)) {
419 string text = url.Substring (10);
420 int num = text.IndexOf (":");
421 string text2 = text.Substring (0, num);
423 if (!int.TryParse (text2, out id)) {
424 Console.Error.WriteLine ("Failed to parse source-id url: {0} `{1}'", url, text2);
427 HelpSource helpSourceFromId = this.GetHelpSourceFromId (id);
428 return helpSourceFromId.GetImage (text.Substring (num + 1));
430 Assembly assembly = Assembly.GetAssembly (typeof (RootTree));
431 return assembly.GetManifestResourceStream (url);
434 public IndexReader GetIndex ()
436 var paths = GetIndexesPathPrefixes ().Select (bp => Path.Combine (bp, "monodoc.index"));
437 var p = paths.FirstOrDefault (File.Exists);
438 return p == null ? (IndexReader)null : IndexReader.Load (p);
441 public static void MakeIndex ()
443 RootTree rootTree = RootTree.LoadTree ();
444 rootTree.GenerateIndex ();
447 public bool GenerateIndex ()
449 IndexMaker indexMaker = new IndexMaker ();
450 foreach (HelpSource current in this.helpSources)
451 current.PopulateIndex (indexMaker);
453 var paths = GetIndexesPathPrefixes ().Select (bp => Path.Combine (bp, "monodoc.index"));
454 bool successful = false;
456 foreach (var path in paths) {
458 indexMaker.Save (path);
461 RootTree.chmod (path, 420);
462 } catch (UnauthorizedAccessException) {
466 Console.WriteLine ("You don't have permissions to write on any of [" + string.Join (", ", paths) + "]");
470 Console.WriteLine ("Documentation index updated");
474 public SearchableIndex GetSearchIndex ()
476 var paths = GetIndexesPathPrefixes ().Select (bp => Path.Combine (bp, "search_index"));
477 var p = paths.FirstOrDefault (Directory.Exists);
478 return p == null ? (SearchableIndex)null : SearchableIndex.Load (p);
481 public static void MakeSearchIndex ()
483 RootTree rootTree = RootTree.LoadTree ();
484 rootTree.GenerateSearchIndex ();
487 public bool GenerateSearchIndex ()
489 Console.WriteLine ("Loading the monodoc tree...");
490 IndexWriter indexWriter = null;
491 var analyzer = new StandardAnalyzer (Lucene.Net.Util.Version.LUCENE_CURRENT);
492 var paths = GetIndexesPathPrefixes ().Select (bp => Path.Combine (bp, "search_index"));
493 bool successful = false;
495 foreach (var path in paths) {
497 if (!Directory.Exists (path))
498 Directory.CreateDirectory (path);
499 var directory = Lucene.Net.Store.FSDirectory.Open (path);
500 indexWriter = new IndexWriter (directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
502 } catch (UnauthorizedAccessException) {}
505 Console.WriteLine ("You don't have permissions to write on any of [" + string.Join (", ", paths) + "]");
508 Console.WriteLine ("Collecting and adding documents...");
509 foreach (HelpSource current in this.helpSources) {
510 current.PopulateSearchableIndex (indexWriter);
512 Console.WriteLine ("Closing...");
513 indexWriter.Optimize ();
514 indexWriter.Close ();
519 static extern int chmod (string filename, int mode);
521 IEnumerable<string> GetIndexesPathPrefixes ()
523 yield return basedir;
524 yield return Settings.Get ("docPath");
525 var indexDirectory = Settings.Get ("monodocIndexDirectory");
526 if (!string.IsNullOrEmpty (indexDirectory))
527 yield return indexDirectory;
528 yield return Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "monodoc");