Giter Site home page Giter Site logo

amphelper's Introduction

AmpHelper

AmpHelper helps you generate html tags for AMP. To know AmpHelper ability, just look Usage section.

Installation

Add this line to your application's Gemfile:

gem 'amp_helper', '~> 1.0.0.pre'

And then execute:

$ bundle
$ rails g amp_helper

Helpers usage

image_tag(source, options = {})

image_tag method creates a <amp-img> tag if it's passed amp option as true.
Learn more about amp-img tag

String Source

  $ image_tag('http://placehold.it/350x150', width: 20, height: 20, amp: true)
  #=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" width="20" /></amp-img>'

  $ image_tag('http://placehold.it/350x150', size: '20x20', amp: true)
  #=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" width="20" /></amp-img>'

  $ image_tag('http://placehold.it/350x150', amp: true)
  #=> '<amp-img alt="350x150" height="150" src="http://placehold.it/350x150" width="350" /></amp-img>'

Carrierwave Source

  $ image_tag(ThumbUploader.new.square, amp: true)
  #=> '<amp-img alt="Square 350x150" height="20" src="http://placehold.it/square_350x150" width="20" /></amp-img>'

Retina

  $ image_tag('http://placehold.it/350x150', srcset: 'http://placehold.it/700x300 2x', size: '20x20', amp: true)
  #=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" srcset="http://placehold.it/700x300 2x" width="20" /></amp-img>'

  $ image_tag(ThumbUploader.new.square, format_2x: '%s_2x', amp: true)
  #=> '<amp-img alt="Square 350x150" height="20" src="http://placehold.it/square_350x150" srcset="http://placehold.it/square_2x_350x150 2x" width="20" /></amp-img>'
Example of CarrierWave::Uploader versions.
class ThumbUploader < CarrierWave::Uploader::Base
  storage :file
  version :square do
    process resize_to_fill: [20, 20]
  end
  version :square_2x do
    process resize_to_fill: [40, 40]
  end
  def default_url(*args)
    'http://placehold.it/' + [version_name, '350x150'].compact.join('_')
  end
end

link_to(name = nil, options = nil, html_options = nil, &block)

link_to method creates a <a> tag links to AMP cache if it's passed amp option as true.
Learn more about using AMP cache.

If indexed AMP cache is found

  $ link_to(@title, @path, amp: true)
  #=> "<a data-vars-link-url=\"https://ampbyexample.com/components/amp-form/preview/\" href=\"https://ampbyexample-com.cdn.ampproject.org/c/s/ampbyexample.com/components/amp-form/preview/\">link title</a>"

  $ link_to(@path, amp: true) { @title }
  #=> "<a data-vars-link-url=\"https://ampbyexample.com/components/amp-form/preview/\" href=\"https://ampbyexample-com.cdn.ampproject.org/c/s/ampbyexample.com/components/amp-form/preview/\">link title</a>"

If indexed AMP cache is NOT found

  $ link_to(@title, @path, amp: true)
  #=> "<a data-vars-link-url=\"https://ampbyexample.com/components/amp-form/preview/noamp\" href=\"https://ampbyexample.com/components/amp-form/preview/noamp\">link title</a>"

amp-analytics conbination

As you can see, AmpHelper puts the data-vars-link-url option on <a> tags. It's gonna help to tracking count of link clicking.

Learn more about Variables as data attribute for amp-analytics.

Configure

Configure ratina version name format For CarrierWave::Uploader

config/initializers/amp_helper.rb

AmpHelper.configure do |config|
  # Enable amp image.
  # Default: true
  # config.enable_amp_image = true
  #
  # Configure ratina version name format For CarrierWave::Uploader
  # Default: nil
  # config.format_2x = '%s_2x'

  # Enable amp cache link.
  # Default: true
  # config.enable_amp_link = Rails.env.production?
end

Development

bundle exec rspec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/amp_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

amphelper's People

Contributors

ggtmtmgg avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.