9f85e12719478a408f87489b2c7bf59a8c0cb310
[citadel] / 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.  It runs great on the
7 // Linux operating system (and probably elsewhere).  You can use,
8 // copy, and run it under the terms of the GNU General Public
9 // License version 3.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 #include "webcit.h"
17
18
19 /*
20  * Convert a text/plain message to text/html
21  */
22 StrBuf *text2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf * Source)
23 {
24         StrBuf *sj = NULL;
25
26         sj = NewStrBuf();
27         if (!sj) {
28                 return (sj);
29         }
30
31         StrBufAppendPrintf(sj, "<pre>");
32         StrEscAppend(sj, Source, NULL, 0, 0);   // FIXME - add code here to activate links
33         StrBufAppendPrintf(sj, "</pre>\n");
34
35         return (sj);
36 }
37
38
39 /*
40  * Convert a text/x-citadel-variformat message to text/html
41  */
42 StrBuf *variformat2html(StrBuf * Source)
43 {
44         StrBuf *Target = NULL;
45
46         Target = NewStrBuf();
47         if (!Target) {
48                 return (Target);
49         }
50
51         const char *ptr, *pte;
52         const char *BufPtr = NULL;
53         StrBuf *Line = NewStrBufPlain(NULL, SIZ);
54         StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
55         StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
56         int bn = 0;
57         int bq = 0;
58         int i;
59         long len;
60         int intext = 0;
61
62         if (StrLength(Source) > 0)
63                 do {
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)
91                                 StrBufCutLeft(Line, i);
92
93                         for (i = bn; i < bq; i++)
94                                 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
95                         for (i = bq; i < bn; i++)
96                                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
97                         bn = bq;
98
99                         if (StrLength(Line) == 0)
100                                 continue;
101
102                         /* Activate embedded URL's */
103                         UrlizeText(Line1, Line, Line2);
104
105                         StrEscAppend(Target, Line1, NULL, 0, 0);
106
107                         StrBufAppendBufPlain(Target, HKEY("\n"), 0);
108                 }
109                 while ((BufPtr != StrBufNOTNULL) && (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 }