bffb90e8811c50461017760fdf4c892cd53ab395
[citadel.git] / webcit-ng / text2html.c
1 /*
2  * Convert text/plain to text/html
3  *
4  * Copyright (c) 2017-2018 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16
17
18 /*
19  * Convert a text/plain message to text/html
20  */
21 StrBuf *text2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source)
22 {
23         StrBuf *sj = NULL;
24
25         sj = NewStrBuf();
26         if (!sj) {
27                 return(sj);
28         }
29
30         StrBufAppendPrintf(sj, "<pre>");
31         StrEscAppend(sj, Source, NULL, 0, 0);           // FIXME - add code here to activate links
32         StrBufAppendPrintf(sj, "</pre>\n");
33
34         return(sj);
35 }
36
37
38 /*
39  * Convert a text/x-citadel-variformat message to text/html
40  */
41 StrBuf *variformat2html(StrBuf *Source)
42 {
43         StrBuf *Target = NULL;
44
45         Target = NewStrBuf();
46         if (!Target) {
47                 return(Target);
48         }
49
50         const char *ptr, *pte;
51         const char *BufPtr = NULL;
52         StrBuf *Line = NewStrBufPlain(NULL, SIZ);
53         StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
54         StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
55         int bn = 0;
56         int bq = 0;
57         int i;
58         long len;
59         int intext = 0;
60
61         if (StrLength(Source) > 0) 
62                 do 
63                 {
64                         StrBufSipLine(Line, Source, &BufPtr);
65                         bq = 0;
66                         i = 0;
67                         ptr = ChrPtr(Line);
68                         len = StrLength(Line);
69                         pte = ptr + len;
70
71                         if ((intext == 1) && (isspace(*ptr))) {
72                                 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
73                         }
74                         intext = 1;
75                         if (isspace(*ptr)) {
76                                 while ((ptr < pte) && ((*ptr == '>') || isspace(*ptr))) {
77                                         if (*ptr == '>') {
78                                                 bq++;
79                                         }
80                                         ptr ++;
81                                         i++;
82                                 }
83                         }
84
85                         /*
86                          * Quoted text should be displayed in italics and in a
87                          * different colour.  This code understands Citadel-style
88                          * " >" quotes and will convert to <BLOCKQUOTE> tags.
89                          */
90                         if (i > 0) StrBufCutLeft(Line, i);
91
92                         for (i = bn; i < bq; i++)                               
93                                 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
94                         for (i = bq; i < bn; i++)                               
95                                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
96                         bn = bq;
97
98                         if (StrLength(Line) == 0)
99                                 continue;
100
101                         /* Activate embedded URL's */
102                         UrlizeText(Line1, Line, Line2);
103
104                         StrEscAppend(Target, Line1, NULL, 0, 0);
105
106                         StrBufAppendBufPlain(Target, HKEY("\n"), 0);
107                 }
108                 while ((BufPtr != StrBufNOTNULL) &&
109                        (BufPtr != NULL));
110
111         for (i = 0; i < bn; i++) {
112                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
113         }
114         StrBufAppendBufPlain(Target, HKEY("<br>\n"), 0);
115         FreeStrBuf(&Line);
116         FreeStrBuf(&Line1);
117         FreeStrBuf(&Line2);
118         return(Target);
119 }
120
121