summaryrefslogtreecommitdiff
path: root/render/textplain.c
blob: 4b08e0acc65c1f5d47f2ba1c4897048ac559b9c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 */

#include "libxml/HTMLparser.h"
#include "netsurf/content/content.h"
#include "netsurf/render/html.h"
#include "netsurf/render/textplain.h"
#include "netsurf/utils/messages.h"


static const char header[] = "<html><body><pre>";
static const char footer[] = "</pre></body></html>";


bool textplain_create(struct content *c, const char *params[])
{
	if (!html_create(c, params))
		/* html_create() must have broadcast MSG_ERROR already, so we
		 * don't need to. */
		return false;
	htmlParseChunk(c->data.html.parser, header, sizeof(header) - 1, 0);
	return true;
}

bool textplain_process_data(struct content *c, char *data,
		unsigned int size)
{
	unsigned int i, s;
	char *d, *p;
	union content_msg_data msg_data;
	bool ret;

	/* count number of '<' in data buffer */
	for (d = data, i = 0, s = 0; i != size; i++, d++) {
		if (*d == '<')
			s++;
	}

	/* create buffer for modified input */
	d = calloc(size + 3*s, sizeof(char));
	if (!d) {
		msg_data.error = messages_get("NoMemory");
		content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
		return false;
	}

	/* copy data across to modified buffer,
	 * replacing occurrences of '<' with '&lt;'
	 * This prevents the parser stripping sequences of '<...>'
	 */
	for (p = d, i = 0, s = 0; i != size; i++, data++) {
		if (*data == '<') {
			*p++ = '&';
			*p++ = 'l';
			*p++ = 't';
			*p++ = ';';
			s += 4;
		}
		else {
			*p++ = *data;
			s++;
		}
	}

	ret = html_process_data(c, d, s);

	free(d);

	return ret;
}

bool textplain_convert(struct content *c, int width, int height)
{
	htmlParseChunk(c->data.html.parser, footer, sizeof(footer) - 1, 0);
	c->type = CONTENT_HTML;
	return html_convert(c, width, height);
}