YeiZeta

nasa perl programation IndexRemplace Hacked by..>

Dec 14th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.82 KB | None | 0 0
  1. Horribly Hacked by: Michael Nelson ([email protected]) 1/18/94 ---> WHAT
  2.  
  3. #!/usr/local/bin/perl -s
  4. #
  5. # bib --- format refer files for framemaker, latex, HTML etc.
  6. #
  7. # Horribly Hacked by: Michael Nelson ([email protected]) 1/18/94
  8. #
  9. # Author: Oscar Nierstrasz (Revised: 11/10/88)
  10. #
  11. # Uses: accent.pl
  12. #
  13. #v = "bib v1.0"; # Re-written in perl 4/7/93
  14. $v = "bib v1.1"; # 7/7/93: added -ha and -hk options
  15.  
  16. unshift(@INC,"/ump/csb/home/mln/ltrs/bin");
  17. require("accent.pl");
  18.  
  19. $usg = 'Usage: bib -<output style> [<refer file> ...]
  20. -txt -- plain text
  21. -f -- framemaker (numbered references)
  22. -fl -- framemaker (labelled references)
  23. -ms -- troff -ms macros
  24. -tb -- latex (thebibliography)
  25. -tn -- latex (enumerated list)
  26. -td -- latex (description list)
  27. -h -- generate HTML list of references
  28. -ha -- also generate separate abstract files
  29. -hk -- split output into files by first keyword
  30. -s -- search and print all (plausible) labels
  31. -l -- generate new labels (%L)
  32. -y -- re-write labels as YYAUTHchar (for chronological sorting)
  33. ';
  34.  
  35. chop($date = `date +%d.%m.%y`);
  36. $omn = '<A HREF="http://cui_www.unige.ch/OSG/omn.html"><I>OMN</I></A><P>';
  37. $sig = "<I>This file was generated by $v on $date.</I>\n$omn<P>\n";
  38.  
  39. $/ = "";
  40.  
  41. if ($txt) { &txt_init; open(STDOUT,"| fmt");
  42. while (<>) { &getref; print "$ref\n\n"; }
  43. }
  44. elsif ($ms) { &ms_init;
  45. while (<>) { &ms_accent; &getref; print ".IP \"$lbl\"\n$ref"; }
  46. }
  47. elsif ($f) { &fm_init; &mml_init;
  48. while (<>) { &mml_accent; &getref; print "<Reference>$ref"; }
  49. }
  50. elsif ($fl) { &fm_init;
  51. while (<>) { &mml_accent; &getref; print "<UnNumRef>[$lbl]\n$ref"; }
  52. }
  53. elsif ($fm) { &fm_init;
  54. while (<>) { &mml_accent; &getref; print "<Reference>[MText:$lbl]\n$ref"; }
  55. }
  56. elsif ($tb) {
  57. print "\\begin{thebibliography}{99}\n\n";
  58. &tex_init;
  59. while (<>) { &tex_accent; &getref; print "\\bibitem{$lbl}\n$ref\n"; }
  60. print "\n\\end{thebibliography}\n";
  61. }
  62. elsif ($tn) {
  63. print "\\begin{enumerate}\n\n";
  64. &tex_init;
  65. while (<>) { &tex_accent; &getref; print "\\item\n$ref\n"; }
  66. print "\n\\end{enumerate}\n";
  67. }
  68. elsif ($td) {
  69. print "\\begin{description}\n\n";
  70. &tex_init;
  71. while (<>) { &tex_accent; &getref; print "\\item{[$lbl]}\n$ref\n"; }
  72. print "\n\\end{description}\n";
  73. }
  74.  
  75. elsif ($h || $ha || $hk) {
  76. # NB: prints index to STDOUT; abstracts to $lbl.html
  77. if (!$hk) {
  78. print "<TITLE>Langley Technical Report Server</TITLE>\n\n";
  79. print "<i><a href=\"http://techreports.larc.nasa.gov/ltrs/ltrs.html\">Langley Technical Report Server</a></i>\n\n<OL>\n";
  80. }
  81. &html_init;
  82. while (<>) {
  83. &html_accent;
  84. &getref;
  85. if ($hk) {
  86. # ($kwd = $keys) =~ s/\s.*//;
  87. &setfile($ARGV);
  88. }
  89. # print "<LI>$ref\n";
  90. print "\n<LI><A NAME=\"$lbl\">$ref</A>\n";
  91. if ($ha && ($keys ne "")) {
  92. print "<P>\n<B>Keywords: </B>$keys";
  93. }
  94. if ($ha && ($abstract ne "")) {
  95. print "<P>\n<B>Abstract: </B>\n$abstract";
  96. }
  97. if ($ftp ne "") {
  98. print "<B>ftp:</B><A HREF=\"$url\">$ftp</A>";
  99. }
  100. print "<P>\n\n";
  101. }
  102. # if ($hk) { &endfiles; }
  103. if ($hk) { ; }
  104. else { print "</OL>\n$sig"; }
  105. }
  106.  
  107. # generate new labels:
  108. elsif ($l) { &genlabels; }
  109. # re-write labels with YEAR first:
  110. elsif ($y) {
  111. $/ = "\n";
  112. while (<>) {
  113. s/^%L ([A-Za-z]+)(\d+)([a-z]+)/%L $2 $1$2$3/;
  114. print;
  115. }
  116. }
  117. # search and print (plausible) labels:
  118. elsif ($s) {
  119. while (<>) {
  120. while (s/\b([a-zA-Z]{2,4}[0-9]{2}[a-z]?)\b//) {
  121. push(@labels,$1);
  122. }
  123. }
  124. $, = "\n";
  125. print sort(@labels);
  126. }
  127. else {
  128. die $usg;
  129. }
  130.  
  131. # build up a reference:
  132. sub getref {
  133. $ref = $lbl = $keys = $auth = $ed = $title = "";
  134. $abstract = $ftp = $url = $junk = "";
  135.  
  136. # study; # strangely, this slows us down!
  137.  
  138. s/\n\s+/\n/g; # remove leading white space
  139. s/%L (.*)\n// && ($lbl = $1); # label
  140. s/%K (.*)\n// && ($keys = $1); # keywords
  141. if ($lbl eq "") { print STDERR "Warning -- missing label:\n$_"; }
  142.  
  143. # Collect authors:
  144. while (s/%[AQ] (.*)\n(%[AQ] .*\n%[AQ])/$2/) { $auth .= "$1,\n"; }
  145. s/%[AQ] (.*)\n%[AQ] (.*)\n// && ($auth .= "$1 and\n$2");
  146. s/%[AQ] (.*)\n// && ($auth = $1);
  147.  
  148. # Collect editors:
  149. while (s/%E (.*)\n(%E .*\n%E)/$2/) { $ed .= "$1,\n"; }
  150. s/%E (.*)\n%E (.*)\n// && ($ed .= "$1 and\n$2");
  151. s/%E (.*)\n// && ($ed = $1);
  152.  
  153. # Check for missing authors:
  154. if ($auth eq "") {
  155. if ($ed ne "") { $auth = "$ed (Ed.)"; $ed = ""; }
  156. else {
  157. $auth = "(Anonymous)";
  158. print STDERR "Warning ($lbl): missing author\n";
  159. }
  160. }
  161. else { $ref = "$auth,\n"; }
  162.  
  163. # from this point on, ref ends without newline so commas
  164. # can be added incrementally.
  165.  
  166. # grab the title:
  167. s/%T ([^%]*)\n// && ($title = $1);
  168. if (/%U/) {
  169. $URL_TEMP = "";
  170. s/%U ([^%]*)\n// && ($URL_TEMP .= "$1");
  171. $LQ = "<B><A HREF=\"" . $URL_TEMP . "\">";
  172. $RQ = "</A></B>";
  173. }
  174. else {
  175. $LQ = "<B>";
  176. $RQ = "</B>";
  177. }
  178.  
  179. # determine kind of publication:
  180. if (/%J/) { # Journal paper
  181. $ref .= "$LQ$title$RQ";
  182. s/%J ([^%]*)\n// && ($ref .= ",\n$I$1$R");
  183. }
  184. elsif(/%[BR]/) { # Article in book
  185. $ref .= "$LQ$title$RQ";
  186. s/%B ([^%]*)\n// && ($ref .= ",\n$I$1$R");
  187. s/%R ([^%]*)\n// && ($ref .= ",\n$1");
  188. }
  189. elsif(/%B/) { # Article in book
  190. $ref .= "$LQ$title$RQ";
  191. s/%B ([^%]*)\n// && ($ref .= ",\n$I$1$R");
  192. }
  193. elsif(/%R/) { # Technical report
  194. $ref .= "$LQ$title$RQ";
  195. s/%R ([^%]*)\n// && ($ref .= ",\n$1");
  196. }
  197. else { $ref .= "$I$title$R"; } # Book
  198. # If more than one of J, B or R, will show up as JUNK:
  199. # if (/(%[JBR])/) {
  200. # print STDERR "Warning ($lbl): type conflict [$1]\n";
  201. # }
  202.  
  203. # add remaining fields in standard ord:
  204. if ($ed ne "") { $ref .= ",\n$ed (Ed.)"; }
  205. s/%S (.*)\n// && ($ref .= ",\n$1"); # series
  206. s/%V (.*)\n// && ($ref .= ",\nvol. $1"); # volume
  207. s/%N (.*)\n// && ($ref .= ", no. $1"); # number
  208. # s/%I ([^%]*)\n// && ($ref .= ",\n$1"); # institution
  209. s/%C ([^%]*)\n// && ($ref .= ",\n$1"); # city
  210. s/%D (.*)\n// && ($ref .= ", $1"); # date
  211. s/%P (.*)\n// && ($ref .= ",\npp. $1"); # page numbers
  212. s/%O ([^%]*)\n// && ($ref .= ",\n$1"); # other (e.g. to appear)
  213.  
  214. # these may not necessarily be printed:
  215. s/%X ([^%]*)\n// && do { $abstract = $1; }; # abstract
  216. s/%% ftp: (.*)\n// && ($ftp = $1); # should build a list?
  217. ($url = $ftp) =~ s!^([^:]+):(.*)$!ftp://$1/$2!;
  218.  
  219. while(s/%% ([^%]*)\n//) { $junk .= $1; }; # comments
  220.  
  221. $ref =~ s/$RQ,/,$RQ/go; # fix commas
  222. $ref .= ".\n";
  223.  
  224. # If anything is left, complain:
  225. if ($_ =~ /./) { print STDERR "Warning ($lbl) -- extra fields:\n$_\n"; }
  226. }
  227.  
  228. sub nextchar { local($c) = @_; return pack("c",1+unpack("c",$c)); }
  229.  
  230. sub fm_init { $I = "<Italic>"; $R = "<Plain>"; $LQ = "``"; $RQ = "''";
  231. print '
  232. <MML file -- generated by "bib">
  233. <Units pica>
  234. <!DefineTag Reference>
  235. <!DefineTag UnNumRef>
  236. <!DefineTag UnNumSub>
  237. ';
  238. }
  239.  
  240. sub ms_init { $I = "\\fI"; $R = "\\fR"; $LQ = "``"; $RQ = "''"; }
  241. sub tex_init { $I = "{\\it "; $R = "}"; $LQ = "``"; $RQ = "''"; }
  242. sub txt_init { $I = ""; $R = ""; $LQ = "\""; $RQ = "\""; }
  243. sub html_init {
  244. $I = "<I>"; $R = "</I>";
  245. # if (/%U/) { # Add URL
  246. # $LQ = "<A HREF=\"<B>``";
  247. # s/%U ([^%]*)\n// && ($LQ .= ",\n$1");
  248. $RQ = "''</B></A>";
  249. # }
  250. # else {
  251. # $LQ = "<B>``"; $RQ = "''</B>";
  252. # }
  253. }
  254.  
  255. sub genlabels {
  256. while (<>) {
  257. $auth = $date = $yr = "";
  258. # Get old label & year:
  259. s/^%L (.*)\n// && ($old = $1);
  260. ($oldyr = $old) =~ s/.*([0-9]{2}).*/$1/;
  261. # Get first author:
  262. /%[AEQ] (.*)/ && ($auth = $1);
  263. if ($auth eq "") { $auth = $old; }
  264. $auth =~ s/\\[:"]([aou])/\1e/g; # expand umlauts
  265. $auth =~ s/\\.//g; # delete other accents
  266. $auth =~ s/,.*//;
  267. $auth =~ s/.*\s(\S+)$/$1/;
  268. $auth =~ s/[^A-Za-z]//g; # delete nonalphas
  269. ($lbl = $auth) =~ s/(\S{4}).*/\1/;
  270. # Get year:
  271. /%D .*[0-9]{2}([0-9]{2})/ && ($yr = $1);
  272. if ($yr eq "") {
  273. print STDERR "Warning ($old): missing year ($oldyr)\n";
  274. $yr = $oldyr;
  275. }
  276. elsif (($oldyr ne "") && ($yr ne $oldyr)) {
  277. print STDERR "Warning ($old): date changed to $yr\n";
  278. }
  279. $lbl .= "$yr";
  280. push(@list,"%L $lbl\n$_");
  281. }
  282. $prev = "";
  283. @list = sort(@list);
  284. while ($ref = shift(@list)){
  285. $ref =~ s/^%L (.*)\n// && ($lbl = $1);
  286. if ($lbl eq $prev) { $char = &nextchar($char); }
  287. else { $char = "a"; $prev = $lbl; }
  288. $lbl .= $char;
  289. print "%L $lbl\n$ref";
  290. # if ($old ne $lbl) { print "$old -> $lbl\n"; }
  291. }
  292. }
  293.  
  294. sub setfile {
  295. local($kwd) = @_;
  296. local($file) = "$kwd.html";
  297. local($name);
  298. if ($file ne $prev) {
  299. close(STDOUT);
  300. if ($opened{$kwd}) {
  301. open(STDOUT,">>$file");
  302. # print STDERR "Reopening $file\n";
  303. }
  304. else {
  305. $opened{$kwd} = 1;
  306. open(STDOUT,">$file");
  307. $name = "Abstracts -- $kwd";
  308. #s/%T ([^%]*)\n// && ($html_title = $1);
  309.  
  310. print "<TITLE>$title</TITLE>\n\n";
  311. print "<i><A HREF=\"http://www.larc.nasa.gov/ltrs/ltrs.html\">Langley Technical Report Server</A></i><hr>\n\n";
  312. print "<OL>\n";
  313. # print STDERR "Creating $file\n";
  314. }
  315. $prev = $file;
  316. }
  317. }
  318.  
  319. sub endfiles {
  320. local($file);
  321. open(INDEX,">index.html");
  322. $name = "Index of References by Primary Keyword";
  323. print INDEX "<TITLE>$name</TITLE>\n\n";
  324. print INDEX "<H1>$name</H1>\n\n<DL>\n";
  325. close(STDOUT);
  326. foreach $kwd (sort(keys(%opened))) {
  327. $file = "$kwd.html";
  328. open(STDOUT,">>$file");
  329. print "</OL>\n\n$sig";
  330. close(STDOUT);
  331. print INDEX "<DD><A HREF=\"$file\">$kwd</A>\n";
  332. }
  333. print INDEX "</DL>\n$sig";
  334. close(INDEX);
  335. }
  336.  
  337. __END__
Add Comment
Please, Sign In to add comment