Skip to content

Instantly share code, notes, and snippets.

@hyuki
Last active January 17, 2019 15:53
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 hyuki/1b5a3be07c1e74b6b1bf008f416c5b83 to your computer and use it in GitHub Desktop.
Save hyuki/1b5a3be07c1e74b6b1bf008f416c5b83 to your computer and use it in GitHub Desktop.
update-shelf.rb - Extract first jpeg file from pdf files.

convert(ImageMagick) is required.

$ vi update-shelf     (Edit PDFDIR and SHELFDIR)
$ update-shelf
$ erb shelf.erb > index.html
$ open -a safari index.html
<%
BASE_LIST = []
Dir.glob("*.jpg").sort.each do |jpgfile|
BASE_LIST << File.join(File.dirname(jpgfile), File.basename(jpgfile, '.*'))
end
%>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<title>shelf</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/readable/bootstrap.min.css" rel="stylesheet" integrity="sha384-Li5uVfY2bSkD3WQyiHX8tJd0aMF91rMrQP5aAewFkHkVSTT2TmD2PehZeMmm7aiL" crossorigin="anonymous">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style type="text/css">
body { padding-top: 80px; background-color: white;}
p { margin: 0 0 1.5em; }
</style>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Shelf</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-left">
<li class="hidden">
<a href="#page-top"></a>
</li>
<li><a href="#">Tool</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="">About</a></li>
</ul>
</div><!--/.navbar-collapse -->
</div>
</div>
<div class="container-fluid">
<div class="row">
<% BASE_LIST.each do |base| %>
<span xclass="col-xs-2 thumb">
<a xclass="thumbnail" href="file:///Users/YOU/PDFDIR/<%= base %>.pdf">
<img style="padding:10px; border: 1px solid gray; width:10%" xclass="img-responsive" alt="" src="<%= base %>.jpg">
</a>
</span>
<% end %>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>
#! /usr/bin/env ruby
PDFDIR = '/Users/YOU/PDFDIR'
SHELFDIR = '/Users/YOU/SHELFDIR'
Dir.chdir(PDFDIR) do
count = 0
Dir.glob("*.pdf").each do |pdffile|
jpgfile = "#{SHELFDIR}/#{File.join(File.dirname(pdffile), File.basename(pdffile, '.*'))}.jpg"
if File.exists?(jpgfile)
puts "#{count}: #{jpgfile} is found, already."
else
print "#{count}: Creating #{jpgfile}..."
system(%Q(convert "#{pdffile}[0]" "#{jpgfile}"))
puts
end
count += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment