summaryrefslogtreecommitdiff
path: root/src/htmlrenderer.cpp
blob: aa12c862cd7efe95aedd179b72cfb238e5e25c2a (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
56
57
58
59
60
61
62
#include <htmlrenderer.h>
#include <iostream>

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;
	std::string 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';
				if (str[0] == '\n' || str[0] == '\r') {
					str[0] = ' ';
				}
				cleaned_source.append(str);
			} else {
				addchar = false;
			}
		} else {
			if (*begin == '>') {
				addchar = true;
				cleaned_source.append(" ");
			}
		}
		++begin;
	}

	std::cerr << "cleaned_source: `" << cleaned_source << "'" << std::endl;

	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[end-begin] = '\0';
			curline = x;
		}
		lines.push_back(curline);
		begin += curline.length() + 1;
	}
	lines.push_back(std::string(begin));
	return lines;
}