[Mono-list] monodoc links patch

Johannes Roith johannes@jroith.de
14 Jun 2003 21:31:43 +0200


--=-W3YYgMAAaegGNkfoM5kA
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

monodoc links patch attached

--=-W3YYgMAAaegGNkfoM5kA
Content-Disposition: attachment; filename=file.diff
Content-Type: text/x-patch; name=file.diff; charset=UTF-8
Content-Transfer-Encoding: 7bit

Index: xhtml-provider.cs
===================================================================
RCS file: /cvs/public/monodoc/browser/xhtml-provider.cs,v
retrieving revision 1.2
diff -u -r1.2 xhtml-provider.cs
--- xhtml-provider.cs	13 Jun 2003 07:39:10 -0000	1.2
+++ xhtml-provider.cs	14 Jun 2003 19:39:49 -0000
@@ -1,4 +1,4 @@
-//
+//.
 // A provider that uses Windows help file xhtml TOC files and looks for the
 // referenced documents to create the help source. 
 //
@@ -67,9 +67,98 @@
 		return docToProcess;
 	}
 
-	private XmlDocument RewriteLinks(XmlDocument docToProcess)
+
+
+
+
+	public static string GetAbsoluteLink(string target, string url) {
+
+				string value = null;
+
+				if  (target.Substring(0,1) != "#") {
+
+				char[] splitter = {':'};
+				string protocol = (target.Split(splitter))[0];
+
+
+				switch (protocol) {
+				case "mailto": 
+					//TODO
+					Console.WriteLine("Mail has to be implemented.");
+				break;
+				case "http":
+					//TODO
+					Console.WriteLine("Http has to be implemented.");
+				break;
+				case "ftp":
+					//TODO
+					Console.WriteLine("Ftp has to be implemented.");
+				break;
+				default:
+
+				int slash = url.LastIndexOf ("/");
+				string tmpurl  = url.Substring(0, slash);
+
+				/////// Kind of a Dirty hack :( - sorry
+
+				bool done = false;
+				while(!done)
+				{
+					if (target.Replace("../", "") != target) {
+						target = target.Replace("../", "");
+						slash = tmpurl.LastIndexOf ("/");
+						tmpurl  = tmpurl.Substring(0, slash);
+					}
+					else {
+						done = true;
+					}
+
+				}
+
+				value = tmpurl + "/" + target;
+
+				break;
+				}	
+				}
+
+				return value;
+	}
+
+
+
+
+
+	private XmlDocument RewriteLinks(XmlDocument docToProcess, string url)
 	{
-		// FIXME: Rewrite any links here that need extra necessary info...
+                XmlNodeList nodeList = docToProcess.GetElementsByTagName("a");
+                                                                                                    
+        foreach(XmlNode node in nodeList)
+                {
+	
+			if (((XmlElement)node).HasAttribute("href") ){
+				
+				string target = ((XmlElement)node).GetAttribute("href");
+
+				target = GetAbsoluteLink(target, url);
+				if (target != null)
+				{
+					string newtarget = String.Format ("source-id:{0}:xhtml:{1}", SourceID, target);
+	
+					XmlAttribute newAttr = docToProcess.CreateAttribute("href");
+					newAttr.Value = newtarget;
+					node.Attributes.RemoveNamedItem("href");
+					node.Attributes.SetNamedItem(newAttr);	
+					Console.WriteLine(target);
+				}
+
+
+			}
+                }
+                                                                                                    
+                                                                                                    
+
+
+
 		return docToProcess;
 	}
 
@@ -119,7 +208,7 @@
 			}
 			
 			XmlDocument processedDoc = ProcessContent(newdoc);
-			XmlDocument docForMonodoc = RewriteLinks(processedDoc);
+			XmlDocument docForMonodoc = RewriteLinks(processedDoc, url);
 			return docForMonodoc.InnerXml;
 		}
 
@@ -192,34 +281,41 @@
 			if (File.Exists(filename))
 				nodeToAddChildrenTo.tree.HelpSource.PackFile (filename, filename);
 
-		/*	if(File.Exists(Environment.CurrentDirectory + "\\" + attribs[1])) {
+			if(File.Exists(Environment.CurrentDirectory + "/" + attribs[1])) {
 
 				try {
-					newdoc = new XmlDocument();
-					newdoc.Load(Environment.CurrentDirectory + "\\" + attribs[1]);
-		
-					XmlNodeList nodeList = newdoc.GetElementsByTagName("a");
+					XmlDocument newdoc2 = new XmlDocument();
+
+					newdoc2.Load(Environment.CurrentDirectory + "/" + attribs[1]);
+					Console.WriteLine("there");
+					XmlNodeList nodeList = newdoc2.GetElementsByTagName("a");
+					Console.WriteLine("there");
 					foreach(XmlNode node in nodeList) {
 	
-						try {
 							Console.WriteLine(spaces + "   " + node.Attributes.GetNamedItem("href").Value);
-						}
-						catch
-						{
-						}
-					}
+
+							string linkfilename = node.Attributes.GetNamedItem("href").Value; 
+
+							linkfilename = XhtmlHelpSource.GetAbsoluteLink(linkfilename, filename);
+							if (linkfilename != null)
+							{
+								if (File.Exists(linkfilename))
+								nodeToAddChildrenTo.tree.HelpSource.PackFile (linkfilename, linkfilename);
+
+							}
 	
+					}
+
 				}
 
-				catch
-				{
+				catch {
 					Console.WriteLine(spaces + "-- PARSE ERROR --");
 				}
 
-*/			
-
 		}
 
+}
+
 		if (items[i].LocalName == "ul")
 		{
 			spaces += "      ";
@@ -227,12 +323,9 @@
 			nodeToAddChildrenTo = latestNodeAddition;
 			spaces = spaces.Substring(6);
 		}
-
 	}
 
-	
-   }
-
+}
 
    public string[] ParseLi(XmlNode me)
    {

--=-W3YYgMAAaegGNkfoM5kA--