-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmaksummary.rb
114 lines (101 loc) · 3.37 KB
/
maksummary.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
require "yaml"
class MakeSummary
def extract_dir_info(entry, path, indent)
dir_keys = extract_keys_from_directory(entry, path)
title = dir_keys.fetch(:title, entry)
order = dir_keys.fetch(:order, 100)
type = "directory"
text_string = "#{' ' * indent}* [#{title}](#{path}/README.md)"
{text: text_string, indent:, title:, order:, type:}
end
def generate_entry_for_file(entry, path, indent)
stat = File::Stat.new(path)
mdate = stat.mtime
file_keys = extract_keys_from_markdown(entry, path)
title = file_keys.fetch(:title, entry)
order = file_keys.fetch(:order, 100)
type = file_keys.fetch(:type, "unspecified")
type = file_keys.fetch(:type, "unspecified")
status = file_keys.fetch(:status, "unspecified")
text_string = "#{' ' * indent}* [#{title}](#{path})"
{text: text_string, indent:, title:, order:, type:, status:, path:, mdate:}
end
def skip_entry?(entry)
%w[. .. images SUMMARY.md docs .git .gitbook README.md .vscode .DS_Store .bookignore .gitbook.yaml].include?(entry)
end
def valid_markdown_file?(entry)
entry.end_with?(".md")
end
def extract_keys_from_directory(entry, current_path)
result = {title: entry, order: 100}
info_file = File.join(current_path, "info.yml")
if File.exist?(info_file)
info = YAML.load_file(info_file)
info.transform_keys!(&:to_sym)
title = info.fetch(:title, entry)
order = info.fetch(:order, 100)
type = "directory"
result.merge!(order:, title:, type:)
end
result
end
def extract_keys_from_markdown(entry, current_path)
result = {title: entry}
begin
md_content = YAML.load_file(current_path)
md_content.transform_keys!(&:to_sym)
rescue StandardError
end
result.merge!(md_content) unless md_content.nil? || !md_content.is_a?(Hash)
result
end
def process(current, depth)
all_entries = []
Dir.entries(current).each do |entry|
next if skip_entry?(entry)
current_path = File.join(current, entry)
if File.directory?(current_path)
dir_info = extract_dir_info(entry, current_path, depth)
dir_entries = process(current_path, depth + 1)
x = dir_info.merge!(lines: dir_entries)
all_entries << x
elsif valid_markdown_file?(entry)
x = generate_entry_for_file(entry, current_path, depth)
all_entries << x
end
end
all_entries
end
def generate_special_sections(all_entries)
puts "## Special Sections"
puts "* New Entries"
generate_special_section(all_entries, "new")
puts "* Needs Review"
generate_special_section(all_entries, "needs review")
end
def generate_special_section(all_entries, status_val)
all_entries.each do |entry|
if entry[:status] == status_val
puts " * [#{entry[:title]}](#{entry[:path]})"
end
next unless entry[:lines]
generate_special_section(entry[:lines], status_val)
end
end
def generate_output(all_entries)
all_entries.sort_by { |x| [x[:order], x[:title]] }
.each do |entry|
puts entry[:text]
next unless entry[:lines]
generate_output(entry[:lines])
end
end
def run
current_path = File.join(".")
all_entries = process(current_path, 0)
puts "## CampusRover Lab Notebook"
generate_output(all_entries)
# generate_special_sections(all_entries)
end
end
MakeSummary.new.run