Skip to content

Commit

Permalink
Andreas Krennmair:
Browse files Browse the repository at this point in the history
	fixed ugly crash in HTML rendering.
  • Loading branch information
akrennmair committed Dec 6, 2006
1 parent f98bdfd commit 98f17f5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
4 changes: 3 additions & 1 deletion include/htmlrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

#include <vector>
#include <string>
#include <istream>

namespace noos {

class htmlrenderer {
public:
htmlrenderer(unsigned int width = 80);
std::vector<std::string> render(const std::string& source);
void render(const std::string&, std::vector<std::string>& );
void render(std::istream &, std::vector<std::string>& );
private:
unsigned int w;
void prepare_newline(std::string& line, int indent_level);
Expand Down
19 changes: 12 additions & 7 deletions src/htmlrenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#include <htmlrenderer.h>
#include <xmlpullparser.h>
#include <sstream>
#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;
void htmlrenderer::render(const std::string& source, std::vector<std::string>& lines) {
std::istringstream input(source);
render(input, lines);
}

void htmlrenderer::render(std::istream& input, std::vector<std::string>& lines) {
std::vector<std::string> links;
unsigned int link_count = 0;
std::string curline;
int indent_level = 0;
bool inside_list = false, inside_li = false, is_ol = false;
unsigned int ol_count = 1;

std::istringstream input(source);
xmlpullparser xpp;
xpp.setInput(input);

Expand Down Expand Up @@ -53,7 +57,7 @@ std::vector<std::string> htmlrenderer::render(const std::string& source) {
} else if (xpp.getText() == "p") {
if (curline.length() > 0)
lines.push_back(curline);
if (lines[lines.size()-1].length() > static_cast<unsigned int>(indent_level*2))
if (lines.size() > 0 && lines[lines.size()-1].length() > static_cast<unsigned int>(indent_level*2))
lines.push_back(std::string(""));
prepare_newline(curline, indent_level);
} else if (xpp.getText() == "ol") {
Expand Down Expand Up @@ -131,7 +135,8 @@ std::vector<std::string> htmlrenderer::render(const std::string& source) {
unsigned int i=0;
for (std::vector<std::string>::iterator it=words.begin();it!=words.end();++it,++i) {
if ((curline.length() + it->length()) >= w) {
lines.push_back(curline);
if (curline.length() > 0)
lines.push_back(curline);
prepare_newline(curline, indent_level);
}
curline.append(*it);
Expand All @@ -145,7 +150,8 @@ std::vector<std::string> htmlrenderer::render(const std::string& source) {
break;
}
}
lines.push_back(curline);
if (curline.length() > 0)
lines.push_back(curline);

if (links.size() > 0) {
lines.push_back(std::string(""));
Expand All @@ -157,7 +163,6 @@ std::vector<std::string> htmlrenderer::render(const std::string& source) {
}
}

return lines;
}

void htmlrenderer::prepare_newline(std::string& line, int indent_level) {
Expand Down
9 changes: 7 additions & 2 deletions src/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <itemlist.h>
#include <itemview.h>
#include <iostream>
#include <assert.h>

extern "C" {
#include <stfl.h>
Expand Down Expand Up @@ -309,7 +310,11 @@ bool view::run_itemview(rss_item& item) {

htmlrenderer rnd(render_width);

std::vector<std::string> lines = rnd.render(item.description());
// const char * desc = item.description().c_str();
// std::cerr << desc << std::endl;
std::vector<std::string> lines;
// std::cerr << "`" << item.description() << "'" << std::endl;
rnd.render(item.description(), lines);

for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
std::string line = std::string("{listitem text:") + std::string(stfl_quote(it->c_str())) + std::string("}");
Expand Down Expand Up @@ -406,7 +411,7 @@ void view::set_feedlist(std::vector<rss_feed>& feeds) {

if (show_read_feeds || unread_count > 0) {
snprintf(buf,sizeof(buf),"(%u/%u) ",unread_count,static_cast<unsigned int>(it->items().size()));
snprintf(buf2,sizeof(buf2),"%14s",buf);
snprintf(buf2,sizeof(buf2)," %c %11s",unread_count > 0 ? 'N' : ' ',buf);
std::string newtitle(buf2);
newtitle.append(title);
title = newtitle;
Expand Down
16 changes: 10 additions & 6 deletions src/xmlpullparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ xmlpullparser::event xmlpullparser::next() {

std::vector<std::string> tokens = tokenize(s);
if (tokens.size() > 0) {
text = tokens[0];
std::vector<std::string>::iterator it = tokens.begin();
++it;
while (it != tokens.end()) {
add_attribute(*it);
++it;
if (tokens[0].length() > 0)
text = tokens[0];
if (tokens.size() > 1) {
std::vector<std::string>::iterator it = tokens.begin();
++it;
while (it != tokens.end()) {
add_attribute(*it);
++it;
}
}
} else {
throw xmlexception("empty tag found");
Expand Down Expand Up @@ -158,6 +161,7 @@ xmlpullparser::event xmlpullparser::next() {
getline(*inputstream,tmp,'<');
remove_trailing_whitespace(tmp);
text.append(tmp);
text = decode_entities(text);
current_event = TEXT;
}
} else {
Expand Down

0 comments on commit 98f17f5

Please sign in to comment.