[Xfce4-commits] <postler:master> Convert HTML to plain text without lynx

Christian Dywan noreply at xfce.org
Tue Aug 16 04:32:03 CEST 2011


Updating branch refs/heads/master
         to 3b330fa77b47566d565203c29b718d2a530dfb4d (commit)
       from ce4e71d5bb573442a563054f16d38d52f8194a74 (commit)

commit 3b330fa77b47566d565203c29b718d2a530dfb4d
Author: Christian Dywan <christian at twotoasts.de>
Date:   Mon Aug 15 22:47:35 2011 +0200

    Convert HTML to plain text without lynx

 README                       |    2 +-
 postler/postler-message.vala |   52 +++++++++++++++++++++++------------------
 wscript                      |    3 --
 3 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/README b/README
index fbd45c8..3491c4e 100644
--- a/README
+++ b/README
@@ -10,7 +10,7 @@ integration are provided by Dexter.
 Requirements: GIO 2.26, GTK+ 2.18, WebkitGTK+ 1.1.18, Unique 0.9, libnotify,
               libcanberra, (Berkeley) db, openssl
 
-Recommended: libfolks, Zeitgeist, libindicate, Dexter, lynx, libstemmer, Getting Things GNOME!
+Recommended: libfolks, Zeitgeist, libindicate, Dexter, libstemmer, Getting Things GNOME!
 
 For installation instructions read INSTALL.
 
diff --git a/postler/postler-message.vala b/postler/postler-message.vala
index 6f8553b..9bb696b 100644
--- a/postler/postler-message.vala
+++ b/postler/postler-message.vala
@@ -44,32 +44,38 @@ namespace Postler {
         }
 
         public string get_plain_text () {
-            string body_plain = body.str;
             if ((body != null) && (mime_type != null)) {
-                try {
-                    string plain_text = "";
-                    int std_in;
-                    int std_out;
-                    string[] argv = { "lynx", "-force_html", "-width=900", "-dump",
-                                      "-display_charset=utf-8", "-stdin" };
-                    Process.spawn_async_with_pipes (null, argv, null,
-                        SpawnFlags.SEARCH_PATH, null, null,
-                        out std_in, out std_out, null);
-                    IOChannel ioc_stdout = new IOChannel.unix_new (std_out);
-                    IOChannel ioc_stdin = new IOChannel.unix_new (std_in);
-                    char[] content_chars = (char[]) (body.str.data);
-                    var status = ioc_stdin.write_chars (content_chars, null);
-                    ioc_stdin.shutdown (true);
-                    if (status != IOStatus.ERROR)
-                        status = ioc_stdout.read_to_end (out plain_text, null);
-                    if (status != IOStatus.ERROR)
-                        body_plain = plain_text;
-                }
-                catch (Error error) {
-                    GLib.message (_("Error converting HTML to text: %s"), error.message);
+                /* TODO:
+                    <a href="URL">URL</a>
+                    render href in brackets or as footnote
+                    turn <br> into \n
+                 */
+                var plain_text = new StringBuilder.sized (body.str.size () * 2);
+                uint none_open_close = 0;
+                StringBuilder tag = new StringBuilder ();
+                for (unowned string s = body.str; s.get_char () != 0 ; s = s.next_char ()) {
+                    unichar character = s.get_char ();
+                    if (character.iscntrl ())
+                        plain_text.append_unichar (' ');
+                    if (!character.validate ())
+                        plain_text.append_unichar ('�');
+                    else if (character == '<') {
+                        none_open_close = 1;
+                        tag = new StringBuilder ();
+                    }
+                    else if (character == '>') {
+                        none_open_close = 2;
+                        if (tag.str == "br")
+                            plain_text.append_c ('\n');
+                    }
+                    else if (none_open_close == 1)
+                        tag.append_unichar (character);
+                    else if (none_open_close != 1)
+                        plain_text.append_unichar (character);
                 }
+                return plain_text.str;
             }
-            return body_plain;
+            return "";
         }
     }
 
diff --git a/wscript b/wscript
index 2b0d618..301b5ac 100644
--- a/wscript
+++ b/wscript
@@ -211,9 +211,6 @@ def configure (conf):
         Utils.pprint ('YELLOW', 'Dexter is not installed. It is recommended ' \
                                 'because it provides contact completion \n' \
                                 'and allows adding of contacts from Postler.')
-    if not conf.find_program ('lynx'):
-        Utils.pprint ('YELLOW', 'Lynx is not installed. It is recommended ' \
-                                'because it enables HTML to text conversion.')
 
     conf.define ('PACKAGE', APPNAME)
     conf.define ('VERSION', VERSION)


More information about the Xfce4-commits mailing list