Module: Jekyll::WikiRefs::TypeFilters

Defined in:
lib/jekyll-wikirefs/plugins/filter.rb

Instance Method Summary collapse

Instance Method Details

#doc_type(links, doc_type) ⇒ Object

usage: assign note_links = page.links | doc_type: “notes” %



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
# File 'lib/jekyll-wikirefs/plugins/filter.rb', line 11

def doc_type(links, doc_type)
  Jekyll.logger.error("Jekyll-WikiRefs: 'links' invalid") if links.nil?
  Jekyll.logger.error("Jekyll-WikiRefs: 'doc_type' invalid") if doc_type.nil? || doc_type.empty?
  return [] if links.empty?

  site = @context.registers[:site]

  links_of_type = []
  links.each do |l|
    # links
    if l.keys.include?('url')
      docs = site.documents.select{ |d| d.url == l['url'] && d.type.to_s == doc_type.to_s }
      if !docs.nil? && docs.size == 1
        links_of_type << l
      end
    # attributes
    elsif l.keys.include?('urls')
      l['urls'].each do |lurl|
        docs = site.documents.select{ |d| d.url == lurl && d.type.to_s == doc_type.to_s }
        if !docs.nil? && docs.size == 1
          links_of_type << l
        end
      end
    else
      Jekyll.logger.error("Jekyll-WikiRefs: In 'doc_type' filter, 'links' do not have 'url' or 'urls'")
    end
  end
  return links_of_type.uniq
end

usage: assign author_links = page.links | link_type: “author” %



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jekyll-wikirefs/plugins/filter.rb', line 42

def link_type(links, link_type)
  Jekyll.logger.error("Jekyll-WikiRefs: 'links' invalid") if links.nil?
  Jekyll.logger.error("Jekyll-WikiRefs: 'link_type' invalid") if link_type.nil?
  return [] if links.empty?

  site = @context.registers[:site]

  links_of_type = []
  links.each do |l|
    if l['type'].to_s == link_type.to_s
      # links
      if l.keys.include?('url')
        docs = site.documents.select{ |d| d.url == l['url'] }
        if !doc.nil? && docs.size != 1
          links_of_type << l
        end
      # attributes
      elsif l.keys.include?('urls')
        all_docs_exist = true
        l['urls'].each do |lurl|
          docs = site.documents.select{ |d| d.url == lurl }
          if !docs.nil? && docs.size != 1
            all_docs_exist = false
          end
        end
        if all_docs_exist
          links_of_type << l
        end
      else
        Jekyll.logge.error("Jekyll-WikiRefs: In 'link_type' filter, 'links' do not have 'url' or 'urls'")
      end
    end
  end
  return links_of_type.uniq
end