Class: Jekyll::WebmentionIO::WebmentionTag

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ WebmentionTag

Returns a new instance of WebmentionTag.



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

def initialize(tag_name, text, tokens)
  super
  cache_file = WebmentionIO.get_cache_file_path "incoming"
  @cached_webmentions = if File.exist? cache_file
                          WebmentionIO.load_yaml(cache_file)
                        else
                          {}
                        end
end

Instance Method Details

#extract_type(type, webmentions) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll/tags/webmention.rb', line 47

def extract_type(type, webmentions)
  WebmentionIO.log "info", "Looking for #{type}"
  keep = {}
  if !WebmentionIO.types.include? type
    WebmentionIO.log "warn", "#{type} are not extractable"
  else
    type = type.to_singular
    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
  end
  keep
end

#lookup(context, name) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/jekyll/tags/webmention.rb', line 26

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

#render(context) ⇒ Object



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
97
98
99
100
101
# File 'lib/jekyll/tags/webmention.rb', line 65

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

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

  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)
    set_data(webmentions, types)
  end

  render_into_template(context.registers)
end

#set_data(data, types) ⇒ Object



43
44
45
# File 'lib/jekyll/tags/webmention.rb', line 43

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

#template=(template) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/jekyll/tags/webmention.rb', line 34

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