]> code.citadel.org Git - citadel.git/blobdiff - webcit/html2html.c
* Fixes to wiki view to handle "short" links.
[citadel.git] / webcit / html2html.c
index 32f76777f561bae82db7431c205951a2d25efe53..6992c406272c77e90c4811a2174e48462c61503c 100644 (file)
@@ -1,21 +1,23 @@
 /*
  * $Id$
- *
- * Output an HTML message, modifying it slightly to make sure it plays nice
+ */
+/**
+ * \defgroup HTML2HTML Output an HTML message, modifying it slightly to make sure it plays nice
  * with the rest of our web framework.
  *
  */
-
+/*@{*/
 #include "webcit.h"
 #include "vcard.h"
 #include "webserver.h"
 
 
-/*
- * Sanitize and enhance an HTML message for display.
+/**
+ * \brief Sanitize and enhance an HTML message for display.
  * Also convert weird character sets to UTF-8 if necessary.
+ * \param charset the input charset
  */
-void output_html(char *charset) {
+void output_html(char *charset, int treat_as_wiki) {
        char buf[SIZ];
        char *msg;
        char *ptr;
@@ -33,26 +35,26 @@ void output_html(char *charset) {
        int linklen;
 #ifdef HAVE_ICONV
        iconv_t ic = (iconv_t)(-1) ;
-       char *ibuf;                   /* Buffer of characters to be converted */
-       char *obuf;                   /* Buffer for converted characters      */
-       size_t ibuflen;               /* Length of input buffer               */
-       size_t obuflen;               /* Length of output buffer              */
-       char *osav;                   /* Saved pointer to output buffer       */
+       char *ibuf;                   /**< Buffer of characters to be converted */
+       char *obuf;                   /**< Buffer for converted characters      */
+       size_t ibuflen;               /**< Length of input buffer               */
+       size_t obuflen;               /**< Length of output buffer              */
+       char *osav;                   /**< Saved pointer to output buffer       */
 #endif
 
        msg = strdup("");
-       sprintf(new_window, "<A TARGET=\"%s\" HREF=", TARGET);
+       sprintf(new_window, "<a target=\"%s\" href=", TARGET);
 
        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                line_length = strlen(buf);
                buffer_length = content_length + line_length + 2;
                msg = realloc(msg, buffer_length);
                if (msg == NULL) {
-                       wprintf("<B>");
+                       wprintf("<b>");
                        wprintf(_("realloc() error! couldn't get %d bytes: %s"),
                                buffer_length + 1,
                                strerror(errno));
-                       wprintf("</B><br /><br />\n");
+                       wprintf("</b><br /><br />\n");
                        return;
                }
                strcpy(&msg[content_length], buf);
@@ -63,10 +65,13 @@ void output_html(char *charset) {
 
 #ifdef HAVE_ICONV
        if ( (strcasecmp(charset, "us-ascii"))
-          && (strcasecmp(charset, "UTF-8")) ) {
+          && (strcasecmp(charset, "UTF-8"))
+          && (strcasecmp(charset, ""))
+       ) {
                ic = iconv_open("UTF-8", charset);
                if (ic == (iconv_t)(-1) ) {
-                       lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
+                       lprintf(5, "%s:%d iconv_open() failed: %s\n",
+                               __FILE__, __LINE__, strerror(errno));
                }
        }
        if (ic != (iconv_t)(-1) ) {
@@ -90,13 +95,14 @@ void output_html(char *charset) {
 
        while (ptr < msgend) {
 
-               /* Advance to next tag */
+               /** Advance to next tag */
                ptr = strchr(ptr, '<');
                if ((ptr == NULL) || (ptr >= msgend)) break;
                ++ptr;
                if ((ptr == NULL) || (ptr >= msgend)) break;
 
-               /* Any of these tags cause everything up to and including
+               /**
+                * Any of these tags cause everything up to and including
                 * the tag to be removed.
                 */     
                if ( (!strncasecmp(ptr, "HTML", 4))
@@ -110,7 +116,8 @@ void output_html(char *charset) {
                        msgstart = ptr;
                }
 
-               /* Any of these tags cause everything including and following
+               /**
+                * Any of these tags cause everything including and following
                 * the tag to be removed.
                 */
                if ( (!strncasecmp(ptr, "/HTML", 5))
@@ -128,37 +135,57 @@ void output_html(char *charset) {
        strcpy(converted_msg, "");
        ptr = msgstart;
        while (ptr < msgend) {
-               /* Change mailto: links to WebCit mail, by replacing the
+               /**
+                * Change mailto: links to WebCit mail, by replacing the
                 * link with one that points back to our mail room.  Due to
                 * the way we parse URL's, it'll even handle mailto: links
                 * that have "?subject=" in them.
                 */
-               if (!strncasecmp(ptr, "<A HREF=\"mailto:", 16)) {
+               if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
                        content_length += 64;
                        converted_msg = realloc(converted_msg, content_length);
                        sprintf(&converted_msg[output_length],
-                               "<A HREF=\"/display_enter"
+                               "<a href=\"display_enter"
                                "?force_room=_MAIL_&recp=");
                        output_length += 47;
                        ptr = &ptr[16];
                        ++alevel;
                }
-               /* Make links open in a separate window */
-               else if (!strncasecmp(ptr, "<A HREF=", 8)) {
-                       content_length += 64;
-                       converted_msg = realloc(converted_msg, content_length);
-                       sprintf(&converted_msg[output_length], new_window);
-                       output_length += strlen(new_window);
-                       ptr = &ptr[8];
+               /** Make external links open in a separate window */
+               else if (!strncasecmp(ptr, "<a href=", 8)) {
                        ++alevel;
+                       if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
+                            &&  ((strchr(ptr, '/') < strchr(ptr, '>'))) 
+                            ) {
+                               /* open external links to new window */
+                               content_length += 64;
+                               converted_msg = realloc(converted_msg, content_length);
+                               sprintf(&converted_msg[output_length], new_window);
+                               output_length += strlen(new_window);
+                               ptr = &ptr[8];
+                       }
+                       else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
+                               lprintf(9, "converting wiki link\n");
+                               content_length += 64;
+                               converted_msg = realloc(converted_msg, content_length);
+                               sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
+                               output_length += 19;
+                               ptr = &ptr[9];
+                       }
+                       else {
+                               sprintf(&converted_msg[output_length], "<a href=");
+                               output_length += 8;
+                               ptr = &ptr[8];
+                       }
                }
-               /* Turn anything that looks like a URL into a real link, as long
+               /**
+                * Turn anything that looks like a URL into a real link, as long
                 * as it's not inside a tag already
                 */
                else if ( (brak == 0) && (alevel == 0)
                     && (!strncasecmp(ptr, "http://", 7))) {
                                linklen = 0;
-                               /* Find the end of the link */
+                               /** Find the end of the link */
                                for (i=0; i<=strlen(ptr); ++i) {
                                        if ((ptr[i]==0)
                                           ||(isspace(ptr[i]))
@@ -195,7 +222,7 @@ void output_html(char *charset) {
                                }
                }
                else {
-                       /*
+                       /**
                         * We need to know when we're inside a tag,
                         * so we don't turn things that look like URL's into
                         * links, when they're already links - or image sources.
@@ -208,14 +235,15 @@ void output_html(char *charset) {
                }
        }
 
-       /* Output our big pile of markup */
+       /** Output our big pile of markup */
        client_write(converted_msg, output_length);
 
-       /* A little trailing vertical whitespace... */
+       /** A little trailing vertical whitespace... */
        wprintf("<br /><br />\n");
 
-       /* Now give back the memory */
+       /** Now give back the memory */
        free(converted_msg);
        free(msg);
 }
 
+/*@}*/