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

Commit

Permalink
Defer text wrapping until the very end (#256)
Browse files Browse the repository at this point in the history
This lets us handle wrapping in one single place instead of smearing it
all over HTML renderer and listformatter. HTML renderer now signals how
it wants a string formatted—it can be either wrappable or
non-wrappable—and textformatter does the actual wrapping.
  • Loading branch information
Minoru committed Jun 11, 2016
1 parent f3a25ce commit 885760c
Show file tree
Hide file tree
Showing 14 changed files with 735 additions and 356 deletions.
2 changes: 1 addition & 1 deletion doc/configcommands.dsv
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ show-read-articles|[yes/no]|yes|If yes, then all articles of a feed are listed i
show-read-feeds|[yes/no]|yes|If yes, then all feeds, including those without unread articles, are listed. If no, then only feeds with one or more unread articles are list.|show-read-feeds no
suppress-first-reload|[yes/no]|no|If yes, then the first automatic reload will be suppressed if auto-reload is set to yes.|suppress-first-reload yes
swap-title-and-hints|[yes/no]|no|If yes, then the title at the top of screen and keymap hints at the bottom of screen will be swapped.|swap-title-and-hints yes
text-width|<number>|0|If set to a number greater than 0, then all HTML will be rendered to this maximum line length. If set to 0, the terminal width will be used.|text-width 72
text-width|<number>|0|If set to a number greater than 0, then all HTML will be rendered to this maximum line length. If set to 0, the terminal width will be used. Does not apply when using external renderer or viewing the source.|text-width 72
ttrss-flag-publish|<character>|""|If this is set and Tiny Tiny RSS support is used, then all articles that are flagged with the specified flag are being marked as "published" in Tiny Tiny RSS.|ttrss-flag-publish "b"
ttrss-flag-star|<character>|""|If this is set and Tiny Tiny RSS support is used, then all articles that are flagged with the specified flag are being "starred" in Tiny Tiny RSS.|ttrss-flag-star "a"
ttrss-login|<username>|""|Sets the username for use with Tiny Tiny RSS.|ttrss-login "admin"
Expand Down
33 changes: 26 additions & 7 deletions include/htmlrenderer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef NEWSBEUTER_HTMLRENDERER__H
#define NEWSBEUTER_HTMLRENDERER__H

#include <textformatter.h>
#include <vector>
#include <string>
#include <istream>
Expand All @@ -21,9 +22,16 @@ typedef std::pair<std::string,link_type> linkpair;

class htmlrenderer {
public:
htmlrenderer(unsigned int width = 80, bool raw = false);
void render(const std::string&, std::vector<std::string>& lines, std::vector<linkpair>& links, const std::string& url);
void render(std::istream &, std::vector<std::string>& lines, std::vector<linkpair>& links, const std::string& url);
htmlrenderer(bool raw = false);
void render(const std::string& source,
std::vector<std::pair<LineType, std::string>>& lines,
std::vector<linkpair>& links,
const std::string& url);
void render(std::istream & input,
std::vector<std::pair<LineType, std::string>>& lines,
std::vector<linkpair>& links,
const std::string& url);
static std::string render_hr(const unsigned int width);
// only public for unit testing purposes:
std::string format_ol_count(unsigned int count, char type);

Expand Down Expand Up @@ -59,17 +67,28 @@ class htmlrenderer {
};

private:
unsigned int w;
void prepare_new_line(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;
void render_table(const Table& table, std::vector<std::string>& lines);
void add_nonempty_line(const std::string& curline, std::vector<Table>& tables, std::vector<std::string>& lines);
void add_line(const std::string& curline, std::vector<Table>& tables, std::vector<std::string>& lines);
void render_table(
const Table& table,
std::vector<std::pair<LineType, std::string>>& lines);
void add_nonempty_line(
const std::string& curline,
std::vector<Table>& tables,
std::vector<std::pair<LineType, std::string>>& lines);
void add_line(
const std::string& curline,
std::vector<Table>& tables,
std::vector<std::pair<LineType, std::string>>& lines);
void add_line_verbatim(
const std::string& line,
std::vector<std::pair<LineType, std::string>>& lines);
void add_hr(std::vector<std::pair<LineType, std::string>>& lines);
std::string get_char_numbering(unsigned int count);
std::string get_roman_numbering(unsigned int count);
bool raw_;
Expand Down
11 changes: 9 additions & 2 deletions include/itemview_formaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <formaction.h>
#include <htmlrenderer.h>
#include <textformatter.h>
#include <regexmanager.h>
#include <rss.h>

Expand Down Expand Up @@ -33,7 +34,11 @@ class itemview_formaction : public formaction {

virtual void finished_qna(operation op);

std::vector<std::string> render_html(const std::string& source, std::vector<linkpair>& links, const std::string& feedurl, unsigned int render_width);
std::vector<std::pair<LineType, std::string>>
render_html(
const std::string& source,
std::vector<linkpair>& thelinks,
const std::string& url);

void set_regexmanager(regexmanager * r);

Expand All @@ -44,7 +49,9 @@ class itemview_formaction : public formaction {
void set_head(const std::string& s, const std::string& feedtitle, unsigned int unread, unsigned int total);
void highlight_text(const std::string& searchphrase);

void render_source(std::vector<std::string>& lines, std::string desc, unsigned int width);
void render_source(
std::vector<std::pair<LineType, std::string>>& lines,
std::string source);

void do_search();

Expand Down
40 changes: 40 additions & 0 deletions include/textformatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef TEXTFORMATTER__H
#define TEXTFORMATTER__H

#include <climits>
#include <vector>
#include <string>
#include <utility>
#include <regexmanager.h>

namespace newsbeuter {

enum LineType {
wrappable = 1,
nonwrappable,
hr
};

class textformatter {

public:
textformatter();
~textformatter();
void add_line(LineType type, std::string line);
void add_lines(
const std::vector<std::pair<LineType, std::string>> lines);
std::string format_text_to_list(regexmanager * r = NULL,
const std::string& location = "",
const size_t width = 80);
std::string format_text_plain(const size_t width = 80);

inline void clear() {
lines.clear();
}
private:
std::vector<std::pair<LineType, std::string>> lines;
};

}

#endif
2 changes: 2 additions & 0 deletions mk/mk.deps
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@ src/rss_parser.o: include/rss_parser.h include/configcontainer.h include/cache.h
src/ttrss_api.o: include/ttrss_api.h

src/newsblur_api.o: include/newsblur_api.h

src/textformatter.o: include/textformatter.h include/utils.h include/htmlrenderer.h include/stflpp.h
2 changes: 1 addition & 1 deletion mk/newsbeuter.deps
Original file line number Diff line number Diff line change
@@ -1 +1 @@
newsbeuter.cpp src/cache.cpp src/htmlrenderer.cpp src/urlreader.cpp src/logger.cpp src/view.cpp src/controller.cpp src/reloadthread.cpp src/tagsouppullparser.cpp src/downloadthread.cpp src/rss.cpp src/rss_parser.cpp src/formaction.cpp src/feedlist_formaction.cpp src/itemlist_formaction.cpp src/itemview_formaction.cpp src/help_formaction.cpp src/filebrowser_formaction.cpp src/urlview_formaction.cpp src/select_formaction.cpp src/history.cpp src/filtercontainer.cpp src/listformatter.cpp src/regexmanager.cpp src/dialogs_formaction.cpp src/ttrss_api.cpp src/ttrss_urlreader.cpp src/newsblur_api.cpp src/newsblur_urlreader.cpp src/markreadthread.cpp src/oldreader_urlreader.cpp src/oldreader_api.cpp src/feedhq_api.cpp src/feedhq_urlreader.cpp
newsbeuter.cpp src/cache.cpp src/htmlrenderer.cpp src/urlreader.cpp src/logger.cpp src/view.cpp src/controller.cpp src/reloadthread.cpp src/tagsouppullparser.cpp src/downloadthread.cpp src/rss.cpp src/rss_parser.cpp src/formaction.cpp src/feedlist_formaction.cpp src/itemlist_formaction.cpp src/itemview_formaction.cpp src/help_formaction.cpp src/filebrowser_formaction.cpp src/urlview_formaction.cpp src/select_formaction.cpp src/history.cpp src/filtercontainer.cpp src/listformatter.cpp src/regexmanager.cpp src/dialogs_formaction.cpp src/ttrss_api.cpp src/ttrss_urlreader.cpp src/newsblur_api.cpp src/newsblur_urlreader.cpp src/markreadthread.cpp src/oldreader_urlreader.cpp src/oldreader_api.cpp src/feedhq_api.cpp src/feedhq_urlreader.cpp src/textformatter.cpp
26 changes: 13 additions & 13 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,42 +1381,42 @@ void controller::write_item(std::shared_ptr<rss_item> item, const std::string& f
}

void controller::write_item(std::shared_ptr<rss_item> item, std::ostream& ostr) {
std::vector<std::string> lines;
std::vector<std::pair<LineType, std::string>> lines;
std::vector<linkpair> links; // not used

std::string title(_("Title: "));
title.append(item->title());
lines.push_back(title);
lines.push_back(std::make_pair(newsbeuter::wrappable, title));

std::string author(_("Author: "));
author.append(item->author());
lines.push_back(author);
lines.push_back(std::make_pair(newsbeuter::wrappable, author));

std::string date(_("Date: "));
date.append(item->pubDate());
lines.push_back(date);
lines.push_back(std::make_pair(newsbeuter::wrappable, date));

std::string link(_("Link: "));
link.append(item->link());
lines.push_back(link);
lines.push_back(std::make_pair(newsbeuter::nonwrappable, link));

if (item->enclosure_url() != "") {
std::string dlurl(_("Podcast Download URL: "));
dlurl.append(item->enclosure_url());
lines.push_back(dlurl);
lines.push_back(std::make_pair(newsbeuter::nonwrappable, dlurl));
}

lines.push_back(std::string(""));
lines.push_back(std::make_pair(newsbeuter::wrappable, std::string("")));

htmlrenderer rnd(true);
rnd.render(item->description(), lines, links, item->feedurl());
textformatter txtfmt;
txtfmt.add_lines(lines);

unsigned int width = cfg.get_configvalue_as_int("text-width");
if (width == 0)
width = 80;
htmlrenderer rnd(width, true);
rnd.render(item->description(), lines, links, item->feedurl());

for (auto l : lines) {
ostr << l << std::endl;
}
ostr << txtfmt.format_text_plain(width) << std::endl;
}

void controller::mark_deleted(const std::string& guid, bool b) {
Expand Down
Loading

0 comments on commit 885760c

Please sign in to comment.