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