home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / fj / lang / perl / 150 < prev    next >
Encoding:
Text File  |  1992-11-20  |  6.4 KB  |  282 lines

  1. Path: sparky!uunet!stanford.edu!sun-barr!sh.wide!wnoc-kyo!kuis!s.u-tokyo!kekux!crimson!ume!is.tsukuba.ac.jp!coins!bizenya
  2. From: bizenya@coins.is.tsukuba.ac.jp (Katsujiroh NAKANO)
  3. Newsgroups: fj.lang.perl
  4. Subject: jfmt [Re: jcode.pl]
  5. Message-ID: <9253@coins.is.tsukuba.ac.jp>
  6. Date: 21 Nov 92 11:56:37 GMT
  7. References: <Bw27JJ.EAF@sran230.sra.co.jp> <BxwFqC.3F8@sran230.sra.co.jp> <9227@coins.is.tsukuba.ac.jp>
  8. Distribution: fj
  9. Organization: Coll. Of Info. Sci., U. of Tsukuba, Tsukuba-City, Ibaraki, Japan
  10. Lines: 270
  11.  
  12. >>>>> In <9227@coins.is.tsukuba.ac.jp>,
  13. >>>>>     bizenya@coins.is.tsukuba.ac.jp == $BCfLn(B said ....
  14. $BCfLn(B> $BCfLn(B@$BC^GHBg3X>pJs$G$9(B. 
  15.  
  16. $BCfLn(B> $B$$$d$"(B, $BD>@\$$$?$@$$$?$N$K(B,  report $B$b$7$J$$$G?=$7Lu$J$$$G(B
  17. $BCfLn(B> $B$9(B. $B$H$j$"$($:(B, $B<j;}$A$N<+J,$N:n$C$?(B jperl + kconv$BHG$N$b$N(B
  18. $BCfLn(B> $B$r(B jcode $B$G=q$-D>$7$F$_$^$7$?(B.
  19.  
  20.     $B$H$j$"$($:=P$7$^$9(B. 
  21.     $BF|K\8lHG(B fmt + $B&A$N(B jfmt $B$G$9(B. 
  22.     $B%P%0$^$@$$$C$Q$$$"$k$H;W$$$^$9$,(B, 
  23.     $B3'$5$s$H0l=o$KH/E8$7$F$$$-$?$$$H;W$$$^$9$N$G(B, 
  24.     $B$h$m$7$/$*4j$$$7$^$9(B 8-}
  25.  
  26.     README + source$B$G$9(B. 
  27.  
  28. -- cut here -- "README"
  29.  
  30.     jfmt -- $BF|K\8l(Bformatter
  31.  
  32.     { > $B$G;O$^$k9T$O0zMQ$G:8C<$G$"$k!    (B}
  33.     { $BNc$K;H$C$F$$$k(Bjfmt$B$N(Bwidth$B$O(B40    }
  34.  
  35. 1. $B35MW(B
  36.     $BF|K\8l$r@5$7$/07$&(B fmt clone $B$,M_$7$$$H;W$C$F:n$j$^$7$?(B. 
  37.     $B1Q8lHG(Bfmt$B$H0c$&E@$O!"EvA3F|K\8l$r@5$7$/07$&$H$$$&$3$H$NB>$K!"4JC1(B
  38. $B$J(Bformatting$B$b9T$J$$$^$9!#Nc$r<($7$^$9!#(B
  39.  
  40.     $B!&(B $itemhead $B$G;O$^$k9T$O2U>r=q$N9T$N;O$^$j$H$_$J$7$F!"$9$3$7(B
  41.           indent$B$r$D$1$F!"5M$a$k!#(B
  42.  
  43.         before:
  44.         >    $B!&(B $B:#F|$N@$3&>p@*$OHs>o$KIT0BDj$G!"C/$K$b9T$/Kv$,$o$+$i$J$$!#(B
  45.  
  46.         +--------------------------------------+
  47.         after:
  48.         >    $B!&(B $B:#F|$N@$3&>p@*$OHs>o$KIT0BDj$G!"(B
  49.         >      $BC/$K$b9T$/Kv$,$o$+$i$J$$!#(B
  50.  
  51.     $B!&(B /^\s+>/ $B$G;O$^$k9T$O0zMQ$H$_$J$7$F!"(Bindentation$B$rJQ$($J$$!#(B
  52.  
  53.         before:
  54.         >    $B!&(B $BJ?2>L>$GF~NO$9$k$N$K$O<!$N$h$&$K(B
  55.         >
  56.         >        > % dict -k yanbarukuina
  57.         >
  58.         >$B$HBG$C$F2<$5$$!#(B
  59.  
  60.         +--------------------------------------+
  61.         after:
  62.         >    $B!&(B $BJ?2>L>$GF~NO$9$k$N$K$O<!$N$h$&$K(B
  63.         >
  64.         >        > % dict -k yanbarukuina
  65.         >
  66.         >      $B$HBG$C$F2<$5$$!#(B
  67.  
  68. 2. install
  69.  
  70.     jcode.pl $B$H(B perl $B$,I,MW$G$9(B. $B@hF,$N(B perl $B$N(B path $B$@$1JQ99$7$F(B, $B$=(B
  71. $B$N$^$^$*;H$$2<$5$$(B.
  72.  
  73. 3. customize
  74.  
  75.     $B<!$NJQ?t$,(B, $B$=$l$>$l$N%Q%?!<%s$rI=$7$^$9(B. 
  76.  
  77.     $B!&(B [$postkinsoku]    $B9TKv$KMh$k$Y$-$G$J$$J8;z(B. 
  78.     $B!&(B [$prekinsoku]    $B9TF,$KMh$k$Y$-$G$J$$J8;z(B. 
  79.     $B!&(B [$itemhead]    $B0zMQ$N@hF,$H$_$J$9(B pattern. 
  80.     $B!&(B [$untermword]    $B9T$r1[$($FM_$7$/$J$$(B pattern $B$r9=@.$9$kJ8;z(B. 
  81.  
  82.     $B$^$?(B, 
  83.  
  84. 4. options
  85.  
  86.     -N    N $B$,B7$($kI}$G$9(B. -w N $B$G$bF1$8$$$_$K$J$j$^$9(B. 
  87.     -o    $B=PNO%3!<%I$G$9(B. 'jis', 'euc', 'sjis'$B$NCf$+$i$*A*$S2<$5$$(B. 
  88.  
  89. 5. bugs
  90.  
  91.     $B2?$,(B bug $B$+$C$F(B, $B$=$j$cCY$/$F$?$^$s$J$$$3$H$K$D$-$^$9(B. 
  92.     $B$=$N$&$A(B C $B$G=q$-D>$7$^$9$M(B. 
  93.  
  94. -- cut here -- "jfmt"
  95. #!/usr/local/bin/perl
  96.  
  97. #
  98. # jfmt - Japanese fmt
  99. #
  100. # usage: jfmt [-(w|width) N] [-joutcode JCODE] [filename ...]
  101. #
  102. $version = 'release 0 revision 0.1';
  103. #
  104.  
  105. require 'jcode.pl';
  106.  
  107. # inner kanji code for perl
  108.  
  109. $icode = 'euc';
  110.  
  111. # standard values
  112.  
  113. $tabstop = 8;
  114.  
  115. $w = 70;
  116.  
  117. $ocode = 'jis';
  118.  
  119. $SRC = <<'_END_KANJI_BLOCK_';
  120.  
  121. $postkinsoku = '(\?|\>|\)|\}|\]|$B$C(B|$B$c(B|$B$e(B|$B$g(B|$B$!(B|$B%!(B|$B$#(B|$B%#(B|$B$%(B|$B%%(B|$B$'(B|$B%'(B|$B$)(B|$B%)(B|$B$C(B|$B!<(B|$B!#(B|$B!"(B|$B!W(B|$B!K(B|$B![(B|$B!M(B|$B!O(B|$B!Q(B|$B!S(B|$B!U(B|\,|\.|\')';
  122. $prekinsoku = '($B!V(B|$B!J(B|$B!N(B|$B!P(B|$B!R(B|$B!T(B|$B!L(B|\[|\(|\<|\{|\`)';
  123. $itemhead = '($B!z(B|$B!y(B|$B!v(B|$B!{(B|$B!}(B|$B!|(B|$B""(B|$B"#(B|-|$B!&(B|#|\*|\(?[0-9]+[.)])';
  124. $untermword = '[-a-zA-Z0-9\026]';
  125.  
  126. _END_KANJI_BLOCK_
  127. $sp = '(\026*\025| )';
  128.  
  129. &jcode'convert(*SRC, $icode);
  130. eval $SRC || die "KANJI BLOCK:".$!."\n";
  131.  
  132. # user customise values
  133.  
  134. $ENV{'JFMT'} && eval $ENV{'JFMT'};
  135.  
  136. # parse command line option
  137.  
  138. while ($ARGV[0] =~ /^-/) {
  139.     $_ = shift;
  140.     if (/^-(width|w)$/) {$w = shift;}
  141.     elsif (/^-(\d+)$/) {$w = $1;}
  142.     elsif (/^-v/) {warn $version."\n";exit(0);}
  143.     elsif (/^-ocode$/) {
  144.     $joutcode = shift;
  145.     if (!/^(jis|euc|sjis)$/){
  146.         warn "jfmt: no such kanji code '$joutcode'\n";
  147.         exit(1);
  148.     }
  149.     }
  150.     else {
  151.     print STDERR 'jfmt: \'$_\', no such option.';
  152.     exit(1);
  153.     }
  154. }
  155.  
  156. #
  157. # main routine
  158. #
  159.  
  160. $k = 1;
  161. $indent = '';
  162. $line = $buf = '';
  163.  
  164. while (<>) {
  165.     &wraping(*_);
  166. }
  167. &flush;
  168.  
  169. sub wraping {
  170.     local(*_) = @_;
  171.     chop;
  172.     &jcode'convert(*_, $icode);
  173.     if (/\t/) {&untabify(*_);}
  174.     if (/^(>>>>>|${sp}+>|[^\s]+> )/){
  175.     $line .= $buf;
  176.     &flush;
  177.     $line = "$_";
  178.     &flush;
  179.     print "\n";
  180.     $quote = 1;
  181.     next;
  182.     }
  183.     if (/^${sp}*$/) {
  184.     if ($quote) {$indent = $recindent;}
  185.     else {$recindent = $indent; $indent = '';}
  186.     &flush;
  187.     print "\n";
  188.     $quote = 0;
  189.     next;
  190.     }
  191.     if (/^(${sp}+)([^\025 ].*)$/) {
  192.     $line .= $buf;
  193.     $buf = '';
  194.     $_ = $+;
  195.     $ws = $1;
  196.     if (length($indent) != length($ws)){
  197.         &flush;
  198.         $line = $ws;
  199.         if (/^$itemhead/) {
  200.         $indent = ' ' x (length($ws) + 2);
  201.         }
  202.         else {$indent = '';}
  203.     }
  204.     #else {$line =~ s/^${indent}//;}
  205.     }
  206.  
  207.     if (/($untermword|[.,)(])$/) {
  208.     $_ .= ' ';
  209.     }
  210.  
  211.     foreach (/[\200-\377]?./g) {
  212.     $k = 1;
  213.     if ($_ =~ /\s/) {
  214.         if (length($buffer.$i.$_) >= $w) {
  215.         $line .= $buf;
  216.         &flush;
  217.         }
  218.         else {
  219.         $line .= $buf . $_;
  220.         }
  221.         $buf = '';
  222.         next;
  223.     }
  224.  
  225.     if (/$prekinsoku/ 
  226.         && length($line.$buf.$_) >= ($w - 2)) {
  227.         $line .= $buf;
  228.         $buf = '';
  229.         &flush;
  230.         $line .= $_;
  231.         next;
  232.     }
  233.         
  234.     if (/$postkinsoku/) {
  235.         $line .= $buf . $_;
  236.         $buf = '';
  237.         next;
  238.     }
  239.  
  240.     if (/$untermword/) {
  241.         $buf .= $_;
  242.         if(length($line.$buf) >= $w) {
  243.         &flush;
  244.         }
  245.         next;
  246.     }
  247.  
  248.     # otherwise ...
  249.  
  250.     if (length($line.$buf.$_) >= $w) {
  251.         &flush;
  252.     }
  253.     $line .= $buf . $_;
  254.     $buf = '';
  255.     }
  256. }
  257.  
  258. sub untabify {
  259.     local(*_) = @_;
  260.     s/^([^\t]*)\t/$1 . 
  261.         ("\026" x ($tabstop - (length($1) % $tabstop) - 1)). "\025"/eg;
  262. }
  263.  
  264. sub tabify {
  265.     local(*_) = @_;
  266.     s/[^026]\026*\025/\t/g;
  267. }
  268.  
  269. sub flush {
  270.     if ($line =~ /^${sp}+$/) {return;}
  271.     $line =~ s/([^\026 ])[\026 ]+$/$1/;
  272.     if ($line) {
  273.     local($l) = $line;
  274.     &tabify(*l);
  275.     &jcode'convert(*l, $ocode, $icode);
  276.     if ($k) {print $l."\n";}
  277.     else {print $l;}
  278.     $k = 0;
  279.     }
  280.     $line = $indent;
  281. }
  282.