dedent 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/dedent.gemspec +26 -0
- data/lib/dedent.rb +11 -0
- data/lib/dedent/version.rb +3 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
ruby-dedent
|
2
|
+
===========
|
3
|
+
|
4
|
+
Installation
|
5
|
+
------------
|
6
|
+
|
7
|
+
gem install dedent
|
8
|
+
|
9
|
+
Only tested with Ruby 1.9.2 and 1.8.7.
|
10
|
+
|
11
|
+
Usage
|
12
|
+
-----
|
13
|
+
|
14
|
+
This is a tiny gem that adds the `dedent` method to String. This method strips leading whitespace from each
|
15
|
+
line, preserving indentation. So, the line with the least amount of leading whitespace will have all leading
|
16
|
+
whitespace removed, while other lines will have whitespace relative to that line's offset.
|
17
|
+
|
18
|
+
The main use of this is with heredocs. It can be annoying because even with the `<<-` form of heredocs, all
|
19
|
+
the initial whitespace on each line of the string is preserved, so if you keep the string in the same
|
20
|
+
indentation as the code, you end up with a bunch of extra space.
|
21
|
+
|
22
|
+
Here's an example of using dedent with heredocs:
|
23
|
+
|
24
|
+
``` ruby
|
25
|
+
# Suppose we're already at some indentation within the code.
|
26
|
+
foo = <<-EOS.dedent
|
27
|
+
This might be a usage banner for a cli tool. Or, it might be some javascript. Or maybe shell code.
|
28
|
+
for f in $(ls /foo/bar/); do
|
29
|
+
cat $f >> $myfile;
|
30
|
+
done
|
31
|
+
EOS
|
32
|
+
```
|
33
|
+
|
34
|
+
Now the `foo` variable contains the following string:
|
35
|
+
|
36
|
+
```
|
37
|
+
This might be a usage banner for a cli tool. Or, it might be some javascript. Or maybe shell code.
|
38
|
+
for f in $(ls /foo/bar/); do
|
39
|
+
cat $f >> $myfile;
|
40
|
+
done
|
41
|
+
```
|
42
|
+
|
43
|
+
Author
|
44
|
+
------
|
45
|
+
|
46
|
+
This gem was written by Caleb Spare ([cespare](https://github.com/cespare) on github). It was inspired by
|
47
|
+
[Coffeescript's heredocs](http://jashkenas.github.com/coffee-script/#strings).
|
48
|
+
|
49
|
+
License
|
50
|
+
-------
|
51
|
+
|
52
|
+
This project is release under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/dedent.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "dedent/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "dedent"
|
7
|
+
s.version = Dedent::VERSION
|
8
|
+
s.authors = ["Caleb Spare"]
|
9
|
+
s.email = ["cespare@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/cespare/ruby-dedent"
|
11
|
+
s.summary = %q{Adds a dedent method to String.}
|
12
|
+
s.description = <<-EOS
|
13
|
+
This gem adds a dedent method to strings to strip leading spaces from each line while preserving indentation.
|
14
|
+
EOS
|
15
|
+
|
16
|
+
s.rubyforge_project = "dedent"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
# s.add_development_dependency "rspec"
|
25
|
+
# s.add_runtime_dependency "rest-client"
|
26
|
+
end
|
data/lib/dedent.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "dedent/version"
|
2
|
+
|
3
|
+
class String
|
4
|
+
def dedent
|
5
|
+
lines = split "\n"
|
6
|
+
return self if lines.empty?
|
7
|
+
min_indent = lines.map { |line| line.start_with?(" ") ? line.match(/^ +/).offset(0)[1] : 0 }.min
|
8
|
+
return self if min_indent.zero?
|
9
|
+
lines.map { |line| line.gsub(/^ {#{min_indent}}/, "") }.join("\n")
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dedent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Caleb Spare
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-10 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'This gem adds a dedent method to strings to strip leading spaces from
|
15
|
+
each line while preserving indentation.
|
16
|
+
|
17
|
+
'
|
18
|
+
email:
|
19
|
+
- cespare@gmail.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- .gitignore
|
25
|
+
- Gemfile
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- dedent.gemspec
|
29
|
+
- lib/dedent.rb
|
30
|
+
- lib/dedent/version.rb
|
31
|
+
homepage: https://github.com/cespare/ruby-dedent
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project: dedent
|
51
|
+
rubygems_version: 1.8.10
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Adds a dedent method to String.
|
55
|
+
test_files: []
|