polymer 1.0.0.beta.6 → 1.0.0.beta.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +3 -0
- data/README.md +5 -10
- data/Rakefile +1 -1
- data/lib/polymer.rb +16 -2
- data/lib/polymer/cli.rb +7 -2
- data/lib/polymer/deviant_finder.rb +3 -3
- data/lib/polymer/dsl.rb +1 -1
- data/lib/polymer/man/polymer-bond.1 +1 -1
- data/lib/polymer/man/polymer-bond.1.txt +1 -1
- data/lib/polymer/man/polymer-init.1 +1 -1
- data/lib/polymer/man/polymer-init.1.txt +1 -1
- data/lib/polymer/man/polymer-optimise.1 +1 -1
- data/lib/polymer/man/polymer-optimise.1.txt +1 -1
- data/lib/polymer/man/polymer-position.1 +1 -1
- data/lib/polymer/man/polymer-position.1.txt +1 -1
- data/lib/polymer/man/polymer.1 +1 -1
- data/lib/polymer/man/polymer.1.txt +1 -1
- data/lib/polymer/man/polymer.5 +1 -1
- data/lib/polymer/man/polymer.5.txt +1 -1
- data/lib/polymer/optimisation.rb +1 -1
- data/lib/polymer/project.rb +5 -1
- data/lib/polymer/sass_generator.rb +3 -9
- data/lib/polymer/source.rb +3 -3
- data/lib/polymer/sprite.rb +23 -24
- data/lib/polymer/version.rb +1 -1
- data/polymer.gemspec +8 -9
- metadata +7 -17
- data/lib/polymer/core_ext.rb +0 -78
data/History.md
CHANGED
@@ -4,6 +4,9 @@ v1.0.0 / HEAD (Unreleased)
|
|
4
4
|
* Montage has been renamed to Polymer and is now released under the BSD
|
5
5
|
three-clause license.
|
6
6
|
|
7
|
+
* RMagick has been replaced with ChunkyPNG; any GIF or JPEG source files
|
8
|
+
should be converted to PNG.
|
9
|
+
|
7
10
|
* The Polymer configuration must now be located at the project root and
|
8
11
|
should be named either ".polymer" or, for Windows users, "polymer.yml".
|
9
12
|
You should rename your existing ".montage" file to ".polymer", and
|
data/README.md
CHANGED
@@ -26,15 +26,10 @@ The recommended way to install Polymer is with Rubygems:
|
|
26
26
|
|
27
27
|
$ [sudo] gem install polymer
|
28
28
|
|
29
|
-
Polymer
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
Most Linux and BSD distributions include ImageMagick in their packaging
|
34
|
-
system; while Mac OS X users can install it with [Homebrew][homebrew]:
|
35
|
-
|
36
|
-
$ brew install ghostscript
|
37
|
-
$ brew install imagemagick
|
29
|
+
Polymer uses ChunkyPNG to read source files, and write the final
|
30
|
+
sprites; as a pure-Ruby library it is much easier to install than
|
31
|
+
RMagick, while not being so much slower as to pose a problem
|
32
|
+
(optimisation with PNGOut takes far longer than creating the sprite).
|
38
33
|
|
39
34
|
If you wish to install Polymer from source:
|
40
35
|
|
@@ -60,7 +55,7 @@ running `polymer init`.
|
|
60
55
|
supported options. `polymer init` also places some example source
|
61
56
|
images into your project.
|
62
57
|
|
63
|
-
2. Run `polymer
|
58
|
+
2. Run `polymer bon)`: this is the main task which converts the source
|
64
59
|
images into the final sprites. If you have PNGOUT, OptiPNG, or
|
65
60
|
PNGCrush installed, Polymer will also optimise the generates sprites
|
66
61
|
to reduce them to the smallest possible filesize.
|
data/Rakefile
CHANGED
data/lib/polymer.rb
CHANGED
@@ -4,11 +4,15 @@ require 'pathname'
|
|
4
4
|
require 'yaml'
|
5
5
|
|
6
6
|
# Gems.
|
7
|
-
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'oily_png' # Load the C version of ChunkyPNG when available.
|
10
|
+
rescue LoadError
|
11
|
+
require 'chunky_png' # Fall back to pure-Ruby.
|
12
|
+
end
|
8
13
|
|
9
14
|
# On with the library...
|
10
15
|
require 'polymer/cache'
|
11
|
-
require 'polymer/core_ext'
|
12
16
|
require 'polymer/css_generator'
|
13
17
|
require 'polymer/deviant_finder'
|
14
18
|
require 'polymer/dsl'
|
@@ -46,4 +50,14 @@ module Polymer
|
|
46
50
|
|
47
51
|
# Raised when sprite is defined with a name which has already been used.
|
48
52
|
DuplicateName = Class.new(DslError)
|
53
|
+
|
54
|
+
# A utility method; given a string, removes leading and trailing whitespace
|
55
|
+
# from each line, then joins all the lines into one.
|
56
|
+
#
|
57
|
+
# @param [String] str A string
|
58
|
+
# @return [String]
|
59
|
+
#
|
60
|
+
def self.compress_lines(str)
|
61
|
+
str.split($/).map { |line| line.strip }.join(' ')
|
62
|
+
end
|
49
63
|
end
|
data/lib/polymer/cli.rb
CHANGED
@@ -86,8 +86,13 @@ module Polymer
|
|
86
86
|
# Finish by writing the new cache.
|
87
87
|
project.cache.write
|
88
88
|
|
89
|
+
# Clean up temporary directories from data URI sprites.
|
90
|
+
if project.data_uri_sprites.any?
|
91
|
+
FileUtils.remove_entry_secure(project.tmpdir)
|
92
|
+
end
|
93
|
+
|
89
94
|
rescue Polymer::MissingSource, Polymer::TargetNotWritable => e
|
90
|
-
say e.message
|
95
|
+
say Polymer.compress_lines(e.message), :red
|
91
96
|
exit 1
|
92
97
|
end
|
93
98
|
|
@@ -302,7 +307,7 @@ module Polymer
|
|
302
307
|
def find_project!
|
303
308
|
find_project
|
304
309
|
rescue Polymer::MissingProject
|
305
|
-
say <<-ERROR
|
310
|
+
say Polymer.compress_lines(<<-ERROR), :red
|
306
311
|
Couldn't find a Polymer project in the current directory, or any of
|
307
312
|
the parent directories. Run "polymer init" if you want to create a new
|
308
313
|
project here.
|
@@ -21,13 +21,13 @@ module Polymer
|
|
21
21
|
return false if sprite.sources.size < 2
|
22
22
|
|
23
23
|
mean, std_dev = standard_deviation(sprite.sources.map do |source|
|
24
|
-
source.image.
|
24
|
+
source.image.width
|
25
25
|
end)
|
26
26
|
|
27
27
|
return false if std_dev < 100 # Skip images with a < 100px deviation.
|
28
28
|
|
29
29
|
deviants = sprite.sources.select do |source|
|
30
|
-
width = source.image.
|
30
|
+
width = source.image.width
|
31
31
|
width > mean + std_dev || width < mean - std_dev
|
32
32
|
end
|
33
33
|
|
@@ -37,7 +37,7 @@ module Polymer
|
|
37
37
|
# Print a warning if the sprite contains wide sources.
|
38
38
|
def self.format_ui_message(sprite, deviants)
|
39
39
|
if deviants
|
40
|
-
<<-MESSAGE
|
40
|
+
Polymer.compress_lines(<<-MESSAGE)
|
41
41
|
Your "#{sprite.name}" sprite contains one or more source images
|
42
42
|
which deviate significantly from the average source width. You might
|
43
43
|
want to consider removing these sources from the sprite in order to
|
data/lib/polymer/dsl.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
3
3
|
.ad l
|
4
4
|
.
|
5
|
-
.TH "POLYMER\-BOND" "1" "
|
5
|
+
.TH "POLYMER\-BOND" "1" "February 2011" "POLYMER 1.0.0.BETA.7" "Polymer Manual"
|
6
6
|
.
|
7
7
|
.SH "NAME"
|
8
8
|
\fBpolymer\-bond\fR \- Create sprite images defined in your \.polymer file
|
@@ -2,7 +2,7 @@
|
|
2
2
|
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
3
3
|
.ad l
|
4
4
|
.
|
5
|
-
.TH "POLYMER\-INIT" "1" "
|
5
|
+
.TH "POLYMER\-INIT" "1" "February 2011" "POLYMER 1.0.0.BETA.7" "Polymer Manual"
|
6
6
|
.
|
7
7
|
.SH "NAME"
|
8
8
|
\fBpolymer\-init\fR \- Create a new Polymer project in the current directory
|
@@ -2,7 +2,7 @@
|
|
2
2
|
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
3
3
|
.ad l
|
4
4
|
.
|
5
|
-
.TH "POLYMER\-OPTIMISE" "1" "
|
5
|
+
.TH "POLYMER\-OPTIMISE" "1" "February 2011" "POLYMER 1.0.0.BETA.7" "Polymer Manual"
|
6
6
|
.
|
7
7
|
.SH "NAME"
|
8
8
|
\fBpolymer\-optimise\fR \- Optimise PNG images
|
@@ -2,7 +2,7 @@
|
|
2
2
|
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
3
3
|
.ad l
|
4
4
|
.
|
5
|
-
.TH "POLYMER\-POSITION" "1" "
|
5
|
+
.TH "POLYMER\-POSITION" "1" "February 2011" "POLYMER 1.0.0.BETA.7" "Polymer Manual"
|
6
6
|
.
|
7
7
|
.SH "NAME"
|
8
8
|
\fBpolymer\-position\fR \- Information about your sprite sources
|
data/lib/polymer/man/polymer.1
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
3
3
|
.ad l
|
4
4
|
.
|
5
|
-
.TH "POLYMER" "1" "
|
5
|
+
.TH "POLYMER" "1" "February 2011" "POLYMER 1.0.0.BETA.7" "Polymer Manual"
|
6
6
|
.
|
7
7
|
.SH "NAME"
|
8
8
|
\fBpolymer\fR \- Image spriting for web applications
|
data/lib/polymer/man/polymer.5
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
3
3
|
.ad l
|
4
4
|
.
|
5
|
-
.TH "\.POLYMER" "5" "
|
5
|
+
.TH "\.POLYMER" "5" "February 2011" "POLYMER 1.0.0.BETA.7" "Polymer Manual"
|
6
6
|
.
|
7
7
|
.SH "NAME"
|
8
8
|
\fB\.polymer\fR \- a format for describing image sprites and their sources
|
data/lib/polymer/optimisation.rb
CHANGED
@@ -20,7 +20,7 @@ module Polymer
|
|
20
20
|
# @param [Pathname] path
|
21
21
|
# Path to the file to be optimised.
|
22
22
|
#
|
23
|
-
# @return [Integer
|
23
|
+
# @return [Integer]
|
24
24
|
# Returns the number of bytes by which the filesize was reduced.
|
25
25
|
# @return [false]
|
26
26
|
# Returns false if the current machine has no optimisers available.
|
data/lib/polymer/project.rb
CHANGED
@@ -23,7 +23,7 @@ module Polymer
|
|
23
23
|
#
|
24
24
|
attr_reader :root
|
25
25
|
|
26
|
-
# @return [Pathname
|
26
|
+
# @return [Pathname]
|
27
27
|
# The path to the Sass mixin file.
|
28
28
|
# @return [false]
|
29
29
|
# False if Sass has been disabled.
|
@@ -87,6 +87,10 @@ module Polymer
|
|
87
87
|
@css = extract_path :css, options
|
88
88
|
@cachefile = extract_path :cache, options
|
89
89
|
|
90
|
+
if @sass and @sass.to_s[-5..-1] != '.sass'
|
91
|
+
@sass = @sass + '_polymer.sass'
|
92
|
+
end
|
93
|
+
|
90
94
|
# Sprites which are to be saved as a data URI need to have a save
|
91
95
|
# path explicitly set.
|
92
96
|
@data_uri_sprites = @sprites.select do |sprite|
|
@@ -19,18 +19,12 @@ module Polymer
|
|
19
19
|
def self.generate(project)
|
20
20
|
return false unless project.sass
|
21
21
|
|
22
|
-
|
23
|
-
project.sass.dirname.mkpath
|
24
|
-
save_to = project.sass
|
25
|
-
else
|
26
|
-
project.sass.mkpath
|
27
|
-
save_to = project.sass + '_polymer.sass'
|
28
|
-
end
|
22
|
+
project.sass.dirname.mkpath
|
29
23
|
|
30
24
|
# We need to keep track of any existing data URI values since, if the
|
31
25
|
# sprite is unchanged, we won't have access to it.
|
32
26
|
existing_data_uris = extract_existing_data_uris(
|
33
|
-
|
27
|
+
project.sass, project.data_uri_sprites)
|
34
28
|
|
35
29
|
data_uris = project.data_uri_sprites.inject({}) do |memo, sprite|
|
36
30
|
if sprite.save_path.file?
|
@@ -48,7 +42,7 @@ module Polymer
|
|
48
42
|
memo
|
49
43
|
end
|
50
44
|
|
51
|
-
File.open(
|
45
|
+
File.open(project.sass, 'w') do |file|
|
52
46
|
file.puts ERB.new(File.read(TEMPLATE), nil, '<>').result(binding)
|
53
47
|
end
|
54
48
|
|
data/lib/polymer/source.rb
CHANGED
@@ -19,13 +19,13 @@ module Polymer
|
|
19
19
|
@name = @path.basename(@path.extname).to_s
|
20
20
|
end
|
21
21
|
|
22
|
-
# Returns the
|
22
|
+
# Returns the Image instance representing the source.
|
23
23
|
#
|
24
|
-
# @return [
|
24
|
+
# @return [ChunkyPNG::Image]
|
25
25
|
#
|
26
26
|
def image
|
27
27
|
assert_file!
|
28
|
-
@image ||=
|
28
|
+
@image ||= ChunkyPNG::Image.from_file(@path)
|
29
29
|
end
|
30
30
|
|
31
31
|
# Returns a digest which represents the sprite name and file contents.
|
data/lib/polymer/sprite.rb
CHANGED
@@ -44,8 +44,10 @@ module Polymer
|
|
44
44
|
# The name of the source whose position is to be returned, or the
|
45
45
|
# Polymer::Source instance itself.
|
46
46
|
#
|
47
|
-
# @return [Integer
|
47
|
+
# @return [Integer]
|
48
48
|
# The vertical position of the source image.
|
49
|
+
# @return [nil]
|
50
|
+
# nil is returned if no sprite identified by +name+ exists.
|
49
51
|
#
|
50
52
|
def position_of(name)
|
51
53
|
name = name.name if name.is_a?(Polymer::Source)
|
@@ -64,7 +66,7 @@ module Polymer
|
|
64
66
|
@positions = {}
|
65
67
|
@sources.inject(0) do |offset, src|
|
66
68
|
@positions[src.name] = offset
|
67
|
-
offset + src.image.
|
69
|
+
offset + src.image.height + @padding
|
68
70
|
end
|
69
71
|
end
|
70
72
|
|
@@ -105,34 +107,31 @@ module Polymer
|
|
105
107
|
ERROR
|
106
108
|
end
|
107
109
|
|
108
|
-
|
110
|
+
width, height = @sources.inject([1, 0]) do |dimensions, source|
|
111
|
+
# Determine the width of the sprite by finding the widest source.
|
112
|
+
source_width = source.image.width
|
113
|
+
dimensions[0] = source_width if source_width > dimensions[0]
|
109
114
|
|
110
|
-
|
111
|
-
|
115
|
+
# Determine the height of the sprite by summing the height of each
|
116
|
+
# source.
|
117
|
+
dimensions[1] += source.image.height
|
112
118
|
|
113
|
-
|
114
|
-
list << Magick::Image.new(1, @padding) do
|
115
|
-
self.background_color = '#F000'
|
116
|
-
end
|
117
|
-
end
|
119
|
+
dimensions
|
118
120
|
end
|
119
121
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
self.tile = Magick::Geometry.new(1, sources_length * 2)
|
122
|
+
if @padding and @padding > 0
|
123
|
+
# Adjust the height to account for padding.
|
124
|
+
height += (@sources.length - 1) * @padding
|
125
|
+
end
|
126
|
+
|
127
|
+
canvas = ChunkyPNG::Canvas.new(width, height)
|
128
|
+
|
129
|
+
# Add each source to the canvas.
|
130
|
+
@sources.each do |source|
|
131
|
+
canvas.compose(source.image, 0, position_of(source))
|
131
132
|
end
|
132
133
|
|
133
|
-
|
134
|
-
montage.crop!(0, 0, 0, (montage.first.rows) - @padding)
|
135
|
-
montage.write("PNG32:#{@save_path}")
|
134
|
+
canvas.save(@save_path, :best_compression)
|
136
135
|
|
137
136
|
true
|
138
137
|
end
|
data/lib/polymer/version.rb
CHANGED
data/polymer.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
# rake task. It it completely safe to edit them, but using the rake task
|
9
9
|
# is easier.
|
10
10
|
s.name = 'polymer'
|
11
|
-
s.version = '1.0.0.beta.
|
12
|
-
s.date = '
|
11
|
+
s.version = '1.0.0.beta.7'
|
12
|
+
s.date = '2011-02-02'
|
13
13
|
s.rubyforge_project = 'polymer'
|
14
14
|
|
15
15
|
# You may safely edit the section below.
|
@@ -29,12 +29,12 @@ Gem::Specification.new do |s|
|
|
29
29
|
s.executables = ['polymer']
|
30
30
|
s.require_path = 'lib'
|
31
31
|
|
32
|
-
s.add_runtime_dependency '
|
33
|
-
s.add_runtime_dependency 'thor',
|
34
|
-
s.add_development_dependency 'rspec',
|
35
|
-
s.add_development_dependency 'cucumber',
|
36
|
-
s.add_development_dependency 'haml',
|
37
|
-
s.add_development_dependency 'ronn',
|
32
|
+
s.add_runtime_dependency 'chunky_png', '>= 0.12'
|
33
|
+
s.add_runtime_dependency 'thor', '>= 0.14.0'
|
34
|
+
s.add_development_dependency 'rspec', '>= 2.0.0.beta.19'
|
35
|
+
s.add_development_dependency 'cucumber', '>= 0.8.5'
|
36
|
+
s.add_development_dependency 'haml', '>= 3.0.18'
|
37
|
+
s.add_development_dependency 'ronn', '>= 0.7.3'
|
38
38
|
|
39
39
|
# The manifest is created by the "gemspec" rake task. Do not edit it
|
40
40
|
# directly; your changes will be wiped out when you next run the task.
|
@@ -50,7 +50,6 @@ Gem::Specification.new do |s|
|
|
50
50
|
lib/polymer.rb
|
51
51
|
lib/polymer/cache.rb
|
52
52
|
lib/polymer/cli.rb
|
53
|
-
lib/polymer/core_ext.rb
|
54
53
|
lib/polymer/css_generator.rb
|
55
54
|
lib/polymer/deviant_finder.rb
|
56
55
|
lib/polymer/dsl.rb
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 62196367
|
5
4
|
prerelease: true
|
6
5
|
segments:
|
7
6
|
- 1
|
8
7
|
- 0
|
9
8
|
- 0
|
10
9
|
- beta
|
11
|
-
-
|
12
|
-
version: 1.0.0.beta.
|
10
|
+
- 7
|
11
|
+
version: 1.0.0.beta.7
|
13
12
|
platform: ruby
|
14
13
|
authors:
|
15
14
|
- Anthony Williams
|
@@ -17,22 +16,21 @@ autorequire:
|
|
17
16
|
bindir: bin
|
18
17
|
cert_chain: []
|
19
18
|
|
20
|
-
date:
|
19
|
+
date: 2011-02-02 00:00:00 +00:00
|
21
20
|
default_executable:
|
22
21
|
dependencies:
|
23
22
|
- !ruby/object:Gem::Dependency
|
24
|
-
name:
|
23
|
+
name: chunky_png
|
25
24
|
prerelease: false
|
26
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
27
26
|
none: false
|
28
27
|
requirements:
|
29
28
|
- - ">="
|
30
29
|
- !ruby/object:Gem::Version
|
31
|
-
hash: 25
|
32
30
|
segments:
|
33
|
-
-
|
34
|
-
-
|
35
|
-
version: "
|
31
|
+
- 0
|
32
|
+
- 12
|
33
|
+
version: "0.12"
|
36
34
|
type: :runtime
|
37
35
|
version_requirements: *id001
|
38
36
|
- !ruby/object:Gem::Dependency
|
@@ -43,7 +41,6 @@ dependencies:
|
|
43
41
|
requirements:
|
44
42
|
- - ">="
|
45
43
|
- !ruby/object:Gem::Version
|
46
|
-
hash: 39
|
47
44
|
segments:
|
48
45
|
- 0
|
49
46
|
- 14
|
@@ -59,7 +56,6 @@ dependencies:
|
|
59
56
|
requirements:
|
60
57
|
- - ">="
|
61
58
|
- !ruby/object:Gem::Version
|
62
|
-
hash: 62196421
|
63
59
|
segments:
|
64
60
|
- 2
|
65
61
|
- 0
|
@@ -77,7 +73,6 @@ dependencies:
|
|
77
73
|
requirements:
|
78
74
|
- - ">="
|
79
75
|
- !ruby/object:Gem::Version
|
80
|
-
hash: 53
|
81
76
|
segments:
|
82
77
|
- 0
|
83
78
|
- 8
|
@@ -93,7 +88,6 @@ dependencies:
|
|
93
88
|
requirements:
|
94
89
|
- - ">="
|
95
90
|
- !ruby/object:Gem::Version
|
96
|
-
hash: 35
|
97
91
|
segments:
|
98
92
|
- 3
|
99
93
|
- 0
|
@@ -109,7 +103,6 @@ dependencies:
|
|
109
103
|
requirements:
|
110
104
|
- - ">="
|
111
105
|
- !ruby/object:Gem::Version
|
112
|
-
hash: 5
|
113
106
|
segments:
|
114
107
|
- 0
|
115
108
|
- 7
|
@@ -138,7 +131,6 @@ files:
|
|
138
131
|
- lib/polymer.rb
|
139
132
|
- lib/polymer/cache.rb
|
140
133
|
- lib/polymer/cli.rb
|
141
|
-
- lib/polymer/core_ext.rb
|
142
134
|
- lib/polymer/css_generator.rb
|
143
135
|
- lib/polymer/deviant_finder.rb
|
144
136
|
- lib/polymer/dsl.rb
|
@@ -189,7 +181,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
189
181
|
requirements:
|
190
182
|
- - ">="
|
191
183
|
- !ruby/object:Gem::Version
|
192
|
-
hash: 3
|
193
184
|
segments:
|
194
185
|
- 0
|
195
186
|
version: "0"
|
@@ -198,7 +189,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
198
189
|
requirements:
|
199
190
|
- - ">"
|
200
191
|
- !ruby/object:Gem::Version
|
201
|
-
hash: 25
|
202
192
|
segments:
|
203
193
|
- 1
|
204
194
|
- 3
|
data/lib/polymer/core_ext.rb
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
# Some simple string extensions to make things easier.
|
2
|
-
class String
|
3
|
-
|
4
|
-
# Replace sequences of whitespace (including newlines) with either
|
5
|
-
# a single space or remove them entirely (according to param _spaced_)
|
6
|
-
#
|
7
|
-
# <<QUERY.compress_lines
|
8
|
-
# SELECT name
|
9
|
-
# FROM users
|
10
|
-
# QUERY => "SELECT name FROM users"
|
11
|
-
#
|
12
|
-
# @return [String] Receiver with whitespace (including newlines) replaced
|
13
|
-
#
|
14
|
-
def compress_lines
|
15
|
-
split($/).map { |line| line.strip }.join(' ')
|
16
|
-
end
|
17
|
-
|
18
|
-
# Removes leading whitespace from each line, such as might be added when
|
19
|
-
# using a HEREDOC string.
|
20
|
-
#
|
21
|
-
# @return [String] Receiver with leading whitespace removed.
|
22
|
-
#
|
23
|
-
def unindent
|
24
|
-
(other = dup) and other.unindent! and other
|
25
|
-
end
|
26
|
-
|
27
|
-
# Bang version of #unindent.
|
28
|
-
#
|
29
|
-
# @return [String] Receiver with leading whitespace removed.
|
30
|
-
#
|
31
|
-
def unindent!
|
32
|
-
gsub!(/^[ \t]{#{minimum_leading_whitespace}}/, '')
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
# Checks each line and determines the minimum amount of leading whitespace.
|
38
|
-
#
|
39
|
-
# @return [Integer] The number of leading whitespace characters.
|
40
|
-
#
|
41
|
-
def minimum_leading_whitespace
|
42
|
-
whitespace = split("\n", -1).inject(0) do |indent, line|
|
43
|
-
if line.strip.empty?
|
44
|
-
indent # Ignore completely blank lines.
|
45
|
-
elsif line =~ /^(\s+)/
|
46
|
-
(1.0 / $1.length) > indent ? 1.0 / $1.length : indent
|
47
|
-
else
|
48
|
-
1.0
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
whitespace == 1.0 ? 0 : (1.0 / whitespace).to_i
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
# String#compress_lines is extracted from the extlib gem
|
58
|
-
# ------------------------------------------------------
|
59
|
-
#
|
60
|
-
# Copyright (c) 2009 Dan Kubb
|
61
|
-
#
|
62
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
63
|
-
# of this software and associated documentation files (the "Software"), to
|
64
|
-
# deal in the Software without restriction, including without limitation the
|
65
|
-
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
66
|
-
# sell copies of the Software, and to permit persons to whom the Software is
|
67
|
-
# furnished to do so, subject to the following conditions:
|
68
|
-
#
|
69
|
-
# The above copyright notice and this permission notice shall be included in
|
70
|
-
# all copies or substantial portions of the Software.
|
71
|
-
#
|
72
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
73
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
74
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
75
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
76
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
77
|
-
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
78
|
-
# IN THE SOFTWARE.
|