Class: BetterSeo::Generators::AmpGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/better_seo/generators/amp_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canonical_url: nil, title: nil, description: nil, image: nil, structured_data: nil) ⇒ AmpGenerator

Returns a new instance of AmpGenerator.



10
11
12
13
14
15
16
# File 'lib/better_seo/generators/amp_generator.rb', line 10

def initialize(canonical_url: nil, title: nil, description: nil, image: nil, structured_data: nil)
  @canonical_url = canonical_url
  @title = title
  @description = description
  @image = image
  @structured_data = structured_data
end

Instance Attribute Details

#canonical_urlObject

Returns the value of attribute canonical_url.



8
9
10
# File 'lib/better_seo/generators/amp_generator.rb', line 8

def canonical_url
  @canonical_url
end

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/better_seo/generators/amp_generator.rb', line 8

def description
  @description
end

#imageObject

Returns the value of attribute image.



8
9
10
# File 'lib/better_seo/generators/amp_generator.rb', line 8

def image
  @image
end

#structured_dataObject

Returns the value of attribute structured_data.



8
9
10
# File 'lib/better_seo/generators/amp_generator.rb', line 8

def structured_data
  @structured_data
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/better_seo/generators/amp_generator.rb', line 8

def title
  @title
end

Instance Method Details

#to_amp_script_tagObject

Generate AMP runtime script tag



27
28
29
# File 'lib/better_seo/generators/amp_generator.rb', line 27

def to_amp_script_tag
  '<script async src="https://cdn.ampproject.org/v0.js"></script>'
end

#to_boilerplateObject

Generate AMP boilerplate CSS



19
20
21
22
23
24
# File 'lib/better_seo/generators/amp_generator.rb', line 19

def to_boilerplate
  boilerplate = <<~HTML
    <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  HTML
  boilerplate.strip
end

#to_custom_css(css) ⇒ Object

Wrap custom CSS in amp-custom style tag



57
58
59
60
61
# File 'lib/better_seo/generators/amp_generator.rb', line 57

def to_custom_css(css)
  return "" if css.nil? || css.empty?

  %(<style amp-custom>\n#{css}\n</style>)
end

#to_meta_tagsObject

Generate meta tags for AMP page



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/better_seo/generators/amp_generator.rb', line 32

def to_meta_tags
  tags = []

  tags << %(<link rel="canonical" href="#{escape_html(@canonical_url)}">) if @canonical_url

  tags << %(<meta property="og:title" content="#{escape_html(@title)}">) if @title

  if @description
    tags << %(<meta name="description" content="#{escape_html(@description)}">)
    tags << %(<meta property="og:description" content="#{escape_html(@description)}">)
  end

  tags << %(<meta property="og:image" content="#{escape_html(@image)}">) if @image

  tags.join("\n")
end

#to_structured_dataObject

Generate structured data script tag



50
51
52
53
54
# File 'lib/better_seo/generators/amp_generator.rb', line 50

def to_structured_data
  return "" unless @structured_data

  %(<script type="application/ld+json">\n#{JSON.generate(@structured_data)}\n</script>)
end