nutils 0.5.5 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/nutils/filters/crop.rb +51 -0
- data/lib/nutils/filters/sass.rb +82 -0
- data/lib/nutils/filters/svg2png.rb +20 -6
- data/lib/nutils/filters.rb +2 -0
- data/lib/nutils.rb +1 -1
- metadata +9 -7
data/README.md
CHANGED
@@ -5,6 +5,7 @@ A set of utilities for Nanoc3.
|
|
5
5
|
## Filters
|
6
6
|
|
7
7
|
* **beautify** Retabs HTML code. Tested in Mac OS X 10.6, Ruby 1.8.7 and Nanoc 3.1.6.
|
8
|
+
* **crop** Extracts the specified rectangle from the image.. Tested in Mac OS X 10.6, Ruby 1.8.7 and Nanoc 3.1.6.
|
8
9
|
* **sprockets** Runs the content through [Sprockets](http://getsprockets.org)
|
9
10
|
* **svg_to_png** Converts an SVG to PNG. Tested in Mac OS X 10.6, Ruby 1.8.7, Nanoc 3.1.6 and Batik 1.7.
|
10
11
|
* **yuicompressor** Compress the content with [YUI Compressor](http://developer.yahoo.com/yui/compressor/). Tested in Mac OS X 10.6, Ruby 1.8.7, Nanoc 3.1.6 and yui-compressor 0.9.1.
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Nutils
|
2
|
+
module Filters
|
3
|
+
|
4
|
+
# @author Arnau Siches
|
5
|
+
#
|
6
|
+
# @version 0.1.0
|
7
|
+
#
|
8
|
+
# @note Requires «RMagick»
|
9
|
+
class Crop < Nanoc3::Filter
|
10
|
+
|
11
|
+
identifier :crop
|
12
|
+
type :binary
|
13
|
+
|
14
|
+
# Runs the content through {http://www.simplesystems.org/RMagick/doc/image1.html#crop RMagick#crop}.
|
15
|
+
#
|
16
|
+
# @param [String] filename The filename of the temporal file to filter.
|
17
|
+
#
|
18
|
+
# @option params [Integer] :x The x-offset of the rectangle relative
|
19
|
+
# to the upper-left corner of the image.
|
20
|
+
#
|
21
|
+
# @option params [Integer] :y The y-offset of the rectangle relative
|
22
|
+
# to the upper-left corner of the image.
|
23
|
+
#
|
24
|
+
# @option params [String] :width The width of the rectangle. This param can
|
25
|
+
# handle string-expressions like +"image.columns - 1"+ where +image.columns+
|
26
|
+
# is the width of the input image.
|
27
|
+
#
|
28
|
+
# @option params [String] :height The height of the rectangle. This param can
|
29
|
+
# handle string-expressions like +"image.rows - 1"+ where +image.rows+
|
30
|
+
# is the height of the input image.
|
31
|
+
#
|
32
|
+
# @return [File] The filtered content.
|
33
|
+
def run(filename, opts = {})
|
34
|
+
require 'RMagick'
|
35
|
+
|
36
|
+
raise ArgumentError, "x, y, width and height are required parameters." unless opts[:x] and opts[:y] and opts[:width] and opts[:height]
|
37
|
+
|
38
|
+
image = Magick::ImageList.new filename
|
39
|
+
|
40
|
+
width = eval(opts[:width])
|
41
|
+
height = eval(opts[:height])
|
42
|
+
|
43
|
+
image.crop(opts[:x], opts[:y], width, height).write output_filename
|
44
|
+
|
45
|
+
rescue Exception => e
|
46
|
+
raise ArgumentError, "#{e}\nSome parameters cannot be promoted to Integer."
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nutils
|
4
|
+
module Filters
|
5
|
+
class Sass < Nanoc3::Filters::Sass
|
6
|
+
identifier :zass
|
7
|
+
type :text
|
8
|
+
|
9
|
+
# Runs the content through [Sass](http://sass-lang.com/).
|
10
|
+
# Parameters passed to this filter will be passed on to Sass.
|
11
|
+
#
|
12
|
+
# @param [String] content The content to filter
|
13
|
+
#
|
14
|
+
# @return [String] The filtered content
|
15
|
+
def run(content, params={})
|
16
|
+
require 'sass'
|
17
|
+
|
18
|
+
# Add imported_filename read accessor to ImportNode
|
19
|
+
# … but… but… nex3 said I could monkey patch it! :(
|
20
|
+
methods = ::Sass::Tree::ImportNode.instance_methods
|
21
|
+
if !methods.include?(:import_filename) && !methods.include?('import_filename')
|
22
|
+
::Sass::Tree::ImportNode.send(:attr_reader, :imported_filename)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get options
|
26
|
+
options = params.dup
|
27
|
+
sass_filename = options[:filename] || (@item && @item[:content_filename])
|
28
|
+
options[:filename] ||= sass_filename
|
29
|
+
|
30
|
+
# Build engine
|
31
|
+
engine = ::Sass::Engine.new(content, options)
|
32
|
+
|
33
|
+
# Get import nodes
|
34
|
+
require 'set'
|
35
|
+
imported_nodes = []
|
36
|
+
unprocessed_nodes = Set.new([ engine.to_tree ])
|
37
|
+
until unprocessed_nodes.empty?
|
38
|
+
# Get an unprocessed node
|
39
|
+
node = unprocessed_nodes.each { |n| break n }
|
40
|
+
unprocessed_nodes.delete(node)
|
41
|
+
|
42
|
+
# Add to list of import nodes if necessary
|
43
|
+
imported_nodes << node if node.is_a?(::Sass::Tree::ImportNode)
|
44
|
+
|
45
|
+
# Mark children of this node for processing
|
46
|
+
node.children.each { |c| unprocessed_nodes << c }
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get import paths
|
50
|
+
import_paths = (options[:load_paths] || []).dup
|
51
|
+
import_paths.unshift(File.dirname(sass_filename)) if sass_filename
|
52
|
+
# Get imported filenames
|
53
|
+
imported_filenames = imported_nodes.map do |node|
|
54
|
+
::Sass::Files.find_file_to_import(node.imported_filename, import_paths)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Convert to items
|
58
|
+
imported_items = imported_filenames.map do |filename|
|
59
|
+
pathname = Pathname.new(filename)
|
60
|
+
next unless pathname.file?
|
61
|
+
normalized_filename = pathname.realpath
|
62
|
+
@items.find { |i| i[:content_filename] && Pathname.new(i[:content_filename]).realpath == normalized_filename }
|
63
|
+
end.compact
|
64
|
+
|
65
|
+
# Require compilation of each item
|
66
|
+
imported_items.each do |item|
|
67
|
+
# Notify
|
68
|
+
Nanoc3::NotificationCenter.post(:visit_started, item)
|
69
|
+
Nanoc3::NotificationCenter.post(:visit_ended, item)
|
70
|
+
|
71
|
+
# Raise unmet dependency error if item is not yet compiled
|
72
|
+
any_uncompiled_rep = item.reps.find { |r| !r.compiled? }
|
73
|
+
raise Nanoc3::Errors::UnmetDependency.new(any_uncompiled_rep) if any_uncompiled_rep
|
74
|
+
end
|
75
|
+
|
76
|
+
# Done
|
77
|
+
engine.render
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -4,7 +4,7 @@ module Nutils
|
|
4
4
|
# @author Arnau Siches
|
5
5
|
# @author Choan Gálvez
|
6
6
|
#
|
7
|
-
# @version 0.
|
7
|
+
# @version 0.3.0
|
8
8
|
#
|
9
9
|
# @note Requires «rjb»
|
10
10
|
class SvgToPng < Nanoc3::Filter
|
@@ -28,24 +28,31 @@ module Nutils
|
|
28
28
|
super
|
29
29
|
end
|
30
30
|
|
31
|
-
# Runs the content through
|
31
|
+
# Runs the content through {http://xmlgraphics.apache.org/batik/ Batik}.
|
32
32
|
#
|
33
33
|
# @param [String] content The content to filter.
|
34
34
|
#
|
35
35
|
# @option params [String] :background_color (nil) The background color with the
|
36
36
|
# form +#FFCC00+ or +FC0+ of the result. Mask for the
|
37
|
-
# +KEY_BACKGROUND_COLOR+ key.
|
37
|
+
# {http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/transcoder/image/ImageTranscoder.html#KEY_BACKGROUND_COLOR +KEY_BACKGROUND_COLOR+} key.
|
38
38
|
#
|
39
39
|
# @option params [Integer] :dpi (72) This parameter lets you use the pixel
|
40
40
|
# to millimeter conversion factor.
|
41
41
|
# This factor is used to determine how units are converted into pixels.
|
42
|
-
# Mask for the +KEY_PIXEL_TO_MM+ key.
|
42
|
+
# Mask for the {http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/transcoder/SVGAbstractTranscoder.html#KEY_PIXEL_TO_MM +KEY_PIXEL_TO_MM+} key.
|
43
|
+
#
|
44
|
+
# @option params [Integer] :depth (nil) The color bit depth (i.e. 1, 2, 4, 8).
|
45
|
+
# The resultant PNG will be an indexed PNG with color bit depth specified.
|
46
|
+
# The height of the result. Mask for the {http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/transcoder/image/PNGTranscoder.html#KEY_INDEXED +KEY_INDEXED+} key.
|
47
|
+
#
|
48
|
+
# @option params [Float] :gamma (0) The gamma correction of the result.
|
49
|
+
# Mask for the {http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/transcoder/image/PNGTranscoder.html#KEY_GAMMA +KEY_GAMMA+} key.
|
43
50
|
#
|
44
51
|
# @option params [Integer] :heigth (nil) The height of the result. Mask for the
|
45
|
-
# +KEY_HEIGHT+ key.
|
52
|
+
# {http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/transcoder/SVGAbstractTranscoder.html#KEY_HEIGHT +KEY_HEIGHT+} key.
|
46
53
|
#
|
47
54
|
# @option params [Integer] :width (nil) The width of the result. Mask for the
|
48
|
-
# +KEY_WIDTH+ key.
|
55
|
+
# {http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/transcoder/SVGAbstractTranscoder.html#KEY_WIDTH +KEY_WIDTH+} key.
|
49
56
|
#
|
50
57
|
# @return [String] The filtered content.
|
51
58
|
def run(content, params = {})
|
@@ -56,6 +63,8 @@ module Nutils
|
|
56
63
|
:dpi => 72,
|
57
64
|
:height => nil,
|
58
65
|
:width => nil,
|
66
|
+
:gamma => 0,
|
67
|
+
:depth => nil,
|
59
68
|
}.merge(params)
|
60
69
|
|
61
70
|
t.addTranscodingHint(@pngTranscoder.KEY_WIDTH, @float.new(opts[:width])) if opts[:width]
|
@@ -64,6 +73,10 @@ module Nutils
|
|
64
73
|
t.addTranscodingHint(@pngTranscoder.KEY_BACKGROUND_COLOR, @color.new(hex2int(opts[:background_color]))) if opts[:background_color] and opts[:background_color] != 'transparent'
|
65
74
|
# t.addTranscodingHint(@pngTranscoder.KEY_BACKGROUND_COLOR, @color.decode('#FC0'))
|
66
75
|
|
76
|
+
t.addTranscodingHint(@pngTranscoder.KEY_GAMMA, @float.new(opts[:gamma]))
|
77
|
+
t.addTranscodingHint(@pngTranscoder.KEY_INDEXED, @integer.new(opts[:depth])) if opts[:depth]
|
78
|
+
|
79
|
+
|
67
80
|
tempfile = Tempfile.new(filename.gsub(/[^a-zA-Z0-9]/, '-'), ".")
|
68
81
|
temppath = tempfile.path
|
69
82
|
tempfile.puts content
|
@@ -80,6 +93,7 @@ module Nutils
|
|
80
93
|
ostream.close()
|
81
94
|
end
|
82
95
|
|
96
|
+
private
|
83
97
|
# Converts an hexadecimal number with the format +#FC0+ or +#FFCC00+ to its integer representation.
|
84
98
|
#
|
85
99
|
# @param [String] input The hexadecimal number.
|
data/lib/nutils/filters.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
module Nutils
|
2
2
|
module Filters
|
3
3
|
autoload "Beautify", "nutils/filters/beautify"
|
4
|
+
autoload "Crop", "nutils/filters/crop"
|
4
5
|
autoload "SprocketWheel", "nutils/filters/sprockets"
|
5
6
|
autoload "SvgToPng", "nutils/filters/svg2png"
|
6
7
|
autoload "YuiCompressor", "nutils/filters/yuicompressor"
|
7
8
|
|
8
9
|
::Nanoc3::Filter.register "::Nutils::Filters::Beautify", :beautify
|
10
|
+
::Nanoc3::Filter.register "::Nutils::Filters::Crop", :crop
|
9
11
|
::Nanoc3::Filter.register "::Nutils::Filters::SprocketWheel", :sprockets
|
10
12
|
::Nanoc3::Filter.register "::Nutils::Filters::SvgToPng", :svg_to_png
|
11
13
|
::Nanoc3::Filter.register "::Nutils::Filters::YuiCompressor", :yuicompressor
|
data/lib/nutils.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 7
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 6
|
9
|
+
- 0
|
10
|
+
version: 0.6.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Arnau Siches
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-04 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -106,6 +106,8 @@ files:
|
|
106
106
|
- lib/nutils/data_sources/filesystem_customizable.rb
|
107
107
|
- lib/nutils/data_sources.rb
|
108
108
|
- lib/nutils/filters/beautify.rb
|
109
|
+
- lib/nutils/filters/crop.rb
|
110
|
+
- lib/nutils/filters/sass.rb
|
109
111
|
- lib/nutils/filters/sprockets.rb
|
110
112
|
- lib/nutils/filters/svg2png.rb
|
111
113
|
- lib/nutils/filters/yuicompressor.rb
|
@@ -145,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
147
|
requirements: []
|
146
148
|
|
147
149
|
rubyforge_project:
|
148
|
-
rubygems_version: 1.
|
150
|
+
rubygems_version: 1.4.1
|
149
151
|
signing_key:
|
150
152
|
specification_version: 3
|
151
153
|
summary: A set of utilities for Nanoc3.
|