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