gallerize-cli 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -4
  3. data/Gemfile +2 -2
  4. data/{assets/css → app/assets/images}/blank.gif +0 -0
  5. data/{assets/css → app/assets/images}/fancybox_loading.gif +0 -0
  6. data/{assets/css → app/assets/images}/fancybox_loading@2x.gif +0 -0
  7. data/{assets/css → app/assets/images}/fancybox_overlay.png +0 -0
  8. data/{assets/css → app/assets/images}/fancybox_sprite.png +0 -0
  9. data/{assets/css → app/assets/images}/fancybox_sprite@2x.png +0 -0
  10. data/{assets/js → app/assets/javascripts/plugins}/jquery-1.10.1.min.js +0 -0
  11. data/{assets/js → app/assets/javascripts/plugins}/jquery.fancybox.js +0 -0
  12. data/app/assets/javascripts/ready.js +14 -0
  13. data/app/assets/stylesheets/_gallerize.scss +144 -0
  14. data/app/assets/stylesheets/_jquery.fancybox.scss +273 -0
  15. data/app/assets/stylesheets/styles.scss +2 -0
  16. data/app/config/gallerize_cli.yml +28 -0
  17. data/app/templates/layout.html.haml +37 -0
  18. data/bin/{gallerize → gallerize_cli} +5 -2
  19. data/gallerize-cli.gemspec +15 -15
  20. data/lib/gallerize_cli/directory.rb +113 -0
  21. data/lib/gallerize_cli/image/version.rb +99 -0
  22. data/lib/gallerize_cli/image.rb +87 -0
  23. data/lib/gallerize_cli/render.rb +70 -0
  24. data/lib/gallerize_cli.rb +42 -0
  25. data/readme.md +35 -0
  26. metadata +35 -34
  27. data/Gemfile.lock +0 -37
  28. data/README.md +0 -50
  29. data/assets/css/jquery.fancybox.css +0 -274
  30. data/assets/css/styles.css +0 -76
  31. data/assets/js/imagesloaded.js +0 -7
  32. data/assets/js/jquery.masonry.js +0 -9
  33. data/config/global.yml.example +0 -10
  34. data/lib/gallerize/output_dir.rb +0 -44
  35. data/lib/gallerize/source_dir.rb +0 -17
  36. data/lib/gallerize.rb +0 -210
  37. data/templates/layout.html.erb +0 -59
@@ -0,0 +1,2 @@
1
+ @import 'jquery.fancybox';
2
+ @import 'gallerize';
@@ -0,0 +1,28 @@
1
+ site_title: Gallerize Title
2
+ site_url: '.'
3
+ output_path: gallerize
4
+
5
+ images_per_page: 40
6
+
7
+ file_patterns:
8
+ - "*.jpg"
9
+ - "*.png"
10
+ - "*.gif"
11
+
12
+ versions:
13
+ thumb:
14
+ width: 400
15
+ height: 250
16
+ approach: crop
17
+
18
+ large:
19
+ width: 1000
20
+ height: 800
21
+ approach: resize
22
+
23
+ parallel:
24
+ in_processes: 6
25
+
26
+ image_attributes:
27
+ example.jpg:
28
+ title: The Title
@@ -0,0 +1,37 @@
1
+ !!!
2
+ %html{"xml:lang" => "en", :xmlns => "http://www.w3.org/1999/xhtml"}
3
+ %head
4
+ %meta{:content => "text/html;charset=UTF-8", "http-equiv" => "Content-Type"}/
5
+ %meta{:content => "noindex", :name => "robots"}/
6
+ %meta{:content => "noindex", :name => "googlebot"}/
7
+
8
+ %script{src: javascripts_min_path, type: 'text/javascript'}
9
+ %link{href: stylesheets_min_path, media: 'screen', rel: 'stylesheet', type: 'text/css'}
10
+
11
+ %meta{:content => "width=device-width, user-scalable=no", :name => "viewport"}/
12
+
13
+ %title= config.site_title
14
+
15
+ %body
16
+ %h1.page-title= config.site_title
17
+ #images-container
18
+ - if total_pages > 1
19
+ .navigation
20
+ %ul
21
+ %li{class: "#{'active' if current_page == 1}"}
22
+ %a{href: site_url('index.html')} 1
23
+ - total_pages.times do |i|
24
+ %li{class: "#{'active' if current_page == i + 2}"}
25
+ %a{href: site_url("page-#{i + 2}.html")}= i + 2
26
+
27
+ .images
28
+ - images.each do |image|
29
+ .image
30
+ .image-inner
31
+ - title = capture_haml do
32
+ %a.original-link{href: image.original_url, target: :blank } Download Full Resolution
33
+ %a.image-link.fancybox{rel: 'group', href: image.large_url, title: title, data: {original: image.original_url} }
34
+ %img{src: image.thumb_url }
35
+ .image-title
36
+ %a.original-link{href: image.original_url, target: :blank }
37
+ = image.attributes['title'] || image.name
@@ -1,8 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: UTF-8
3
3
  require "pathname"
4
-
5
4
  bin_file = Pathname.new(__FILE__).realpath
5
+
6
+ # add self to libpath
6
7
  $:.unshift File.expand_path("../../lib", bin_file)
7
8
 
8
- require 'gallerize'
9
+ require 'gallerize_cli'
10
+
11
+ GallerizeCli.perform
@@ -1,26 +1,26 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "gallerize"
3
+ require "gallerize_cli"
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "gallerize-cli"
7
- s.version = Gallerize::VERSION
8
- s.authors = ["Blake Hilscher"]
9
- s.email = ["blake@hilscher.ca"]
10
- s.homepage = "http://blake.hilscher.ca/"
11
- s.license = "MIT"
12
- s.summary = "Generate a static gallery from a folder of images."
6
+ s.name = "gallerize-cli"
7
+ s.version = GallerizeCli::VERSION
8
+ s.authors = ["Blake Hilscher"]
9
+ s.email = ["blake@hilscher.ca"]
10
+ s.homepage = "http://blake.hilscher.ca/"
11
+ s.license = "MIT"
12
+ s.summary = "Generate a static gallery from a folder of images."
13
13
  s.description = "https://github.com/blakehilscher/gallerize"
14
14
 
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.executables = ["gallerize"]
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = ["gallerize_cli"]
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_runtime_dependency 'mini_magick', '3.7.0'
21
- s.add_runtime_dependency 'activesupport', '4.1.1'
22
- s.add_runtime_dependency 'parallel', '1.0.0'
23
- s.add_runtime_dependency 'exifr', '1.1.3'
20
+ s.add_runtime_dependency 'mini_magick', '3.7.0'
21
+ s.add_runtime_dependency 'parallel', '1.0.0'
22
+ s.add_runtime_dependency 'sass', '3.4.7'
23
+ s.add_runtime_dependency 'uglifier'
24
24
 
25
25
  s.add_development_dependency "pry", "~> 0.9"
26
26
  end
@@ -0,0 +1,113 @@
1
+ require 'fileutils'
2
+ require 'gallerize_cli/image'
3
+ require 'sass'
4
+ require 'sass/engine'
5
+ require 'uglifier'
6
+ require 'parallel'
7
+
8
+ module GallerizeCli
9
+ class Directory
10
+ include FileUtils::Verbose
11
+
12
+ attr_reader :root_path
13
+
14
+ def initialize(path)
15
+ @root_path = File.expand_path(path)
16
+ end
17
+
18
+ def process
19
+ install
20
+ Parallel.each(images, in_processes: config.parallel['in_processes']) do |image|
21
+ image.process
22
+ end
23
+ end
24
+
25
+ def total_images_count
26
+ images.count
27
+ end
28
+
29
+ def images
30
+ @images ||= load_images
31
+ end
32
+
33
+ def config
34
+ @config ||= OpenStruct.new(YAML.load(File.read(File.join(app_install_path, 'config/gallerize_cli.yml'))))
35
+ end
36
+
37
+ def assets_path
38
+ @assets_path ||= File.join(output_path, 'assets')
39
+ end
40
+
41
+ def images_path
42
+ @images_path ||= File.join(output_path, 'images')
43
+ end
44
+
45
+ def output_path
46
+ @output_path ||= File.expand_path(config.output_path)
47
+ end
48
+
49
+ def app_install_path
50
+ File.join(root_path, '.gallerize_cli')
51
+ end
52
+
53
+ def javascripts_min_path
54
+ @javascripts_min_path ||= compile_javascripts.gsub(output_path, config.site_url)
55
+ end
56
+
57
+ def stylesheets_min_path
58
+ @stylesheets_min_path ||= compile_stylesheets.gsub(output_path, config.site_url)
59
+ end
60
+
61
+ private
62
+
63
+ def compile_javascripts
64
+ # compile files
65
+ load_path = File.join(app_install_path, 'assets/javascripts')
66
+ output = Dir.glob(File.join(load_path, '**/*.js')).collect do |js_file|
67
+ Uglifier.new.compile(File.read(js_file))
68
+ end.join
69
+ # write minified file
70
+ output_file = File.join(assets_path, "gallerize_cli-#{Digest::MD5.hexdigest(output)}.min.js")
71
+ GallerizeCli.logger.debug("generated #{output_file}")
72
+ File.write(output_file, output)
73
+ output_file
74
+ end
75
+
76
+ def compile_stylesheets
77
+ # configure load_paths
78
+ load_path = File.join(app_install_path, 'assets/stylesheets')
79
+ # this is undefined for some unknown reason
80
+ Sass.define_singleton_method(:load_paths) { [load_path] }
81
+ # compile styles.scss
82
+ scss_file = File.join(load_path, 'styles.scss')
83
+ source = File.read(scss_file)
84
+ output = Sass::Engine.new(source, style: :compressed, syntax: :scss).render
85
+ # write new file
86
+ output_file = File.join(assets_path, "gallerize_cli-#{Digest::MD5.hexdigest(output)}.min.css")
87
+ GallerizeCli.logger.debug("generated #{output_file}")
88
+ File.write(output_file, output)
89
+ output_file
90
+ end
91
+
92
+ def load_images
93
+ output = []
94
+ Dir.chdir(root_path) do
95
+ config.file_patterns.each do |file_pattern|
96
+ Dir.glob(file_pattern) do |file_path|
97
+ output << GallerizeCli::Image.new(self, file_path)
98
+ end
99
+ end
100
+ end
101
+ output
102
+ end
103
+
104
+ def install
105
+ cp_r(GallerizeCli.app_source_path, app_install_path) unless Dir.exists?(app_install_path)
106
+ mkdir_p(output_path) unless Dir.exists?(output_path)
107
+ mkdir_p(images_path) unless Dir.exists?(images_path)
108
+ mkdir_p(assets_path) unless Dir.exists?(assets_path)
109
+ cp_r(File.join(app_install_path, 'assets/images'), File.join(assets_path))
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,99 @@
1
+ require 'mini_magick'
2
+ require 'exifr'
3
+
4
+ module GallerizeCli
5
+ class Image
6
+ class Version
7
+ include FileUtils::Verbose
8
+
9
+ attr_accessor :image, :name, :options
10
+
11
+ def initialize(image, name, options)
12
+ @image = image
13
+ @name = name
14
+ @options = OpenStruct.new(options)
15
+ @valid = true
16
+ end
17
+
18
+ def process
19
+ generate
20
+ end
21
+
22
+ def url
23
+ file_path.to_s.gsub(image.directory.output_path, config.site_url)
24
+ end
25
+
26
+ def file_path
27
+ @file_path ||= load_file_path
28
+ end
29
+
30
+ def width
31
+ options.width.to_i
32
+ end
33
+
34
+ def height
35
+ options.height.to_i
36
+ end
37
+
38
+ def valid?
39
+ !!@valid
40
+ end
41
+
42
+ def config
43
+ image.config
44
+ end
45
+
46
+ private
47
+
48
+ def generate
49
+ # do we have width and height?
50
+ if width <= 0 || height <= 0
51
+ GallerizeCli.logger.debug "version: #{name} is missing width: #{width} or height: #{height}"
52
+
53
+ elsif !File.exists?(file_path)
54
+ GallerizeCli.logger.debug "generating #{options.approach} #{file_path}"
55
+ # open it up
56
+ mini_image = MiniMagick::Image.open(image.file_path)
57
+ mini_image.auto_orient
58
+ if options.approach == 'crop'
59
+ crop(mini_image, width, height)
60
+ else
61
+ resize(mini_image, width, height)
62
+ end
63
+ # landscape?
64
+ mini_image.write file_path
65
+ end
66
+ rescue => err
67
+ @valid = false
68
+ GallerizeCli.logger.debug "#{err} image.file_name: #{image.file_name} name: #{name} options: #{options}"
69
+ end
70
+
71
+ def resize(mini_image, width, height)
72
+ if mini_image[:width] > mini_image[:height]
73
+ mini_image.resize "#{width}x#{height}>"
74
+ else
75
+ # portrait
76
+ mini_image.resize "#{height}x#{width.to_i * 1.25}"
77
+ end
78
+ end
79
+
80
+ def crop(mini_image, width, height)
81
+ max = width > height ? width : height
82
+ if mini_image[:width] > mini_image[:height]
83
+ resize(mini_image, max * 2, height)
84
+ else
85
+ resize(mini_image, width, max * 2)
86
+ end
87
+
88
+ mini_image.crop "#{width}x#{height}+0+0"
89
+ end
90
+
91
+ def load_file_path
92
+ path = File.join(image.directory.images_path, name)
93
+ mkdir_p(path) unless Dir.exists?(path)
94
+ File.join(path, image.file_name)
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,87 @@
1
+ require 'gallerize_cli/image/version'
2
+
3
+ module GallerizeCli
4
+ class Image
5
+ include FileUtils::Verbose
6
+
7
+ attr_reader :directory, :file_path, :versions
8
+
9
+ class << self
10
+
11
+ def generate_version_method(name)
12
+ unless public_method_defined?("#{name}_url")
13
+ define_method("#{name}_url") { self.versions[name].url }
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ def attributes
20
+ @attributes ||= fetch_attributes
21
+ end
22
+
23
+ def original_url
24
+ destination = File.join(directory.images_path, file_name)
25
+ cp(file_path, destination) unless File.exists?(destination)
26
+ destination.gsub(directory.output_path, config.site_url)
27
+ end
28
+
29
+ def initialize(directory, file_path)
30
+ @success = true
31
+ @directory = directory
32
+ @file_path = file_path
33
+ generate_version_methods
34
+ end
35
+
36
+ def process
37
+ versions.each { |name, version| version.process }
38
+ end
39
+
40
+ def name
41
+ return @name if defined?(@name)
42
+ parts = file_name.split('.')
43
+ parts.pop
44
+ @name = parts.join('.')
45
+ end
46
+
47
+ def file_name
48
+ @file_name ||= File.basename(file_path)
49
+ end
50
+
51
+ def config
52
+ directory.config
53
+ end
54
+
55
+ def versions
56
+ @versions ||= config.versions.inject({}) do |hash, (version_name, options)|
57
+ hash[version_name] = GallerizeCli::Image::Version.new(self, version_name, options)
58
+ hash
59
+ end
60
+ end
61
+
62
+ def valid?
63
+ versions.collect(&:valid?).include?(false)
64
+ end
65
+
66
+ private
67
+
68
+ def fetch_attributes
69
+ hash = {}
70
+ if config.image_attributes.is_a?(Hash)
71
+ hash = config.image_attributes[file_name] || {}
72
+ end
73
+ OpenStruct.new(hash)
74
+ end
75
+
76
+ def generate_version_methods
77
+ versions.each do |name, version|
78
+ self.class.generate_version_method(name)
79
+ end
80
+ end
81
+
82
+ def success?
83
+ @success
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,70 @@
1
+ require 'haml'
2
+ require 'forwardable'
3
+
4
+ module GallerizeCli
5
+ class Render
6
+
7
+ attr_accessor :directory
8
+
9
+ def initialize(directory)
10
+ @directory = directory
11
+ end
12
+
13
+ def perform
14
+ index = 1
15
+ directory.images.each_slice(directory.config.images_per_page) do |images|
16
+ file = File.join(directory.output_path, 'index.html')
17
+ file = File.join(directory.output_path, "page-#{index}.html") if index != 1
18
+ View.new(directory, file, {images: images, page_index: index}).render
19
+ index += 1
20
+ end
21
+ end
22
+
23
+ def output_file
24
+ File.join(directory.output_path, 'index.html')
25
+ end
26
+
27
+ class View
28
+ extend Forwardable
29
+
30
+ attr_reader :directory, :file_path, :locals
31
+
32
+ def_delegators :@directory, :javascripts_min_path, :stylesheets_min_path, :config, :total_images_count
33
+
34
+ def initialize(directory, file_path, locals={})
35
+ @directory = directory
36
+ @file_path = file_path
37
+ @locals = locals
38
+ end
39
+
40
+ def render
41
+ GallerizeCli.logger.debug("generate #{file_path}")
42
+ File.write(file_path, Haml::Engine.new(template).render(self, locals))
43
+ end
44
+
45
+ def site_url(path=nil)
46
+ File.join(config.site_url, path)
47
+ end
48
+
49
+ def current_page
50
+ (locals[:page_index] + 1 / directory.config.images_per_page)
51
+ end
52
+
53
+ def total_pages
54
+ directory.total_images_count / directory.config.images_per_page
55
+ end
56
+
57
+ private
58
+
59
+ def template
60
+ @template ||= File.read(File.join(templates_path, 'layout.html.haml'))
61
+ end
62
+
63
+ def templates_path
64
+ @templates_path ||= File.join(directory.app_install_path, 'templates')
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+ end