shithub: riscv

Download patch

ref: 6336c8387b7b426cd4e149171df65653cf964e5c
parent: 4eaea14f763dfa6e9bd695d7cd359a8c76aae144
author: cinap_lenrek <[email protected]>
date: Tue Feb 21 06:08:05 EST 2012

mothra: linkify

--- a/sys/src/cmd/mothra/rdhtml.c
+++ b/sys/src/cmd/mothra/rdhtml.c
@@ -259,9 +259,37 @@
 	dst[len]=0;
 	return dst;
 }
+int alnumchar(int c){
+	return 'a'<=c && c<='z' || 'A'<=c && c<='Z' || '0'<=c && c<='9';
+}
 int entchar(int c){
-	return c=='#' || 'a'<=c && c<='z' || 'A'<=c && c<='Z' || '0'<=c && c<='9';
+	return c=='#' || alnumchar(c);
 }
+
+/* return url if text token looks like a hyperlink */
+char *linkify(char *s){
+	if(!cistrncmp(s, "http://", 7))
+		return strdup(s);
+	if(!cistrncmp(s, "https://", 8))
+		return strdup(s);
+	if(!cistrncmp(s, "www.", 4)){
+		int d, i;
+
+		d = 1;
+		for(i=4; s[i]; i++){
+			if(s[i] == '.'){
+				if(s[i-1] == '.')
+					return 0;
+				d++;
+			} else if(!alnumchar(s[i]))
+				break;
+		}
+		if(d >= 2)
+			return smprint("http://%s", s);
+	}
+	return 0;
+}
+
 /*
  * remove entity references, in place.
  * Potential bug:
@@ -1041,7 +1069,13 @@
 		}
 		break;
 	case TEXT:
-		pl_htmloutput(&g, g.nsp, g.token, 0);
+		if(g.state->link[0]==0 && (str = linkify(g.token))){
+			strncpy(g.state->link, str, sizeof(g.state->link));
+			pl_htmloutput(&g, g.nsp, g.token, 0);
+			g.state->link[0] = 0;
+			free(str);
+		} else
+			pl_htmloutput(&g, g.nsp, g.token, 0);
 		break;
 	case EOF:
 		for(;g.state!=g.stack;--g.state)