Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 189 lines (157 sloc) 5.253 kB
54e85fa @jgm initial commit
authored
1 #!/usr/bin/perl -w
2 #
3 # sep-offprint 0.3 - John MacFarlane - May 25, 2005
4 #
5 # produces a PDF or postscript "offprint" of a Stanford
6 # Encyclopedia of Philosophy (SEP) article
7 #
8 # Argument is an entry name from SEP, as it appears in the URL.
9 # For example, to get the article on classical logic, which is at
10 # http://plato.stanford.edu/entries/logic-classical/, just type
11 #
12 # sep-offprint logic-classical
13 #
14 # if this script is executable and in your path, otherwise
15 #
16 # perl sep-offprint logic-classical
17 #
18 # and it will create logic-classical.pdf. If you list multiple entries,
19 # sep-offprint will get them all: for example,
20 #
21 # sep-offprint frege russell wittgenstein
22 #
23 # There are many command-line options. For a list, type
24 #
25 # sep-offprint --help
26 #
27 # The programs html2ps and ps2pdf must be in the user's path:
28 #
29 # html2ps can be found at http://user.it.uu.se/~jan/html2ps.html.
30 # Download the tarball or zip file and run the "install" script.
31 #
32 # ps2pdf is part of Ghostscript -- many users will have it
33 # already: http://www.cs.wisc.edu/~ghost/doc/AFPL/get851.htm
34 #
35 # For more information and updates, see
36 # http://philosophy.berkeley.edu/macfarlane/sep-offprint.html
37 #
38
39 use Getopt::Long;
40
41 sub printhelp {
42 die
43 "Produces a PDF offprint from a Stanford Encyclopedia of Philosophy article.
44 (http://plato.stanford.edu/)
45
46 Usage: sep-offprint [options] <entry names>
47
48 Examples: sep-offprint russell
49 sep-offprint frege russell wittgenstein
50 sep-offprint --1up --ps --paper a4 frege
51 sep-offprint --1up --fontfamily Helvetica frege
52
53 Options:
54
55 --1up print one page per sheet, portrait orientation
56 --2up print two pages per sheet, landscape orientation (default)
57 --ps produce postscript (PS) output
58 --pdf produce PDF output (default)
59 --font <font> use <font> (Times, Helvetica, Palatino, Courier) (default Times)
60 --size <size> use <size> (10pt, 12pt, 14pt, 16pt) (default 14pt)
61 --align <align> use <align> (left, justified) (default justified)
62 --paper <papersize> specify <papersize> (letter, legal, a4) (default letter)
63 --localpath <path> look for entry in a subdirectory of <path> on local filesystem
64 --help this message
65 --version prints version number\n";
66 }
67
68 GetOptions( '1up|1' => \$oneup,
69 '2up|2' => \$twoup,
70 'ps' => \$ps,
71 'pdf' => \$pdf,
72 'font=s' => \$fontfamily,
73 'size=s' => \$fontsize,
74 'align=s' => \$textalign,
75 'paper=s' => \$papersize,
76 'localpath=s' => \$localpath,
77 'help|h' => \$help,
78 'version|v' => \$version);
79
80 if ($help) {&printhelp;};
81 if ($version) {die "sep-offprint ver. 0.3\n";};
82 if (not ($pdf or $ps)) {$pdf=1};
83 if ($oneup) {$twoup = 0} else {$twoup = 1};
84 if (not $fontsize) {$fontsize = "14pt"};
85 if (not $fontfamily) {$fontfamily = "Times"};
86 if (not $textalign) {$textalign = "justify"};
87 if (not $papersize) {$papersize = "letter"};
88 if ($localpath) {$pathname = $localpath} else {$pathname = "http://plato.stanford.edu/entries"};
89
90 if ($#ARGV < 0) {&printhelp;};
91
92 #
93 # Create blank html file to work around html2ps bug.
94 #
95 # Without this blank file after notes.html, html2ps will cut off
96 # the last page of an entry if it occurs in the left column in 2up mode.
97 #
98
99 $blank = "blankhtml" . time;
100
101 open FILE, ">$blank" or die "unable to open $blank: $!";
102
103
104 print FILE <<EOF;
105 <html>
106 <head>
107 <title>&nbsp;</title>
108 </head>
109 <body>
110 <p>&nbsp;</p>
111 </body>
112 </html>
113 EOF
114
115 close FILE;
116
117 #
118 # For each argument, create a configuration file with appropriate footers
119 # and run html2ps and html2pdf on that argument.
120 #
121
122 foreach $entryname (@ARGV) {
123
124 # remove uppercase and spaces:
125 $entryname =~ tr/A-Z/a-z/;
126 $entryname =~ tr/ /-/;
127
128 # remove SEP pathname if specified:
129 $entryname =~ s{http://plato.stanford.edu/entries/}{};
130 # remove /index.html if specified:
131 $entryname =~ s{/.*}{};
132
133 #
134 # Create temporary configuration file
135 #
136
137 $html2psrc = "html2psrc" . time;
138
139 open FILE, ">$html2psrc" or die "unable to open $html2psrc: $!";
140
141 print FILE <<EOF;
142 BODY {
143 font-size: $fontsize;
144 font-family: $fontfamily;
145 text-align: $textalign;
146 }
147 \@page {
148 margin-left: 2.5cm;
149 margin-right: 2.5cm;
150 margin-top: 2.5cm;
151 margin-bottom: 2.5cm;
152 }
153 \@html2ps {
154 option {
155 twoup: $twoup;
156 landscape: $twoup;
157 number: 0;
158 }
159 paper { type: $papersize }
160 header {
161 right: "STANFORD ENCYCLOPEDIA OF PHILOSOPHY";
162 left: \$T;
163 }
164 footer {
165 left: \$N;
166 right: "$pathname/$entryname";
167 }
168 }
169 EOF
170
171 close FILE;
172
173 # name of temporary file to hold postscript output of html2ps
174 $pstemp = $entryname . time;
175
176 system("html2ps -D -f $html2psrc -o $pstemp $pathname/$entryname/index.html $pathname/$entryname/notes.html $blank");
177
178 if ($pdf) {system("ps2pdf -sPAPERSIZE=$papersize $pstemp $entryname.pdf") || print "Created $entryname.pdf.\n";};
179
180 if ($ps) {rename($pstemp, "$entryname.ps"); print "Created $entryname.ps\n";} else {unlink $pstemp;} ;
181
182 # delete temporary config file
183 unlink $html2psrc;
184 }
185
186 # delete blank html file
187 unlink $blank;
188
Something went wrong with that request. Please try again.