summaryrefslogtreecommitdiff
path: root/contrib/gpinyin/gpinyin.pl
blob: 14cbb453fc6f6505171c7e2749161a121734334c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#! /usr/bin/env perl

# gpinyin - European-like Chinese writing `pinyin' into `groff'

# Source file position: <groff-source>/contrib/gpinyin/gpinyin.pl
# Installed position: <prefix>/bin/gpinyin

# Copyright (C) 2014  Free Software Foundation, Inc.

# Written by Bernd Warken <groff-bernd.warken-72@web.de>.

my $version = '1.0.4';

# This file is part of `gpinyin', which is part of `groff'.

# `groff' is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# `groff' is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You can find a copy of the GNU General Public License in the internet
# at <http://www.gnu.org/licenses/gpl-2.0.html>.

########################################################################

use strict;
use warnings;
#use diagnostics;

# temporary dir and files
use File::Temp qw/ tempfile tempdir /;

# needed for temporary dir
use File::Spec;

# for `copy' and `move'
use File::Copy;

# for fileparse, dirname and basename
use File::Basename;

# current working directory
use Cwd;

# $Bin is the directory where this script is located
use FindBin;


########################################################################
# system variables and exported variables
########################################################################

$\ = "\n";	# final part for print command

########################################################################
# read-only variables with double-@ construct
########################################################################

our $File_split_env_sh;
our $File_version_sh;
our $Groff_Version;

my $before_make;		# script before run of `make'
{
  my $at = '@';
  $before_make = 1 if '@VERSION@' eq "${at}VERSION${at}";
}

my %at_at;
my $file_gpinyin_test_pl;
my $gpinyin_libdir;

if ($before_make) {
  my $gpinyin_source_dir = $FindBin::Bin;
  $at_at{'BINDIR'} = $gpinyin_source_dir;
  $at_at{'G'} = '';
  $gpinyin_libdir = '@gpinyin_dir@';
} else {
  $at_at{'BINDIR'} = '@BINDIR@';
  $at_at{'G'} = '@g@';
  $gpinyin_libdir = '@gpinyin_dir@';
  unshift(@INC, $gpinyin_libdir);
}

require 'subs.pl';


########################################################################
# options
########################################################################

foreach (@ARGV) {
  if ( /^(-h|--h|--he|--hel|--help)$/ ) {
    print q(Usage for the 'gpinyin' program:);
    print 'gpinyin [-] [--] [filespec...] normal file name arguments';
    print 'gpinyin [-h|--help]            gives usage information';
    print 'gpinyin [-v|--version]         displays the version number';
    print q(This program is a 'groff' preprocessor that handles ) .
      q(pinyin parts in 'roff' files.);
    exit;
  } elsif (/^(-v|--v|--ve|--ver|--vers|--versi|--versio|--version)$/) {
    print q('gpinyin' version ) . $version;
    exit;
  }
}


########################################################################
# input
########################################################################

my $pinyin_mode = 0;	# not in Pinyin mode

my @output_n =	# nroff
  (
   '.ie n \\{\\',
  );

my @output_t =	# troff
  (
   '.el \\{\\',
  );

foreach (<>) {	# get line from input
  chomp;
  s/\s+$//;		# remove final spaces
# &err('gpinyin: ' . $_);

  my $line = $_;	# with starting blanks

  # .pinyin start or begin line
  if ( $line =~ /^[.']\s*pinyin\s+(start|begin)$/ ) {
    if ( $pinyin_mode ) {
      # `.pinyin' was started twice, ignore
      &err( q['.pinyin' starter was run several times] );
    } else {	# new pinyin start
      $pinyin_mode = 1;
    }
    next;
  }

  # .pinyin stop or end line
  if ( $line =~ /^[.']\s*pinyin\s+(stop|end)$/ ) {
    if ( $pinyin_mode ) {		# normal stop
      $pinyin_mode = 0;
      &finish_pinyin_mode( \@output_n, \@output_t );
    } else {	# ignore
      &err( 'gpinyin: there was a .pinyin stop without ' .
	'being in pinyin mode' );
    }
    next;
  }

  # now not a .pinyin line


  if ( $pinyin_mode ) {	# within Pinyin
    my $starting_blanks = '';
    $starting_blanks = $1 if ( s/^(s+)// );	# handle starting spaces

    my %outline = &handle_line($starting_blanks, $line);
#&err('gpinyin outline n: ' . $outline{'n'} );
#&err('gpinyin outline t: ' . $outline{'t'} );
    push @output_n, $outline{'n'};
    push @output_t, $outline{'t'};
  } else {	# normal roff line, not within Pinyin
    print $line;
  }
  next;
}	# end of input line


########################################################################
# end of file without stopping `pinyin' mode
if ( $pinyin_mode ) {
  &finish_pinyin_mode( \@output_n, \@output_t );
}

########################################################################


1;
########################################################################
### Emacs settings
# Local Variables:
# mode: CPerl
# End: