Debian Codegraph @

texk/texlive/linked_scripts/easydtx/edtx2dtx.pl

raw
1 #!/usr/bin/env perl
2
3 # This file is a part of TeX package EasyDTX, available at
4 # https://ctan.org/pkg/easydtx and https://github.com/sasozivanovic/easydtx.
5 #
6 # Copyright (c) 2023- Saso Zivanovic <[email protected]>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 #
21 # The files belonging to this program and covered by GPL are listed in
22 # <texmf>/doc/support/easydtx/FILES.
23
24 use strict;
25 use Getopt::Long;
26
27 my $usage = <<END;
28 EasyDTX: convert .edtx into .dtx
29 Usage: edtx2dtx [options] filename.edtx > filename.dtx
30 Options: TODO
31 END
32
33 my $VERSION = '0.2.0';
34 my ($help, $print_version);
35
36 my $comment = '%';
37 my $begin_macrocode = '^% \\\\begin{macrocode}';
38 my $end_macrocode = '% \\\\end{macrocode}';
39 my $Begin_macrocode = '';
40 my $End_macrocode = '';
41 my $strip = '';
42
43 Getopt::Long::Configure (qw/bundling no_ignore_case/);
44 GetOptions(
45 "comment|c=s" => \$comment,
46 "begin-macrocode|b=s" => \$begin_macrocode,
47 "end-macrocode|e=s" => \$end_macrocode,
48 "Begin-macrocode|B=s" => \$Begin_macrocode,
49 "End-macrocode|E=s" => \$End_macrocode,
50 "strip-empty|s" => \$strip,
51 "help|h|?" => \$help,
52 "version|V" => \$print_version,
53 ) or die $usage;
54 if ($help) {print($usage); exit 0}
55 if ($print_version) { print("edtx2dtx $VERSION\n"); exit 0 }
56 die $usage unless @ARGV == 1;
57
58 my $keep_begin_macrocode = '';
59 my $keep_end_macrocode = '';
60 if ($Begin_macrocode) {
61 $begin_macrocode = $Begin_macrocode;
62 $keep_begin_macrocode = 1;
63 }
64 if ($End_macrocode) {
65 $end_macrocode = $End_macrocode;
66 $keep_end_macrocode = 1;
67 }
68
69 sub begin_macrocode { print("% \\begin{macrocode}\n"); }
70 sub end_macrocode { print("% \\end{macrocode}\n"); }
71
72 # doc: replace initial in-comments with out-comments
73 sub convert_comments { s/^\Q$comment\E+// && print('%' x length($&)); }
74
75 my $indoc;
76 sub process_edtx {
77 my $done;
78 my $empty;
79 while (<>) {
80 if (/$end_macrocode/) { # trailer starts here
81 if ($keep_end_macrocode) {
82 $done = 1;
83 } else {
84 end_macrocode unless $indoc;
85 last;
86 }
87 } elsif (/$begin_macrocode/) {
88 die "Nested '$begin_macrocode'";
89 }
90 if (/^ *(\Q$comment$comment\E+)/) { # code: multiple comments
91 begin_macrocode if $indoc;
92 print($empty); $empty = '';
93 print;
94 $indoc = 0;
95 } elsif (/^\Q$comment\E(<[^>]*>)(.*)$/) { # code: unindented guard
96 begin_macrocode if $indoc;
97 print;
98 $indoc = 0;
99 } elsif (/^( *)\Q$comment\E(<[^>]*>) *(.*)$/) { # code: indented guard
100 begin_macrocode if $indoc;
101 print("%$2$1$3\n");
102 $indoc = 0;
103 } elsif (/^\Q$comment\E *(.*)$/) { # doc: unindented comment
104 end_macrocode unless $indoc;
105 convert_comments;
106 print;
107 $indoc = 1;
108 } elsif (/^( ?)( *)\Q$comment\E *(.*)$/) { # doc: indented comment
109 end_macrocode unless $indoc;
110 print "%$2$3\n";
111 $indoc = 1;
112 } elsif (/^\s*$/) { # code: empty line
113 if ($strip) {
114 $empty .= $_ unless $indoc;
115 } else {
116 print;
117 }
118 } else { # code
119 print($empty); $empty = '';
120 begin_macrocode if $indoc;
121 print;
122 $indoc = 0;
123 }
124 if ($done) {
125 end_macrocode unless $indoc;
126 last;
127 }
128 }
129 }
130
131 my $edtx = @ARGV[0];
132 my $dtx = $edtx;
133 $dtx =~ s/\.edtx$/.dtx/;
134
135 my $first = 1;
136 print("% \\iffalse\n%\n");
137 while (<>) {
138 if ($first and s/$edtx +(.*)/$dtx (generated from $edtx by edtx2dtx)/) {
139 print;
140 $first = 0;
141 } elsif (/$begin_macrocode/) {
142 print("% \\fi\n%\n");
143 if ($keep_begin_macrocode) {
144 $indoc = 1 if /^\Q$comment\E/;
145 begin_macrocode unless $indoc;
146 print;
147 } else {
148 $indoc = 1;
149 }
150 process_edtx;
151 } elsif (/$end_macrocode/) {
152 die "'$end_macrocode' without the opening '$begin_macrocode'!";
153 } else {
154 convert_comments;
155 print;
156 }
157 }