Subject: [sup-talk] [PATCH] first pass at a simple interface to extract URIs from a message

From: [email-masked] (brabuhr)
Date: Tue, 29 Jan 2008 22:23:31 -0500

On Jan 29, 2008 2:47 PM, Daniel Wagner <daniel at wagner-home.com> wrote:
> One thing I find myself doing all the time is highlighting a web address
> and pasting it into Firefox.

This adds a new key binding to the thread-view mode (L) to assist with
loading URIs from the body of the message.  This first pass will pull
the URIs from the message text and loop through them while asking if
that URI should be loaded.  Currently, there is no code to actually
perform the load.
---
 lib/sup/modes/thread-view-mode.rb |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/lib/sup/modes/thread-view-mode.rb
b/lib/sup/modes/thread-view-mode.rb
index 961fc5d..5fae8fa 100644
--- a/lib/sup/modes/thread-view-mode.rb
+++ b/lib/sup/modes/thread-view-mode.rb
@@ -1,4 +1,5 @@
 require 'open3'
+require 'uri'
 module Redwood

 class ThreadViewMode < LineCursorMode
@@ -48,6 +49,7 @@ EOS
     k.add :subscribe_to_list, "Subscribe to/unsubscribe from mailing list", "("
     k.add :unsubscribe_from_list, "Subscribe to/unsubscribe from
mailing list", ")"
     k.add :pipe_message, "Pipe message or attachment to a shell command", '|'
+    k.add :follow_link, "Follow a web link", "L"

     k.add_multi "(a)rchive/(d)elete/mark as (s)pam/mark as
u(N)read:", '.' do |kk|
       kk.add :archive_and_kill, "Archive this thread and kill buffer", 'a'
@@ -456,6 +458,34 @@ EOS
     end
   end

+  def follow_link
+    chunk = @chunk_lines[curpos]
+
+    return unless chunk.is_a?(Chunk::Text)
+
+    uris = URI.extract(chunk.lines.join("\n")).uniq.sort
+
+    if uris.size > 0
+      Redwood::log uris.join("\n")
+      Redwood::BufferManager.flash "#{uris.size} URIs found."
+      sleep 1
+
+      uris.each do |uri|
+        case Redwood::BufferManager.ask_yes_or_no "Load URI: #{uri} (y/n)"
+        when true
+          Redwood::BufferManager.flash "<add launch the url code here
please :-)>"
+          break
+        when false
+          next
+        else
+          break
+        end
+      end
+    else
+      Redwood::BufferManager.flash "No URIs found."
+    end
+  end
+
 private

   def initial_state_for m
-- 
1.5.2.5

---

From: [email-masked] (William Morgan)
Date: Sat, 02 Feb 2008 18:56:40 -0800

> that URI should be loaded.  Currently, there is no code to actually

Looks good. I have a slight preference for a "if uris.size == 0" block
that flashes a message and returns, so that the meat of the method
doesn't have to be too indented.

My limited research indicates that there isn't a standard way (like
mailcap) of viewing a URL. So probably a "view-url" hook is the best way
to approach the actual viewing code.

-- 
William <wmorgan-sup at masanjin.net>

---

From: [email-masked] (brabuhr at gmail.com)
Date: Fri, 8 Feb 2008 21:01:31 -0500

> doesn't have to be too indented.

I was also thinking that it would be better to display the discovered URIs
in a list instead of the prompting, but I wasn't sure how to implement that.
Another problem I see is that the extractor is too-greedy and will pull in
the punctuation following the URL; so, perhaps the list should allow
line editing before calling the view-url hook?

---

From: [email-masked] (William Morgan)
Date: Sat, 16 Feb 2008 12:04:32 -0800

[forgot to send to list]

Reformatted excerpts from brabuhr's message of 2008-02-08:
> I was also thinking that it would be better to display the discovered
> URIs in a list instead of the prompting, but I wasn't sure how to
> implement that.  Another problem I see is that the extractor is
> too-greedy and will pull in the punctuation following the URL; so,
> perhaps the list should allow line editing before calling the view-url
> hook?

If you want to display them as a list, I would add a mode called
UrlListMode or something like that which was a subclass of
LineCursorMode. You can look to BufferListMode as an example, though it
will be even simpler than that because the content won't ever change.

Spawn an instance of that mode when the user presses 'L', and have
enter in UrlListMode trigger the URL viewing hook, possibly first
running it through BufferManager#ask for editing. We can also trim
trailing punctuation from whatever URI.extract pulls out, since that
sounds like the common case.

---

From: [email-masked] (William Morgan)
Date: Sat, 16 Feb 2008 12:06:32 -0800

---
Here's a "starter patch". Still needs HookManager integration to
actually view the URLs, and probably some touching up of the extraction
to trim out punctuation and the like. I found that URI.extract is very
general unless you restrict it to particular schemata, so this limits it
to http and ftp only. Feel free to expand/tweak any of it.

 lib/sup.rb                        |    1 +
 lib/sup/modes/thread-view-mode.rb |   13 +++++++++++++
 lib/sup/modes/url-list-mode.rb    |   23 +++++++++++++++++++++++
 3 files changed, 37 insertions(+), 0 deletions(-)
 create mode 100644 lib/sup/modes/url-list-mode.rb

diff --git a/lib/sup.rb b/lib/sup.rb
index cec36b6..3c591e6 100644
--- a/lib/sup.rb
+++ b/lib/sup.rb
@@ -276,6 +276,7 @@ require "sup/modes/buffer-list-mode"
 require "sup/modes/poll-mode"
 require "sup/modes/file-browser-mode"
 require "sup/modes/completion-mode"
+require "sup/modes/url-list-mode"
 require "sup/sent"
 
 $:.each do |base|
diff --git a/lib/sup/modes/thread-view-mode.rb b/lib/sup/modes/thread-view-mode.rb
index fb324c1..99b7908 100644
--- a/lib/sup/modes/thread-view-mode.rb
+++ b/lib/sup/modes/thread-view-mode.rb
@@ -48,6 +48,7 @@ EOS
     k.add :subscribe_to_list, "Subscribe to/unsubscribe from mailing list", "("
     k.add :unsubscribe_from_list, "Subscribe to/unsubscribe from mailing list", ")"
     k.add :pipe_message, "Pipe message or attachment to a shell command", '|'
+    k.add :view_urls, "View URLs in message", 'L'
 
     k.add_multi "(a)rchive/(d)elete/mark as (s)pam/mark as u(N)read:", '.' do |kk|
       kk.add :archive_and_kill, "Archive this thread and kill buffer", 'a'
@@ -118,6 +119,18 @@ EOS
   def lines; @text.length; end
   def [] i; @text[i]; end
 
+  def view_urls
+    m = @message_lines[curpos] or return
+    chunks = m.chunks.select { |c| c.is_a? Chunk::Text }
+    return if chunks.empty?
+
+    lines = chunks.map { |c| c.lines }.flatten.join(" ")
+    urls = URI.extract(lines, %w(http ftp)).uniq.sort
+    return if urls.empty?
+
+    BufferManager.spawn "URLs for #{m.subj}", UrlListMode.new(urls)
+  end
+
   def show_header
     m = @message_lines[curpos] or return
     BufferManager.spawn_unless_exists("Full header for #{m.id}") do
diff --git a/lib/sup/modes/url-list-mode.rb b/lib/sup/modes/url-list-mode.rb
new file mode 100644
index 0000000..f51cd52
--- /dev/null
+++ b/lib/sup/modes/url-list-mode.rb
@@ -0,0 +1,23 @@
+module Redwood
+class UrlListMode < LineCursorMode
+  register_keymap do |k|
+    k.add :view_url, "View selected URL", :enter
+  end
+
+  def initialize urls
+    @urls = urls
+    @text = urls.map { |u| u.to_s }
+    super()
+  end
+
+  def lines; @text.length end
+  def [] i; @text[i] end
+
+  def view_url
+    url = BufferManager.ask :url, "URL: ", @urls[curpos].to_s
+    return unless url
+    BufferManager.flash "viewing #{url.inspect}"
+  end
+end
+
+end
-- 
1.5.4.rc2.69.g10f0

---

From: [email-masked] (Nicolas Pouillard)
Date: Tue, 19 Feb 2008 00:24:54 +0100

> sounds like the common case.

You  should look the rxvt-unicode terminal, there is an extension to deal with
URLs [1]. That's pretty handy in fact and applies not only to sup.

[1]: http://www.jukie.net/~bart/blog/20070503013555

Regards,

-- 
Nicolas Pouillard aka Ertai
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 194 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/sup-talk/attachments/20080219/cb877745/attachment.bin

---

From: [email-masked] (Daniel Wagner)
Date: Mon, 18 Feb 2008 19:12:15 -0800

> [1]: http://www.jukie.net/~bart/blog/20070503013555

Sounds nice, does it handle multi-line URLs gracefully?  In particular,
in sup a multi-line URL could have a line-break in it... care to report
how it handles this?

http://www.yahoo.com/?this-is-a-really-long-url-for-you-to-test-with-if-you-would-be-so-kind-that-contains-no-newline-in-the-actual-message-as-sent
http://www.yahoo.com/?this-is-a-really-long-url-for-you-to-test-with-if-you-woul
d-be-so-kind-that-contains-a-newline-in-the-eightieth-column

Either way, thanks for the tip!
~d

---

From: [email-masked] (Nicolas Pouillard)
Date: Tue, 19 Feb 2008 11:17:25 +0100

Excerpts from Daniel Wagner's message of Tue Feb 19 04:12:15 +0100 2008:
> http://www.yahoo.com/?this-is-a-really-long-url-for-you-to-test-with-if-you-would-be-so-kind-that-contains-a-newline-in-the-eightieth-column

It  doesn't  handle  br0ken  URLs, however here that's perhaps sup jobs to fix
them. I send a patch to the list to fix this.

Regards,

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 240 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/sup-talk/attachments/20080219/61653c81/attachment-0001.bin