forked from reutenauer/polyglossia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makepolyglossiadtx.pl
executable file
·854 lines (654 loc) · 19.4 KB
/
makepolyglossiadtx.pl
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#!/usr/bin/perl -w
####### THIS IS BASED ON makedtx.pl ########
# File : makedtx
# Author : Nicola L. C. Talbot
# Date : 29 Oct 2004
# Last Modified : 19 Aug 2007
# Version : 0.94b
# usage : makedtx [options] -src <expr>=><expr> -doc <filename> <basename>
#
# -h : help message
# -src <expr>=><expr> : e.g. -src "(foo)src\.(bar)=>$1.$2" will add foosrc.bar to <basename>.dtx to be extracted to foo.bar
# -doc <filename> : file containing documentation.
# -prefinale <string> : text to add to dtx file just before \Finale (added to version 0.91b)
# <basename> : create <basename>.dtx and <basename>.ins
#BEGIN {
# system("rm *.{dtx,sty,def,ldf,map,txt,tex,glo,log,ins,out,idx,aux,lua}");
#};
use open IO => ':encoding(utf8)';
use utf8;
use strict;
use Cwd;
my $basedir = getcwd ;
my $srcdir = "$basedir/tex";
my $docsrcdir = "$basedir/doc";
my $fontmapsrcdir = "$basedir/fontmapping";
my $docsrc = "$docsrcdir/polyglossia.tex";
my $readme = "$basedir/README";
my @docsrcfiles = qw(../README
Changelog
examples.tex
example-arabic.tex
example-thai.tex);
my $patternop = "=";
my $verbose = 0;
my $noins = 0;
my $askforoverwrite = 0;
my $version = '0.94b';
my $author = 'Arthur Reutenauer';
my $email = "<arthur 'dot' reutenauer 'at' normalesup 'dot' org>";
(my $sec, my $min, my $hour, my $mday, my $mon, my $year, my $wday, my $yday, my $isdst)
= localtime(time);
$year = $year + 1900;
my $preamble = <<_END
____________________________
The polyglossia package
(C) 2008–2010 François Charette
(C) 2011-$year Arthur Reutenauer
(C) 2013 Elie Roux
License information appended
_END
;
my $postamble = <<_END
Copyright (C) $year by $author $email
This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either version 1.3
of this license of (at your option) any later version.
The latest version of this license is in
http://www.latex-project.org/lppl.txt
and version 1.3 or later is part of all distributions of LaTeX
version 2005/12/01 or later.
This work has the LPPL maintenance status `maintained'.
The Current Maintainer of this work is $author.
_END
;
my $stopeventually = "";
my $prefinale = "";
my $codetitle = "Implementation";
my @source = qw/(polyglossia\.sty)=>$1 (.+cal\.sty)=>$1 (.+\.lua)=>$1 (.+\.def)=>$1 (gloss-.+\.ldf$)=>$1/;
my $basename = "polyglossia";
open DTX, ">$basename.dtx" or die "Can't open '$basename.dtx'\n";
binmode( DTX, ':utf8' );
if ($verbose)
{
print "Documentation source : " . $docsrc . "\n";
}
# work out the derived files
my @srcdirfile = glob("$srcdir/*.{sty,ldf,def,lua}");
my @srcdirluafile = glob("$srcdir/*.{lua}");
my @derivedfiles = ();
my @outputfiles = ();
my @derivedluafiles = ();
my @outputluafiles = ();
my @deriveddocfiles = ();
my @outputdocfiles = ();
my $numoutput = 0;
foreach my $source (@source)
{
my ($infile, $outfile, $remainder) = split /=>/, $source;
if ($outfile eq "")
{
print "-src $source argument invalid (no output file specified)\n";
&syntaxerror();
}
if ($remainder && $remainder ne "")
{
print "-src $source argument invalid (too many => specified)\n";
&syntaxerror();
}
foreach my $srcdirfile (@srcdirfile)
{
my $fileexp = $srcdir . "/" . $infile;
$_ = $srcdirfile;
my $expr = "s$patternop$fileexp$patternop$outfile$patternop";
if (eval($expr))
{
my $thisoutfile = $_;
my $thisinfile = $srcdirfile;
$derivedfiles[$numoutput]{'in'} = $thisinfile;
$derivedfiles[$numoutput]{'out'} = $thisoutfile;
$outputfiles[$numoutput] = $thisoutfile;
$numoutput++;
}
}
$numoutput = 0;
foreach my $srcdirluafile (@srcdirluafile)
{
my $fileexp = $srcdir . "/" . $infile;
$_ = $srcdirluafile;
my $expr = "s$patternop$fileexp$patternop$outfile$patternop";
if (eval($expr))
{
my $thisoutfile = $_;
my $thisinfile = $srcdirluafile;
$derivedluafiles[$numoutput]{'in'} = $thisinfile;
$derivedluafiles[$numoutput]{'out'} = $thisoutfile;
$outputluafiles[$numoutput] = $thisoutfile;
$numoutput++;
}
}
}
foreach my $docfile (@docsrcfiles) {
my $docin = "$docsrcdir/$docfile";
push @deriveddocfiles, { in => $docin, out => $docfile };
push @outputdocfiles, $docfile;
}
my @fontmapsrcfiles = glob("$fontmapsrcdir/*.map");
foreach my $fontmap (@fontmapsrcfiles) {
my $fontmapout = $fontmap;
$fontmapout =~ s|.+/([^/]+\.map)|$1|;
push @derivedfiles, { in => $fontmap, out => $fontmapout };
push @outputfiles, $fontmapout;
}
print DTX "\%\\iffalse\n";
print DTX "\% $basename.dtx generated using mkpolyglossiadtx.pl\n";
print DTX "\% (derived from makedtx.pl version $version (c) Nicola Talbot)\n";
print DTX "\% \n";
print DTX "\% To extract the files, use xetex polyglossia.dtx or luatex polyglossia.dtx\n";
print DTX "\% \n";
print DTX <<_END
%<*internal>
\\iffalse
%</internal>
%<*README>
_END
;
open README, "< $readme";
while (<README>) {
print DTX $_;
};
close README;
print DTX <<_END
%</README>
%<*internal>
\\fi
%</internal>
%
%<*internal>
\\begingroup
%</internal>
%<*batchfile>
\\input docstrip.tex
\\keepsilent
\\let\\MetaPrefix\\relax
\\preamble
$preamble
\\endpreamble
\\postamble
$postamble
\\endpostamble
\\let\\MetaPrefix\\DoubleperCent
\\askforoverwritefalse
_END
;
for (my $idx = 0; $idx <= $#derivedfiles; $idx++) {
my $outfile = $derivedfiles[$idx]{'out'};
print DTX "\\generate{\\file{$outfile}{\\from{polyglossia.dtx}{$outfile}}}\n"
}
print DTX <<_END
\\def\\MetaPrefix{-- }
_END
;
for (my $idx = 0; $idx <= $#derivedluafiles; $idx++) {
my $outfile = $derivedluafiles[$idx]{'out'};
print DTX "\\generate{\\file{$outfile}{\\from{polyglossia.dtx}{$outfile}}}\n"
}
print DTX <<_END
\\let\\MetaPrefix\\DoubleperCent
%</batchfile>
%<batchfile>\\endbatchfile
%<*internal>
\\generate{\\file{polyglossia.ins}{\\from{polyglossia.dtx}{batchfile}}}
\\nopreamble\\nopostamble
_END
;
for (my $idx = 0; $idx <= $#deriveddocfiles; $idx++) {
my $outfile = $deriveddocfiles[$idx]{'out'};
print DTX "\\generate{\\file{$outfile}{\\from{polyglossia.dtx}{$outfile}}}\n"
}
print DTX<<_END
\\endgroup
%</internal>
%
_END
;
# driver
print DTX "\%<*driver>\n";
my $indoc=0;
open DOC, $docsrc or die "Can't open '$docsrc'\n";
while (<DOC>)
{
s/\\usepackage{creatdtx}//;
my $restofline = $_;
my $beginline = "";
my $line = $restofline;
while ($restofline =~ /(.*)\\ifmakedtx(.*)/)
{
$beginline = $1;
my ($group,$restofline,$done) = &getnextgroup($2);
my $startline = $.;
while (!$done)
{
if (my $nextline = <DOC>)
{
$line = $line . $nextline;
$restofline = $restofline . $nextline;
($group,$restofline,$done) = &getnextgroup($restofline);
}
else
{
die "EOF found whilst scanning first argument to \\ifmakedtx on line $startline\n";
}
}
# print first arg, ignore second
$beginline = $beginline . $group;
($group,$restofline,$done) = &getnextgroup($restofline);
while (!$done)
{
if (my $nextline = <DOC>)
{
$line = $line . $nextline;
$restofline = $restofline . $nextline;
($group,$restofline,$done) = &getnextgroup($restofline);
}
else
{
die "EOF found whilst scanning second argument to \\ifmakedtx on line $startline\n";
}
}
$line = $restofline;
}
$line = $beginline . $restofline;
print DTX $line;
if ($line=~/\\begin{document}/)
{
$indoc = 1;
last;
}
}
print DTX <<_END
\\ifxetex
\\DocInput{$basename.dtx}
\\fi
\\end{document}
%</driver>
%
% \\fi
%
% \\errorcontextlines=999
% \\makeatletter
%
_END
;
my $inverb=0;
my $stopfound=0;
print DTX "\% ";
while (<DOC>)
{
my $inverb ;
s/^\s*%+/^^A/ ;
if (/\\begin{verbatim}/)
{
$inverb=1;
}
if (/\\end{verbatim}/)
{
$inverb=0;
}
if (/\\StopEventually/ && ($inverb==0))
{
$stopfound=1;
}
my $restofline = $_;
my $beginline = "";
my $line = $restofline;
while ($restofline =~ /(.*)\\ifmakedtx(.*)/)
{
my $group;
my $done ;
$beginline = $1;
($group,$restofline,$done) = &getnextgroup($2);
my $startline = $.;
while (!$done)
{
if (my $nextline = <DOC>)
{
$line = $line . $nextline;
$restofline = $restofline . $nextline;
($group,$restofline,$done) = &getnextgroup($restofline);
}
else
{
die "EOF found whilst scanning first argument to \\ifmakedtx on line $startline\n";
}
}
# print first arg, ignore second
$beginline = $beginline . $group;
($group,$restofline,$done) = &getnextgroup($restofline);
while (!$done)
{
if (my $nextline = <DOC>)
{
$line = $line . $nextline;
$restofline = $restofline . $nextline;
($group,$restofline,$done) = &getnextgroup($restofline);
}
else
{
die "EOF found whilst scanning second argument to \\ifmakedtx on line $startline\n";
}
}
$line = $restofline;
}
$line = $beginline . $restofline;
if (($line=~/\\end{document}/) and not $inverb)
{
$indoc=0;
$line=~s/\\end{document}//;
}
$line=~s/\n/\n\% /mg;
print DTX "$line";
}
close DOC;
print DTX "\n";
if ($stopfound==0)
{
print DTX "\% \\StopEventually{$stopeventually}\n";
}
print DTX "\% \\section{$codetitle}\n";
@derivedfiles = (@derivedfiles,@derivedluafiles);
for (my $idx = 0; $idx <= $#derivedfiles; $idx++)
{
my $thisinfile = $derivedfiles[$idx]{'in'};
my $thisoutfile = $derivedfiles[$idx]{'out'};
# if ($verbose)
# {
# print "$srcdirfile -> $_ \n";
# }
open SRC, $thisinfile or die "Can't open infile no $idx `$thisinfile'\n";
print DTX "\% \\iffalse\n" if $idx == 0;
print DTX "\%<*$thisoutfile>\n";
print DTX "\% \\fi\n";
print DTX "\% \\clearpage\n";
print DTX "\% \n";
print DTX "\% \\subsection{$thisoutfile}\n";
## TODO only put this before the first occurence of \\ProvidePackage :
print DTX "\% \\begin{macrocode}\n";
my $macrocode = 0;
my $comment = 0;
# foreach my $expr (@comment)
# {
# if ($thisoutfile =~ m/$expr/)
# {
# print DTX "\%\\iffalse\n";
#
# $comment = 1;
# }
# }
#
# foreach $expr (@macrocode)
# {
# if ($thisoutfile =~ m/$expr/)
# {
# print DTX "\% \\begin{macrocode}\n";
#
# $macrocode = 1;
# }
# }
#
while (<SRC>)
{
last if m/^\\endinput/;
print DTX "$_";
}
if ($macrocode == 1)
{
print DTX "\% \\end{macrocode}\n";
}
if ($comment == 1)
{
print DTX "\%\\fi\n";
}
print DTX "\% \\end{macrocode}\n";
print DTX "\% \\iffalse\n";
print DTX "\%</$thisoutfile>\n";
close SRC;
}
print DTX <<_END
% \\fi
% \\clearpage
% \\PrintChanges
% \\Finale
%
_END
;
# auxiliary files
for (my $idx = 0; $idx <= $#deriveddocfiles; $idx++)
{
my $thisinfile = $deriveddocfiles[$idx]{'in'};
my $thisoutfile = $deriveddocfiles[$idx]{'out'};
open SRC, $thisinfile or die "Can't open infile no $idx `$thisinfile'\n";
print DTX "\% \\iffalse\n" if $idx == 0;
print DTX "\%<*$thisoutfile>\n";
while (<SRC>)
{
print DTX "$_";
}
print DTX "\%</$thisoutfile>\n";
close SRC;
}
print DTX <<_END
% \\fi
%
% \\typeout{*************************************************************}
% \\typeout{*}
% \\typeout{* To finish the installation you have to move the following}
% \\typeout{* file into a directory searched by TeX:}
% \\typeout{*}
% \\typeout{* \\space\\space\\space all *.sty, *.lua, *.def and *.ldf files}
% \\typeout{*}
% \\typeout{* You also need to compile the *.map files with teckit_compile}
% \\typeout{* and place the resulting *.tec files under}
% \\typeout{* .../fonts/misc/xetex/fontmapping}
% \\typeout{*}
% \\typeout{*************************************************************}
\\endinput
_END
;
close DTX;
sub syntaxerror
{
die "Syntax : makedtx [options] <basename>\nUse -h for help\n";
}
sub help
{
print "makedtx Help\n\n";
print "Current Version : $version\n\n";
print "usage : makedtx [options] -src \"<expr>=><expr>\" -doc <filename> <basename>\n\n";
print "makedtx can be used to construct a LaTeX2e dtx and ins file from\n";
print "the specified source code. The final command line argument\n";
print "<basename> should be used to specify the basename of the dtx\n";
print "and ins files.\n\n";
print "-src \"<expr1>=><expr2>\"\n";
print "The command line switch -src identifies the original source code and the name\n";
print "of the file to which it will utimately be extracted on latexing the ins file\n";
print "<expr1> can be a Perl expression, such as (foo)src.(sty), and <expr2> can\n";
print "a Perl substitution style expression, such as $1.$2\n";
print "Note that double quotes must be used to prevent shell expansion\n";
print "Multiple invocations of -src are permitted\n";
print "See examples below.\n\n";
print "-doc <filename>\n";
print "The name of the documentation source code. This should be a LaTeX2e document\n\n";
print "Optional Arguments:\n\n";
print "-dir <directory> : search for source files in <directory>\n";
print "-op <character> : set the pattern matching operator (default '$patternop')\n";
print "-askforoverwrite : set askforoverwrite switch in INS file to true\n";
print "-noaskforoverwrite : set askforoverwrite switch in INS file to false (default)\n";
print "-preamble <text> : set the preamble. Standard one inserted if omitted\n";
print "-postamble <text> : set the postamble.\n";
print "-setambles \"<pattern>=><text>\" : set pre- and postambles to <text> if file matches pattern\n";
print "-author <text> : name of author (inserted into standard preamble. User name inserted if omitted)\n";
print "-date <text> : copyright date\n";
print "-ins : create the ins file (default)\n";
print "-noins : don't create the ins file\n";
print "-prefinale <text> : add <text> immediately prior to \\Finale\n";
print "-macrocode <expr> : surround any file which matches <expr> in a macrocode environment\n";
print "-comment <expr> : surround any file which matches <expr> with \\iffalse \\fi pair\n";
print "-codetitle <text> : The title for the documented code section (default: The Code)\n";
print "-license <license> : use the given license for the preamble.\n";
print " Known licenses: lppl (default), bsd, gpl.\n";
print "-h : help message\n";
print "-v : verbose\n\n";
print "Examples:\n\n";
print "Example 1:\n";
print "Documentation is in foodoc.tex\n";
print "Source code is in foosrc.sty. The final extracted version should be \n";
print "called foo.sty. The dtx file should be called foo.dtx and the ins file\n";
print " should be called foo.ins\n\n";
print "makedtx -src \"foosrc\\.sty=>foo.sty\" -doc foodoc.tex foo\n\n";
print "Example 2:\n";
print "Documentation is in bardoc.tex\n";
print "Source code is in barsrc.sty. The final extracted version should be\n";
print "called bar.sty. Source code is also in barsrc.bst. The final extracted\n";
print "version should be called bar.bst. The dtx file should be called bar.dtx and\n";
print "the ins file should be called bar.ins\n\n";
print "makedtx -src \"barsrc\\.sty=>bar.sty\" -src \"barsrc\\.bst=>bar.bst\" -doc bardoc.tex bar\n\n";
print "Or\n\n";
print "makedtx -src \"barsrc\\.(bst|sty)=>bar.\$1\" -doc bardoc.tex bar\n\n";
die;
}
sub eatinitialspaces
{
my ($STR) = @_;
while (substr($STR,0,1) =~ /^\s$/)
{
$STR = substr($STR,1);
}
return $STR;
}
sub getnextgroup
{
my($curline) = @_;
$curline = &eatinitialspaces($curline);
# check to see if current string is blank
if ($curline!~/[^\s]+/m)
{
return ("","",0);
}
if ((my $group = substr($curline,0,1)) ne "{")
{
# next group hasn't been delimited with braces
# return first non-whitespace character
$curline = substr($curline,1);
# unless it's a backslash, in which case get command name
if ($group eq "\\")
{
if ($curline=~/([a-zA-Z]+)(^[a-zA-Z].*)/m)
{
$group = $1;
$curline = $2;
}
else
{
# command is made up of backslash followed by symbol
$curline=~/([\W_0-9\s\\])(.*)/m;
$group = $1;
$curline = $2;
}
}
return ($group,$curline,1);
}
my $pos=index($curline, "{");
my $startpos=$pos;
my $posopen=0;
my $posclose=0;
my $bracelevel = 1;
my $done=0;
while (!$done)
{
$pos++;
$posopen = index($curline, "{", $pos);
# check to make sure it's not a \{
while ((substr($curline, $posopen-1,1) eq "\\") and ($posopen > 0))
{
# count how many backlashes come before it.
my $i = $posopen-1;
my $numbs = 1;
while ((substr($curline, $i-1,1) eq "\\") and ($i > 0))
{
$numbs++;
$i--;
}
# is $numbs is odd, we have a \{, otherwise we have \\{
if ($numbs%2 == 0)
{
last;
}
else
{
$posopen = index($curline, "{", $posopen+1);
}
}
$posclose= index($curline, "}", $pos);
# check to make sure it's not a \}
while ((substr($curline, $posclose-1,1) eq "\\") and ($posclose > 0))
{
# count how many backlashes come before it.
my $i = $posclose-1;
my $numbs = 1;
while ((substr($curline, $i-1,1) eq "\\") and ($i > 0))
{
$numbs++;
$i--;
}
# is $numbs is odd, we have a \}, otherwise we have \\}
if ($numbs%2 == 0)
{
last;
}
else
{
$posclose = index($curline, "}", $posclose+1);
}
}
if (($posopen==-1) and ($posclose==-1))
{
$done=1;
}
elsif ($posopen==-1)
{
$pos=$posclose;
$bracelevel--;
if ($bracelevel==0)
{
my $group = substr($curline, $startpos+1, $pos-$startpos-1);
$curline = substr($curline, $pos+1);
return ($group,$curline,1);
}
}
elsif ($posclose==-1)
{
$pos=$posopen;
$bracelevel++;
}
elsif ($posopen<$posclose)
{
$pos=$posopen;
$bracelevel++;
}
elsif ($posclose<$posopen)
{
$pos=$posclose;
$bracelevel--;
if ($bracelevel==0)
{
my $group = substr($curline, $startpos+1, $pos-$startpos-1);
$curline = substr($curline, $pos+1);
return ($group,$curline,1);
}
}
}
# closing brace must be on another line
return ("", $curline, 0);
}
1;