-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.rb
63 lines (49 loc) · 1.74 KB
/
graph.rb
1
2
3
4
5
6
7
8
9
10
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require_relative 'WikiPage.rb'
require 'rubygems'
require 'graphviz'
require 'optparse'
#options = {}
#OptionParser.new do |opts|
# opts.banner 'Usage graph.rb: ruby graph.rb [options]'
# opts.on('-L', '--level NUM', 'Recursively maps NUM levels of transclusions. Default = 1.')
# opts.on('-w', '--wiki', 'URI for wiki api on any wiki. Default is Wikipedia.')
# opts.on('-p', '--page' 'Page title to start the map') do |page|
#
# end
#end.parse!
LEVELS = 10
$g = GraphViz.new(:G, type: :digraph)
$levels = 1
def make_tree(pagename)
if WikiPages.pages.find_all { |p| p.name == pagename }.count == 0
w = WikiPages.add(pagename)
puts "Processing: #{w.name} (level #{$levels})"
w_name = w.name.gsub(/Template:/, '')
parent = $g.get_node(w_name)
# Deletes templates from the list if a link already exists in the graph
unless parent.nil?
w.templates.reject! { |t| parent.neighbors.map(&:id).include? t }
deleted = w.templates.select { |t| parent.neighbors.map(&:id).include? t }
puts "--- Templates deleted: #{deleted}" unless deleted.empty?
end
w.templates.each do |t|
puts " #{t}"
w_name = w.name.gsub(/Template:/, '')
tt = t.gsub(/Template:/, '')
node1 = parent.nil? ? $g.add_nodes(w_name) : parent
node2 = $g.get_node(tt).nil? ? $g.add_nodes(tt) : $g.get_node(tt)
$g.add_edges(node1, node2)
end
return if $levels == LEVELS
$levels += 1
w.templates.each do |t|
tt = t.gsub(/Template:/, '')
child = $g.get_node(tt)
make_tree(t) if child.neighbors.count == 0
end
$levels -= 1
end
end
make_tree('Font')
$g.output(png: "font.png")
#$g.output(dot: "nodes.dot")