war on BSD style curly braces
[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                         StrBufSipLine(Line, Source, &BufPtr);
64                         bq = 0;
65                         i = 0;
66                         ptr = ChrPtr(Line);
67                         len = StrLength(Line);
68                         pte = ptr + len;
69
70                         if ((intext == 1) && (isspace(*ptr))) {
71                                 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
72                         }
73                         intext = 1;
74                         if (isspace(*ptr)) {
75                                 while ((ptr < pte) && ((*ptr == '>') || isspace(*ptr))) {
76                                         if (*ptr == '>') {
77                                                 bq++;
78                                         }
79                                         ptr++;
80                                         i++;
81                                 }
82                         }
83
84                         /*
85                          * Quoted text should be displayed in italics and in a
86                          * different colour.  This code understands Citadel-style
87                          * " >" quotes and will convert to <BLOCKQUOTE> tags.
88                          */
89                         if (i > 0)
90                                 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) && (BufPtr != NULL));
109
110         for (i = 0; i < bn; i++) {
111                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
112         }
113         StrBufAppendBufPlain(Target, HKEY("<br>\n"), 0);
114         FreeStrBuf(&Line);
115         FreeStrBuf(&Line1);
116         FreeStrBuf(&Line2);
117         return (Target);
118 }