File: opengraph.rb

package info (click to toggle)
ruby-html-proofer 3.19.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,040 kB
  • sloc: ruby: 3,203; sh: 9; makefile: 4; javascript: 1; php: 1
file content (46 lines) | stat: -rw-r--r-- 1,275 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

class OpenGraphElement < ::HTMLProofer::Element
  attr_reader :src

  def initialize(obj, check, logger)
    super(obj, check, logger)
    # Fake up src from the content attribute
    instance_variable_set('@src', @content)

    @src.insert 0, 'http:' if %r{^//}.match?(@src)
  end
end

class OpenGraphCheck < ::HTMLProofer::Check
  def missing_src?
    !@opengraph.src
  end

  def empty_src?
    blank?(@opengraph.src)
  end

  def run
    @html.css('meta[property="og:url"], meta[property="og:image"]').each do |m|
      @opengraph = OpenGraphElement.new(m, self, @logger)

      next if @opengraph.ignore?

      # does the opengraph exist?
      if missing_src?
        add_issue('open graph has no content attribute', line: m.line, content: m.content)
      elsif empty_src?
        add_issue('open graph content attribute is empty', line: m.line, content: m.content)
      elsif !@opengraph.valid?
        add_issue("#{@opengraph.src} is an invalid URL", line: m.line)
      elsif @opengraph.remote?
        add_to_external_urls(@opengraph.url)
      else
        add_issue("internal open graph #{@opengraph.url} does not exist", line: m.line, content: m.content) unless @opengraph.exists?
      end
    end

    external_urls
  end
end