Class: Jekyll::WebmentionIO::WebmentionTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/tags/webmention.rb

Instance Method Summary collapse

Instance Method Details

#extract_type(type, webmentions) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jekyll/tags/webmention.rb', line 36

def extract_type(type, webmentions)
  WebmentionIO.log 'info', "Looking for #{type}"
  keep = {}
  if WebmentionIO.types.include? type
    type = ActiveSupport::Inflector.singularize(type)
    WebmentionIO.log 'info', "Searching #{webmentions.length} webmentions for type==#{type}"
    if webmentions.is_a? Hash
      webmentions = webmentions.values
    end
    webmentions.each do |webmention|
      keep[webmention['id']] = webmention if webmention['type'] == type
    end
  else
    WebmentionIO.log 'warn', "#{type} are not extractable"
  end
  keep
end

#lookup(context, name) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/jekyll/tags/webmention.rb', line 15

def lookup(context, name)
  lookup = context
  name&.split('.')&.each do |value|
    lookup = lookup[value]
  end
  lookup
end

#render(context) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jekyll/tags/webmention.rb', line 54

def render(context)
  # Initialize an empty set of webmentions (we'll populate later if
  # there actually are any).
  webmentions = []

  # Capture the types in case JS needs them
  types = []

  # Get the URI
  args = @text.split(/\s+/).map(&:strip)
  uri = args.shift
  uri = lookup(context, uri)

  cached_webmentions = WebmentionIO.caches.incoming_webmentions

  if cached_webmentions.key? uri
    all_webmentions = cached_webmentions[uri].clone
    WebmentionIO.log 'info', "#{all_webmentions.length} total webmentions for #{uri}"

    if args.length.positive?
      WebmentionIO.log 'info', "Requesting only #{args.inspect}"
      webmentions = {}
      args.each do |type|
        types.push type
        extracted = extract_type(type, all_webmentions)
        WebmentionIO.log 'info', "Merging in #{extracted.length} #{type}"
        webmentions = webmentions.merge(extracted)
      end
    else
      WebmentionIO.log 'info', 'Grabbing all webmentions'
      webmentions = all_webmentions
    end

    if webmentions.is_a? Hash
      webmentions = webmentions.values
    end

    webmentions = sort_webmentions(webmentions)
  end

  set_data(webmentions, types, WebmentionIO.config.html_proofer_ignore)
  render_into_template(context.registers)
end

#set_data(data, types, html_proofer_ignore) ⇒ Object



32
33
34
# File 'lib/jekyll/tags/webmention.rb', line 32

def set_data(data, types, html_proofer_ignore)
  @data = { 'webmentions' => data, 'types' => types, 'html_proofer_ignore' => html_proofer_ignore }
end

#template=(template) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/jekyll/tags/webmention.rb', line 23

def template=(template)
  unless WebmentionIO.templates.supported_templates.include? template
    WebmentionIO.log 'error', "#{template.capitalize} is not supported"
  end
  @template_name = template
  @template = WebmentionIO.templates.template_contents(template)
  WebmentionIO.log 'info', "#{template.capitalize} template:\n\n#{@template}\n\n"
end