middleman-social_image 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e367633b01f5990facae751ae7f5c615c240dfec330167cc5d632b726903c3ce
4
- data.tar.gz: aeaf53cf8ac3b9bf8f3d36908d504038f31c2d070d6c4780a35454b57098797b
3
+ metadata.gz: f46e2e521dcd3e3860d9a150e0d466b089819d4a7403f3e5a007ba12b4dc609a
4
+ data.tar.gz: 0b319ab37bbd2b8208bb89dc82e731d630068d4e782124c11bcbb2794f17503d
5
5
  SHA512:
6
- metadata.gz: 5d5eae315f60e07165afa0ee33c2e7e9bcea9909a664c47f609654619c3251dbbb192a99c06285a4a8b7f20219fe49230711ecadb1ea767fed164a6c4bcf3899
7
- data.tar.gz: 8ffab1b034b3d31b8296db23547d27c5753e69fd534634958efe67b0cb22ea9ceb4487b3a96bea65debfbd767124c76ea30bb0eb9ebd158bef3fe29c057ffa4f
6
+ metadata.gz: 2969d71e7d9d4d55cf5ea6fc4ac1f977da8e2aca3d75eac4dabea975e1645bc92fb9436b3bf11bd77ba4a8e35d79057edf9b92aac9143cd5afe5ce2104acec4c
7
+ data.tar.gz: b821e5d978da93de9a846b8c9de40cc215740d448164f70febe53686f618659c34d6baa418114f8a0ea2317fe915f9b5dc389ef973016be93d836788707bc62e
data/README.md CHANGED
@@ -25,18 +25,20 @@ Optionally, you can configure this extension with the following (defaults are sh
25
25
  ```ruby
26
26
  activate :social_image do |social_image|
27
27
  social_image.window_size = '1200,600' # The size of the screenshot
28
- social_image.base_url = 'http://localhost:4567/' # Base URL of running middleman server
29
- social_image.base_asset_dir = 'assets/images/social-images' # Where to save the generated images
30
- social_image.social_image_url_pattern = %r{(/social-image)/$} # Screenshot URLs matching this pattern
31
- social_image.social_image_url_substitution = '\1.png' # When generating screenshots, replace URLs matching social_image_url_pattern with this
32
28
  social_image.selector = 'body > *' # Used to test that the social image url has loaded properly. The more specific this is, the better the chance of catching errors.
29
+ social_image.always_generate = true # Always regenerate images. Takes a long time, but ensures any changes are reflected.
30
+ social_image.parallel = true # Generate images in parallel.
33
31
  end
34
32
  ```
35
33
 
36
- Create a HTML page in your middleman app that contains the HTML you want to be rendered. The URL for this should match the social_image_url_pattern (so by default, ending with `/social-image/`).
34
+ Create a HTML page in your middleman app that contains the HTML you want to be rendered. Add `social_image: true` to the page's front matter to indicate it is the source for a social image. You can also add it as an option if you're using a proxy page like the following.
37
35
 
38
- Start up the middleman server with `bundle exec middleman server`
39
-
40
- Run the `bundle exec middleman social_image` command to generate images from any URLs matching social_image_url_pattern. The image will be saved to a location matching the URL of the resource used to generate it, with the social_image_url_substitution applied, in the base_asset_dir. So with the default settings, if for instance you had created a page at `/examples/social-image/`, this image would be saved at `assets/images/social-images/examples/social-image.png`.
36
+ ``` ruby
37
+ ["tom", "dick", "harry"].each do |name|
38
+ proxy "/social-image/#{name}.html", "/templates/social-image.html", social_image: true, locals: { name: name }
39
+ end
40
+ ```
41
41
 
42
42
  You can now refer to this image as normal within your middleman application.
43
+
44
+ In preview mode, you don't need any other special setup. However, to generate the images in build, you also need to be running the preview server with `bundle exec middleman server`.
@@ -0,0 +1,12 @@
1
+ require "capybara"
2
+ Capybara.server = :webrick
3
+ Capybara.register_driver :selenium_chrome_headless do |app|
4
+ Capybara::Selenium::Driver.load_selenium
5
+ browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
6
+ opts.args << '--headless'
7
+ opts.args << '--disable-gpu' if Gem.win_platform?
8
+ opts.args << '--disable-site-isolation-trials'
9
+ opts.args << '--hide-scrollbars'
10
+ end
11
+ Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
12
+ end
@@ -0,0 +1,43 @@
1
+ require "middleman-social_image/capybara"
2
+ class Middleman::SocialImage::Converter
3
+ def initialize(app, window_size, selector, always_generate)
4
+ @app = app
5
+ @window_size = window_size
6
+ @selector = selector
7
+ @always_generate = always_generate
8
+ end
9
+
10
+ def image_path(resource)
11
+ File.join(@app.root, "tmp/middleman-social_image", resource.path.sub(".html", ".png"))
12
+ end
13
+
14
+ def convert(resource)
15
+ image_path = image_path(resource)
16
+ if File.exist?(image_path) && !@always_generate
17
+ @app.logger.debug "== social_image: skipping #{resource.path} as already in cache"
18
+ else
19
+ @app.logger.debug "== social_image: converting #{resource.path}"
20
+ session.visit(resource.url)
21
+ raise "#{resource.url} did not contain '#{@selector}'." unless session.has_selector?(@selector)
22
+ FileUtils.mkdir_p(File.dirname(image_path))
23
+ session.save_screenshot(image_path)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def session
30
+ @session ||= begin
31
+ if @app.server?
32
+ rack_app = ::Middleman::Rack.new(@app).to_app
33
+ session = Capybara::Session.new(:selenium_chrome_headless, rack_app)
34
+ else
35
+ protocol = @app.config.https ? "https" : "http"
36
+ Capybara.app_host = "#{protocol}://localhost:#{@app.config.port}/"
37
+ session = Capybara::Session.new(:selenium_chrome_headless)
38
+ end
39
+ session.current_window.resize_to(*@window_size.split(","))
40
+ session
41
+ end
42
+ end
43
+ end
@@ -1,11 +1,63 @@
1
1
  require 'middleman-core'
2
- require 'middleman-social_image/commands'
3
2
 
4
3
  class Middleman::SocialImage::Extension < ::Middleman::Extension
5
4
  option :window_size, '1200,600'
6
- option :base_url, 'http://localhost:4567/'
7
- option :base_asset_dir, 'assets/images/social-images'
8
- option :social_image_url_pattern, %r{(/social-image)/$}
9
- option :social_image_url_substitution, '\1.png'
10
5
  option :selector, "body > *"
6
+ option :always_generate, true
7
+ option :parallel, true
8
+
9
+ def initialize(app, options_hash={}, &block)
10
+ super
11
+ require "middleman-social_image/converter"
12
+ require "middleman-social_image/resource"
13
+ @converter = Middleman::SocialImage::Converter.new(app, options.window_size, options.selector, options.always_generate)
14
+ end
15
+
16
+ def manipulate_resource_list(resources)
17
+ if app.build?
18
+ manipulate_resource_list_for_build(resources)
19
+ else
20
+ manipulate_resource_list_for_preview(resources)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def social_image_source_resources
27
+ app.sitemap.resources.select {|resource| resource.options[:social_image] || resource.data[:social_image] }
28
+ end
29
+
30
+ def path_for_source_resource(resource)
31
+ resource.path.sub(".html", ".png")
32
+ end
33
+
34
+ def manipulate_resource_list_for_build(resources)
35
+ convert_resource = proc do |resource|
36
+ @converter.convert(resource)
37
+ end
38
+ if options.parallel
39
+ ::Parallel.each(social_image_source_resources, &convert_resource)
40
+ else
41
+ social_image_source_resources.each(&convert_resource)
42
+ end
43
+ social_image_resources = social_image_source_resources.map do |resource|
44
+ Middleman::Sitemap::Resource.new(
45
+ @app.sitemap,
46
+ path_for_source_resource(resource),
47
+ @converter.image_path(resource)
48
+ )
49
+ end
50
+ resources + social_image_resources - social_image_source_resources
51
+ end
52
+
53
+ def manipulate_resource_list_for_preview(resources)
54
+ resources + social_image_source_resources.map do |resource|
55
+ Middleman::SocialImage::Resource.new(
56
+ @app.sitemap,
57
+ path_for_source_resource(resource),
58
+ @converter,
59
+ resource
60
+ )
61
+ end
62
+ end
11
63
  end
@@ -0,0 +1,12 @@
1
+ class Middleman::SocialImage::Resource < ::Middleman::Sitemap::Resource
2
+ def initialize(store, path, converter, base_resource)
3
+ super(store, path)
4
+ @converter = converter
5
+ @base_resource = base_resource
6
+ end
7
+
8
+ def render(*)
9
+ @converter.convert(@base_resource)
10
+ File.read(@converter.image_path(@base_resource))
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  module SocialImage
3
- VERSION = '0.0.3'.freeze
3
+ VERSION = '0.0.4'.freeze
4
4
  end
5
5
  end
@@ -3,6 +3,5 @@ require 'middleman-social_image/version'
3
3
 
4
4
  Middleman::Extensions.register :social_image do
5
5
  require "middleman-social_image/extension"
6
- require 'middleman-social_image/commands'
7
6
  Middleman::SocialImage::Extension
8
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-social_image
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul McMahon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-14 00:00:00.000000000 Z
11
+ date: 2020-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleman-core
@@ -67,8 +67,10 @@ files:
67
67
  - bin/console
68
68
  - bin/setup
69
69
  - lib/middleman-social_image.rb
70
- - lib/middleman-social_image/commands.rb
70
+ - lib/middleman-social_image/capybara.rb
71
+ - lib/middleman-social_image/converter.rb
71
72
  - lib/middleman-social_image/extension.rb
73
+ - lib/middleman-social_image/resource.rb
72
74
  - lib/middleman-social_image/version.rb
73
75
  - middleman-social_image.gemspec
74
76
  homepage: https://github.com/pwim/middleman-social_image
@@ -1,60 +0,0 @@
1
- require 'middleman-cli'
2
-
3
- module Middleman
4
- module Cli
5
- class SocialImage < Thor::Group
6
- include Thor::Actions
7
-
8
- check_unknown_options!
9
-
10
- namespace :social_image
11
-
12
- def self.exit_on_failure?
13
- true
14
- end
15
-
16
- def social_image
17
- require "capybara"
18
- app = ::Middleman::Application.new do
19
- config[:mode] = :config
20
- config[:watcher_disable] = true
21
- end
22
- options = app.extensions[:social_image].options
23
- Capybara.register_driver :selenium_chrome_headless do |app|
24
- Capybara::Selenium::Driver.load_selenium
25
- browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
26
- opts.args << "--window-size=#{options.window_size}"
27
- opts.args << '--headless'
28
- opts.args << '--disable-gpu' if Gem.win_platform?
29
- opts.args << '--disable-site-isolation-trials'
30
- opts.args << '--hide-scrollbars'
31
- end
32
- Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
33
- end
34
- session = Capybara::Session.new(:selenium_chrome_headless)
35
-
36
- app.sitemap.resources.each do |resource|
37
- if resource.url =~ options.social_image_url_pattern
38
- image_path = File.join(app.source_dir, options.base_asset_dir, resource.url.sub(options.social_image_url_pattern, options.social_image_url_substitution))
39
- if File.exists?(image_path)
40
- say "Skipping #{image_path} as already generated"
41
- else
42
- say "Generating #{image_path}"
43
- FileUtils.mkdir_p(File.dirname(image_path))
44
- url = File.join(options.base_url, resource.url)
45
- session.visit(url)
46
- if session.has_selector?(options.selector)
47
- session.save_screenshot(image_path)
48
- else
49
- say "Aborting. #{image_path} did not contain '#{options.selector}'. Is the preview server running?"
50
- exit
51
- end
52
- end
53
- end
54
- end
55
- end
56
- end
57
-
58
- Base.register(Middleman::Cli::SocialImage, 'social_image', 'social_image [options]', 'Generates social images.')
59
- end
60
- end