This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new 368dc050 Reflow the new statement to 80 chars
368dc050 is described below

commit 368dc0505e57b46474f0d49406cdb0d2e599de28
Author: Sebb <[email protected]>
AuthorDate: Wed Feb 1 23:31:42 2023 +0000

    Reflow the new statement to 80 chars
---
 lib/whimsy/asf/member-files.rb |  7 ++--
 lib/whimsy/asf/string-utils.rb | 81 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 3 deletions(-)

diff --git a/lib/whimsy/asf/member-files.rb b/lib/whimsy/asf/member-files.rb
index 2c14dea8..4cfcc1de 100644
--- a/lib/whimsy/asf/member-files.rb
+++ b/lib/whimsy/asf/member-files.rb
@@ -1,6 +1,9 @@
+require 'whimsy/asf/string-utils'
+
 require_relative 'config'
 require_relative 'ldap'
 require_relative 'svn'
+
 module ASF
 
   class MemberFiles
@@ -89,9 +92,7 @@ module ASF
         "   Seconded by: #{secby}",
         '',
         '   Nomination statement:',
-        statement.split("\n").map do |l|
-          "    #{l}"
-        end,
+        statement.asf_reflow(4, 80),
         ''
       ].compact.join("\n") + "\n"
     end
diff --git a/lib/whimsy/asf/string-utils.rb b/lib/whimsy/asf/string-utils.rb
new file mode 100644
index 00000000..2417a40d
--- /dev/null
+++ b/lib/whimsy/asf/string-utils.rb
@@ -0,0 +1,81 @@
+# update String class to add some useful methods
+# These are prefixed with asf_ to avoid clashing with built-in methods
+# derived from agenda/helpers/string.rb
+
+# This addon must be required before use
+
+class String
+  # wrap a text block containing long lines
+  def asf_word_wrap(line_width=80)
+    self.split("\n").collect do |line|
+      if line.length > line_width
+        line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
+      else
+        line
+      end
+    end * "\n"
+  end
+
+  # reflow an indented block
+  # indent = number of spaces to indent by (default 4)
+  # len = length of line including the indent (default 80)
+  def asf_reflow(indent=4, len=80)
+    strip.split(/\n\s*\n/).map {|line|
+      line.gsub!(/\s+/, ' ')
+      line.strip!
+      line.asf_word_wrap(len - indent).gsub(/^/, ' ' * indent)
+    }.join("\n\n")
+  end
+
+  # replace matched expressions with the result of the block being called
+  def asf_mreplace(regexp, &block)
+    matches = []
+    off = 0
+    while self[off..-1] =~ regexp
+      matches << [off, $~]
+      off += $~.end($~.size - 1)
+    end
+    raise 'unmatched' if matches.empty?
+
+    matches.reverse.each do |offset, match|
+      slice = self[offset...-1]
+      send = (1...match.size).map {|i| slice[match.begin(i)...match.end(i)]}
+      if send.length == 1
+        recv = block.call(send.first)
+        self[offset + match.begin(1)...offset + match.end(1)] = recv
+      else
+        recv = block.call(*send)
+        next unless recv
+        (1...match.size).map {|i| [match.begin(i), match.end(i), i - 1]}.sort.
+          reverse.each do |start, fin, i|
+          self[offset + start...offset + fin] = recv[i]
+        end
+      end
+    end
+    self
+  end
+
+  # fix encoding errors
+  def asf_fix_encoding
+    result = self
+
+    if encoding == Encoding::BINARY
+      result = encode('utf-8', invalid: :replace, undef: :replace)
+    end
+
+    result
+  end
+end
+
+if __FILE__ == $0
+  txt = "
+  The quick brown fox jumped over the lazy dog. The quick brown fox jumped 
over the lazy dog. The quick brown fox jumped over the lazy dog.
+
+  The quick brown fox jumped over the lazy dog. The quick brown fox jumped 
over the lazy dog. The quick brown fox jumped over the lazy dog.
+
+  The quick brown fox jumped over the lazy dog. The quick brown fox jumped 
over the lazy dog. The quick brown fox jumped over the lazy dog.
+  "
+  # puts txt
+  puts txt.asf_word_wrap
+  puts txt.asf_reflow
+end

Reply via email to