-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_builder.rb
173 lines (146 loc) · 4.37 KB
/
index_builder.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env ruby
class IndexBuilder
attr_accessor :current_path
def initialize(root: Dir.pwd)
@root_dir = root.gsub(' ', '\ ')
@output_file = 'index.html'
@file_count = 0
@dir_count = 0
if overwrite_index?
`rm #{@root_dir}/index.html`
else
`mv #{@root_dir}/index.html #{@root_dir}/index2.html`
end if index_file_exists?
end
def build_index(relative_links: true)
@relative = relative_links
@last_update = Time.now
add_top_html
add_body_header
add_iframe
add_buttons
add_subdirs_and_files(@root_dir)
add_bottom_html
write_files
end
private
def add_body_header
@output += "<header><h1>Index for #{@root_dir}</h1></header>"
end
def add_bottom_html
@output += '<script src="./treeify_assets/main.js" type="text/javascript"></script></body></html>'
end
def add_buttons
@output += '<div id="tree-viewer">'
@output += '<div class="all-buttons">'
@output += '<button id="expand-all">Expand All</button>'
@output += '<button id="collapse-all">Collapse All</button></div>'
end
def add_files(dir)
# TODO: add file size info; potentially return agg size too for dir size
result = `ls '#{dir}' 2>&1`
unless $?.success?
puts "Error (in add_subdirs_and_files) while processing: #{dir}"
end
files = result.split("\n").select do |item|
File.file?("#{dir}/#{item}")
end
files.each do |file|
path = @relative ? dir.gsub(@root_dir, '.') : dir
@output += "<li class=\"file\" id=\"#{dir}/#{file}\">"
@output += "<a href=\"#{path}/#{file}\" target=\"file-viewer\">#{file}</a></li>"
@file_count += 1
end
end
def add_iframe
@output += '<div id="file-viewer">'
@output += '<button id="show-tree">Back to Tree View</button>'
@output += '<iframe name="file-viewer" src=""></iframe></div>'
end
def add_subdir(dir, subdir)
curr_dir = "#{dir}/#{subdir}"
@output += "<li class=\"directory\" id=\"#{curr_dir}\">"
@output += "<button class=\"toggle\">+</button>#{subdir}"
add_subdirs_and_files(curr_dir)
@output += '</li>'
@dir_count += 1
end
def add_subdirs_and_files(dir)
result = `ls '#{dir}' 2>&1`
unless $?.success?
puts "Error (in add_subdirs_and_files) while processing: #{dir}"
end
subdirs = result.split("\n").select do |item|
File.directory?("#{dir}/#{item}")
end
@output += '<ul id="root">'
subdirs.each do |subdir|
subdir.gsub!(' ', '\ ')
add_subdir(dir, subdir)
end
add_files(dir)
@output += '</ul>'
progress_report
# eventually return bubbled up dir size here
end
def add_top_html
@output = '<!DOCTYPE html><html><head><meta charset="utf-8">'
@output += '<title>Tree Index for #{@root_dir}</title>'
@output += '<link rel="stylesheet" type="text/css" href="./treeify_assets/main.css" />'
@output += '</head><body>'
end
def index_file_exists?
File.exist?("#{@root_dir}/index.html")
end
def overwrite_index?
puts 'Rename and keep existing index.html file? (y/n)'
gets[0].downcase != 'y'
end
def progress_report
return unless (Time.now - @last_update) > 2
puts "Directories processed: #{@dir_count}"
puts "Files processed: #{@file_count}"
@last_update = Time.now
end
def write_files
File.write("#{@root_dir}/#{@output_file}", @output)
if File.exist?("#{@root_dir}/treeify_assets")
puts 'treeify_assets directory already exists'
else
`cp -r ./treeify_assets #{@root_dir}`
end
end
end
if $PROGRAM_NAME == __FILE__
puts 'Build index for current directory? (y/n)'
use_pwd = gets[0].downcase == 'y'
if use_pwd
ib = IndexBuilder.new
else
puts 'Provide path for target directory:'
resp = gets.chomp
root =
case resp[0]
when '/'
resp
when '~'
`echo ~`.chomp
when '.'
if resp[1] == '/'
Dir.pwd + resp[1..-1]
elsif resp[1] == '.'
until resp[0] != '.'
dir = (dir || Dir.pwd).split('/')[0...-1].join('/')
resp = resp.split('/')[1..-1].join('/')
end
"#{dir}/#{resp}"
end
else
Dir.pwd + '/' + resp
end
ib = IndexBuilder.new(root: root)
end
# puts 'Use absolute or relative paths for file links? (a/r)'
relative = true # gets[0].downcase == 'r'
ib.build_index(relative_links: relative)
end