Class: JekyllRPG::References

Inherits:
Object
  • Object
show all
Defined in:
lib/references.rb

Overview

References within Jekyll Collections

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ References

Returns a new instance of References.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/references.rb', line 14

def initialize(site)
  @site = site
  @dm_mode = @site.config['dm_mode']
  @graph = Graph.new
  @broken_links = []
  @collection_keys = @site.collections.keys - ['posts']

  reference_pass
  referent_pass

  # Create list of broken links
  @graph.unviewable.each do |edge|
    @broken_links.push(edge.hash)
  end
end

Instance Attribute Details

Returns the value of attribute broken_links.



12
13
14
# File 'lib/references.rb', line 12

def broken_links
  @broken_links
end

#collection_keysObject

Returns the value of attribute collection_keys.



12
13
14
# File 'lib/references.rb', line 12

def collection_keys
  @collection_keys
end

#graphObject

Returns the value of attribute graph.



12
13
14
# File 'lib/references.rb', line 12

def graph
  @graph
end

Instance Method Details

#collection_documentsObject



78
79
80
81
82
# File 'lib/references.rb', line 78

def collection_documents
  @collection_keys.flat_map do |collection|
    @site.collections[collection].docs
  end
end

Find all markdown links in document TODO - fails to find markdown link at very start of doc due to not finding any character that isn’t an exclamation mark



87
88
89
# File 'lib/references.rb', line 87

def markdown_links(doc)
  doc.to_s.scan(%r{(?<=[^!])\[.*?\]\(/.*?/.*?\)})
end

#reference_passObject

Generating data on how documents reference other documents



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
# File 'lib/references.rb', line 31

def reference_pass
  # Parse references from markdown links
  collection_documents.each do |doc|
    # Do not publish or reference a page if the site is not in DM Mode
    # And the page is marked as for dms
    doc_dm = doc.data['dm']
    if doc_dm && !@dm_mode
      doc.data['published'] = false
    else
      unviewable_links = []
      # Extract details of the referent from the document
      referent = CollectionDocument.new.extract_doc(doc)

      # make a reference from each link on the page
      markdown_links(doc).each do |link|
        md_link = MarkdownLink.new(link)
        reference = CollectionDocument.new.extract_markdown(@site, md_link)

        # if the reference isn't viewable in the current configuration
        # append that link to the array of links to strikethrough
        unviewable_links << reference.markdown_link unless reference.viewable

        # Make a new edge on the graph
        @graph.edges.push(Edge.new(referent, reference))
      end

      # Unviewable links are struck through
      unviewable_links.uniq.each do |link|
        doc.content = doc.content.sub! link, "~~#{link}~~"
      end
    end
  end
end

#referent_passObject

Generating data on how documents are referenced to



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/references.rb', line 66

def referent_pass
  # For each collection page, add where it is referenced
  collection_documents.each do |doc|
    # Put the reference data on the doc
    doc.data['referenced_by'] = @graph.document_references(doc)

    # If the references table option is configured, append the table
    table = ReferenceTable.new(@site, doc).html
    doc.content = doc.content + table
  end
end