[Mono-docs-list] Web browser hyperlink cleanup

Fawad Halim fawad@fawad.net
Thu, 16 Oct 2003 17:12:21 -0500


This is a multi-part message in MIME format.
--------------080204020501090605060202
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Sorry about that. Here is the unified diff format.

Regards
-fawad

Miguel de Icaza wrote:

>Hello,
>
>  
>
>>    Attached is a patch that makes monodoc.ashx fix the <a> links on the 
>>server side instead of using javascript on the client side.
>>    
>>
>
>Great patch, one of the things I wanted to see. 
>
>Would you mind resending this in diff -u format, so it is easier to
>study/apply?
>
>Miguel
>_______________________________________________
>Mono-docs-list maillist  -  Mono-docs-list@lists.ximian.com
>http://lists.ximian.com/mailman/listinfo/mono-docs-list
>
>  
>


--------------080204020501090605060202
Content-Type: text/plain;
 name="Monodoc_ashx.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="Monodoc_ashx.diff"

Index: browser/web/monodoc.ashx
===================================================================
RCS file: /mono/monodoc/browser/web/monodoc.ashx,v
retrieving revision 1.18
diff -u -r1.18 monodoc.ashx
--- browser/web/monodoc.ashx	31 Aug 2003 23:45:50 -0000	1.18
+++ browser/web/monodoc.ashx	16 Oct 2003 22:09:49 -0000
@@ -21,6 +21,7 @@
 using System.Xml;
 using System.Xml.Xsl;
 using Monodoc;
+using System.Text.RegularExpressions;
 
 namespace Mono.Website.Handlers
 {
@@ -195,6 +196,7 @@
 			output.Flush();
 		}
 
+		string requestPath;
 		void PrintDocs (string content, HttpContext ctx)
 		{
 			ctx.Response.Write (@"
@@ -204,14 +206,12 @@
 <!--
 function load ()
 {
-	objs = document.getElementsByTagName('a');
-	for (i = 0; i < objs.length; i++) {
-		e = objs [i];
-		if (e.href == null) continue;
-		
-		objs[i].href = makeLink (objs[i].href);
+	// If topic loaded in a window by itself, load index.aspx with the same set of params.
+	if (top.location == document.location)
+	{
+		top.location.href = 'index.aspx'+document.location.search;
 	}
-	
+
 	objs = document.getElementsByTagName('img');
 	for (i = 0; i < objs.length; i++)
 	{
@@ -249,12 +249,43 @@
 		<body onLoad='load()'>
 
 		");
-			ctx.Response.Write (content);
+			// Set up object variable, as it's required by the MakeLink delegate
+			requestPath=ctx.Request.Path;
+			string content_changed=MakeLinks(content);
+			ctx.Response.Write (content_changed);
 			ctx.Response.Write (@"
 		</body>
 		</html>");
 		}
 
+
+		string MakeLinks(string content)
+		{
+			MatchEvaluator linkUpdater=new MatchEvaluator(MakeLink);
+			if(content.Trim().Length<1|| content==null)
+				return content;
+			try
+			{
+				string updatedContents=Regex.Replace(content,"(<a[^>]*href=['\"])([^'\"]+)(['\"][^>]*>)", linkUpdater);
+				return(updatedContents);
+			}
+			catch(Exception e)
+			{
+				return content+"!<!--Exception:"+e.Message+"-->";
+			}
+		}
+		
+		// Delegate to be called from MakeLinks for fixing <a> tag
+		string MakeLink(Match theMatch)
+		{
+			string updatedLink=String.Format("{0}{1}?link={2}{3}",
+				theMatch.Groups[1].ToString(),
+				requestPath, 
+				HttpUtility.UrlEncode(theMatch.Groups[2].ToString().Replace("file://","")),
+				theMatch.Groups[3].ToString());
+			return updatedLink;
+		}
+		
 		bool IHttpHandler.IsReusable
 		{
 			get {

--------------080204020501090605060202--