summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--html.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/html.c b/html.c
index 2d987f8..ae4c76f 100644
--- a/html.c
+++ b/html.c
@@ -14,10 +14,13 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <assert.h>
 #include <err.h>
 #include <inttypes.h>
+#include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sysexits.h>
 #include <time.h>
 
@@ -389,9 +392,39 @@ htmlEvent(struct khtmlreq *html, struct Scope scope, struct Event event) {
 		|| khtml_closeelem(html, 1);
 }
 
+enum kcgi_err linkify(struct khtmlreq *html, const char *str, size_t len) {
+	static const char *Pattern = "https?://([^[:space:]>\"()]|[(][^)]*[)])+";
+	static regex_t regex;
+	if (!regex.re_nsub) {
+		int error = regcomp(&regex, Pattern, REG_EXTENDED);
+		assert(!error);
+	}
+
+	regmatch_t match = {0};
+	while (!regexec(&regex, str, 1, &match, 0)) {
+		if ((size_t)match.rm_eo > len) break;
+
+		char *href = strndup(&str[match.rm_so], match.rm_eo - match.rm_so);
+		if (!href) err(EX_OSERR, "strndup");
+
+		enum kcgi_err error = 0
+			|| khtml_write(str, match.rm_so, html)
+			|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
+			|| khtml_write(&str[match.rm_so], match.rm_eo - match.rm_so, html)
+			|| khtml_closeelem(html, 1);
+		free(href);
+		if (error) return error;
+
+		str += match.rm_eo;
+		len -= match.rm_eo;
+	}
+	if (len) return khtml_write(str, len, html);
+	return KCGI_OK;
+}
+
 enum kcgi_err htmlIRC(struct khtmlreq *html, const char *str) {
 	return 0
 		|| khtml_attr(html, KELEM_SPAN, KATTR_CLASS, "irc", KATTR__MAX)
-		|| khtml_puts(html, str)
+		|| linkify(html, str, strlen(str))
 		|| khtml_closeelem(html, 1);
 }
gheader'>2024-09-19Add photos from September 5June McEnroe Had to prefix the folder number onto these file names manually because they must have come out of a different scanner or something. 2024-09-15Add some more film stocks to the listJune McEnroe 2024-09-13Add photos from September 2June McEnroe 2024-09-13Add Fomapan 200 to films listJune McEnroe 2024-09-10Add August 29 picnic photosJune McEnroe 2024-09-08Apply some bold to trips renderingJune McEnroe This seems easier to visually scan. The only other thing I'd like is a nicer date rendering but JavaScript is useless for that. 2024-09-08Render trips hopefully more efficientlyJune McEnroe 2024-09-08Allow removing bodies and lensesJune McEnroe 2024-09-08Limit body width so it looks less silly on desktopJune McEnroe 2024-09-07Handle no film being loadedJune McEnroe 2024-09-07Fancy up the text a littleJune McEnroe