Skip to content
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 8dae93a579a99ef60c24906a42c78c1ad83b25c5 @mynyml committed Mar 15, 2009
Showing with 173 additions and 0 deletions.
  1. +24 −0 README
  2. +48 −0 Rakefile
  3. +1 −0 TODO
  4. +15 −0 examples/simple.rb
  5. +15 −0 examples/simple2.rb
  6. +11 −0 lib/unindent.rb
  7. +10 −0 test/test_helper.rb
  8. +49 −0 test/test_unindent.rb
24 README
@@ -0,0 +1,24 @@
+==== Summary
+
+Simple Ruby method to unindent strings. Useful for multiline strings embeded in already indented code.
+
+==== Examples
+
+class Profile
+ def default_text
+ <<-STR.unindent
+ Anonymous Coward
+ - Community Guest
+ STR
+ end
+end
+
+puts Profile.new.default_text
+
+will output:
+Anonymous Coward
+ - Community Guest
+
+instead of:
+ Anonymous Coward
+ - Community Guest
48 Rakefile
@@ -0,0 +1,48 @@
+require 'rake/gempackagetask'
+require 'pathname'
+require 'yaml'
+
+def gem
+ RUBY_1_9 ? 'gem19' : 'gem'
+end
+
+def all_except(paths)
+ Dir['**/*'] - paths.map {|path| path.strip.gsub(/^\//,'').gsub(/\/$/,'') }
+end
+
+spec = Gem::Specification.new do |s|
+ s.name = 'unindent'
+ s.version = '0.8.0'
+ s.summary = "Ruby method to unindent strings."
+ s.description = "Ruby method to unindent strings. Useful for multiline strings embeded in already indented code."
+ s.author = "Martin Aumont"
+ s.email = 'mynyml@gmail.com'
+ s.homepage = ''
+ s.has_rdoc = true
+ s.require_path = "lib"
+ s.files = Dir['**/*']
+end
+
+Rake::GemPackageTask.new(spec) do |p|
+ p.gem_spec = spec
+end
+
+
+desc "Remove package products"
+task :clean => :clobber_package
+
+desc "Update the gemspec for GitHub's gem server"
+task :gemspec do
+ Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
+end
+
+desc "Install gem"
+task :install => [:clobber, :package] do
+ sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
+end
+
+desc "Uninstall gem"
+task :uninstall => :clean do
+ sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
+end
+
1 TODO
@@ -0,0 +1 @@
+o Update readme with better exaples
15 examples/simple.rb
@@ -0,0 +1,15 @@
+require 'pathname'
+root = Pathname(__FILE__).dirname.parent
+require root.join('lib/unindent')
+
+class Profile
+ def default_text
+ <<-STR
+ Anonymous Coward
+ - Community Guest
+ STR
+ end
+end
+
+puts Profile.new.default_text
+puts Profile.new.default_text.unindent
15 examples/simple2.rb
@@ -0,0 +1,15 @@
+require 'pathname'
+root = Pathname(__FILE__).dirname.parent
+require root.join('lib/unindent')
+
+class Linker
+ def self.parse(text)
+ #text.gsub(%r%http://([^\s]+)%, <<-STR.unindent)
+ # <a href="http://\1">
+ # \1
+ # </a>
+ #STR
+ end
+end
+
+puts Linker.parse("find all you need at http://example.com")
11 lib/unindent.rb
@@ -0,0 +1,11 @@
+module Unindentation
+ def unindent
+ indent = self.select {|line| !line.strip.empty? }.map {|line| line.index(/[^\s]/) }.compact.min
+ self.gsub(/^[[:blank:]]{#{indent}}/, '')
+ end
+ def unindent!
+ self.replace(self.unindent)
+ end
+end
+
+String.class_eval { include Unindentation }
10 test/test_helper.rb
@@ -0,0 +1,10 @@
+require 'test/unit'
+require 'rubygems'
+require 'context'
+require 'matchy'
+begin
+ require 'ruby-debug'
+ require 'quietbacktrace'
+rescue LoadError, RuntimeError
+ # pass
+end
49 test/test_unindent.rb
@@ -0,0 +1,49 @@
+require 'pathname'
+root = Pathname(__FILE__).dirname.parent
+require root.join('test/test_helper')
+require root.join('lib/unindent')
+
+class UnindentTest < Test::Unit::TestCase
+ context "Unindent" do
+ context "simple indentation" do
+ test "removes space indentation" do
+ source = "\s\sabc"
+ expect = "abc"
+ source.unindent.should be(expect)
+ end
+ test "removes tab indentation" do
+ source = "\tabc"
+ expect = "abc"
+ source.unindent.should be(expect)
+ end
+ test "removes space/tab indentation" do
+ source = "\t\s\sabc"
+ expect = "abc"
+ source.unindent.should be(expect)
+ end
+ end
+ context "multiple lines" do
+ test "removes indentation" do
+ source = "\tabc\n\tabc"
+ expect = "abc\nabc"
+ source.unindent.should be(expect)
+ end
+ test "keeps relative indentation" do
+ source = "\tabc\n\t\tabc"
+ expect = "abc\n\tabc"
+ source.unindent.should be(expect)
+ end
+ test "ignores blank lines for indent calculation" do
+ source = "\n\tabc\n\n\t\tabc\n"
+ expect = "\nabc\n\n\tabc\n"
+ source.unindent.should be(expect)
+ end
+ end
+ test "modifies string in-place" do
+ source = "\s\sabc"
+ expect = "abc"
+ source.unindent!
+ source.should be(expect)
+ end
+ end
+end

0 comments on commit 8dae93a

Please sign in to comment.
Something went wrong with that request. Please try again.