Created
February 24, 2011 21:34
-
-
Save kemitchell/842933 to your computer and use it in GitHub Desktop.
Format Google Scholar case law for reading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -wKU | |
require "rubygems" | |
require 'open-uri' | |
require 'escape' | |
require "nokogiri" | |
unless ARGV.count == 2 | |
puts "usage: <google case id> <basename>" | |
exit | |
end | |
basename = ARGV[1] | |
pattern = "http://scholar.google.com/scholar_case?case=%s" | |
case_url = format(pattern, ARGV[0]) | |
$doc = Nokogiri::HTML(open(case_url)) | |
opinion = $doc.css('#gsl_opinion') | |
opinion.search('.//script').remove | |
opinion.search('.//a[@href]').each do |link| | |
if /^\/scholar_case.+$/ =~ link['href'] | |
link['href'] = 'http://scholar.google.com' + link['href'] | |
end | |
end | |
opinion.xpath('//center[count(*)=0]').remove | |
opinion.xpath('//p[count(*)=0]').remove | |
htmlfile = "#{basename}.html" | |
pdffile = "#{basename}.pdf" | |
File.open(htmlfile,'w') do |f| | |
f.write <<-eos | |
<head> | |
<meta charset="utf-8"> | |
<title>#{basename}</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script> | |
<script src="http://www.kemitchell.com/law/edit.js" type="text/javascript"></script> | |
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/reset/reset-min.css"> | |
<link rel="stylesheet" href="http://www.kemitchell.com/law/styles.css"> | |
</head> | |
<body> | |
#{opinion.to_xml} | |
</body> | |
</html> | |
eos | |
end | |
`ln -s "#{htmlfile}" src.html` | |
`wkpdf --source src.html --format Letter --margin 24.0 --output out.pdf` | |
`mv out.pdf "#{pdffile}"` | |
`rm src.html` | |
`kindlegen '#{htmlfile}'` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment