Class: Jekyll::Graph::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Includes:
Filters::URLFilters
Defined in:
lib/jekyll-graph.rb

Constant Summary collapse

CONVERTER_CLASS =
Jekyll::Converters::Markdown

Instance Method Summary collapse

Instance Method Details

#file_exists?(file_path) ⇒ Boolean

Checks if a file already exists in the site source

Returns:

  • (Boolean)


111
112
113
# File 'lib/jekyll-graph.rb', line 111

def file_exists?(file_path)
  @site.static_files.any? { |p| p.url == "/#{file_path}" }
end

#generate(site) ⇒ Object



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
64
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-graph.rb', line 32

def generate(site)
  # check what's enabled
  return if $graph_conf.disabled?
  # deprecated: 'net_web' -> 'web'
  if !$graph_conf.disabled_net_web? && !site.respond_to?(:link_index)
    Jekyll.logger.error("Jekyll-Graph: To generate the net-web graph, please either add and enable the 'jekyll-wikirefs' or 'jekyll-wikilinks' plugin or disable the net-web in the jekyll-graph config")
    return
  end
  if !$graph_conf.disabled_web? && !site.respond_to?(:link_index)
    Jekyll.logger.error("Jekyll-Graph: To generate the web graph, please either add and enable the 'jekyll-wikirefs' or 'jekyll-wikilinks' plugin or disable the web in the jekyll-graph config")
    return
  end
  if !$graph_conf.disabled_tree? && !site.respond_to?(:tree)
    Jekyll.logger.error("Jekyll-Graph: To generate the tree graph, please either add and enable the 'jekyll-semtree' or 'jekyll-namespaces' plugin, or disable the tree in the jekyll-graph config")
    return
  end

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

  # setup markdown docs
  docs = []
  docs += @site.pages if !$graph_conf.excluded?(:pages)
  docs += @site.docs_to_write.filter { |d| !$graph_conf.excluded?(d.type) }
  @md_docs = docs.filter { |doc| markdown_extension?(doc.extname) }
  if @md_docs.empty?
    Jekyll.logger.warn("Jekyll-Graph: No documents to process.")
  end

  # write graph
  if !$graph_conf.disabled_net_web?
    # generate json data
    json_net_web_nodes, json_net_web_links = self.generate_json_net_web()
    self.set_neighbors(json_net_web_nodes, json_net_web_links)
    net_web_graph_content = JSON.dump(
      nodes: json_net_web_nodes,
      links: json_net_web_links,
    )
    # create json file
    json_net_web_graph_file = self.new_page($graph_conf.path_assets, "graph-net-web.json", net_web_graph_content)
  end
  if !$graph_conf.disabled_web?
    # generate json data
    json_web_nodes, json_web_links = self.generate_json_net_web()
    self.set_neighbors(json_web_nodes, json_web_links)
    web_graph_content = JSON.dump(
      nodes: json_web_nodes,
      links: json_web_links,
    )
    # create json file
    json_web_graph_file = self.new_page($graph_conf.path_assets, "graph-web.json", web_graph_content)
  end
  if !$graph_conf.disabled_tree?
    # generate json data
    json_tree_nodes, json_tree_links = self.generate_json_tree(@site.tree.root)
    self.set_lineage(json_tree_nodes, json_tree_links)
    tree_graph_content = JSON.dump(
      nodes: json_tree_nodes,
      links: json_tree_links,
    )
    # create json file
    json_tree_graph_file = self.new_page($graph_conf.path_assets, "graph-tree.json", tree_graph_content)
  end
  # add graph drawing scripts
  script_filename = "jekyll-graph.js"
  graph_script_content = File.read(source_path(script_filename))
  # create js file
  static_file = self.new_page($graph_conf.path_scripts, script_filename, graph_script_content)
end

#generate_json_net_webObject

deprecated: ‘net_web’ -> ‘web’



181
182
183
# File 'lib/jekyll-graph.rb', line 181

def generate_json_net_web()
  return generate_json_web()
end

#generate_json_tree(node, json_parent = "", tree_nodes = [], tree_links = [], level = 0) ⇒ Object

used for both plugins: jekyll-semtree jekyll-namespace



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/jekyll-graph.rb', line 269

def generate_json_tree(node, json_parent="", tree_nodes=[], tree_links=[], level=0)
  node = node.is_a?(String) ? @site.tree.nodes.detect { |n| n.text == node } : node
  #
  # missing nodes
  #
  if node.missing
    missing_text = node.namespace ? node.namespace : node.text
    Jekyll.logger.warn("Jekyll-Graph: Tree node missing: ", missing_text)

    if node.namespace
      leaf = node.namespace.split('.').pop()
    end
    missing_node = {
      id: node.namespace ? node.namespace : node.text,
      label: node.namespace ? leaf.gsub('-', ' ') : node.text,
      url: "",
      level: level,
      lineage: {
        nodes: [],
        links: [],
      },
      siblings: [],
    }
    if node.namespace
      missing_node['namespace'] = node.namespace
    end
    # non-root handling
    if !json_parent.empty?
      missing_node[:parent] = json_parent[:id]
      tree_links << {
        source: json_parent[:id],
        target: node.namespace ? node.namespace : node.text,
      }
    end
    tree_nodes << missing_node
    json_parent = missing_node
  #
  # existing nodes
  #
  else
    existing_node = {
      id: node.url,
      label: node.title,
      url: relative_url(node.url),
      level: level,
      lineage: {
        nodes: [],
        links: [],
      },
      siblings: [],
    }
    if node.namespace
      existing_node['namespace'] = node.namespace
    end
    # non-root handling
    if !json_parent.empty?
      existing_node[:parent] = json_parent[:id]
      tree_links << {
        source: json_parent[:id],
        target: node.url,
      }
    end
    tree_nodes << existing_node
    json_parent = existing_node
  end
  node.children.each do |child|
    self.generate_json_tree(child, json_parent, tree_nodes, tree_links, (level + 1))
  end
  return tree_nodes, tree_links
end

#generate_json_webObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/jekyll-graph.rb', line 185

def generate_json_web()
  web_nodes, web_links = [], []

  @md_docs.each do |doc|
    if !$graph_conf.excluded?(doc.type)

      Jekyll.logger.debug("Jekyll-Graph: Processing graph nodes for doc: ", doc.data['title'])
      #
      # missing nodes
      #
      @site.link_index.index[doc.url].missing.each do |missing_link_name|
        if web_nodes.none? { |node| node[:id] == missing_link_name }
          Jekyll.logger.warn("Jekyll-Graph: Net-Web node missing: #{missing_link_name}, in: #{File.basename(doc.basename, File.extname(doc.basename))}")
          web_nodes << {
            id: missing_link_name, # an id is necessary for link targets
            url: '',
            label: missing_link_name,
            neighbors: {
              nodes: [],
              links: [],
            },
          }
          web_links << {
            source: doc.url,
            target: missing_link_name,
          }
        end
      end
      #
      # existing nodes
      #
      web_nodes << {
        # TODO: when using real ids, be sure to convert id to string (to_s)
        id: doc.url,
        url: relative_url(doc.url),
        label: doc.data['title'],
        neighbors: {
          nodes: [],
          links: [],
        },
      }
      # TODO: this link calculation ends up with duplicates -- re-visit this later.
      if $graph_conf.use_attrs?
        @site.link_index.index[doc.url].attributes.each do |link| # link = { 'type' => str, 'urls' => [str, str, ...] }
          # TODO: Header + Block-level wikilinks
          link['urls'].each do |lu|
            link_no_anchor = lu.match(/([^#]+)/i)[0]
            link_no_baseurl = @site.baseurl.nil? ? link_no_anchor : link_no_anchor.gsub(@site.baseurl, "")
            linked_doc = @md_docs.select{ |d| d.url == link_no_baseurl }
            if !linked_doc.nil? && linked_doc.size == 1 && !$graph_conf.excluded?(linked_doc.first.type)
              # TODO: add link['type'] to d3 graph
              web_links << {
                source: doc.url,
                target: linked_doc.first.url,
              }
            end
          end
        end
      end
      if $graph_conf.use_links?
        @site.link_index.index[doc.url].forelinks.each do |link| # link = { 'type' => str, 'url' => str }
          # TODO: Header + Block-level wikilinks
          link_no_anchor = link['url'].match(/([^#]+)/i)[0]
          link_no_baseurl = @site.baseurl.nil? ? link_no_anchor : link_no_anchor.gsub(@site.baseurl, "")
          linked_doc = @md_docs.select{ |d| d.url == link_no_baseurl }
          if !linked_doc.nil? && linked_doc.size == 1 && !$graph_conf.excluded?(linked_doc.first.type)
            # TODO: add link['type'] to d3 graph
            web_links << {
              source: doc.url,
              target: linked_doc.first.url,
            }
          end
        end
      end

    end
  end

  return web_nodes, web_links
end

#markdown_converterObject



119
120
121
# File 'lib/jekyll-graph.rb', line 119

def markdown_converter
  @markdown_converter ||= @site.find_converter_instance(CONVERTER_CLASS)
end

#markdown_extension?(extension) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/jekyll-graph.rb', line 115

def markdown_extension?(extension)
  markdown_converter.matches(extension)
end

#new_page(path, filename, content) ⇒ Object

generator helpers



125
126
127
128
129
130
131
132
# File 'lib/jekyll-graph.rb', line 125

def new_page(path, filename, content)
  new_file = PageWithoutAFile.new(@site, __dir__, "", filename)
  new_file.content = content
  new_file.data["layout"] = nil
  new_file.data["permalink"] = File.join(path, filename)
  @site.pages << new_file unless file_exists?(filename)
  return new_file
end

#set_lineage(json_nodes, json_links) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/jekyll-graph.rb', line 157

def set_lineage(json_nodes, json_links)
  # TODO: json nodes have relative_url, but node.id's/urls are doc urls.
  json_nodes.each do |json_node|
    # set lineage

    ancestor_node_ids, descendent_node_ids = @site.tree.get_all_lineage_ids(json_node[:id])
    lineage_node_ids = ancestor_node_ids.concat(descendent_node_ids)
    json_node[:lineage][:nodes] = lineage_node_ids if !lineage_node_ids.nil?

    # include current node when filtering for links along entire relative lineage
    lineage_ids = lineage_node_ids.concat([json_node[:id]])

    json_lineage_links = json_links.select { |l| lineage_ids.include?(l[:source]) && lineage_ids.include?(l[:target]) }
    json_node[:lineage][:links] = json_lineage_links if !json_lineage_links.nil?

    # set siblings

    json_node[:siblings] = @site.tree.get_sibling_ids(json_node[:id])
  end
end

#set_neighbors(json_nodes, json_links) ⇒ Object

json population helpers

set ids here, full javascript objects are populated in client-side javascript.


144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/jekyll-graph.rb', line 144

def set_neighbors(json_nodes, json_links)
  json_links.each do |json_link|
    source_node = json_nodes.detect { |n| n[:id] == json_link[:source] }
    target_node = json_nodes.detect { |n| n[:id] == json_link[:target] }

    source_node[:neighbors][:nodes] << target_node[:id]
    target_node[:neighbors][:nodes] << source_node[:id]

    source_node[:neighbors][:links] << json_link
    target_node[:neighbors][:links] << json_link
  end
end

#source_path(file) ⇒ Object



106
107
108
# File 'lib/jekyll-graph.rb', line 106

def source_path(file)
  File.expand_path "jekyll-graph/#{file}", __dir__
end