summaryrefslogtreecommitdiff
path: root/desktop/save_text.c
blob: e63c96eb97eb9e873fcce217b4ae92f5b37cc6a3 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk>
 * Copyright 2008 Michael Drake <tlsa@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
  * Text export of HTML (implementation).
  */

#include <assert.h>
#include <stdbool.h>
#include <string.h>

#include <dom/dom.h>

#include "utils/config.h"
#include "utils/log.h"
#include "utils/utf8.h"
#include "utils/utils.h"
#include "netsurf/content.h"
#include "render/box.h"
#include "render/html.h"

#include "netsurf/utf8.h"
#include "desktop/gui_internal.h"
#include "desktop/save_text.h"

static void extract_text(struct box *box, bool *first,
		save_text_whitespace *before, struct save_text_state *save);
static bool save_text_add_to_buffer(const char *text, size_t length,
		struct box *box, const char *whitespace_text,
		size_t whitespace_length, struct save_text_state *save);


/**
 * Extract the text from an HTML content and save it as a text file. Text is
 * converted to the local encoding.
 *
 * \param  c		An HTML content.
 * \param  path		Path to save text file too.
 */

void save_as_text(struct hlcache_handle *c, char *path)
{
	FILE *out;
	struct save_text_state save = { NULL, 0, 0 };
	save_text_whitespace before = WHITESPACE_NONE;
	bool first = true;
	nserror ret;
	char *result;

	if (!c || content_get_type(c) != CONTENT_HTML) {
		return;
	}

	extract_text(html_get_box_tree(c), &first, &before, &save);
	if (!save.block)
		return;

	ret = guit->utf8->utf8_to_local(save.block, save.length, &result);
	free(save.block);

	if (ret != NSERROR_OK) {
		LOG("failed to convert to local encoding, return %d", ret);
		return;
	}

	out = fopen(path, "w");
	if (out) {
		int res = fputs(result, out);

		if (res < 0) {
			LOG("Warning: write failed");
		}

		res = fputs("\n", out);
		if (res < 0) {
			LOG("Warning: failed writing trailing newline");
		}

		fclose(out);
	}

	free(result);
}


/**
 * Decide what whitespace to place before the next bit of content-related text
 * that is saved. Any existing whitespace is overridden if the whitespace for
 * this box is more "significant".
 *
 * \param  box		Pointer to box.
 * \param  first	Whether this is before the first bit of content-related
 *			text to be saved.
 * \param  before	Type of whitespace currently intended to be placed
 *			before the next bit of content-related text to be saved.
 *			Updated if this box is worthy of more significant
 *			whitespace.
 * \param  whitespace_text    Whitespace to place before next bit of
 *			      content-related text to be saved.
 *			      Updated if this box is worthy of more significant
 *			      whitespace.
 * \param  whitespace_length  Length of whitespace_text.
 *			      Updated if this box is worthy of more significant
 *			      whitespace.
 */

void save_text_solve_whitespace(struct box *box, bool *first,
		save_text_whitespace *before, const char **whitespace_text,
		size_t *whitespace_length)
{
	/* work out what whitespace should be placed before the next bit of
	 * text */
	if (*before < WHITESPACE_TWO_NEW_LINES &&
			/* significant box type */
			(box->type == BOX_BLOCK ||
			 box->type == BOX_TABLE ||
			 box->type == BOX_FLOAT_LEFT ||
			 box->type == BOX_FLOAT_RIGHT) &&
			/* and not a list element */
			!box->list_marker &&
			/* and not a marker... */
			(!(box->parent && box->parent->list_marker == box) ||
			 /* ...unless marker follows WHITESPACE_TAB */
			 ((box->parent && box->parent->list_marker == box) &&
			  *before == WHITESPACE_TAB))) {
		*before = WHITESPACE_TWO_NEW_LINES;
	} else if (*before <= WHITESPACE_ONE_NEW_LINE &&
			(box->type == BOX_TABLE_ROW ||
			 box->type == BOX_BR ||
			 (box->type != BOX_INLINE &&
			 (box->parent && box->parent->list_marker == box)) ||
			 (box->parent && box->parent->style &&
			  (css_computed_white_space(box->parent->style) ==
			   CSS_WHITE_SPACE_PRE ||
			   css_computed_white_space(box->parent->style) ==
			   CSS_WHITE_SPACE_PRE_WRAP) &&
			  box->type == BOX_INLINE_CONTAINER))) {
		if (*before == WHITESPACE_ONE_NEW_LINE)
			*before = WHITESPACE_TWO_NEW_LINES;
		else
			*before = WHITESPACE_ONE_NEW_LINE;
	}
	else if (*before < WHITESPACE_TAB &&
			(box->type == BOX_TABLE_CELL ||
			 box->list_marker)) {
		*before = WHITESPACE_TAB;
	}

	if (*first) {
		/* before the first bit of text to be saved; there is
		 * no preceding whitespace */
		*whitespace_text = "";
		*whitespace_length = 0;
	} else {
		/* set the whitespace that has been decided on */
		switch (*before) {
			case WHITESPACE_TWO_NEW_LINES:
				*whitespace_text = "\n\n";
				*whitespace_length = 2;
				break;
			case WHITESPACE_ONE_NEW_LINE:
				*whitespace_text = "\n";
				*whitespace_length = 1;
				break;
			case WHITESPACE_TAB:
				*whitespace_text = "\t";
				*whitespace_length = 1;
				break;
			case WHITESPACE_NONE:
				*whitespace_text = "";
				*whitespace_length = 0;
				break;
			default:
				*whitespace_text = "";
				*whitespace_length = 0;
				break;
		}
	}
}


/**
 * Traverse though the box tree and add all text to a save buffer.
 *
 * \param  box		Pointer to box.
 * \param  first	Whether this is before the first bit of content-related
 *			text to be saved.
 * \param  before	Type of whitespace currently intended to be placed
 *			before the next bit of content-related text to be saved.
 *			Updated if this box is worthy of more significant
 *			whitespace.
 * \param  save		our save_text_state workspace pointer
 * \return true iff the file writing succeeded and traversal should continue.
 */

void extract_text(struct box *box, bool *first, save_text_whitespace *before,
		struct save_text_state *save)
{
	struct box *child;
	const char *whitespace_text = "";
	size_t whitespace_length = 0;

	assert(box);

	/* If box has a list marker */
	if (box->list_marker) {
		/* do the marker box before continuing with the rest of the
		 * list element */
		extract_text(box->list_marker, first, before, save);
	}

	/* read before calling the handler in case it modifies the tree */
	child = box->children;

	save_text_solve_whitespace(box, first, before, &whitespace_text,
			&whitespace_length);

	if (box->type != BOX_BR && !((box->type == BOX_FLOAT_LEFT ||
			box->type == BOX_FLOAT_RIGHT) && !box->text) &&
			box->length > 0 && box->text) {
		/* Box meets criteria for export; add text to buffer */
		save_text_add_to_buffer(box->text, box->length, box,
				whitespace_text, whitespace_length, save);
		*first = false;
		*before = WHITESPACE_NONE;
	}

	/* Work though the children of this box, extracting any text */
	while (child) {
		extract_text(child, first, before, save);
		child = child->next;
	}

	return;
}


/**
 * Add text to save text buffer. Any preceding whitespace or following space is
 * also added to the buffer.
 *
 * \param  text		Pointer to text being added.
 * \param  length	Length of text to be appended (bytes).
 * \param  box		Pointer to text box.
 * \param  whitespace_text    Whitespace to place before text for formatting
 *                            may be NULL.
 * \param  whitespace_length  Length of whitespace_text.
 * \param  save		Our save_text_state workspace pointer.
 * \return true iff the file writing succeeded and traversal should continue.
 */

bool save_text_add_to_buffer(const char *text, size_t length, struct box *box,
		const char *whitespace_text, size_t whitespace_length,
		struct save_text_state *save)
{
	size_t new_length;
	int space = 0;

	assert(save);

	if (box->space > 0)
		space = 1;

	if (whitespace_text)
		length += whitespace_length;

	new_length = save->length + whitespace_length + length + space;
	if (new_length >= save->alloc) {
		size_t new_alloc = save->alloc + (save->alloc / 4);
		char *new_block;

		if (new_alloc < new_length) new_alloc = new_length;

		new_block = realloc(save->block, new_alloc);
		if (!new_block) return false;

		save->block = new_block;
		save->alloc = new_alloc;
	}
	if (whitespace_text) {
		memcpy(save->block + save->length, whitespace_text,
				whitespace_length);
	}
	memcpy(save->block + save->length + whitespace_length, text, length);
	save->length += length;

	if (space == 1)
		save->block[save->length++] = ' ';

	return true;
}