Skip to content

Instantly share code, notes, and snippets.

@dru
Created July 5, 2009 15:16
Show Gist options
  • Save dru/140996 to your computer and use it in GitHub Desktop.
Save dru/140996 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# Супер рекурсивный индексер. Версия 3
def recurse_dir(o, dirname)
entries = Dir.entries(dirname)
directories = entries.find_all {|e| FileTest.directory? File.join(dirname, e)}.sort{|a,b| a.gsub(/[^0-9A-z]/, "").to_i <=> b.gsub(/[^0-9A-z]/, "").to_i}
files = entries.find_all {|e| FileTest.file? File.join(dirname, e)}.sort{|a,b| a.gsub(/[^0-9A-z]/, "").to_i <=> b.gsub(/[^0-9A-z]/, "").to_i}
directories.reject! {|a| a =~ /^\./}
files.reject! {|a| a =~ /^\./}
files.each do |filename|
o.puts "<a href=\"#{File.join(dirname, filename)}\" class=\"file\">#{html_escape(filename)}</a></br>"
end
directories.each do |filename|
o.puts "<div class=\"folder\">"
o.puts "<div class=\"title\">#{html_escape(filename)}</div>"
recurse_dir o, File.join(dirname, filename)
o.puts "</div>"
end
end
def html_escape h
h # Влом ескейпить (
end
doc = <<EOF
<html>
<head>
<style type='text/css'>
<!--
.folder {
background-color: #EEEEEE;
border: 1px solid #333333;
display: block;
margin-top: 10px;
margin-right: 5px;
margin-left: 20px;
margin-bottom: 6px;
padding-bottom: 3px;
}
.title {
font-weight: bold;
background-color: #CCCCCC;
width: 99%;
padding-top: 5px;
padding-left: 1%;
padding-bottom: 5px;
}
.file {
padding-right: 12px;
padding-left: 12px;
} //-->
</style>
</head>
<body>
EOF
final_document = File.open('index.html', 'w') do |f|
f << doc
f << "<div class='folder'>\n\t"
f << "<div class=\"title\">#{html_escape(File.basename(File.expand_path('.')))}</div>"
recurse_dir f, "."
f << "</div>"
f << "\n</body></html>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment