Imported Upstream version 1.22.4
[platform/upstream/groff.git] / contrib / gpinyin / gpinyin.pl
1 #! /usr/bin/env perl
2
3 # gpinyin - European-like Chinese writing 'pinyin' into 'groff'
4
5 # Source file position: <groff-source>/contrib/gpinyin/gpinyin.pl
6 # Installed position: <prefix>/bin/gpinyin
7
8 # Copyright (C) 2014-2018 Free Software Foundation, Inc.
9
10 # Written by Bernd Warken <groff-bernd.warken-72@web.de>.
11
12 my $version = '1.0.4';
13
14 # This file is part of 'gpinyin', which is part of 'groff'.
15
16 # 'groff' is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 2 of the License, or
19 # (at your option) any later version.
20
21 # 'groff' is distributed in the hope that it will be useful, but
22 # WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 # General Public License for more details.
25
26 # You can find a copy of the GNU General Public License in the internet
27 # at <http://www.gnu.org/licenses/gpl-2.0.html>.
28
29 ########################################################################
30
31 use strict;
32 use warnings;
33 #use diagnostics;
34
35 # temporary dir and files
36 use File::Temp qw/ tempfile tempdir /;
37
38 # needed for temporary dir
39 use File::Spec;
40
41 # for 'copy' and 'move'
42 use File::Copy;
43
44 # for fileparse, dirname and basename
45 use File::Basename;
46
47 # current working directory
48 use Cwd;
49
50 # $Bin is the directory where this script is located
51 use FindBin;
52
53
54 ########################################################################
55 # system variables and exported variables
56 ########################################################################
57
58 $\ = "\n";      # final part for print command
59
60 ########################################################################
61 # read-only variables with double-@ construct
62 ########################################################################
63
64 our $File_split_env_sh;
65 our $File_version_sh;
66 our $Groff_Version;
67
68 my $before_make;                # script before run of 'make'
69 {
70   my $at = '@';
71   $before_make = 1 if '@VERSION@' eq "${at}VERSION${at}";
72 }
73
74 my %at_at;
75 my $file_gpinyin_test_pl;
76 my $gpinyin_libdir;
77
78 if ($before_make) {
79   my $gpinyin_source_dir = $FindBin::Bin;
80   $at_at{'BINDIR'} = $gpinyin_source_dir;
81   $at_at{'G'} = '';
82   $gpinyin_libdir = '@gpinyin_dir@';
83 } else {
84   $at_at{'BINDIR'} = '@BINDIR@';
85   $at_at{'G'} = '@g@';
86   $gpinyin_libdir = '@gpinyin_dir@';
87   unshift(@INC, $gpinyin_libdir);
88 }
89
90 require 'subs.pl';
91
92
93 ########################################################################
94 # options
95 ########################################################################
96
97 foreach (@ARGV) {
98   if ( /^(-h|--h|--he|--hel|--help)$/ ) {
99     print q(Usage for the 'gpinyin' program:);
100     print 'gpinyin [-] [--] [filespec...] normal file name arguments';
101     print 'gpinyin [-h|--help]            gives usage information';
102     print 'gpinyin [-v|--version]         displays the version number';
103     print q(This program is a 'groff' preprocessor that handles ) .
104       q(pinyin parts in 'roff' files.);
105     exit;
106   } elsif (/^(-v|--v|--ve|--ver|--vers|--versi|--versio|--version)$/) {
107     print q('gpinyin' version ) . $version;
108     exit;
109   }
110 }
111
112
113 ########################################################################
114 # input
115 ########################################################################
116
117 my $pinyin_mode = 0;    # not in Pinyin mode
118
119 my @output_n =  # nroff
120   (
121    '.ie n \\{\\',
122   );
123
124 my @output_t =  # troff
125   (
126    '.el \\{\\',
127   );
128
129 foreach (<>) {  # get line from input
130   chomp;
131   s/\s+$//;             # remove final spaces
132 # &err('gpinyin: ' . $_);
133
134   my $line = $_;        # with starting blanks
135
136   # .pinyin start or begin line
137   if ( $line =~ /^[.']\s*pinyin\s+(start|begin)$/ ) {
138     if ( $pinyin_mode ) {
139       # '.pinyin' was started twice, ignore
140       &err( q['.pinyin' starter was run several times] );
141     } else {    # new pinyin start
142       $pinyin_mode = 1;
143     }
144     next;
145   }
146
147   # .pinyin stop or end line
148   if ( $line =~ /^[.']\s*pinyin\s+(stop|end)$/ ) {
149     if ( $pinyin_mode ) {               # normal stop
150       $pinyin_mode = 0;
151       &finish_pinyin_mode( \@output_n, \@output_t );
152     } else {    # ignore
153       &err( 'gpinyin: there was a .pinyin stop without ' .
154         'being in pinyin mode' );
155     }
156     next;
157   }
158
159   # now not a .pinyin line
160
161
162   if ( $pinyin_mode ) { # within Pinyin
163     my $starting_blanks = '';
164     $starting_blanks = $1 if ( s/^(s+)// );     # handle starting spaces
165
166     my %outline = &handle_line($starting_blanks, $line);
167 #&err('gpinyin outline n: ' . $outline{'n'} );
168 #&err('gpinyin outline t: ' . $outline{'t'} );
169     push @output_n, $outline{'n'};
170     push @output_t, $outline{'t'};
171   } else {      # normal roff line, not within Pinyin
172     print $line;
173   }
174   next;
175 }       # end of input line
176
177
178 ########################################################################
179 # end of file without stopping 'pinyin' mode
180 if ( $pinyin_mode ) {
181   &finish_pinyin_mode( \@output_n, \@output_t );
182 }
183
184 ########################################################################
185
186
187 1;
188 ########################################################################
189 ### Emacs settings
190 # Local Variables:
191 # mode: CPerl
192 # End: