class JekyllWikiLinks::Generator

Constants

CONFIG_KEY

config

CONVERTER_CLASS
ENABLED_GRAPH_DATA_KEY
ENABLED_KEY
EXCLUDE_GRAPH_KEY
EXCLUDE_KEY
GRAPH_ASSETS_LOCATION_KEY
GRAPH_DATA_KEY

graph config

identify missing links in doc via .invalid-wiki-link class and nested doc-text.

Attributes

config[RW]
doc_manager[RW]
graph_nodes[RW]
md_docs[RW]
parser[RW]
site[RW]

Public Class Methods

new(config) click to toggle source
# File lib/jekyll-wikilinks.rb, line 35
def initialize(config)
        @config ||= config
        @testing ||= config['testing'] if config.keys.include?('testing')
end

Public Instance Methods

disabled?() click to toggle source

config helpers

# File lib/jekyll-wikilinks.rb, line 82
def disabled?
        option(ENABLED_KEY) == false
end
disabled_graph_data?() click to toggle source

graph config helpers

# File lib/jekyll-wikilinks.rb, line 109
def disabled_graph_data?
        option_graph(ENABLED_GRAPH_DATA_KEY) == false
end
exclude?(type) click to toggle source
# File lib/jekyll-wikilinks.rb, line 86
def exclude?(type)
        return false unless option(EXCLUDE_KEY)
        return option(EXCLUDE_KEY).include?(type.to_s)
end
excluded_in_graph?(type) click to toggle source
# File lib/jekyll-wikilinks.rb, line 113
def excluded_in_graph?(type)
        return false unless option_graph(EXCLUDE_KEY)
        return option_graph(EXCLUDE_KEY).include?(type.to_s)
end
generate(site) click to toggle source
# File lib/jekyll-wikilinks.rb, line 40
          def generate(site)
                  return if disabled?
self.old_config_warn()
                  Jekyll.logger.debug "Excluded jekyll types: ", option(EXCLUDE_KEY)
                  Jekyll.logger.debug "Excluded jekyll types in graph: ", option_graph(EXCLUDE_GRAPH_KEY)

# setup site
                  @site = site    
                  @context ||= JekyllWikiLinks::Context.new(site)

# setup markdown docs
                  docs = []
                  docs += site.pages if !exclude?(:pages)
                  docs += site.docs_to_write.filter { |d| !exclude?(d.type) }
                  @md_docs = docs.filter {|doc| markdown_extension?(doc.extname) }

# setup helper classes
@doc_manager = DocManager.new(@md_docs, @site.static_files)
@parser = Parser.new(@context, @markdown_converter, @doc_manager)
@link_index = LinkIndex.new(@site, @doc_manager)

# parse + populate index
@md_docs.each do |doc|
  @parser.parse(doc.content)
  @link_index.populate_attributes(doc, @parser.typed_link_blocks)
end
@link_index.process

# handle graph data
                  @graph_nodes, @graph_links = [], []
                  @md_docs.each do |doc|
                          if !disabled_graph_data? && !self.excluded_in_graph?(doc.type)
                                  self.generate_graph_data(doc) 
                          end
                  end
                  if !disabled_graph_data?
                          self.write_graph_data()
                  end
          end
generate_graph_data(doc) click to toggle source

graph helpers

# File lib/jekyll-wikilinks.rb, line 124
        def generate_graph_data(doc)
                Jekyll.logger.debug "Processing graph nodes for doc: ", doc.data['title']
                # missing nodes
                missing_node_names = doc.content.scan(REGEX_INVALID_WIKI_LINK)
                if !missing_node_names.nil?
                        missing_node_names.each do |missing_node_name_captures| 
                                missing_node_name = missing_node_name_captures[0]
                                if graph_nodes.none? { |node| node[:id] == missing_node_name }
                                        Jekyll.logger.warn "Net-Web node missing: ", missing_node_name
                                        Jekyll.logger.warn " in: ", doc.data['slug']  
                                        graph_nodes << {
                                                id: missing_node_name,
                                                url: '',
                                                label: missing_node_name,
                                        }
                                end
                                graph_links << {
                                        source: relative_url(doc.url),
                                        target: missing_node_name,
                                }
                        end
                end
                # existing nodes
                graph_nodes << {
                        id: relative_url(doc.url),
                        url: relative_url(doc.url),
                        label: doc.data['title'],
                }
                # TODO: this link calculation ends up with duplicates -- re-visit this later.
                all_links = doc.data['attributed'] + doc.data['backlinks']
                all_links.each do |link|
linked_doc = link['doc']
                        if !excluded_in_graph?(linked_doc.type)
                                graph_links << {
                                        source: relative_url(linked_doc.url),
                                        target: relative_url(doc.url),
                                }
                        end
                end
        end
has_custom_assets_path?() click to toggle source
# File lib/jekyll-wikilinks.rb, line 91
def has_custom_assets_path?
        return !!option_graph(GRAPH_ASSETS_LOCATION_KEY)
end
markdown_converter() click to toggle source
# File lib/jekyll-wikilinks.rb, line 99
def markdown_converter
        @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS)
end
markdown_extension?(extension) click to toggle source
# File lib/jekyll-wikilinks.rb, line 95
def markdown_extension?(extension)
        markdown_converter.matches(extension)
end
old_config_warn() click to toggle source

!! deprecated !!

# File lib/jekyll-wikilinks.rb, line 186
def old_config_warn()
  if config.include?("wikilinks_collection")
    Jekyll.logger.warn "As of 0.0.3, 'wikilinks_collection' is no longer used for configs. jekyll-wikilinks will scan all markdown files by default. Check README for details."
  end
end
option(key) click to toggle source
# File lib/jekyll-wikilinks.rb, line 103
def option(key)
        config[CONFIG_KEY] && config[CONFIG_KEY][key]
end
option_graph(key) click to toggle source
# File lib/jekyll-wikilinks.rb, line 118
def option_graph(key)
        config[GRAPH_DATA_KEY] && config[GRAPH_DATA_KEY][key]
end
write_graph_data() click to toggle source
# File lib/jekyll-wikilinks.rb, line 165
def write_graph_data()
        assets_path = has_custom_assets_path? ? option_graph(GRAPH_ASSETS_LOCATION_KEY) : "/assets"
        if !File.directory?(File.join(site.source, assets_path))
                Jekyll.logger.error "Assets location does not exist, please create required directories for path: ", assets_path
        end
        # from: https://github.com/jekyll/jekyll/issues/7195#issuecomment-415696200
        static_file = Jekyll::StaticFile.new(site, site.source, assets_path, "graph-net-web.json")
        # TODO: make write file location more flexible -- requiring a write location configuration feels messy...
        File.write(@site.source + static_file.relative_path, JSON.dump({
                links: graph_links,
                nodes: graph_nodes,
        }))
        # tests fail without manually adding the static file, but actual site builds seem to do ok
        # ...although there does seem to be a race condition which causes a rebuild to be necessary in order to detect the graph data file
        if @testing
                @site.static_files << static_file if !@site.static_files.include?(static_file)
        end
end