"Fossies" - the Fresh Open Source Software Archive

Member "xombrero-1.6.4/ascii2txt.pl" (17 Feb 2015, 5323 Bytes) of package /linux/www/old/xombrero-1.6.4.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ascii2txt.pl" see the Fossies "Dox" file reference documentation.

    1 #!/bin/perl
    2 
    3 # Copyright (c) 2010,2011,2012 Todd T. Fries <todd@fries.net>
    4 #
    5 # Permission to use, copy, modify, and distribute this software for any
    6 # purpose with or without fee is hereby granted, provided that the above
    7 # copyright notice and this permission notice appear in all copies.
    8 #
    9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   16 
   17 # read in 'mandoc -Tascii' formatted man pages, spit out txt useful for further
   18 # processing by other utilities
   19 
   20 use strict;
   21 use warnings;
   22 
   23 our $fileinfo = $ARGV[0];
   24 
   25 our $verbose = 0;
   26 
   27 my $line;
   28 my @lines;
   29 while(<STDIN>) {
   30     $line = $_;
   31     push @lines,$line;
   32 }
   33 my $oline = "";
   34 my $fmtline = "%s";
   35 foreach $line (@lines) {
   36 
   37     my $newline = "";
   38     foreach my $seg (split(/(.\x08.)/,$line)) {
   39         my $newseg = $seg;
   40         $newseg =~ m/^(.)\x08(.)$/;
   41         if (!defined($1) || !defined($2)) {
   42             $newline .= $seg;
   43             next;
   44         }
   45         if ($1 eq $2) {
   46             $newline .= "${2}";
   47             next;
   48         }
   49         if ($1 eq "_") {
   50             $newline .= "${2}";
   51             next;
   52         }
   53         $newline .= $seg;
   54         next;
   55     }
   56     if ($verbose > 0) {
   57         printf STDERR "==> text{bf,it}\n   line: <%s>\nnewline: <%s>\n",$line,$newline;
   58     }
   59     $line = $newline;
   60     $line =~ m/(.)\x08/;
   61     if (defined($1)) {
   62         printf STDERR "Removing %s\\x08\n",$1;
   63     }
   64     $line =~ s/.\x08//g;
   65 
   66     # combine adjacent entries
   67     foreach my $macro (("textbf", "textit")) {
   68         $oline = "";
   69         while ($oline ne $line) {
   70             #printf STDERR "combine adjacent\n";
   71             $oline = $line;
   72             $line =~ s/\xab\\${macro}\{([^\}]*)\}\xbb\xab\\${macro}\{([^\}]*)\}\xbb/\xab\\${macro}\{$1$2\}\xbb/g;
   73         }
   74     }
   75     # combine space separated
   76     foreach my $macro (("textbf")) {
   77         #printf STDERR "combine space\n";
   78         $oline = "";
   79         while ($oline ne $line) {
   80             $oline = $line;
   81             $line =~ s/\xab\\${macro}\{([^\}]*)\}\xbb[ ]+\xab\\${macro}\{([^\}]*)\}\xbb/\xab\\${macro}\{$1 $2\}\xbb/g;
   82         }
   83     }
   84 
   85     # do the substitution one at a time to be sure to add all man pages, not just the last ones per line.
   86     # XXX provide an exceptions list, audio(9) has mono(1) and stereo(2)
   87     # XXX references, which are _not_ man pages
   88     $oline = "";
   89     while ($oline ne $line) {
   90         $oline=$line;
   91         $line =~ s/\{(http|ftp|https):\/\/(.*)\}/ $1:\/\/$2 /;
   92         if (0) {
   93         if ($line =~ m/ ([a-z][a-z0-9\.\-\_]*)\(([1-9])\)([,\.\) ])/) {
   94             my $quote = texquote($1);
   95             $line =~ s/ ([a-z][a-z0-9\.\-\_]*)\(([1-9])\)([,\.\) ])/ \xab\\man{$quote}{$2}\xbb$3/;
   96         }
   97         
   98         if ($line =~ m/ ([a-z][a-z0-9\.\-\_]*)\(([1-9])\)$/) {
   99             my $quote = texquote($1);
  100             $line =~ s/ ([a-z][a-z0-9\.\-\_]*)\(([1-9])\)$/ \xab\\man{$quote}{$2}\xbb/;
  101         }
  102         }
  103     }
  104     my @macros = ("textbf","textit","man","href");
  105     # quote arguments for tex
  106     foreach my $macro (@macros) {
  107         my $newline = "";
  108         foreach my $seg (split(/(\xab\\${macro}\{[^\xbb]*\}\xbb)/,$line)) {
  109             #printf STDERR "quote args\n";
  110             my $newseg = $seg;
  111             # check for nesting first; we only want to escape the
  112             # inner most argument, process nested macro if it has a nested macro
  113             # since the nested macro won't catch in the other regex cases
  114             my $foundnest = 0;
  115             foreach my $nest (@macros) {
  116                 if ($macro eq $nest) {
  117                     next;
  118                 }
  119                 
  120                 $newseg =~ m/^\xab\\${macro}\{[ ]*\\${nest}\{([^\xbb]*)\}\{([^\xbb]*)\}\}\xbb$/;
  121                 if (defined($2)) {
  122                     $foundnest = 1;
  123                     $newline .= "\xab\\${macro}\{\\${nest}\{".texquote($1)."\}\{".texquote(${2})."\}\}\xbb";
  124                     last;
  125                 }
  126                 $newseg =~ m/^\xab\\${macro}\{[ ]*\\${nest}\{([^\xbb]*)\}\}\xbb$/;
  127                 if (defined($1)) {
  128                     $foundnest = 1;
  129                     $newline .= "\xab\\${macro}\{\\${nest}\{".texquote($1)."\}\}\xbb";
  130                     last;
  131                 }
  132             }
  133             if ($foundnest > 0) {
  134                 next;
  135             }
  136                 
  137             # check for 2 args first
  138             $newseg =~ m/^\xab\\${macro}\{([^\xbb]*)\}\{([^\xbb]*)\}\xbb$/;
  139             if (defined($2)) {
  140                 $newline .= "\xab\\${macro}\{".texquote($1)."\}\{".texquote(${2})."\}\xbb";
  141                 next;
  142             }
  143             $newseg =~ m/^\xab\\${macro}\{([^\xbb]*)\}\xbb$/;
  144             if (defined($1)) {
  145                 $newline .= "\xab\\${macro}\{".texquote($1)."\}\xbb";
  146                 next;
  147             }
  148             $newline .= $seg;
  149         }
  150         $line = $newline;
  151     }
  152     printf $fmtline,$line;
  153 }
  154 
  155 1;
  156 
  157 sub texquote {
  158         my ($text) = @_;
  159         my ($ret) = "";
  160         my ($esctest) = "";
  161         my ($escbase) = "BaCkSlAsH";
  162         my ($esccount) = 0;
  163 
  164     #$verbose++;
  165     if ($verbose > 0) {
  166             printf STDERR "\ntexquote: '%s' -> ",$text;
  167     }
  168 
  169         if ($text =~ m/\\/) {
  170                 $esctest=sprintf "%s%d",$escbase,$esccount++;
  171                 while ($text =~ m/$esctest/) {
  172                         $esctest=sprintf "%s%d",$escbase,$esccount++;
  173                 }
  174                 $text =~ s/\\/$esctest/g;
  175         if ($verbose > 0) {
  176                     printf STDERR "'%s' -> ",$text;
  177         }
  178         }
  179 
  180         $text =~ s/([%\{\}_#\&\$\^])/\\$1/g;
  181         $text =~ s/([<>\|\*~])/\{\$$1\$\}/g;
  182 
  183         if ($esccount > 0) {
  184                 $text =~ s/$esctest/\$\\backslash\$/g;
  185         }
  186     if ($verbose > 0) {
  187             printf STDERR "'%s'\n",$text;
  188     }
  189     #$verbose--;
  190 
  191         return $text;
  192 }