Skip to content

Instantly share code, notes, and snippets.

@mro
Created January 7, 2010 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mro/271223 to your computer and use it in GitHub Desktop.
Save mro/271223 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -w
# As the Perl Script http://wiki.getdropbox.com/DropboxAddons/PerlScriptToMakeHTMLIndex
# disappeared I started a ruby script
#
# Ruby Help: http://www.rubycentral.com/pickaxe
# Html&CSS Help: http://de.selfhtml.org/css/
#
DST_NAME = 'index.html'
#####################################################################
# Column formatting helpers
def href name
name = "#{name}/#{DST_NAME}" if File.stat(name).directory?
# TODO: escape
name
end
def size name
return '' if !File.stat(name).file?
"#{File.stat(name).size?.to_i / 1024 + 1} KB"
end
def time tim
tim.strftime '%Y-%m-%d   %H:%M:%S Uhr'
end
def mtime name
time File.stat(name).mtime
end
#####################################################################
# analyze the commandline
recursive = false
force = false
ARGV.each do |p|
case p
when '-f', '--force'
force = true
when '-r'
recursive = true
when '-h', '-?', '--help'
$stderr.puts <<END_OF_STRING
Help!
END_OF_STRING
exit
else
raise "Illegal commandline switch #{p}"
end
end
raise "recursive operation not implemented yet." if recursive
opt = File::TRUNC|File::CREAT|File::WRONLY
opt |= File::EXCL if !force
#####################################################################
# Start the action
File.open(DST_NAME, opt, 0644) do |dst|
dst.puts <<END_OF_STRING
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
/*<![CDATA[*/
a:link { text-decoration:none; }
a:visited{ text-decoration:none; }
a:hover { text-decoration:underline; }
body { font-family:sans-serif; background-color:#eee }
table { background-color:white;padding:1em;border-style:solid;border-width:thin;border-color:#ddd }
.hidden { visibility:hidden;line-height:0 }
th.name { text-align:left }
td { font-size:9pt }
td.size { text-align:right; padding:0 1em }
.foot { font-size:8pt; color:#888 }
/*]]>*/
</style>
<!-- http://groups.google.com/group/iphonewebdev/browse_thread/thread/c7b605e5d43c0b26 -->
<meta name="viewport" content=
"width=550; user-scalable=1; minimum-scale=0.25; maximum-scale=1.6;" />
<link rel="stylesheet" href="index.css" type="text/css" media="all" />
<title>DropBox Index.</title>
</head>
<body>
<p class="foot">Download starten: Rechte Maustaste und "Ziel speichern unter..." w&auml;hlen.</p>
<table summary='file list'>
END_OF_STRING
f = '..'
dst.puts "\t<tr class='hidden'><td class='name'><a href='#{href f}'>In den &uuml;bergeordneten Ordner wechseln</a></td></tr>"
dst.puts "\t<tr><th class='name'>Name</th><th class='size'>Gr&ouml;&szlig;e</th><th class='mtime'>Zuletzt ver&auml;ndert</th></tr>"
Dir.entries('.').sort.each do |f|
# don't list ., .. and index.html
next if f[0]==46 || f == DST_NAME
# dst.puts "\t<tr#{f[0]==46 ? ' class=\'hidden\'': ''}><td class='name'><a href='./#{href f}'>#{f}</a></td><td class='size'>#{size f}</td><td class='mtime'>#{mtime f}</td></tr>"
dst.puts "\t<tr><td class='name'><a href='./#{href f}'>#{f}</a></td><td class='size'>#{size f}</td><td class='mtime'>#{mtime f}</td></tr>"
end
dst.puts <<END_OF_STRING
</table>
<p class='foot'>&Uuml;bersicht zuletzt aktualisiert: &nbsp;
END_OF_STRING
dst.puts time(Time.now)
dst.puts <<END_OF_STRING
</p>
</body>
</html>
END_OF_STRING
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment