| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use File::Basename qw(dirname); |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
sty2dtx -- Converts a LaTeX .sty file to a documented .dtx file |
| 9 |
|
| 10 |
=head1 VERSION |
| 11 |
|
| 12 |
Version: v2.4 - 2022/10/18 |
| 13 |
|
| 14 |
=head1 LINKS |
| 15 |
|
| 16 |
CTAN: https://ctan.org/pkg/sty2dtx |
| 17 |
|
| 18 |
Repository: https://github.com/MartinScharrer/sty2dtx |
| 19 |
|
| 20 |
Issues: https://github.com/MartinScharrer/sty2dtx/issues |
| 21 |
|
| 22 |
=head1 COPYRIGHT |
| 23 |
|
| 24 |
Copyright (c) 2010-2022 Martin Scharrer <[email protected]> |
| 25 |
|
| 26 |
This program is free software: you can redistribute it and/or modify |
| 27 |
it under the terms of the GNU General Public License as published by |
| 28 |
the Free Software Foundation, either version 3 of the License, or |
| 29 |
(at your option) any later version. |
| 30 |
|
| 31 |
This program is distributed in the hope that it will be useful, |
| 32 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 33 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 34 |
GNU General Public License for more details. |
| 35 |
|
| 36 |
You should have received a copy of the GNU General Public License |
| 37 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 38 |
|
| 39 |
|
| 40 |
=head1 DESCRIPTION |
| 41 |
|
| 42 |
Converts a .sty file (LaTeX package) to .dtx format (documented LaTeX source), |
| 43 |
by surrounding macro definitions with 'macro' and 'macrocode' environments. |
| 44 |
The macro name is automatically inserted as an argument to the 'macro' |
| 45 |
environemnt. |
| 46 |
Code lines outside macro definitions are wrapped only in 'macrocode' |
| 47 |
environments. Empty lines are removed. |
| 48 |
The script is not thought to be fool proof and 100% accurate but rather |
| 49 |
as a good start to convert undocumented style files to .dtx files. |
| 50 |
|
| 51 |
|
| 52 |
=head2 Basic Usage |
| 53 |
|
| 54 |
perl sty2dtx.pl infile [infile ...] outfile |
| 55 |
|
| 56 |
or |
| 57 |
|
| 58 |
perl sty2dtx.pl < file.sty > file.dtx |
| 59 |
|
| 60 |
=head2 Supported Definitions |
| 61 |
|
| 62 |
The following macro definitions are detected when they are at the start of a |
| 63 |
line (can be prefixed by \global, \long, \protected and/or \outer): |
| 64 |
|
| 65 |
\def \edef \gdef \xdef |
| 66 |
\newcommand{\name} \newcommand*{\name} |
| 67 |
\newcommand\name \newcommand*\name |
| 68 |
\renewcommand{\name} \renewcommand*{\name} |
| 69 |
\renewcommand\name \renewcommand*\name |
| 70 |
\providecommand{\name} \providecommand*{\name} |
| 71 |
\providecommand\name \providecommand*\name |
| 72 |
\@namedef{\name} \@namedef\name |
| 73 |
|
| 74 |
The following environment definitions are detected when they are at the start |
| 75 |
of a line: |
| 76 |
|
| 77 |
\newenvironment{name} \renewenvironemnt{name} \provideenvironment{name} |
| 78 |
|
| 79 |
The macro and environment definition must either end at the same line or with |
| 80 |
a 'C<}>' on its own on a line. |
| 81 |
|
| 82 |
|
| 83 |
=head1 USAGE |
| 84 |
|
| 85 |
sty2dtx [<options>] [--<VAR>=<VALUE> ...] [--] [<infile(s)>] [<outfile>] |
| 86 |
|
| 87 |
=head2 Files |
| 88 |
|
| 89 |
=over 2 |
| 90 |
|
| 91 |
=item * |
| 92 |
can be 'C<->' for STDIN or STDOUT, which is the default if no files are given |
| 93 |
|
| 94 |
=item * |
| 95 |
multiple input files are merged to one output file |
| 96 |
|
| 97 |
=back |
| 98 |
|
| 99 |
=head2 Variables |
| 100 |
|
| 101 |
Variables can be defined using |
| 102 |
|
| 103 |
--<VAR>=<VALUE> |
| 104 |
|
| 105 |
or |
| 106 |
|
| 107 |
--<VAR> <VALUE> |
| 108 |
|
| 109 |
and will be used for substitutions in the template file. |
| 110 |
|
| 111 |
=head3 Common variables: |
| 112 |
|
| 113 |
author, email, maintainer, year (for copyright), |
| 114 |
version, date, description (of package/class), |
| 115 |
type (either 'package' default or 'class'), |
| 116 |
filebase (automatically set from output or input file name), |
| 117 |
|
| 118 |
|
| 119 |
=head2 Options |
| 120 |
|
| 121 |
=over 8 |
| 122 |
|
| 123 |
=item B<-h> S< > |
| 124 |
Print this help text |
| 125 |
|
| 126 |
=item B<-H> S< > |
| 127 |
Print extended help |
| 128 |
|
| 129 |
=item B<-V> S< > |
| 130 |
Print version and copyright |
| 131 |
|
| 132 |
=item B<-v> S< > |
| 133 |
Be verbose |
| 134 |
|
| 135 |
=item B<-o> F<output> S< > |
| 136 |
Use given file as output |
| 137 |
|
| 138 |
=item B<-O> S< > |
| 139 |
Overwrite already existing output file(s) |
| 140 |
|
| 141 |
=item B<-B> S< > |
| 142 |
Use basename of single input file for output file |
| 143 |
|
| 144 |
=item B<-I> S< > |
| 145 |
Also create .ins (install) file |
| 146 |
|
| 147 |
=item B<-c> S< > |
| 148 |
Only use code section (like v1.0) |
| 149 |
|
| 150 |
=item B<-r> S< > |
| 151 |
Remove existing 'macro', 'macrocode', etc. environments. |
| 152 |
|
| 153 |
=item B<-R> S< > |
| 154 |
Do not remove existing 'macro', 'macrocode', etc. environments. |
| 155 |
|
| 156 |
=item B<-i> F<ins-file> S< > |
| 157 |
Create .ins file with given name |
| 158 |
|
| 159 |
=item B<-t> F<template> S< > |
| 160 |
Use this file as template instead of the default one |
| 161 |
|
| 162 |
=item B<-T> F<template> S< > |
| 163 |
Use this file as template for the .ins file |
| 164 |
|
| 165 |
=item B<-e> F<file> S< > |
| 166 |
Export default .dtx template to file and exit |
| 167 |
|
| 168 |
=item B<-E> F<file> S< > |
| 169 |
Export default .ins template to file and exit |
| 170 |
|
| 171 |
=item B<-D> S< > |
| 172 |
Use current date as file date |
| 173 |
|
| 174 |
=item B<-F> F<file> S< > |
| 175 |
Read more options and variables from file. |
| 176 |
|
| 177 |
=item B<-N> S< > |
| 178 |
Do not read default config file; must be the first option |
| 179 |
|
| 180 |
=back |
| 181 |
|
| 182 |
|
| 183 |
=head2 Config files |
| 184 |
|
| 185 |
A default config file either named 'sty2dtx.cfg' or '.sty2dtx.cfg' is searched |
| 186 |
in the current directory, the users home directory and the directory of this |
| 187 |
script, in this order. The first one found is loaded. If none is found the |
| 188 |
'texmf' tree is searched for a 'sty2dtx.cfg' config file. As with -F files the |
| 189 |
config file should contain one option or variable per line. |
| 190 |
Lines starting with 'C<%>' or 'C<#>' are ignored. |
| 191 |
|
| 192 |
|
| 193 |
=head1 Examples |
| 194 |
|
| 195 |
Produce 'file.dtx' from 'file.sty': |
| 196 |
|
| 197 |
sty2dtx.pl < file.sty > file.dtx |
| 198 |
|
| 199 |
or |
| 200 |
|
| 201 |
sty2dtx.pl file.sty file.dtx |
| 202 |
|
| 203 |
or |
| 204 |
|
| 205 |
sty2dtx.pl -B file.sty |
| 206 |
|
| 207 |
Produce 'file.dtx' and 'file.ins' from 'file.sty': |
| 208 |
|
| 209 |
sty2dtx.pl -I file.sty file.dtx |
| 210 |
|
| 211 |
or |
| 212 |
|
| 213 |
sty2dtx.pl file.sty -i file.sty file.dtx |
| 214 |
|
| 215 |
or |
| 216 |
|
| 217 |
sty2dtx.pl -IB file.sty |
| 218 |
|
| 219 |
Set custom variable values: |
| 220 |
|
| 221 |
sty2dtx.pl --author Me --email [email protected] mypkg.sty mypkg.dtx |
| 222 |
|
| 223 |
Produce DTX file for a class: |
| 224 |
|
| 225 |
sty2dtx.pl --type class mycls.sty mycls.dtx |
| 226 |
|
| 227 |
=head1 AUTHOR |
| 228 |
|
| 229 |
Martin Scharrer |
| 230 |
|
| 231 |
E-mail: L<[email protected]> |
| 232 |
|
| 233 |
WWW: L<http://www.scharrer-online.de> |
| 234 |
|
| 235 |
=cut |
| 236 |
|
| 237 |
################################################################################ |
| 238 |
use Pod::Usage; |
| 239 |
|
| 240 |
my $VERSION = "v2.4"; |
| 241 |
$VERSION =~ tr/-/\//; |
| 242 |
|
| 243 |
my $TITLE = << "EOT"; |
| 244 |
sty2dtx -- Converts a LaTeX .sty file to a documented .dtx file |
| 245 |
Version: $VERSION |
| 246 |
EOT |
| 247 |
|
| 248 |
sub usage { |
| 249 |
print "Version: $VERSION\n\n"; |
| 250 |
pod2usage(-message => $TITLE, -sections => 'USAGE', -verbose=>99 ); |
| 251 |
} |
| 252 |
|
| 253 |
my $ERROR = "sty2dtx: Error:"; |
| 254 |
|
| 255 |
# Used as format string of printf so that the '%' must be doubled: |
| 256 |
my $macrostart = <<'EOT'; |
| 257 |
%% |
| 258 |
%% \begin{macro}{\%s} |
| 259 |
%s%% \begin{macrocode} |
| 260 |
EOT |
| 261 |
|
| 262 |
my $keystart = <<'EOT'; |
| 263 |
%% |
| 264 |
%% \begin{key}{%s}{%s} |
| 265 |
%s%% \begin{macrocode} |
| 266 |
EOT |
| 267 |
|
| 268 |
my $environmentstart = <<'EOT'; |
| 269 |
%% |
| 270 |
%% \begin{environment}{%s} |
| 271 |
%s%% \begin{macrocode} |
| 272 |
EOT |
| 273 |
|
| 274 |
my $macrodescription = <<'EOT'; |
| 275 |
%% |
| 276 |
%% \DescribeMacro{\%s} |
| 277 |
%% |
| 278 |
EOT |
| 279 |
|
| 280 |
my $keydescription = <<'EOT'; |
| 281 |
%% |
| 282 |
%% \DescribeKey{\%s}{\%s} |
| 283 |
%% |
| 284 |
EOT |
| 285 |
|
| 286 |
my $envdescription = <<'EOT'; |
| 287 |
%% |
| 288 |
%% \DescribeEnv{%s} |
| 289 |
%% |
| 290 |
EOT |
| 291 |
|
| 292 |
# Printed normally: |
| 293 |
my $macrostop = <<'EOT'; |
| 294 |
% \end{macrocode} |
| 295 |
% \end{macro} |
| 296 |
% |
| 297 |
EOT |
| 298 |
|
| 299 |
my $keystop = <<'EOT'; |
| 300 |
% \end{macrocode} |
| 301 |
% \end{key} |
| 302 |
% |
| 303 |
EOT |
| 304 |
|
| 305 |
my $environmentstop = <<'EOT'; |
| 306 |
% \end{macrocode} |
| 307 |
% \end{environment} |
| 308 |
% |
| 309 |
EOT |
| 310 |
|
| 311 |
my $macrocodestart = <<'EOT'; |
| 312 |
% \begin{macrocode} |
| 313 |
EOT |
| 314 |
|
| 315 |
my $macrocodestop = <<'EOT'; |
| 316 |
% \end{macrocode} |
| 317 |
EOT |
| 318 |
|
| 319 |
my $USAGE = q(); # Store macro names for usage section |
| 320 |
my $IMPL = q(); # Store implementation section |
| 321 |
|
| 322 |
my $mode = 0; |
| 323 |
# 0 = outside of macro or macrocode environments |
| 324 |
# 1 = inside 'macrocode' environment |
| 325 |
# 2 = inside 'macro' environment |
| 326 |
# 3 = inside 'environment' environment |
| 327 |
# 4 = inside 'key' environment |
| 328 |
|
| 329 |
# RegExs for macro names and defintion: |
| 330 |
my $rmacroname = qr/[a-zA-Z\@:]+/xms; # Add ':' for LaTeX3 style macros |
| 331 |
my $renvname = qr/[a-zA-Z\@:*]+/xms; # |
| 332 |
my $rusermacro = qr/[a-zA-Z]+/xms; # Macros intended for users |
| 333 |
my $rmacrodef = qr/ |
| 334 |
^ # Begin of line (no whitespaces!) |
| 335 |
( |
| 336 |
(?:(?:\\global|\\long|\\protected|\\outer)\s*)* # Prefixes (maybe with whitespace between them) |
| 337 |
) |
| 338 |
\\( |
| 339 |
[gex]?def \s* \\ # TeX definitions |
| 340 |
| (?:new|renew|provide)command\s* \*? \s* \{? \s* \\ # LaTeX definitions |
| 341 |
| \@namedef\{? # Definition by name only |
| 342 |
) |
| 343 |
($rmacroname) # Macro name without backslash |
| 344 |
\s* \}? # Potential closing brace |
| 345 |
(.*) # Rest of line |
| 346 |
/xms; |
| 347 |
|
| 348 |
my $rkeydef = qr/ |
| 349 |
^ # Begin of line (no whitespaces!) |
| 350 |
\\ |
| 351 |
(define\@[a-z]*key) |
| 352 |
\s* |
| 353 |
\{([^}]+)\} # Key family |
| 354 |
\s* |
| 355 |
\{([^}]+)\} # Key name |
| 356 |
(.*) # Rest of line |
| 357 |
/xms; |
| 358 |
|
| 359 |
my $renvdef = qr/ |
| 360 |
^ # Begin of line (no whitespaces!) |
| 361 |
\\( |
| 362 |
(?:new|renew|provide)environment\s* \{ \s* # LaTeX definitions |
| 363 |
) |
| 364 |
($renvname) # Environment names follow same rules as macro names |
| 365 |
\s* \} # closing brace |
| 366 |
(.*) # Rest of line |
| 367 |
/xms; |
| 368 |
|
| 369 |
my $comments = q(); |
| 370 |
|
| 371 |
# Print end of environment, if one is open |
| 372 |
sub close_environment { |
| 373 |
if ( $mode == 1 ) { |
| 374 |
$IMPL .= $macrocodestop; |
| 375 |
} |
| 376 |
elsif ( $mode == 2 ) { |
| 377 |
# Happens only if closing brace is not on a line by its own. |
| 378 |
$IMPL .= $macrostop; |
| 379 |
} |
| 380 |
elsif ( $mode == 3 ) { |
| 381 |
$IMPL .= $environmentstop; |
| 382 |
} |
| 383 |
elsif ( $mode == 4 ) { |
| 384 |
$IMPL .= $keystop; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
my ( $mday, $mon, $year ) = ( localtime(time) )[ 3 .. 5 ]; |
| 389 |
$mday = sprintf( "%02d", $mday ); |
| 390 |
$mon = sprintf( "%02d", $mon + 1 ); |
| 391 |
$year += 1900; |
| 392 |
|
| 393 |
my @files; |
| 394 |
my $outfile = q(); |
| 395 |
my $verbose = 0; |
| 396 |
my $codeonly = 0; |
| 397 |
my $install = 0; |
| 398 |
my $usebase = 0; |
| 399 |
my $overwrite = 0; |
| 400 |
my $removeenvs = 0; |
| 401 |
my $installfile; |
| 402 |
my $templfile; |
| 403 |
my $installtempl; |
| 404 |
my $checksum = 0; |
| 405 |
my $config_file; |
| 406 |
|
| 407 |
# Holds the variables for the templates, is initiated with default values: |
| 408 |
my %vars = ( |
| 409 |
type => 'package', |
| 410 |
class => 'ltxdoc', |
| 411 |
year => "$year", |
| 412 |
); |
| 413 |
|
| 414 |
my $template = *DATA; |
| 415 |
my %OUTFILES; |
| 416 |
|
| 417 |
# Handle options |
| 418 |
sub option { |
| 419 |
my $opt = shift; |
| 420 |
if ( $opt eq 'h' ) { |
| 421 |
usage(); |
| 422 |
} |
| 423 |
elsif ( $opt eq 'H' ) { |
| 424 |
pod2usage(-message => $TITLE, -sections => 'DESCRIPTION', -verbose=>99 ); |
| 425 |
} |
| 426 |
elsif ( $opt eq 'r' ) { |
| 427 |
$removeenvs = 1; |
| 428 |
} |
| 429 |
elsif ( $opt eq 'R' ) { |
| 430 |
$removeenvs = 0; |
| 431 |
} |
| 432 |
elsif ( $opt eq 'c' ) { |
| 433 |
$codeonly = 1; |
| 434 |
} |
| 435 |
elsif ( $opt eq 'B' ) { |
| 436 |
$usebase = 1; |
| 437 |
} |
| 438 |
elsif ( $opt eq 't' ) { |
| 439 |
$templfile = shift @ARGV; |
| 440 |
} |
| 441 |
elsif ( $opt eq 'e' ) { |
| 442 |
my $templ = shift @ARGV; |
| 443 |
if ( $templ ne '-' ) { |
| 444 |
die "$ERROR '$templ' already exists\n" if -e $templ; |
| 445 |
open( STDOUT, '>', $templ ) |
| 446 |
or die "$ERROR Couldn't open new template file '$templ' ($!)\n"; |
| 447 |
} |
| 448 |
while (<DATA>) { |
| 449 |
last if /^__INS__$/; |
| 450 |
print; |
| 451 |
} |
| 452 |
print STDERR |
| 453 |
"Exported default template for .dtx files to file '$templ'\n" |
| 454 |
if $verbose; |
| 455 |
exit(0); |
| 456 |
} |
| 457 |
elsif ( $opt eq 'E' ) { |
| 458 |
my $templ = shift @ARGV; |
| 459 |
if ( $templ ne '-' ) { |
| 460 |
die "$ERROR '$templ' already exists\n" if -e $templ; |
| 461 |
open( STDOUT, '>', $templ ) |
| 462 |
or die "$ERROR Couldn't open new template file '$templ' ($!)\n"; |
| 463 |
} |
| 464 |
while (<DATA>) { |
| 465 |
last if /^__INS__$/; |
| 466 |
} |
| 467 |
while (<DATA>) { |
| 468 |
print; |
| 469 |
} |
| 470 |
print STDERR |
| 471 |
"Exported default template for .ins files to file '$templ'\n" |
| 472 |
if $verbose; |
| 473 |
exit(0); |
| 474 |
} |
| 475 |
elsif ( $opt eq 'v' ) { |
| 476 |
$verbose++; |
| 477 |
} |
| 478 |
elsif ( $opt eq 'I' ) { |
| 479 |
$install = 1; |
| 480 |
} |
| 481 |
elsif ( $opt eq 'i' ) { |
| 482 |
$installfile = shift @ARGV; |
| 483 |
$install = 1; |
| 484 |
} |
| 485 |
elsif ( $opt eq 'T' ) { |
| 486 |
$installtempl = shift @ARGV; |
| 487 |
} |
| 488 |
elsif ( $opt eq 'V' ) { |
| 489 |
pod2usage(-message => $TITLE, -sections => 'COPYRIGHT', -verbose=>99 ); |
| 490 |
} |
| 491 |
elsif ( $opt eq 'N' ) { |
| 492 |
print STDERR "sty2dtx warning: '-N' option used after default config file was already loaded.\n" |
| 493 |
if $config_file && $verbose; |
| 494 |
} |
| 495 |
elsif ( $opt eq 'F' ) { |
| 496 |
my $optfile = shift @ARGV; |
| 497 |
|
| 498 |
# Read more options and variables from file |
| 499 |
open( my $OPT, '<', $optfile ) |
| 500 |
or die("Couldn't open options file '$optfile' ($!)!\n"); |
| 501 |
while ( my $line = <$OPT> ) { |
| 502 |
chomp $line; |
| 503 |
|
| 504 |
# Skip comment lines |
| 505 |
next if $line =~ /^\s*[#%]/; |
| 506 |
|
| 507 |
# Handle variables |
| 508 |
# Split at '=' or space and add as two arguments |
| 509 |
if ( $line =~ /\A\s*(--[a-zA-Z0-9]+?)(?:\s+|=)(.*)/xms ) { |
| 510 |
my $var = $1; |
| 511 |
(my $val = $2) =~ s/^["']|["']$//g; |
| 512 |
unshift @ARGV, $var, $val; |
| 513 |
} |
| 514 |
# Handle options with and without values |
| 515 |
# as well sets of options |
| 516 |
elsif ($line =~ /\A\s*(-[a-zA-Z0-9]+)(?:\s+(.*))?/xms) { |
| 517 |
my $var = $1; |
| 518 |
my $val = $2; |
| 519 |
if (defined $val) { |
| 520 |
$val =~ s/^["']|["']$//g; |
| 521 |
unshift @ARGV, $var, $val; |
| 522 |
} |
| 523 |
else { |
| 524 |
unshift @ARGV, $var; |
| 525 |
} |
| 526 |
} |
| 527 |
else { |
| 528 |
die "$ERROR Could not recognize '$line' in '$optfile'\n"; |
| 529 |
} |
| 530 |
} |
| 531 |
close $OPT; |
| 532 |
} |
| 533 |
elsif ( $opt eq 'D' ) { |
| 534 |
$vars{date} = "$year/$mon/$mday"; |
| 535 |
} |
| 536 |
elsif ( $opt eq 'o' ) { |
| 537 |
$outfile = shift @ARGV; |
| 538 |
} |
| 539 |
elsif ( $opt eq 'O' ) { |
| 540 |
$overwrite = 1; |
| 541 |
} |
| 542 |
elsif ( $opt eq 'A' ) { |
| 543 |
$template = shift @ARGV; |
| 544 |
} |
| 545 |
elsif ( $opt eq 'a' ) { |
| 546 |
my $outname = shift @ARGV; |
| 547 |
$OUTFILES{$outname} = $template; |
| 548 |
} |
| 549 |
else { |
| 550 |
print STDERR "sty2dtx: unknown option '-$opt'!\n"; |
| 551 |
exit(2); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
# Count number of backslashes in code for file checksum |
| 556 |
sub addtochecksum { |
| 557 |
my $line = shift; |
| 558 |
$checksum += $line =~ tr{\\}{\\}; |
| 559 |
} |
| 560 |
|
| 561 |
################################################################################ |
| 562 |
|
| 563 |
my $CONFIG_NAME = "sty2dtx.cfg"; |
| 564 |
|
| 565 |
if (!@ARGV || $ARGV[0] !~ /^-[^-]*N/) { |
| 566 |
|
| 567 |
PATH: |
| 568 |
foreach my $path ('.', $ENV{HOME}, dirname($0)) { |
| 569 |
foreach my $file ($path.'/'.$CONFIG_NAME, $path.'/.'.$CONFIG_NAME) { |
| 570 |
if (-e $file) { |
| 571 |
$config_file = $file; |
| 572 |
last PATH; |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
if (!$config_file) { |
| 577 |
# Find the config file in the TeX tree. |
| 578 |
$config_file = `kpsewhich -must-exist $CONFIG_NAME`; |
| 579 |
} |
| 580 |
if ($config_file) { |
| 581 |
chomp $config_file; |
| 582 |
print STDERR "sty2dtx info: loading config file '$config_file'.\n" |
| 583 |
if ($verbose); |
| 584 |
unshift @ARGV, '-F', $config_file; |
| 585 |
} |
| 586 |
|
| 587 |
} |
| 588 |
|
| 589 |
# Parse arguments |
| 590 |
while (@ARGV) { |
| 591 |
my $arg = shift; |
| 592 |
|
| 593 |
# '--' Marks rest of arguments as files |
| 594 |
if ( $arg eq '--' ) { |
| 595 |
push @files, @ARGV; |
| 596 |
last; |
| 597 |
} |
| 598 |
|
| 599 |
# Options and variables |
| 600 |
elsif ( $arg =~ /^(-+)(.+)$/ ) { |
| 601 |
my $dashes = $1; |
| 602 |
my $name = $2; |
| 603 |
|
| 604 |
# Single dash => option |
| 605 |
if ( length($dashes) == 1 ) { |
| 606 |
foreach my $opt ( split //, $name ) { |
| 607 |
option($opt); |
| 608 |
} |
| 609 |
} |
| 610 |
# Douple Dash => Variable |
| 611 |
# Form "--var=value" |
| 612 |
elsif ($name =~ /^([^=]+)=(.*)$/) { |
| 613 |
$vars{lc($1)} = $2; |
| 614 |
} |
| 615 |
# Form "--var value" |
| 616 |
else { |
| 617 |
if ($name eq "help") { |
| 618 |
usage(); |
| 619 |
} |
| 620 |
else { |
| 621 |
$vars{ lc($name) } = shift; |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
# Files |
| 626 |
else { |
| 627 |
push @files, $arg; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
# Last (but not only) argument is output file, except if it is '-' (=STDOUT) |
| 633 |
if ( $outfile || @files > 1 ) { |
| 634 |
$outfile = pop @files unless $outfile; |
| 635 |
$vars{filebase} = substr( $outfile, 0, rindex( $outfile, '.' ) ) |
| 636 |
if not exists $vars{filebase}; |
| 637 |
} |
| 638 |
elsif ( @files == 1 ) { |
| 639 |
my $infile = $files[0]; |
| 640 |
$vars{filebase} = substr( $infile, 0, rindex( $infile, '.' ) ) |
| 641 |
if not exists $vars{filebase}; |
| 642 |
if ($usebase) { |
| 643 |
$outfile = $vars{filebase} . '.dtx'; |
| 644 |
} |
| 645 |
} |
| 646 |
if ( $outfile && $outfile ne '-' ) { |
| 647 |
if ( !$overwrite && -e $outfile && $outfile ne '/dev/null' ) { |
| 648 |
die( "$ERROR output file '$outfile' does already exists!" |
| 649 |
. " Use the -O option to overwrite.\n" ); |
| 650 |
} |
| 651 |
open( OUTPUT, '>', $outfile ) |
| 652 |
or die("$ERROR Could not open output file '$outfile' ($!)!"); |
| 653 |
select OUTPUT; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
################################################################################ |
| 658 |
# Read input files |
| 659 |
@ARGV = @files; |
| 660 |
while (<>) { |
| 661 |
# Test for macro definition command |
| 662 |
if (/$rmacrodef/) { |
| 663 |
my $pre = $1 || ""; # before command |
| 664 |
my $cmd = $2; # definition command |
| 665 |
my $name = $3; # macro name |
| 666 |
my $rest = $4; # rest of line |
| 667 |
|
| 668 |
MACRO: |
| 669 |
|
| 670 |
# Add to usage section if it is a user level macro |
| 671 |
if ( $name =~ /^$rusermacro$/i ) { |
| 672 |
$USAGE .= sprintf( $macrodescription, $name ); |
| 673 |
} |
| 674 |
|
| 675 |
close_environment(); |
| 676 |
|
| 677 |
# Print 'macro' environment with current line. |
| 678 |
$IMPL .= sprintf( $macrostart, $name, $comments ); |
| 679 |
addtochecksum($_); |
| 680 |
$IMPL .= $_; |
| 681 |
$comments = ''; |
| 682 |
|
| 683 |
# Inside macro mode |
| 684 |
$mode = 2; |
| 685 |
|
| 686 |
# Test for one line definitions. |
| 687 |
# $pre is tested to handle '{\somecatcodechange\gdef\name{short}}' lines |
| 688 |
my $prenrest = $pre . $rest; |
| 689 |
if ( $prenrest =~ tr/{/{/ == $prenrest =~ tr/}/}/ ) { |
| 690 |
$IMPL .= $macrostop; |
| 691 |
# Outside mode |
| 692 |
$mode = 0; |
| 693 |
} |
| 694 |
} |
| 695 |
elsif (/$rkeydef/) { |
| 696 |
my $pre = ""; # before command |
| 697 |
my $cmd = $1; # definition command |
| 698 |
my $keyfamily = $2; # macro name |
| 699 |
my $keyname = $3; # macro name |
| 700 |
my $rest = $4; # rest of line |
| 701 |
|
| 702 |
# Add to usage section if it is a user level macro |
| 703 |
$USAGE .= sprintf( $keydescription, $keyfamily, $keyname ); |
| 704 |
|
| 705 |
close_environment(); |
| 706 |
|
| 707 |
# Print 'key' environment with current line. |
| 708 |
$IMPL .= sprintf( $keystart, $keyfamily, $keyname, $comments ); |
| 709 |
addtochecksum($_); |
| 710 |
$IMPL .= $_; |
| 711 |
$comments = ''; |
| 712 |
|
| 713 |
# Inside key mode |
| 714 |
$mode = 4; |
| 715 |
|
| 716 |
# Test for one line definitions. |
| 717 |
# $pre is tested to handle '{\somecatcodechange\gdef\name{short}}' lines |
| 718 |
my $prenrest = $pre . $rest; |
| 719 |
if ( $prenrest =~ tr/{/{/ == $prenrest =~ tr/}/}/ ) { |
| 720 |
$IMPL .= $keystop; |
| 721 |
# Outside mode |
| 722 |
$mode = 0; |
| 723 |
} |
| 724 |
} |
| 725 |
# Test for environment definition command |
| 726 |
elsif (/$renvdef/) { |
| 727 |
my $cmd = $1; # definition command |
| 728 |
my $name = $2; # macro name |
| 729 |
my $rest = $3; # rest of line |
| 730 |
|
| 731 |
# Add to usage section if it is a user level environment |
| 732 |
# Can use the same RegEx as for macro names |
| 733 |
if ( $name =~ /^$rusermacro$/i ) { |
| 734 |
$USAGE .= sprintf( $envdescription, $name ); |
| 735 |
} |
| 736 |
|
| 737 |
close_environment(); |
| 738 |
|
| 739 |
# Print 'environment' environment with current line. |
| 740 |
$IMPL .= sprintf( $environmentstart, $name, $comments ); |
| 741 |
addtochecksum($_); |
| 742 |
$IMPL .= $_; |
| 743 |
$comments = ''; |
| 744 |
|
| 745 |
# Inside environment mode |
| 746 |
$mode = 3; |
| 747 |
|
| 748 |
# Test for one line definitions. |
| 749 |
my $nopen = ( $rest =~ tr/{/{/ ); |
| 750 |
if ( $nopen >= 2 && $nopen == ( $rest =~ tr/}/}/ ) ) { |
| 751 |
$IMPL .= $environmentstop; |
| 752 |
# Outside mode |
| 753 |
$mode = 0; |
| 754 |
} |
| 755 |
} |
| 756 |
# Collect comment lines, might be inserted as macro or environment description |
| 757 |
# Real comments are either: 1) starting with a '%' at SOL or 2) are followed |
| 758 |
# by at least one whitespace. This exclude (most) commented out code. |
| 759 |
elsif (/^%|^\s*%\s/) { |
| 760 |
if (!$removeenvs || !/^%\s+\\(?:begin|end)\{(?:macro|environment|macrocode|key)\}/) { |
| 761 |
$_ =~ s/^\s*//; |
| 762 |
if ($comments || !/^%\s*$/){ |
| 763 |
$comments .= $_; |
| 764 |
} |
| 765 |
} |
| 766 |
if ( $mode == 1 ) { |
| 767 |
$IMPL .= $macrocodestop; |
| 768 |
$mode = 0; |
| 769 |
} |
| 770 |
} |
| 771 |
# Remove empty lines (mostly between macros) |
| 772 |
elsif (/^$/) { |
| 773 |
if ($comments) { |
| 774 |
# Flush collected outside comments |
| 775 |
$IMPL .= $comments . "%\n"; |
| 776 |
$comments = ''; |
| 777 |
} |
| 778 |
} |
| 779 |
else { |
| 780 |
addtochecksum($_); |
| 781 |
# If inside an environment |
| 782 |
if ($mode) { |
| 783 |
if ($comments) { |
| 784 |
$IMPL .= $macrocodestop . $comments . $macrocodestart; |
| 785 |
$comments = ''; |
| 786 |
} |
| 787 |
$IMPL .= $_; |
| 788 |
# A single '}' on a line ends a 'macro' or 'environment' environment |
| 789 |
if ( $mode > 1 && /^\}\s*$/ ) { |
| 790 |
$IMPL .= ( $mode == 2 ) ? $macrostop : |
| 791 |
( $mode == 3 ) ? $environmentstop : $keystop; |
| 792 |
$mode = 0; |
| 793 |
} |
| 794 |
} |
| 795 |
else { |
| 796 |
# Start macrocode environment |
| 797 |
$IMPL .= $comments . $macrocodestart . $_; |
| 798 |
$mode = 1; |
| 799 |
$comments = ''; |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
close_environment(); |
| 805 |
|
| 806 |
################################################################################ |
| 807 |
# Set extra/auto variables |
| 808 |
$vars{IMPLEMENTATION} = $IMPL; |
| 809 |
$vars{USAGE} = $USAGE; |
| 810 |
$vars{type} = "\L$vars{type}"; |
| 811 |
$vars{Type} = "\L\u$vars{type}"; |
| 812 |
$vars{extension} = $vars{type} eq 'class' ? 'cls' : 'sty'; |
| 813 |
$vars{checksum} = $checksum if not exists $vars{checksum}; # Allow user to overwrite |
| 814 |
$vars{maintainer} = $vars{author} |
| 815 |
if not exists $vars{maintainer} and exists $vars{author}; |
| 816 |
|
| 817 |
################################################################################ |
| 818 |
# Write DTX file |
| 819 |
if ($codeonly) { |
| 820 |
print $IMPL; |
| 821 |
if ($verbose) { |
| 822 |
print STDERR "Generated DTX file"; |
| 823 |
print STDERR " '$outfile'" if $outfile and $outfile ne '-'; |
| 824 |
print STDERR " (code only).\n"; |
| 825 |
} |
| 826 |
} |
| 827 |
else { |
| 828 |
if ($templfile) { |
| 829 |
open( DTX, '<', $templfile ) |
| 830 |
or die "$ERROR Couldn't open template file '$templfile' ($!)\n"; |
| 831 |
} |
| 832 |
else { |
| 833 |
*DTX = *DATA; |
| 834 |
} |
| 835 |
while (<DTX>) { |
| 836 |
last if !$templfile and /^__INS__$/; |
| 837 |
# Substitute template variables |
| 838 |
s/<\+([^+]+)\+>/exists $vars{$1} ? $vars{$1} : "<+$1+>"/eg; |
| 839 |
print; |
| 840 |
} |
| 841 |
|
| 842 |
if ($verbose) { |
| 843 |
print STDERR "Generated DTX file"; |
| 844 |
print STDERR " '$outfile'" if $outfile and $outfile ne '-'; |
| 845 |
print STDERR " using template '$templfile'" if $templfile; |
| 846 |
print STDERR ".\n"; |
| 847 |
} |
| 848 |
} |
| 849 |
select STDOUT; |
| 850 |
################################################################################ |
| 851 |
# Write INS file if requested |
| 852 |
if ($install) { |
| 853 |
|
| 854 |
if ( ( !$outfile || $outfile eq '-' ) && !$installfile ) { |
| 855 |
print STDERR |
| 856 |
"Warning: Did not generate requested .ins file because main file\n"; |
| 857 |
print STDERR " was written to STDOUT and no -i option was given.\n"; |
| 858 |
exit(1); |
| 859 |
} |
| 860 |
|
| 861 |
if ($installtempl) { |
| 862 |
open( DATA, '<', $installtempl ) |
| 863 |
or die "$ERROR Could't open template '$installtempl' for .ins file! ($!)\n"; |
| 864 |
} |
| 865 |
elsif ($codeonly || $templfile) { |
| 866 |
# If DATA template was not used for main file go forward to correct position |
| 867 |
while (<DATA>) { |
| 868 |
last if /^__INS__$/; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
$installfile = $vars{filebase} . '.ins' unless defined $installfile; |
| 873 |
if ( !$overwrite && -e $installfile && $installfile ne '/dev/null' ) { |
| 874 |
die( "$ERROR Output file '$installfile' does already exists!" |
| 875 |
. " Use the -O option to overwrite.\n" ); |
| 876 |
} |
| 877 |
open( INS, '>', $installfile ) |
| 878 |
or die "$ERROR Could't open new .ins file '$installfile' ($!)\n"; |
| 879 |
|
| 880 |
while (<DATA>) { |
| 881 |
# Substitute template variables |
| 882 |
s/<\+([^+]+)\+>/exists $vars{$1} ? $vars{$1} : "<+$1+>"/eg; |
| 883 |
print INS $_; |
| 884 |
} |
| 885 |
|
| 886 |
if ($verbose) { |
| 887 |
print STDERR "Generated INS file '$installfile'"; |
| 888 |
print STDERR " using template '$installtempl'" if $installtempl; |
| 889 |
print STDERR ".\n"; |
| 890 |
} |
| 891 |
|
| 892 |
} |
| 893 |
|
| 894 |
################################################################################ |
| 895 |
# Write additional files if requested |
| 896 |
while (my ($outfile, $template) = each %OUTFILES) { |
| 897 |
my $TEMPLATEHANDLE; |
| 898 |
if (ref $template eq 'GLOB') { |
| 899 |
$TEMPLATEHANDLE = $template; |
| 900 |
} |
| 901 |
# TODO: add support for DTX and INS templates in DATA |
| 902 |
else { |
| 903 |
open( $TEMPLATEHANDLE, '<', $template ) |
| 904 |
or die "$ERROR Could't open template '$template' for file '$outfile' ($!)!\n"; |
| 905 |
} |
| 906 |
open( my $OUTFILEHANDLE, '>', $outfile ) |
| 907 |
or die "$ERROR Could't create output file '$outfile' ($!)!\n"; |
| 908 |
|
| 909 |
while (<$TEMPLATEHANDLE>) { |
| 910 |
# Substitute template variables |
| 911 |
s/<\+([^+]+)\+>/exists $vars{$1} ? $vars{$1} : "<+$1+>"/eg; |
| 912 |
print {$OUTFILEHANDLE} $_; |
| 913 |
} |
| 914 |
close $TEMPLATEHANDLE; |
| 915 |
close $OUTFILEHANDLE; |
| 916 |
|
| 917 |
if ($verbose) { |
| 918 |
print STDERR "Generated file '$outfile' using template '$template'.\n"; |
| 919 |
} |
| 920 |
} |
| 921 |
################################################################################ |
| 922 |
exit (0); |
| 923 |
################################################################################ |
| 924 |
# The templates for the DTX file and INS file |
| 925 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 926 |
# Adepted from the skeleton file provided by the 'dtxtut' (DTX tuturial). |
| 927 |
# The '<+var+>' format was choosen because it is used by the latex suite for Vim. |
| 928 |
# Therfore all variables which are not expanded are easily accessible to the |
| 929 |
# user using a certain feature in the latex suite. |
| 930 |
# |
| 931 |
# Perl modules like 'Template' were not used to support minimal Perl installation |
| 932 |
# for typical LaTeX installations. |
| 933 |
# |
| 934 |
__DATA__ |
| 935 |
% \iffalse meta-comment |
| 936 |
% |
| 937 |
% Copyright (C) <+year+> by <+author+> <<+email+>> |
| 938 |
% --------------------------------------------------------------------------- |
| 939 |
% This work may be distributed and/or modified under the |
| 940 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 941 |
% of this license or (at your option) any later version. |
| 942 |
% The latest version of this license is in |
| 943 |
% http://www.latex-project.org/lppl.txt |
| 944 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 945 |
% version 2005/12/01 or later. |
| 946 |
% |
| 947 |
% This work has the LPPL maintenance status `maintained'. |
| 948 |
% |
| 949 |
% The Current Maintainer of this work is <+maintainer+>. |
| 950 |
% |
| 951 |
% This work consists of the files <+filebase+>.dtx and <+filebase+>.ins |
| 952 |
% and the derived filebase <+filebase+>.<+extension+>. |
| 953 |
% |
| 954 |
% \fi |
| 955 |
% |
| 956 |
% \iffalse |
| 957 |
%<*driver> |
| 958 |
\ProvidesFile{<+filebase+>.dtx} |
| 959 |
%</driver> |
| 960 |
%<<+type+>>\NeedsTeXFormat{LaTeX2e}[1999/12/01] |
| 961 |
%<<+type+>>\Provides<+Type+>{<+filebase+>} |
| 962 |
%<*<+type+>> |
| 963 |
[<+date+> <+version+> <+description+>] |
| 964 |
%</<+type+>> |
| 965 |
% |
| 966 |
%<*driver> |
| 967 |
\documentclass{ltxdoc} |
| 968 |
\usepackage{<+filebase+>}[<+date+>] |
| 969 |
\EnableCrossrefs |
| 970 |
\CodelineIndex |
| 971 |
\RecordChanges |
| 972 |
\begin{document} |
| 973 |
\DocInput{<+filebase+>.dtx} |
| 974 |
\PrintChanges |
| 975 |
\PrintIndex |
| 976 |
\end{document} |
| 977 |
%</driver> |
| 978 |
% \fi |
| 979 |
% |
| 980 |
% \CheckSum{<+checksum+>} |
| 981 |
% |
| 982 |
% \CharacterTable |
| 983 |
% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z |
| 984 |
% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z |
| 985 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 986 |
% Exclamation \! Double quote \" Hash (number) \# |
| 987 |
% Dollar \$ Percent \% Ampersand \& |
| 988 |
% Acute accent \' Left paren \( Right paren \) |
| 989 |
% Asterisk \* Plus \+ Comma \, |
| 990 |
% Minus \- Point \. Solidus \/ |
| 991 |
% Colon \: Semicolon \; Less than \< |
| 992 |
% Equals \= Greater than \> Question mark \? |
| 993 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 994 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 995 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 996 |
% Right brace \} Tilde \~} |
| 997 |
% |
| 998 |
% |
| 999 |
% \changes{<+version+>}{<+date+>}{Converted to DTX file} |
| 1000 |
% |
| 1001 |
% \DoNotIndex{\newcommand,\newenvironment} |
| 1002 |
% |
| 1003 |
% \providecommand*{\url}{\texttt} |
| 1004 |
% \GetFileInfo{<+filebase+>.dtx} |
| 1005 |
% \title{The \textsf{<+filebase+>} package} |
| 1006 |
% \author{<+author+> \\ \url{<+email+>}} |
| 1007 |
% \date{\fileversion~from \filedate} |
| 1008 |
% |
| 1009 |
% \maketitle |
| 1010 |
% |
| 1011 |
% \section{Introduction} |
| 1012 |
% |
| 1013 |
% Put text here. |
| 1014 |
% |
| 1015 |
% \section{Usage} |
| 1016 |
% |
| 1017 |
% Put text here. |
| 1018 |
% |
| 1019 |
<+USAGE+> |
| 1020 |
% |
| 1021 |
% \StopEventually{} |
| 1022 |
% |
| 1023 |
% \section{Implementation} |
| 1024 |
% |
| 1025 |
% \iffalse |
| 1026 |
%<*<+type+>> |
| 1027 |
% \fi |
| 1028 |
% |
| 1029 |
<+IMPLEMENTATION+> |
| 1030 |
% |
| 1031 |
% \iffalse |
| 1032 |
%</<+type+>> |
| 1033 |
% \fi |
| 1034 |
% |
| 1035 |
% \Finale |
| 1036 |
\endinput |
| 1037 |
__INS__ |
| 1038 |
%% Copyright (C) <+year+> by <+author+> <<+email+>> |
| 1039 |
%% -------------------------------------------------------------------------- |
| 1040 |
%% This work may be distributed and/or modified under the |
| 1041 |
%% conditions of the LaTeX Project Public License, either version 1.3 |
| 1042 |
%% of this license or (at your option) any later version. |
| 1043 |
%% The latest version of this license is in |
| 1044 |
%% http://www.latex-project.org/lppl.txt |
| 1045 |
%% and version 1.3 or later is part of all distributions of LaTeX |
| 1046 |
%% version 2005/12/01 or later. |
| 1047 |
%% |
| 1048 |
%% This work has the LPPL maintenance status `maintained'. |
| 1049 |
%% |
| 1050 |
%% The Current Maintainer of this work is <+maintainer+>. |
| 1051 |
%% |
| 1052 |
%% This work consists of the files <+filebase+>.dtx and <+filebase+>.ins |
| 1053 |
%% and the derived filebase <+filebase+>.<+extension+>. |
| 1054 |
%% |
| 1055 |
|
| 1056 |
\input docstrip.tex |
| 1057 |
\keepsilent |
| 1058 |
|
| 1059 |
\usedir{tex/latex/<+filebase+>} |
| 1060 |
|
| 1061 |
\preamble |
| 1062 |
|
| 1063 |
This is a generated file. |
| 1064 |
|
| 1065 |
Copyright (C) <+year+> by <+author+> <<+email+>> |
| 1066 |
-------------------------------------------------------------------------- |
| 1067 |
This work may be distributed and/or modified under the |
| 1068 |
conditions of the LaTeX Project Public License, either version 1.3 |
| 1069 |
of this license or (at your option) any later version. |
| 1070 |
The latest version of this license is in |
| 1071 |
http://www.latex-project.org/lppl.txt |
| 1072 |
and version 1.3 or later is part of all distributions of LaTeX |
| 1073 |
version 2005/12/01 or later. |
| 1074 |
|
| 1075 |
\endpreamble |
| 1076 |
|
| 1077 |
\generate{\file{<+filebase+>.<+extension+>}{\from{<+filebase+>.dtx}{<+type+>}}} |
| 1078 |
|
| 1079 |
\obeyspaces |
| 1080 |
\Msg{*************************************************************} |
| 1081 |
\Msg{* *} |
| 1082 |
\Msg{* To finish the installation you have to move the following *} |
| 1083 |
\Msg{* file into a directory searched by TeX: *} |
| 1084 |
\Msg{* *} |
| 1085 |
\Msg{* <+filebase+>.<+extension+> *} |
| 1086 |
\Msg{* *} |
| 1087 |
\Msg{* To produce the documentation run the file <+filebase+>.dtx *} |
| 1088 |
\Msg{* through LaTeX. *} |
| 1089 |
\Msg{* *} |
| 1090 |
\Msg{* Happy TeXing! *} |
| 1091 |
\Msg{* *} |
| 1092 |
\Msg{*************************************************************} |
| 1093 |
|
| 1094 |
\endbatchfile |