nip and tuck
[citadel.git] / webcit-ng / text2html.c
1 //
2 // Convert text/plain to text/html
3 //
4 // Copyright (c) 2017-2021 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 // Convert a text/plain message to text/html
20 StrBuf *text2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source) {
21         StrBuf *sj = NULL;
22
23         sj = NewStrBuf();
24         if (!sj) {
25                 return (sj);
26         }
27
28         StrBufAppendPrintf(sj, "<pre>");
29         StrEscAppend(sj, Source, NULL, 0, 0);   // FIXME - add code here to activate links
30         StrBufAppendPrintf(sj, "</pre>\n");
31
32         return (sj);
33 }
34
35
36 // Convert a text/x-citadel-variformat message to text/html
37 StrBuf *variformat2html(StrBuf * Source) {
38         StrBuf *Target = NULL;
39
40         Target = NewStrBuf();
41         if (!Target) {
42                 return (Target);
43         }
44
45         const char *ptr, *pte;
46         const char *BufPtr = NULL;
47         StrBuf *Line = NewStrBufPlain(NULL, SIZ);
48         StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
49         StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
50         int bn = 0;
51         int bq = 0;
52         int i;
53         long len;
54         int intext = 0;
55
56         if (StrLength(Source) > 0)
57                 do {
58                         StrBufSipLine(Line, Source, &BufPtr);
59                         bq = 0;
60                         i = 0;
61                         ptr = ChrPtr(Line);
62                         len = StrLength(Line);
63                         pte = ptr + len;
64
65                         if ((intext == 1) && (isspace(*ptr))) {
66                                 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
67                         }
68                         intext = 1;
69                         if (isspace(*ptr)) {
70                                 while ((ptr < pte) && ((*ptr == '>') || isspace(*ptr))) {
71                                         if (*ptr == '>') {
72                                                 bq++;
73                                         }
74                                         ptr++;
75                                         i++;
76                                 }
77                         }
78
79                         // Quoted text should be displayed in italics and in a
80                         // different colour.  This code understands Citadel-style
81                         // " >" quotes and will convert to <BLOCKQUOTE> tags.
82                         if (i > 0) {
83                                 StrBufCutLeft(Line, i);
84                         }
85                         for (i = bn; i < bq; i++) {
86                                 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
87                         }
88                         for (i = bq; i < bn; i++) {
89                                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
90                         }
91                         bn = bq;
92
93                         if (StrLength(Line) == 0)
94                                 continue;
95
96                         // Activate embedded URL's
97                         UrlizeText(Line1, Line, Line2);
98                         StrEscAppend(Target, Line1, NULL, 0, 0);
99                         StrBufAppendBufPlain(Target, HKEY("\n"), 0);
100                 }
101                 while ((BufPtr != StrBufNOTNULL) && (BufPtr != NULL));
102
103         for (i = 0; i < bn; i++) {
104                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
105         }
106         StrBufAppendBufPlain(Target, HKEY("<br>\n"), 0);
107         FreeStrBuf(&Line);
108         FreeStrBuf(&Line1);
109         FreeStrBuf(&Line2);
110         return(Target);
111 }