class JekyllRPG::References

References within Jekyll Collections

Attributes

collection_keys[RW]
graph[RW]

Public Class Methods

new(site) click to toggle source
# 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

Public Instance Methods

collection_documents() click to toggle source
# File lib/references.rb, line 78
def collection_documents
  @collection_keys.flat_map do |collection|
    @site.collections[collection].docs
  end
end
reference_pass() click to toggle source

Generating data on how documents reference other documents

# 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_pass() click to toggle source

Generating data on how documents are referenced to

# 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