moved whitespace around
[citadel.git] / webcit-ng / server / text2html.c
1 // Convert text/plain to text/html
2 //
3 // Copyright (c) 2017-2022 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
6
7 #include "webcit.h"
8
9
10 // Convert a text/plain message to text/html
11 StrBuf *text2html(const char *charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source) {
12         StrBuf *sj = NULL;
13
14         sj = NewStrBuf();
15         if (!sj) {
16                 return (sj);
17         }
18
19         StrBufAppendPrintf(sj, "<pre>");
20         StrEscAppend(sj, Source, NULL, 0, 0);   // FIXME - add code here to activate links
21         StrBufAppendPrintf(sj, "</pre>\n");
22
23         return (sj);
24 }
25
26
27 // Convert a text/x-citadel-variformat message to text/html
28 StrBuf *variformat2html(StrBuf * Source) {
29         StrBuf *Target = NULL;
30
31         Target = NewStrBuf();
32         if (!Target) {
33                 return (Target);
34         }
35
36         const char *ptr, *pte;
37         const char *BufPtr = NULL;
38         StrBuf *Line = NewStrBufPlain(NULL, SIZ);
39         StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
40         StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
41         int bn = 0;
42         int bq = 0;
43         int i;
44         long len;
45         int intext = 0;
46
47         if (StrLength(Source) > 0)
48                 do {
49                         StrBufSipLine(Line, Source, &BufPtr);
50                         bq = 0;
51                         i = 0;
52                         ptr = ChrPtr(Line);
53                         len = StrLength(Line);
54                         pte = ptr + len;
55
56                         if ((intext == 1) && (isspace(*ptr))) {
57                                 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
58                         }
59                         intext = 1;
60                         if (isspace(*ptr)) {
61                                 while ((ptr < pte) && ((*ptr == '>') || isspace(*ptr))) {
62                                         if (*ptr == '>') {
63                                                 bq++;
64                                         }
65                                         ptr++;
66                                         i++;
67                                 }
68                         }
69
70                         // Quoted text should be displayed in italics and in a
71                         // different colour.  This code understands Citadel-style
72                         // " >" quotes and will convert to <BLOCKQUOTE> tags.
73                         if (i > 0) {
74                                 StrBufCutLeft(Line, i);
75                         }
76                         for (i = bn; i < bq; i++) {
77                                 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
78                         }
79                         for (i = bq; i < bn; i++) {
80                                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
81                         }
82                         bn = bq;
83
84                         if (StrLength(Line) == 0)
85                                 continue;
86
87                         // Activate embedded URL's
88                         UrlizeText(Line1, Line, Line2);
89                         StrEscAppend(Target, Line1, NULL, 0, 0);
90                         StrBufAppendBufPlain(Target, HKEY("\n"), 0);
91                 }
92                 while ((BufPtr != StrBufNOTNULL) && (BufPtr != NULL));
93
94         for (i = 0; i < bn; i++) {
95                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
96         }
97         StrBufAppendBufPlain(Target, HKEY("<br>\n"), 0);
98         FreeStrBuf(&Line);
99         FreeStrBuf(&Line1);
100         FreeStrBuf(&Line2);
101         return(Target);
102 }