Class: Jekyll::Commands::WebmentionCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/jekyll/commands/webmention.rb

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/jekyll/commands/webmention.rb', line 8

def self.init_with_program(prog)
  prog.command(:webmention) do |c|
    c.syntax "webmention"
    c.description "Sends queued webmentions"

    c.action { |args, options| process args, options }
  end
end

.process(_args = [], options = {}) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll/commands/webmention.rb', line 17

def self.process(_args = [], options = {})
  options = configuration_from_options(options)
  WebmentionIO.bootstrap(Jekyll::Site.new(options))

  if File.exist? WebmentionIO.cache_file("sent.yml")
    WebmentionIO.log "error", "Your outgoing webmentions queue needs to be upgraded. Please re-build your project."
  end
  count = 0
  cached_outgoing = WebmentionIO.get_cache_file_path "outgoing"
  if File.exist?(cached_outgoing)
    outgoing = WebmentionIO.load_yaml(cached_outgoing)
    outgoing.each do |source, targets|
      targets.each do |target, response|
        # skip ones we’ve handled
        next unless response == false

        # convert protocol-less links
        if target.index("//").zero?
          target = "http:#{target}"
        end

        # skip bad URLs
        next unless WebmentionIO.uri_ok?(target)

        # get the endpoint
        endpoint = WebmentionIO.get_webmention_endpoint(target)
        next unless endpoint

        # get the response
        response = WebmentionIO.webmention(source, target, endpoint)
        next unless response

        # capture JSON responses in case site wants to do anything with them
        begin
          response = JSON.parse response
        rescue JSON::ParserError
          response = ""
        end
        outgoing[source][target] = response
        count += 1
      end
    end
    if count.positive?
      WebmentionIO.dump_yaml(cached_outgoing, outgoing)
    end
    WebmentionIO.log "msg", "#{count} webmentions sent."
  end # file exists (outgoing)
end