mynyml-unindent 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +24 -0
- data/Rakefile +48 -0
- data/TODO +1 -0
- data/examples/simple.rb +15 -0
- data/examples/simple2.rb +15 -0
- data/lib/unindent.rb +11 -0
- data/test/test_helper.rb +10 -0
- data/test/test_unindent.rb +49 -0
- metadata +63 -0
data/README
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
==== Summary
|
2
|
+
|
3
|
+
Simple Ruby method to unindent strings. Useful for multiline strings embeded in already indented code.
|
4
|
+
|
5
|
+
==== Examples
|
6
|
+
|
7
|
+
class Profile
|
8
|
+
def default_text
|
9
|
+
<<-STR.unindent
|
10
|
+
Anonymous Coward
|
11
|
+
- Community Guest
|
12
|
+
STR
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
puts Profile.new.default_text
|
17
|
+
|
18
|
+
will output:
|
19
|
+
Anonymous Coward
|
20
|
+
- Community Guest
|
21
|
+
|
22
|
+
instead of:
|
23
|
+
Anonymous Coward
|
24
|
+
- Community Guest
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rake/gempackagetask'
|
2
|
+
require 'pathname'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
def gem
|
6
|
+
RUBY_1_9 ? 'gem19' : 'gem'
|
7
|
+
end
|
8
|
+
|
9
|
+
def all_except(paths)
|
10
|
+
Dir['**/*'] - paths.map {|path| path.strip.gsub(/^\//,'').gsub(/\/$/,'') }
|
11
|
+
end
|
12
|
+
|
13
|
+
spec = Gem::Specification.new do |s|
|
14
|
+
s.name = 'unindent'
|
15
|
+
s.version = '0.8.0'
|
16
|
+
s.summary = "Ruby method to unindent strings."
|
17
|
+
s.description = "Ruby method to unindent strings. Useful for multiline strings embeded in already indented code."
|
18
|
+
s.author = "Martin Aumont"
|
19
|
+
s.email = 'mynyml@gmail.com'
|
20
|
+
s.homepage = ''
|
21
|
+
s.has_rdoc = true
|
22
|
+
s.require_path = "lib"
|
23
|
+
s.files = Dir['**/*']
|
24
|
+
end
|
25
|
+
|
26
|
+
Rake::GemPackageTask.new(spec) do |p|
|
27
|
+
p.gem_spec = spec
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
desc "Remove package products"
|
32
|
+
task :clean => :clobber_package
|
33
|
+
|
34
|
+
desc "Update the gemspec for GitHub's gem server"
|
35
|
+
task :gemspec do
|
36
|
+
Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
|
37
|
+
end
|
38
|
+
|
39
|
+
desc "Install gem"
|
40
|
+
task :install => [:clobber, :package] do
|
41
|
+
sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "Uninstall gem"
|
45
|
+
task :uninstall => :clean do
|
46
|
+
sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
|
47
|
+
end
|
48
|
+
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
root = Pathname(__FILE__).dirname.parent
|
3
|
+
require root.join('lib/unindent')
|
4
|
+
|
5
|
+
class Profile
|
6
|
+
def default_text
|
7
|
+
<<-STR
|
8
|
+
Anonymous Coward
|
9
|
+
- Community Guest
|
10
|
+
STR
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
puts Profile.new.default_text
|
15
|
+
puts Profile.new.default_text.unindent
|
data/examples/simple2.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
root = Pathname(__FILE__).dirname.parent
|
3
|
+
require root.join('lib/unindent')
|
4
|
+
|
5
|
+
class Linker
|
6
|
+
def self.parse(text)
|
7
|
+
#text.gsub(%r%http://([^\s]+)%, <<-STR.unindent)
|
8
|
+
# <a href="http://\1">
|
9
|
+
# \1
|
10
|
+
# </a>
|
11
|
+
#STR
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
puts Linker.parse("find all you need at http://example.com")
|
data/lib/unindent.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module Unindentation
|
2
|
+
def unindent
|
3
|
+
indent = self.select {|line| !line.strip.empty? }.map {|line| line.index(/[^\s]/) }.compact.min
|
4
|
+
self.gsub(/^[[:blank:]]{#{indent}}/, '')
|
5
|
+
end
|
6
|
+
def unindent!
|
7
|
+
self.replace(self.unindent)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
String.class_eval { include Unindentation }
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
root = Pathname(__FILE__).dirname.parent
|
3
|
+
require root.join('test/test_helper')
|
4
|
+
require root.join('lib/unindent')
|
5
|
+
|
6
|
+
class UnindentTest < Test::Unit::TestCase
|
7
|
+
context "Unindent" do
|
8
|
+
context "simple indentation" do
|
9
|
+
test "removes space indentation" do
|
10
|
+
source = "\s\sabc"
|
11
|
+
expect = "abc"
|
12
|
+
source.unindent.should be(expect)
|
13
|
+
end
|
14
|
+
test "removes tab indentation" do
|
15
|
+
source = "\tabc"
|
16
|
+
expect = "abc"
|
17
|
+
source.unindent.should be(expect)
|
18
|
+
end
|
19
|
+
test "removes space/tab indentation" do
|
20
|
+
source = "\t\s\sabc"
|
21
|
+
expect = "abc"
|
22
|
+
source.unindent.should be(expect)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context "multiple lines" do
|
26
|
+
test "removes indentation" do
|
27
|
+
source = "\tabc\n\tabc"
|
28
|
+
expect = "abc\nabc"
|
29
|
+
source.unindent.should be(expect)
|
30
|
+
end
|
31
|
+
test "keeps relative indentation" do
|
32
|
+
source = "\tabc\n\t\tabc"
|
33
|
+
expect = "abc\n\tabc"
|
34
|
+
source.unindent.should be(expect)
|
35
|
+
end
|
36
|
+
test "ignores blank lines for indent calculation" do
|
37
|
+
source = "\n\tabc\n\n\t\tabc\n"
|
38
|
+
expect = "\nabc\n\n\tabc\n"
|
39
|
+
source.unindent.should be(expect)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
test "modifies string in-place" do
|
43
|
+
source = "\s\sabc"
|
44
|
+
expect = "abc"
|
45
|
+
source.unindent!
|
46
|
+
source.should be(expect)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mynyml-unindent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-14 21:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Ruby method to unindent strings. Useful for multiline strings embeded in already indented code.
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib
|
26
|
+
- lib/unindent.rb
|
27
|
+
- examples
|
28
|
+
- examples/simple.rb
|
29
|
+
- examples/simple2.rb
|
30
|
+
- README
|
31
|
+
- Rakefile
|
32
|
+
- test
|
33
|
+
- test/test_helper.rb
|
34
|
+
- test/test_unindent.rb
|
35
|
+
- TODO
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: ""
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: Ruby method to unindent strings.
|
62
|
+
test_files: []
|
63
|
+
|