2 * html.c -- Functions which handle translation between HTML and plain text
21 #include "sysdep_decls.h"
31 * Convert HTML to plain text.
33 * inputmsg = pointer to raw HTML message
34 * screenwidth = desired output screenwidth
35 * do_citaformat = set to 1 to indent newlines with spaces
37 char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
45 int i, j, ch, did_out, rb;
46 int nest = 0; /* Bracket nesting level */
52 outptr = mallok(strlen(inptr) + 256);
53 if (outptr == NULL) return NULL;
58 /* Fill the input buffer */
59 if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
63 inbuf[strlen(inbuf)+1] = 0;
64 inbuf[strlen(inbuf)] = ch;
73 if (strlen(inbuf)>0) {
75 /* Fold in all the spacing */
76 for (i=0; i<strlen(inbuf); ++i) {
77 if (inbuf[i]==10) inbuf[i]=32;
78 if (inbuf[i]==13) inbuf[i]=32;
79 if (inbuf[i]==9) inbuf[i]=32;
80 if ((inbuf[i]<32) || (inbuf[i]>126))
81 strcpy(&inbuf[i], &inbuf[i+1]);
82 while ((inbuf[i]==32)&&(inbuf[i+1]==32))
83 strcpy(&inbuf[i], &inbuf[i+1]);
86 for (i=0; i<strlen(inbuf); ++i) {
98 if (!strcasecmp(tag, "P")) {
99 strcat(outbuf, "\n\n");
102 if (!strcasecmp(tag, "/DIV")) {
103 strcat(outbuf, "\n\n");
106 else if (!strcasecmp(tag, "H1")) {
107 strcat(outbuf, "\n\n");
110 else if (!strcasecmp(tag, "H2")) {
111 strcat(outbuf, "\n\n");
114 else if (!strcasecmp(tag, "H3")) {
115 strcat(outbuf, "\n\n");
118 else if (!strcasecmp(tag, "H4")) {
119 strcat(outbuf, "\n\n");
122 else if (!strcasecmp(tag, "/H1")) {
123 strcat(outbuf, "\n");
126 else if (!strcasecmp(tag, "/H2")) {
127 strcat(outbuf, "\n");
130 else if (!strcasecmp(tag, "/H3")) {
131 strcat(outbuf, "\n");
134 else if (!strcasecmp(tag, "/H4")) {
135 strcat(outbuf, "\n");
138 else if (!strcasecmp(tag, "HR")) {
139 strcat(outbuf, "\n ");
140 for (j=0; j<screenwidth-2; ++j)
142 strcat(outbuf, "\n");
145 else if (!strcasecmp(tag, "BR")) {
146 strcat(outbuf, "\n");
149 else if (!strcasecmp(tag, "TR")) {
150 strcat(outbuf, "\n");
153 else if (!strcasecmp(tag, "/TABLE")) {
154 strcat(outbuf, "\n");
159 else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
160 tag[strlen(tag)+1] = 0;
161 tag[strlen(tag)] = ch;
165 outbuf[strlen(outbuf)+1] = 0;
166 outbuf[strlen(outbuf)] = ch;
169 strcpy(inbuf, &inbuf[i]);
172 /* Convert &; tags to the forbidden characters */
173 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
175 if (!strncasecmp(&outbuf[i], " ", 6)) {
177 strcpy(&outbuf[i+1], &outbuf[i+6]);
180 else if (!strncasecmp(&outbuf[i], "<", 4)) {
182 strcpy(&outbuf[i+1], &outbuf[i+4]);
185 else if (!strncasecmp(&outbuf[i], ">", 4)) {
187 strcpy(&outbuf[i+1], &outbuf[i+4]);
190 else if (!strncasecmp(&outbuf[i], "&", 5)) {
191 strcpy(&outbuf[i+1], &outbuf[i+5]);
194 else if (!strncasecmp(&outbuf[i], """, 6)) {
196 strcpy(&outbuf[i+1], &outbuf[i+6]);
199 else if (!strncasecmp(&outbuf[i], "©", 6)) {
203 strcpy(&outbuf[i+3], &outbuf[i+6]);
206 else if (!strncasecmp(&outbuf[i], "®", 5)) {
210 strcpy(&outbuf[i+3], &outbuf[i+5]);
215 /* Make sure the output buffer is big enough */
216 if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
218 outptr = realloc(outptr, outlen);
221 /* Output any lines terminated with hard line breaks */
224 if (strlen(outbuf)>0)
225 for (i = 0; i<strlen(outbuf); ++i) {
226 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
227 strncat(outptr, outbuf, i+1);
228 strcat(outptr, "\n");
231 strcpy(outbuf, &outbuf[i+1]);
238 /* Add soft line breaks */
239 if (strlen(outbuf) > (screenwidth - 2)) {
241 for (i=0; i<(screenwidth-2); ++i) {
242 if (outbuf[i]==32) rb = i;
245 strncat(outptr, outbuf, rb);
246 strcat(outptr, "\n");
249 strcpy(outbuf, &outbuf[rb+1]);
252 strncat(outptr, outbuf, screenwidth-2);
253 strcat(outptr, "\n");
256 strcpy(outbuf, &outbuf[screenwidth-2]);
260 } while (done_reading == 0);
262 strcat(outptr, outbuf);
263 strcat(outptr, "\n");