2 // A provider that uses Windows help file xhtml TOC files and looks for the
3 // referenced documents to create the help source.
6 // Copyright 2003 Lee Mallabone <gnome@fonicmonkey.net>
7 // Johannes Roith <johannes@roith.de>
8 // Miguel de Icaza <miguel@ximian.com>
12 using System.Collections.Generic;
14 using System.Text.RegularExpressions;
17 namespace MonkeyDoc.Providers
19 public class XhtmlProvider : Provider
23 public XhtmlProvider (string handbookTocFile)
25 tocFile = handbookTocFile;
26 if (!File.Exists (tocFile))
27 throw new FileNotFoundException (String.Format ("The table of contents, `{0}' does not exist", tocFile));
30 public override void PopulateTree (Tree tree)
32 //new SimpleHandbookTOCParser(tree, tocFile);
36 public override void CloseTree (HelpSource hs, Tree tree)
41 public class XhtmlHelpSource : HelpSource
43 public XhtmlHelpSource (string base_file, bool create) : base (base_file, create)
48 const string XhtmlPrefix = "xhtml:";
50 protected override string UriPrefix {
56 public override DocumentType GetDocumentTypeForId (string id, out Dictionary<string, string> extraArgs)
59 return id == "root:" ? DocumentType.TocXml : DocumentType.MonoBook;
62 public override bool IsGeneratedContent (string id)
67 public override string GetText (string url)
69 return TreeDumper.ExportToTocXml (Tree.RootNode, "Mono Handbook", string.Empty);
72 public static string GetAbsoluteLink(string target, string url)
77 if (target.StartsWith ("#") ||
78 target.StartsWith ("T:") ||
79 target.StartsWith ("M:") ||
80 target.StartsWith ("P:") ||
81 target.StartsWith ("T:") ||
82 target.StartsWith ("E:") ||
83 target.StartsWith ("F:") ||
84 target.StartsWith ("O:") ||
85 target.StartsWith ("N:") ||
86 target.StartsWith ("api:"))
89 int endp = target.IndexOf(':');
93 string protocol = target.Substring(0, endp);
103 // handle absolute urls like: /html/en/images/empty.png
104 if (!target.StartsWith("/")) {
106 // url is something like "gnome/bindings/mono.html"
107 // This will get the path "gnome/bindings"
109 int slash = url.LastIndexOf ("/");
113 tmpurl = url.Substring(0, slash);
115 // Count "../" in target and go one level down
116 // for each in tmpurl, eventually, then remove "../".
118 Regex reg1 = new Regex("../");
119 MatchCollection matches = reg1.Matches(target);
121 for(int i = 1; i < matches.Count; i++) {
122 slash = tmpurl.LastIndexOf ("/");
124 tmpurl = tmpurl.Substring(0, slash);
127 target = target.Replace("../", "");
129 value = tmpurl + "/" + target;
132 value = target.Substring(1, target.Length - 1);
139 XmlDocument RewriteLinks(XmlDocument docToProcess, string url)
141 XmlNodeList nodeList = docToProcess.GetElementsByTagName("a");
143 foreach(XmlNode node in nodeList) {
145 XmlElement element = (XmlElement) node;
147 if (element.HasAttribute("href") ){
149 XmlAttribute href = element.GetAttributeNode("href");
150 string target = href.Value;
152 target = GetAbsoluteLink(target, url);
153 if (target != null) {
154 string newtarget = String.Format ("source-id:{0}:xhtml:{1}", SourceID, target);
155 href.Value = newtarget;
160 nodeList = docToProcess.GetElementsByTagName("img");
162 foreach(XmlNode node in nodeList) {
164 XmlElement element = (XmlElement) node;
166 if (element.HasAttribute("src") ){
168 XmlAttribute href = element.GetAttributeNode("src");
169 string target = href.Value;
171 target = GetAbsoluteLink(target, url);
172 if (target != null) {
173 string newtarget = String.Format ("source-id:{0}:xhtml:{1}", SourceID, target);
174 href.Value = newtarget;
182 public override void PopulateIndex (IndexMaker index_maker)
184 PopulateIndexFromNodes (Tree.RootNode);
187 void PopulateIndexFromNodes (Node start)
189 var nodes = start.Nodes;
192 Console.WriteLine ("Leaf: " + start.Caption);
194 Console.WriteLine ("Root: " + start.Caption);
195 foreach (Node n in nodes)
196 PopulateIndexFromNodes (n);