File: man-provider.cs

package info (click to toggle)
mono 6.12.0.199%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,192 kB
  • sloc: cs: 11,181,844; xml: 2,850,076; ansic: 689,413; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,845; makefile: 19,951; sh: 15,030; python: 4,771; pascal: 925; sql: 859; sed: 16; php: 1
file content (106 lines) | stat: -rwxr-xr-x 2,521 bytes parent folder | download | duplicates (12)
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
//
// A provider to display man pages
//
// Authors:
//   Johannes Roith <johannes@roith.de>
//   Jonathan Pryor <jpryor@novell.com>
//
// (C) 2008 Novell, Inc.

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Linq;
using System.Collections.Generic;

namespace Monodoc.Providers
{
	public class ManProvider : Provider
	{
		string[] tocFiles;
	
		public ManProvider (string[] handbookTocFiles)
		{
			tocFiles = handbookTocFiles;

			// huh...
			if (!File.Exists (tocFiles[0]))
				throw new FileNotFoundException (String.Format ("The table of contents, `{0}' does not exist", tocFiles[0]));
		}

		public override void PopulateTree (Tree tree)
		{
			foreach(string TocFile in tocFiles) {
				XmlDocument doc = new XmlDocument();
				doc.Load (TocFile);

				XmlNodeList nodeList = doc.GetElementsByTagName("manpage");
				Node nodeToAddChildrenTo = tree.RootNode;
				var storage = nodeToAddChildrenTo.Tree.HelpSource.Storage;

				foreach (XmlNode node in nodeList) {

					XmlAttribute name = node.Attributes["name"];
					XmlAttribute page = node.Attributes["page"];

					if (name == null || page == null) continue;

					if (!File.Exists (page.Value))
						continue;

					string target = "man:" + name.Value;
					nodeToAddChildrenTo.CreateNode (name.Value, target);

					if (File.Exists (page.Value))
						using (var file = File.OpenRead (page.Value))
							storage.Store (name.Value, file);
				}
			}
		}

		public override void CloseTree (HelpSource hs, Tree tree)
		{
		}
	}

	public class ManHelpSource : HelpSource
	{
		const string ManPrefix = "man:";
		Dictionary<string, Node> nodesMap;

		public ManHelpSource (string base_file, bool create) : base (base_file, create)
		{
			nodesMap = Tree.RootNode.ChildNodes.ToDictionary (n => n.Element);
		}

		// Since man always has a flat tree and rather small amount of item
		// we store them in a dictionary
		public override Node MatchNode (string url)
		{
			Node result;
			return nodesMap.TryGetValue (url, out result) ? result : null;
		}

		public override DocumentType GetDocumentTypeForId (string id)
		{
			return id == "root:" ? DocumentType.TocXml : DocumentType.Man;
		}

		public override bool IsGeneratedContent (string id)
		{
			return id == "root:";
		}
	
		public override string GetText (string url)
		{
			return TreeDumper.ExportToTocXml (Tree.RootNode, "Mono Documentation Library", "Available man pages:");
		}

		protected override string UriPrefix {
			get {
				return ManPrefix;
			}
		}
	}
}