From 993ea811e51e7aef001ac8e1c038272321da1490 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sat, 14 Jul 2012 21:51:28 -0300 Subject: [PATCH] genkicat/: detect URLs in comments and add PDF hyperlink We recognize http, https, ftp, and file. --- genkicat/pdf.c | 17 ++++++++++++++++- genkicat/tree.c | 24 ++++++++++++++++++++---- genkicat/tree.h | 1 + 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/genkicat/pdf.c b/genkicat/pdf.c index 1a1c024..3eedfd7 100644 --- a/genkicat/pdf.c +++ b/genkicat/pdf.c @@ -254,10 +254,25 @@ static void print_comment(FILE *file, const struct line *comment) n = 0; for (line = comment; line; line = line->next) { n++; - fprintf(file, "%d -%d moveto\n", format->left, + fprintf(file, "newpath %d -%d moveto\n", format->left, format->comment.y+(n-lines)*format->comment_line_skip); + if (line->url) { + fprintf(file, "currentpoint\n"); + fprintf(file, "[ /Rect [ "); + ps_string(file, line->s); + fprintf(file, + " false charpath flattenpath pathbbox ]\n"); + fprintf(file, " /Subtype /Link\n"); + fprintf(file, " /Border [ 0 0 0 ]\n"); + fprintf(file, " /Action << /Subtype /URI /URI "); + ps_string(file, line->s); + fprintf(file, " >>\n"); + fprintf(file, " /ANN pdfmark\n"); + fprintf(file, " moveto\n"); + } ps_string(file, line->s); fprintf(file, " show\n"); + } fprintf(file, "grestore\n"); } diff --git a/genkicat/tree.c b/genkicat/tree.c index 6b5da60..6ab58d8 100644 --- a/genkicat/tree.c +++ b/genkicat/tree.c @@ -118,10 +118,24 @@ static struct node *find_comp(struct node *node, const char *name) static struct line *new_line(char *s) { + const char *uris[] = { + "http://", + "https://", + "ftp://", + "file:/", /* one slash, for RFC3986 file:/localpath */ + NULL + }, **uri = uris; struct line *line; line = alloc_type(struct line); line->s = stralloc(s); + /* + * Require a slash at all to avoid misdetection. Require only one slash + * for compatibility with RFC3986. + */ + while (*uri && strncmp(s, *uri, strlen(*uri))) + uri++; + line->url = !!*uri; line->next = NULL; return line; } @@ -130,17 +144,19 @@ static struct line *new_line(char *s) static void append_line(struct line *line, char *s) { int len1, len2; + int spc = !line->url; len1 = strlen(line->s); len2 = strlen(s); - line->s = realloc(line->s, len1+len2+1+1); /* separating space */ + line->s = realloc(line->s, len1+len2+spc+1); if (!line->s) { perror("realloc"); exit(1); } - line->s[len1] = ' '; - memcpy(line->s+len1+1, s, len2); - line->s[len1+len2+1] = 0; + if (spc) + line->s[len1] = ' '; + memcpy(line->s+len1+spc, s, len2); + line->s[len1+len2+spc] = 0; } diff --git a/genkicat/tree.h b/genkicat/tree.h index dddfa57..ab36f1d 100644 --- a/genkicat/tree.h +++ b/genkicat/tree.h @@ -17,6 +17,7 @@ struct line { char *s; + int url; /* 1 if URL */ struct line *next; };