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"));
107 public static RootTree LoadTree (string indexDir, XmlDocument docTree, IEnumerable<string> sourceFiles)
109 if (docTree == null) {
110 docTree = new XmlDocument ();
111 using (Stream manifestResourceStream = typeof (RootTree).Assembly.GetManifestResourceStream ("monodoc.xml")) {
112 docTree.Load (manifestResourceStream);
116 sourceFiles = (sourceFiles ?? new string[0]);
117 RootTree rootTree = new RootTree ();
118 rootTree.basedir = indexDir;
119 XmlNodeList xml_node_list = docTree.SelectNodes ("/node/node");
120 rootTree.nameToNode["root"] = rootTree.RootNode;
121 rootTree.nameToNode["libraries"] = rootTree.RootNode;
122 rootTree.Populate (rootTree.RootNode, xml_node_list);
124 if (rootTree.LookupEntryPoint ("various") == null) {
125 Console.Error.WriteLine ("No 'various' doc node! Check monodoc.xml!");
126 Node rootNode = rootTree.RootNode;
129 foreach (string current in sourceFiles)
130 rootTree.AddSourceFile (current);
132 foreach (string path in uncompiledHelpSourcePaths) {
133 var hs = new Providers.EcmaUncompiledHelpSource (path);
134 hs.RootTree = rootTree;
135 rootTree.helpSources.Add (hs);
136 string epath = "extra-help-source-" + hs.Name;
137 Node hsn = rootTree.RootNode.CreateNode (hs.Name, "root:/" + epath);
138 rootTree.nameToHelpSource [epath] = hs;
140 foreach (Node n in hs.Tree.RootNode.Nodes)
144 RootTree.PurgeNode (rootTree.RootNode);
145 rootTree.RootNode.Sort ();
149 public void AddSource (string sourcesDir)
151 IEnumerable<string> enumerable = Directory.EnumerateFiles (sourcesDir, "*.source");
152 foreach (string current in enumerable)
153 if (!this.AddSourceFile (current))
154 Console.Error.WriteLine ("Error: Could not load source file {0}", current);
157 public bool AddSourceFile (string sourceFile)
159 if (this.loadedSourceFiles.Contains (sourceFile))
162 Node node = this.LookupEntryPoint ("various") ?? base.RootNode;
163 XmlDocument xmlDocument = new XmlDocument ();
165 xmlDocument.Load (sourceFile);
171 XmlNodeList extra_nodes = xmlDocument.SelectNodes ("/monodoc/node");
172 if (extra_nodes.Count > 0)
173 this.Populate (node, extra_nodes);
175 XmlNodeList sources = xmlDocument.SelectNodes ("/monodoc/source");
176 if (sources == null) {
177 Console.Error.WriteLine ("Error: No <source> section found in the {0} file", sourceFile);
181 loadedSourceFiles.Add (sourceFile);
182 foreach (XmlNode xmlNode in sources) {
183 XmlAttribute a = xmlNode.Attributes["provider"];
185 Console.Error.WriteLine ("Error: no provider in <source>");
188 string provider = a.InnerText;
189 a = xmlNode.Attributes["basefile"];
191 Console.Error.WriteLine ("Error: no basefile in <source>");
194 string basefile = a.InnerText;
195 a = xmlNode.Attributes["path"];
197 Console.Error.WriteLine ("Error: no path in <source>");
200 string path = a.InnerText;
201 string basefilepath = Path.Combine (Path.GetDirectoryName (sourceFile), basefile);
202 HelpSource helpSource = RootTree.GetHelpSource (provider, basefilepath);
203 if (helpSource != null) {
204 helpSource.RootTree = this;
205 this.helpSources.Add (helpSource);
206 this.nameToHelpSource[path] = helpSource;
207 Node node2 = this.LookupEntryPoint (path);
209 Console.Error.WriteLine ("node `{0}' is not defined on the documentation map", path);
212 foreach (Node current in helpSource.Tree.RootNode.Nodes) {
213 node2.AddNode (current);
221 static bool PurgeNode (Node node)
224 if (!node.Documented)
226 List<Node> list = new List<Node> ();
227 foreach (Node current in node.Nodes)
229 bool flag = RootTree.PurgeNode (current);
235 result = (node.Nodes.Count == list.Count);
236 foreach (Node current2 in list)
238 node.DeleteNode (current2);
244 public static string[] GetSupportedFormats ()
256 public static HelpSource GetHelpSource (string provider, string basefilepath)
263 result = new XhtmlHelpSource (basefilepath, false);
266 result = new ManHelpSource (basefilepath, false);
269 result = new ErrorHelpSource (basefilepath, false);
272 result = new EcmaSpecHelpSource (basefilepath, false);
275 result = new EcmaHelpSource (basefilepath, false);
278 Console.Error.WriteLine ("Error: Unknown provider specified: {0}", provider);
282 } catch (FileNotFoundException) {
283 Console.Error.WriteLine ("Error: did not find one of the files in sources/" + basefilepath);
289 public static Provider GetProvider (string provider, params string[] basefilepaths)
293 return new EcmaProvider (basefilepaths[0]);
295 return new EcmaSpecProvider (basefilepaths[0]);
297 return new ErrorProvider (basefilepaths[0]);
299 return new ManProvider (basefilepaths);
302 return new XhtmlProvider (basefilepaths[0]);
305 throw new NotSupportedException (provider);
308 void Populate (Node parent, XmlNodeList xml_node_list)
310 foreach (XmlNode xmlNode in xml_node_list) {
311 XmlAttribute e = xmlNode.Attributes["parent"];
313 if (e != null && this.nameToNode.TryGetValue (e.InnerText, out parent2)) {
314 xmlNode.Attributes.Remove (e);
315 Populate (parent2, xmlNode.SelectNodes ("."));
318 e = xmlNode.Attributes["label"];
320 Console.Error.WriteLine ("`label' attribute missing in <node>");
323 string label = e.InnerText;
324 e = xmlNode.Attributes["name"];
326 Console.Error.WriteLine ("`name' attribute missing in <node>");
329 string name = e.InnerText;
330 Node orCreateNode = parent.GetOrCreateNode (label, "root:/" + name);
331 orCreateNode.EnsureNodes ();
332 this.nameToNode[name] = orCreateNode;
333 XmlNodeList xmlNodeList = xmlNode.SelectNodes ("./node");
334 if (xmlNodeList != null) {
335 this.Populate (orCreateNode, xmlNodeList);
340 public Node LookupEntryPoint (string name)
343 if (!this.nameToNode.TryGetValue (name, out result)) {
349 public TOutput RenderUrl<TOutput> (string url, IDocGenerator<TOutput> generator, HelpSource hintSource = null)
352 return RenderUrl<TOutput> (url, generator, out dummy, hintSource);
355 public TOutput RenderUrl<TOutput> (string url, IDocGenerator<TOutput> generator, out Node node, HelpSource hintSource = null)
358 string internalId = null;
359 HelpSource hs = GetHelpSourceAndIdForUrl (url, hintSource, out internalId, out node);
360 return generator.Generate (hs, internalId);
363 public HelpSource GetHelpSourceAndIdForUrl (string url, out string internalId)
366 return GetHelpSourceAndIdForUrl (url, out internalId, out dummy);
369 public HelpSource GetHelpSourceAndIdForUrl (string url, out string internalId, out Node node)
371 return GetHelpSourceAndIdForUrl (url, null, out internalId, out node);
374 public HelpSource GetHelpSourceAndIdForUrl (string url, HelpSource hintSource, out string internalId, out Node node)
379 if (url.StartsWith ("root:/", StringComparison.OrdinalIgnoreCase))
380 return this.GetHelpSourceAndIdFromName (url.Substring ("root:/".Length), out internalId, out node);
382 HelpSource helpSource = hintSource;
383 if (helpSource == null || string.IsNullOrEmpty (internalId = helpSource.GetInternalIdForUrl (url, out node))) {
385 foreach (var hs in helpSources.Where (h => h.CanHandleUrl (url))) {
386 if (!string.IsNullOrEmpty (internalId = hs.GetInternalIdForUrl (url, out node))) {
396 public HelpSource GetHelpSourceAndIdFromName (string name, out string internalId, out Node node)
398 internalId = "root:";
399 node = this.LookupEntryPoint (name);
401 return node == null ? null : node.Nodes.Select (n => n.Tree.HelpSource).Where (hs => hs != null).Distinct ().FirstOrDefault ();
404 public HelpSource GetHelpSourceFromId (int id)
406 return (id < 0 || id >= this.helpSources.Count) ? null : this.helpSources[id];
409 public Stream GetImage (string url)
411 if (url.StartsWith ("source-id:", StringComparison.OrdinalIgnoreCase)) {
412 string text = url.Substring (10);
413 int num = text.IndexOf (":");
414 string text2 = text.Substring (0, num);
417 id = int.Parse (text2);
419 Console.Error.WriteLine ("Failed to parse source-id url: {0} `{1}'", url, text2);
422 HelpSource helpSourceFromId = this.GetHelpSourceFromId (id);
423 return helpSourceFromId.GetImage (text.Substring (num + 1));
425 Assembly assembly = Assembly.GetAssembly (typeof (RootTree));
426 return assembly.GetManifestResourceStream (url);
429 public IndexReader GetIndex ()
432 string text = Path.Combine (this.basedir, "monodoc.index");
433 if (File.Exists (text))
434 return IndexReader.Load (text);
436 text = Path.Combine (Settings.Get ("monodocIndexDirectory"), "monodoc.index");
437 return IndexReader.Load (text);
443 public static void MakeIndex ()
445 RootTree rootTree = RootTree.LoadTree ();
446 rootTree.GenerateIndex ();
449 public void GenerateIndex ()
451 IndexMaker indexMaker = new IndexMaker ();
452 foreach (HelpSource current in this.helpSources)
453 current.PopulateIndex (indexMaker);
454 string text = Path.Combine (this.basedir, "monodoc.index");
456 indexMaker.Save (text);
457 } catch (UnauthorizedAccessException) {
458 text = Path.Combine (Settings.Get ("docPath"), "monodoc.index");
460 indexMaker.Save (text);
461 } catch (UnauthorizedAccessException) {
462 Console.WriteLine ("Unable to write index file in {0}", Path.Combine (Settings.Get ("docPath"), "monodoc.index"));
467 RootTree.chmod (text, 420);
469 Console.WriteLine ("Documentation index at {0} updated", text);
472 public SearchableIndex GetSearchIndex ()
475 string text = Path.Combine (this.basedir, "search_index");
476 if (System.IO.Directory.Exists (text))
477 return SearchableIndex.Load (text);
478 text = Path.Combine (Settings.Get ("docPath"), "search_index");
479 return SearchableIndex.Load (text);
485 public static void MakeSearchIndex ()
487 RootTree rootTree = RootTree.LoadTree ();
488 rootTree.GenerateSearchIndex ();
491 public void GenerateSearchIndex ()
493 Console.WriteLine ("Loading the monodoc tree...");
494 string text = Path.Combine (this.basedir, "search_index");
495 IndexWriter indexWriter;
496 var analyzer = new StandardAnalyzer (Lucene.Net.Util.Version.LUCENE_CURRENT);
497 var directory = Lucene.Net.Store.FSDirectory.Open (text);
500 if (!Directory.Exists (text))
501 Directory.CreateDirectory (text);
502 indexWriter = new IndexWriter (directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
503 } catch (UnauthorizedAccessException) {
505 text = Path.Combine (Settings.Get ("docPath"), "search_index");
506 if (!Directory.Exists (text))
507 Directory.CreateDirectory (text);
508 indexWriter = new IndexWriter (directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
509 } catch (UnauthorizedAccessException) {
510 Console.WriteLine ("You don't have permissions to write on " + text);
514 Console.WriteLine ("Collecting and adding documents...");
515 foreach (HelpSource current in this.helpSources) {
516 current.PopulateSearchableIndex (indexWriter);
518 Console.WriteLine ("Closing...");
519 indexWriter.Optimize ();
520 indexWriter.Close ();
524 static extern int chmod (string filename, int mode);