| 1 | #!/usr/bin/perl |
|---|
| 2 | # |
|---|
| 3 | # The main intention of this script is to make unchangable help files |
|---|
| 4 | # delivered with some of the integrated software compatible with |
|---|
| 5 | # the arb_hlp2xml help converter. |
|---|
| 6 | |
|---|
| 7 | use strict; |
|---|
| 8 | use warnings; |
|---|
| 9 | |
|---|
| 10 | my $inputFilename; |
|---|
| 11 | my $outputFilename; |
|---|
| 12 | |
|---|
| 13 | use Carp; |
|---|
| 14 | |
|---|
| 15 | sub die_input($) { |
|---|
| 16 | my ($msg) = @_; |
|---|
| 17 | confess("$inputFilename:$.: $msg"); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | sub clustalTopic2Section($) { |
|---|
| 21 | my ($line) = @_; |
|---|
| 22 | if ($line =~ /^\s*\>\>\s*([^<>]+)\s*\<\<\s*(.*)$/o) { |
|---|
| 23 | # '>>topic<< moretext' -> 'SECTION topic moretext' |
|---|
| 24 | $line = "SECTION $1 $2\n"; |
|---|
| 25 | } |
|---|
| 26 | return $line; |
|---|
| 27 | } |
|---|
| 28 | sub fancyitemind2star($) { |
|---|
| 29 | my ($line) = @_; |
|---|
| 30 | $line =~ s/^(\s+)[\+\=]\s/$1\* /; |
|---|
| 31 | return $line; |
|---|
| 32 | } |
|---|
| 33 | sub formatFormatHeadlines($) { |
|---|
| 34 | my ($line) = @_; |
|---|
| 35 | if ($line =~ /File\sFormat/o) { |
|---|
| 36 | $line =~ s/^\s+/ /og; |
|---|
| 37 | } |
|---|
| 38 | elsif ($line =~ /Example\s.*\sfile/o) { |
|---|
| 39 | $line =~ s/^\s+/ /og; |
|---|
| 40 | } |
|---|
| 41 | return $line; |
|---|
| 42 | } |
|---|
| 43 | sub readseq_promote_sections($) { |
|---|
| 44 | my ($line) = @_; |
|---|
| 45 | if ($line =~ /^\s+\|[\|]+\s+/o) { |
|---|
| 46 | $line = "SECTION $'"; |
|---|
| 47 | } |
|---|
| 48 | return $line; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | sub linkToWeb($) { |
|---|
| 52 | my ($line) = @_; |
|---|
| 53 | if ($line =~ /LINK\{(http|ftp)[^\}]*\}/o) { |
|---|
| 54 | die_input "illegal LINK{web}-element in generated help"; |
|---|
| 55 | } |
|---|
| 56 | $line =~ s/((http|ftp):\/\/[^\s]*)(\s|$)/LINK{$1}/og; |
|---|
| 57 | return $line; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | sub elimEOLwhitespace($) { |
|---|
| 61 | my ($line) = @_; |
|---|
| 62 | $line =~ s/\s+$//o; |
|---|
| 63 | return $line; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | sub main() { |
|---|
| 67 | my $args = scalar(@ARGV); |
|---|
| 68 | if ($args!=2) { |
|---|
| 69 | die "Usage: postprocess_genhelp.pl input-filename output-filename\n". |
|---|
| 70 | "Filters stdin to stdout, filenames are only passed to trigger specific behavior\n". |
|---|
| 71 | "OR to print proper error messages.\n "; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | $inputFilename = $ARGV[0]; |
|---|
| 75 | $outputFilename = $ARGV[1]; |
|---|
| 76 | |
|---|
| 77 | my $filename = $outputFilename; |
|---|
| 78 | |
|---|
| 79 | my @reg = (); |
|---|
| 80 | my @fun = (); |
|---|
| 81 | |
|---|
| 82 | if ($filename =~ /clustalw/) { |
|---|
| 83 | push @reg, qr/([^\s]\s)\s+([\(])/; |
|---|
| 84 | push @reg, qr/([\.:])(\s)\s+/; |
|---|
| 85 | push @fun, \&clustalTopic2Section; |
|---|
| 86 | } |
|---|
| 87 | elsif ($filename =~ /readseq/) { |
|---|
| 88 | push @reg, qr/([\.])(\s)\s+/; |
|---|
| 89 | push @reg, qr/(LINE\s)\s+([0-9]+)/; |
|---|
| 90 | push @fun, \&fancyitemind2star; |
|---|
| 91 | push @fun, \&readseq_promote_sections; |
|---|
| 92 | if ($filename =~ /Formats/) { |
|---|
| 93 | push @fun, \&formatFormatHeadlines; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | push @fun, \&linkToWeb; |
|---|
| 98 | push @fun, \&elimEOLwhitespace; |
|---|
| 99 | |
|---|
| 100 | my $titled = 0; |
|---|
| 101 | |
|---|
| 102 | while (defined($_=<STDIN>)) { |
|---|
| 103 | chomp; |
|---|
| 104 | # convert all lines starting at column zero into SECTIONs |
|---|
| 105 | if (s/^([^ \#\n])/SECTION $1/) { |
|---|
| 106 | # convert first SECTION into TITLE |
|---|
| 107 | if ($titled==0) { |
|---|
| 108 | s/^SECTION/TITLE/; |
|---|
| 109 | $titled=1; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | foreach my $re (@reg) { |
|---|
| 114 | while ($_ =~ $re) { |
|---|
| 115 | $_ = $`.$1.$2.$'; |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | foreach my $fn (@fun) { |
|---|
| 119 | $_ = &$fn($_); |
|---|
| 120 | } |
|---|
| 121 | print $_."\n"; |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | main(); |
|---|