Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
Improved HTML rendering of links and underlined and bold text.
Browse files Browse the repository at this point in the history
  • Loading branch information
akrennmair committed Jan 26, 2009
1 parent beb47cb commit be0b9d0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -12,6 +12,7 @@ Changes for newsbeuter:
Added ability to search for text from the article view.
Added basic support for Yahoo Media RSS.
Made article view pager configurable.
Improved HTML rendering of links and underlined and bold text.

1.3 (2008-12-06):
Changed some internal data structures to smart pointers (stability improvement).
Expand Down
3 changes: 2 additions & 1 deletion include/htmlrenderer.h
Expand Up @@ -9,7 +9,7 @@
namespace newsbeuter {

enum link_type { LINK_HREF, LINK_IMG, LINK_EMBED };
enum htmltag { TAG_A = 1, TAG_EMBED, TAG_BR, TAG_PRE, TAG_ITUNESHACK, TAG_IMG, TAG_BLOCKQUOTE, TAG_P, TAG_OL, TAG_UL, TAG_LI, TAG_DT, TAG_DD, TAG_DL, TAG_SUP, TAG_SUB, TAG_HR };
enum htmltag { TAG_A = 1, TAG_EMBED, TAG_BR, TAG_PRE, TAG_ITUNESHACK, TAG_IMG, TAG_BLOCKQUOTE, TAG_P, TAG_OL, TAG_UL, TAG_LI, TAG_DT, TAG_DD, TAG_DL, TAG_SUP, TAG_SUB, TAG_HR, TAG_STRONG, TAG_UNDERLINE };

typedef std::pair<std::string,link_type> linkpair;

Expand All @@ -23,6 +23,7 @@ namespace newsbeuter {
void prepare_newline(std::string& line, int indent_level);
bool line_is_nonempty(const std::string& line);
unsigned int add_link(std::vector<linkpair>& links, const std::string& link, link_type type);
std::string quote_for_stfl(std::string str);
std::string absolute_url(const std::string& url, const std::string& link);
std::string type2str(link_type type);
std::map<std::string, htmltag> tags;
Expand Down
2 changes: 2 additions & 0 deletions include/utils.h
Expand Up @@ -60,6 +60,8 @@ class utils {

static std::string censor_url(const std::string& url);

static std::string quote_for_stfl(std::string str);

private:
static void append_escapes(std::string& str, char c);

Expand Down
27 changes: 22 additions & 5 deletions src/htmlrenderer.cpp
Expand Up @@ -31,6 +31,9 @@ htmlrenderer::htmlrenderer(unsigned int width) : w(width) {
tags["sup"] = TAG_SUP;
tags["sub"] = TAG_SUB;
tags["hr"] = TAG_HR;
tags["b"] = TAG_STRONG;
tags["strong"] = TAG_STRONG;
tags["u"] = TAG_UNDERLINE;
}

void htmlrenderer::render(const std::string& source, std::vector<std::string>& lines, std::vector<linkpair>& links, const std::string& url) {
Expand Down Expand Up @@ -95,10 +98,16 @@ void htmlrenderer::render(std::istream& input, std::vector<std::string>& lines,
}
if (link.length() > 0) {
link_num = add_link(links,utils::censor_url(utils::absolute_url(url,link)), LINK_HREF);
curline.append("[");
curline.append("<u>");
}
}
break;
case TAG_STRONG:
curline.append("<b>");
break;
case TAG_UNDERLINE:
curline.append("<u>");
break;

case TAG_EMBED: {
std::string type;
Expand Down Expand Up @@ -335,11 +344,19 @@ void htmlrenderer::render(std::istream& input, std::vector<std::string>& lines,

case TAG_A:
if (link_num != -1) {
curline.append(utils::strprintf("][%d]", link_num));
curline.append(utils::strprintf("</>[%d]", link_num));
link_num = -1;
}
break;

case TAG_UNDERLINE:
curline.append("</>");
break;

case TAG_STRONG:
curline.append("</>");
break;

case TAG_EMBED:
case TAG_BR:
case TAG_ITUNESHACK:
Expand All @@ -356,7 +373,7 @@ void htmlrenderer::render(std::istream& input, std::vector<std::string>& lines,
{
GetLogger().log(LOG_DEBUG,"htmlrenderer::render: found text `%s'",xpp.getText().c_str());
if (itunes_hack) {
std::vector<std::string> words = utils::tokenize_nl(xpp.getText());
std::vector<std::string> words = utils::tokenize_nl(utils::quote_for_stfl(xpp.getText()));
for (std::vector<std::string>::iterator it=words.begin();it!=words.end();++it) {
if (*it == "\n") {
lines.push_back(curline);
Expand Down Expand Up @@ -385,7 +402,7 @@ void htmlrenderer::render(std::istream& input, std::vector<std::string>& lines,
}
}
} else if (inside_pre) {
std::vector<std::string> words = utils::tokenize_nl(xpp.getText());
std::vector<std::string> words = utils::tokenize_nl(utils::quote_for_stfl(xpp.getText()));
for (std::vector<std::string>::iterator it=words.begin();it!=words.end();++it) {
if (*it == "\n") {
lines.push_back(curline);
Expand All @@ -395,7 +412,7 @@ void htmlrenderer::render(std::istream& input, std::vector<std::string>& lines,
}
}
} else {
std::string s = xpp.getText();
std::string s = utils::quote_for_stfl(xpp.getText());
while (s.length() > 0 && s[0] == '\n')
s.erase(0, 1);
std::vector<std::string> words = utils::tokenize_spaced(s);
Expand Down
3 changes: 2 additions & 1 deletion src/itemview_formaction.cpp
Expand Up @@ -116,7 +116,7 @@ void itemview_formaction::prepare() {
}

if (show_source) {
render_source(lines, item->description(), render_width);
render_source(lines, utils::quote_for_stfl(item->description()), render_width);
} else {
lines = render_html(item->description(), links, item->feedurl(), render_width);
}
Expand Down Expand Up @@ -445,6 +445,7 @@ void itemview_formaction::set_regexmanager(regexmanager * r) {
for (std::vector<std::string>::iterator it=attrs.begin();it!=attrs.end();++it,++i) {
attrstr.append(utils::strprintf("@style_%u_normal:%s ", i, it->c_str()));
}
attrstr.append("@style_b_normal:attr=bold @style_u_normal:attr=underline ");
std::string textview = utils::strprintf("{textview[article] style_normal[article]: style_end[styleend]:fg=blue,attr=bold %s .expand:vh offset[articleoffset]:0 richtext:1}", attrstr.c_str());
f->modify("article", "replace", textview);
}
Expand Down
7 changes: 0 additions & 7 deletions src/regexmanager.cpp
Expand Up @@ -106,13 +106,6 @@ void regexmanager::remove_last_regex(const std::string& location) {
void regexmanager::quote_and_highlight(std::string& str, const std::string& location) {
std::vector<regex_t *>& regexes = locations[location].first;

unsigned int len = str.length();
for (unsigned int i=0;i<len;++i) {
if (str[i] == '<') {
str.insert(i+1, ">");
++len;
}
}
unsigned int i = 0;
for (std::vector<regex_t *>::iterator it=regexes.begin();it!=regexes.end();++it, ++i) {
regmatch_t pmatch;
Expand Down
11 changes: 11 additions & 0 deletions src/utils.cpp
Expand Up @@ -666,4 +666,15 @@ std::string utils::censor_url(const std::string& url) {
return rv;
}

std::string utils::quote_for_stfl(std::string str) {
unsigned int len = str.length();
for (unsigned int i=0;i<len;++i) {
if (str[i] == '<') {
str.insert(i+1, ">");
++len;
}
}
return str;
}

}

0 comments on commit be0b9d0

Please sign in to comment.