summaryrefslogtreecommitdiff
path: root/src/htmlrenderer.cpp
blob: a9f638a02fc09d15da9e66359357f8f924c4f3d5 (plain) (blame)
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
#include <htmlrenderer.h>

using namespace noos;

htmlrenderer::htmlrenderer(unsigned int width) : w(width) { }

std::vector<std::string> htmlrenderer::render(const std::string& source) {
	std::vector<std::string> lines;
	std::string curline, cleaned_source;

	const char * begin = source.c_str();
	bool addchar = true;

	// clean up source:
	while (*begin) {
		if (addchar) {
			if (*begin != '<') {
				char str[2];
				str[0] = *begin;
				str[1] = '\0';
				cleaned_source.append(str);
			} else {
				addchar = false;
			}
		} else {
			if (*begin == '>') {
				addchar = true;
				cleaned_source.append(" ");
			}
		}
		++begin;
	}

	begin = cleaned_source.c_str();
	while (strlen(begin) >= w) {
		const char * end = begin + w;
		while (end > begin && *end != ' ') 
			--end;
		if (begin == end) {
			char x[w+1];
			strncpy(x,begin,w);
			x[w] = '\0';
			curline = x;
		} else {
			char x[end-begin+1];
			strncpy(x,begin,end-begin);
			x[w] = '\0';
			curline = x;
		}
		lines.push_back(curline);
		begin += w;
	}
	lines.push_back(begin);
	return lines;
}