From 7fb3c26633ad6e4b98706b5cc569387dd13012f1 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 10 Mar 2022 01:29:52 -0800 Subject: [PATCH] Add a file to create file dependency graph --- analyze.rb | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 analyze.rb diff --git a/analyze.rb b/analyze.rb new file mode 100644 index 0000000..bb9a17d --- /dev/null +++ b/analyze.rb @@ -0,0 +1,82 @@ +require "pathname" +require "set" +require "json" + +def resolve_path(bp, p) + path = nil + if bp.start_with? "." + path = Pathname.new(File.join(bp, p)).cleanpath.to_s + elsif p.start_with? "blog/" + path = File.join("content", p) + else + path = File.join("content", "blog", p) + end + if File.directory? path + path = File.join(path, "index.md") + elsif !path.end_with? ".md" + path += ".md" + end + path.gsub("blog/blog/", "blog/") +end + +files = Set.new +refs = {} +ARGF.each do |file| + file = file.chomp + files << file + arr = refs[file] || (refs[file] = []) + File.open(file).read.scan(/< relref "([^"]+)" >/) do |ref| + ref = resolve_path(File.dirname(file), ref[0]) + arr << ref + files << ref + end + arr.uniq! +end + +data = {} +id = 0 +files.each do |file| + id += 1 + name = file + tags = [] + group = 1 + value = File.size(file) + url = file.gsub(/^content/, "https://danilafe.com").delete_suffix("/index.md").delete_suffix(".md") + File.readlines(file).each do |l| + if l =~ /^title: (.+)$/ + name = $~[1].delete_prefix('"').delete_suffix('"') + elsif l =~ /^tags: (.+)$/ + tags = $~[1].delete_prefix("[").delete_suffix("]").split(/,\s?/).map { |it| it.gsub('"', '') } + if tags.include? "Compilers" + group = 2 + elsif tags.include? "Coq" + group = 3 + elsif tags.include? "Programming Languages" + group = 4 + elsif tags.include? "Haskell" + group = 5 + elsif tags.include? "Crystal" + group = 6 + end + end + end + data[file] = { :id => id, :label => name, :group => group, :tags => tags, :url => url, :value => value } +end + +edges = [] +files.each do |file1| + # files.each do |file2| + # next if file1 == file2 + # next unless data[file1][:tags].any? { |t| data[file2][:tags].include? t } + # edges << { :from => data[file1][:id], :to => data[file2][:id] } + # end + next unless frefs = refs[file1] + frefs.each do |ref| + edges << { :from => data[file1][:id], :to => data[ref][:id] } + end +end +edges.uniq +# edges.filter! { |e| e[:from] < e[:to] } + +puts ("const nodes = " + JSON.pretty_unparse(data.values) + ";") +puts ("const edges = " + JSON.pretty_unparse(edges) + ";")