Archive of RubyForge sup-talk mailing list
 help / color / mirror / Atom feed
From: nicolas.pouillard@gmail.com (Nicolas Pouillard)
Subject: [sup-talk] [PATCH] Unwrap br0ken URLs.
Date: Tue, 19 Feb 2008 11:17:50 +0100	[thread overview]
Message-ID: <12034162702820-git-send-email-nicolas.pouillard@gmail.com> (raw)

---
 lib/sup/message-chunks.rb |    2 +-
 lib/sup/util.rb           |   25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletions(-)

diff --git a/lib/sup/message-chunks.rb b/lib/sup/message-chunks.rb
index 0606395..98b829c 100644
--- a/lib/sup/message-chunks.rb
+++ b/lib/sup/message-chunks.rb
@@ -147,7 +147,7 @@ EOS
 
     attr_reader :lines
     def initialize lines
-      @lines = lines.map { |l| l.chomp.wrap WRAP_LEN }.flatten # wrap
+      @lines = lines.unwrap_urls.map { |l| l.chomp.wrap WRAP_LEN }.flatten # wrap
 
       ## trim off all empty lines except one
       @lines.pop while @lines.length > 1 && @lines[-1] =~ /^\s*$/ && @lines[-2] =~ /^\s*$/
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
index ceaf0b8..99e73b4 100644
--- a/lib/sup/util.rb
+++ b/lib/sup/util.rb
@@ -401,6 +401,31 @@ class Array
 
   def last= e; self[-1] = e end
   def nonempty?; !empty? end
+
+  URL_CHAR      = /[a-zA-Z0-9\-@;\/?:&=%$_.+!*\x27()~,#]/
+  URL_CHAR_LESS = /[a-zA-Z0-9\-@;\/?:&=%$_.+!*\x27()~]/
+  URL_RE = %r{(?:https?://|ftp://|news://|mailto:|file://)#{URL_CHAR}+}
+  TRAILING_URL_RE = /#{URL_RE}$/
+  LEADING_URL_RE = /^#{URL_RE}/
+  URL_PART_RE = /^#{URL_CHAR}+#{URL_CHAR_LESS}/
+
+  def unwrap_urls
+    res = []
+    len = size
+    i = 0
+    while i < len do
+      x = self[i]
+      y = self[i+1]
+      if y && x =~ TRAILING_URL_RE && y =~ URL_PART_RE && y !~ LEADING_URL_RE
+        res << x.chomp + y
+        i += 2
+      else
+        res << x
+        i += 1
+      end
+    end
+    res
+  end
 end
 
 class Time
-- 
1.5.3.1.109.gacd69


             reply	other threads:[~2008-02-19 10:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-19 10:17 Nicolas Pouillard [this message]
2008-02-28 17:29 ` William Morgan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12034162702820-git-send-email-nicolas.pouillard@gmail.com \
    --to=nicolas.pouillard@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox