comparison libpurple/util.c @ 17895:1b74553c5c08

When converting HTML to XHTML and plain text on message send, links in the HTML are converted to 'text' <'url'> in plain text instead of just 'text'.
author Andreas Monitzer <pidgin@monitzer.com>
date Sun, 08 Jul 2007 20:43:53 +0000
parents d8102e923bd1
children ca20c706ee50
comparison
equal deleted inserted replaced
17894:bbd92ee894f2 17895:1b74553c5c08
1294 purple_markup_html_to_xhtml(const char *html, char **xhtml_out, 1294 purple_markup_html_to_xhtml(const char *html, char **xhtml_out,
1295 char **plain_out) 1295 char **plain_out)
1296 { 1296 {
1297 GString *xhtml = g_string_new(""); 1297 GString *xhtml = g_string_new("");
1298 GString *plain = g_string_new(""); 1298 GString *plain = g_string_new("");
1299 GString *url = NULL;
1299 GList *tags = NULL, *tag; 1300 GList *tags = NULL, *tag;
1300 const char *c = html; 1301 const char *c = html;
1301 1302
1302 while(c && *c) { 1303 while(c && *c) {
1303 if(*c == '<') { 1304 if(*c == '<') {
1313 } 1314 }
1314 if(tag) { 1315 if(tag) {
1315 while(tags) { 1316 while(tags) {
1316 struct purple_parse_tag *pt = tags->data; 1317 struct purple_parse_tag *pt = tags->data;
1317 g_string_append_printf(xhtml, "</%s>", pt->dest_tag); 1318 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
1319 if(!strcmp(pt->src_tag, "a")) {
1320 /* if this is a link, we have to add the url to the plaintext, too */
1321 g_string_append_printf(plain, " <%s>", g_strstrip(url->str));
1322 }
1318 if(tags == tag) 1323 if(tags == tag)
1319 break; 1324 break;
1320 tags = g_list_remove(tags, pt); 1325 tags = g_list_remove(tags, pt);
1321 g_free(pt); 1326 g_free(pt);
1322 } 1327 }
1336 plain = g_string_append_c(plain, '<'); 1341 plain = g_string_append_c(plain, '<');
1337 c++; 1342 c++;
1338 } 1343 }
1339 } 1344 }
1340 } else { /* opening tag */ 1345 } else { /* opening tag */
1341 ALLOW_TAG("a"); 1346 /*ALLOW_TAG("a");*/
1342 ALLOW_TAG("blockquote"); 1347 ALLOW_TAG("blockquote");
1343 ALLOW_TAG("cite"); 1348 ALLOW_TAG("cite");
1344 ALLOW_TAG("div"); 1349 ALLOW_TAG("div");
1345 ALLOW_TAG("em"); 1350 ALLOW_TAG("em");
1346 ALLOW_TAG("h1"); 1351 ALLOW_TAG("h1");
1419 pt->src_tag = "sup"; 1424 pt->src_tag = "sup";
1420 pt->dest_tag = "span"; 1425 pt->dest_tag = "span";
1421 tags = g_list_prepend(tags, pt); 1426 tags = g_list_prepend(tags, pt);
1422 c = strchr(c, '>') + 1; 1427 c = strchr(c, '>') + 1;
1423 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>"); 1428 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>");
1429 continue;
1430 }
1431 if(!g_ascii_strncasecmp(c, "<a", 2) && (*(c+2) == '>' || *(c+2) == ' ')) {
1432 const char *p = c;
1433 struct purple_parse_tag *pt;
1434 while(*p && *p != '>') {
1435 if(!g_ascii_strncasecmp(p, "href=", strlen("href="))) {
1436 const char *q = p + strlen("href=");
1437 g_string_free(url, TRUE);
1438 url = g_string_new("");
1439 if(*q == '\'' || *q == '\"')
1440 q++;
1441 while(*q && *q != '\"' && *q != '\'' && *q != ' ') {
1442 url = g_string_append_c(url, *q);
1443 q++;
1444 }
1445 p = q;
1446 }
1447 p++;
1448 }
1449 if ((c = strchr(c, '>')) != NULL)
1450 c++;
1451 else
1452 c = p;
1453 pt = g_new0(struct purple_parse_tag, 1);
1454 pt->src_tag = "a";
1455 pt->dest_tag = "a";
1456 tags = g_list_prepend(tags, pt);
1457 g_string_append_printf(xhtml, "<a href='%s'>", g_strstrip(url->str));
1424 continue; 1458 continue;
1425 } 1459 }
1426 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) { 1460 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) {
1427 const char *p = c; 1461 const char *p = c;
1428 GString *style = g_string_new(""); 1462 GString *style = g_string_new("");
1596 *xhtml_out = g_strdup(xhtml->str); 1630 *xhtml_out = g_strdup(xhtml->str);
1597 if(plain_out) 1631 if(plain_out)
1598 *plain_out = g_strdup(plain->str); 1632 *plain_out = g_strdup(plain->str);
1599 g_string_free(xhtml, TRUE); 1633 g_string_free(xhtml, TRUE);
1600 g_string_free(plain, TRUE); 1634 g_string_free(plain, TRUE);
1635 g_string_free(url, TRUE);
1601 } 1636 }
1602 1637
1603 /* The following are probably reasonable changes: 1638 /* The following are probably reasonable changes:
1604 * - \n should be converted to a normal space 1639 * - \n should be converted to a normal space
1605 * - in addition to <br>, <p> and <div> etc. should also be converted into \n 1640 * - in addition to <br>, <p> and <div> etc. should also be converted into \n