-
Notifications
You must be signed in to change notification settings - Fork 25
/
convert-to-mbx.pl
executable file
·1777 lines (1550 loc) · 47.9 KB
/
convert-to-mbx.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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# Script to convert this particular LaTeX file to Pretext or PTX (used to be Mathbook or MBX)
#
# I assume this could be adapted to other projects, but it would need to be heavily modified.
# It does not read arbitrary latex.
#
print "running ...\n";
$num_errors = 0;
my @ins;
# FIXME: same script should be used for realanal.tex and realanal2.tex, perhaps both volumes all at once?
open(my $in,'<', "realanal12.tex") or die $!;
open(my $out, '>' ,"realanal-out.xml") or die $!;
$mbxignore = 0;
$commands = "";
print $out <<END;
<?xml version="1.0" encoding="UTF-8" ?>
<pretext>
END
$docinfoextra = "";
$macrosextra = "";
# No \input file reading here (FIXME?)
while($line = <$in>)
{
chomp($line);
if ($line =~ m/^%mbxFIXME/) {
printf("\n\n\nHUH?? FOUND mbxFIXME!\n\n\n");
$num_errors++;
} elsif ($line =~ m/^%mbxSTARTIGNORE/) {
$mbxignore = 1;
} elsif ($line =~ m/^%mbxENDIGNORE/) {
$mbxignore = 0;
} elsif ($line =~ m/^%mbx[ \t](.*)$/) {
print $out "$1\n";
} elsif ($line =~ m/^%mbxdocinfo[ \t](.*)$/) {
$docinfoextra = $docinfoextra . "$1\n";
} elsif ($line =~ m/^%mbxmacro[ \t](.*)$/) {
$macrosextra = $macrosextra . "$1\n";
} elsif ($line =~ m/^\\begin\{document\}/) {
printf ("found begin document\n");
last;
} elsif ($mbxignore == 0 && $line =~ m/^\\renewcommand/) {
$commands = $commands . "$line\n";
} elsif ($mbxignore == 0 && $line =~ m/^\\newcommand/) {
$commands = $commands . "$line\n";
}
}
print $out "<docinfo>\n";
if ($docinfoextra ne "") {
print $out $docinfoextra;
}
if ($commands ne "" || $macrosextra ne "") {
print $out " <macros>\n";
print $out $commands;
print $out $macrosextra;
print $out " </macros>\n";
}
print $out "</docinfo>\n";
print $out "<book xml:id=\"ra\">\n";
$didp = 0;
$inchapter = 0;
$insection = 0;
$insubsection = 0;
$insubsubsection = 0;
$initem = 0;
$inparagraph = 0;
$chapter_num = 0;
$section_num = 0;
$subsection_num = 0;
$subsubsection_num = 0;
$in_appendix = 0;
$exercise_num = 0;
$thm_num = 0;
$figure_num = 0;
$table_num = 0;
#FIXME: equation counter implement
$equation_num = 0;
$list_level = 0;
$list_start = 1;
#print $out $commands;
sub close_paragraph {
if ($inparagraph) {
$inparagraph = 0;
print $out "</p>\n"
}
}
sub close_item {
close_paragraph ();
if ($initem) {
$initem = 0;
print $out "</li>\n"
}
}
sub close_subsubsection {
close_paragraph ();
if ($insubsubsection == 2) {
$insubsubsection = 0;
print $out "</introduction>\n\n"
} elsif ($insubsubsection) {
$insubsubsection = 0;
print $out "</subsubsection>\n\n"
}
}
sub close_subsection {
close_subsubsection ();
if ($insubsection == 2) {
$insubsection = 0;
print $out "</introduction>\n\n"
} elsif ($insubsection == 3) {
$insubsection = 0;
print $out "</exercises>\n\n"
} elsif ($insubsection) {
$insubsection = 0;
print $out "</subsection>\n\n"
}
}
sub close_section {
close_subsection();
if ($insection) {
$insection = 0;
print $out "</section>\n\n"
}
}
sub close_chapter {
close_section ();
if ($inchapter) {
$inchapter = 0;
if ($in_appendix == 0) {
print $out "</chapter>\n\n"
} else {
print $out "</appendix>\n\n"
}
}
}
sub open_paragraph {
close_paragraph ();
$inparagraph = 1;
print $out "<p>\n"
}
sub open_paragraph_if_not_open {
if ($inparagraph == 0) {
$inparagraph = 1;
print $out "<p>\n"
}
}
sub open_item {
close_item ();
$initem = 1;
print $out "<li>\n"
}
sub open_item_id {
my $theid = shift;
close_item ();
$initem = 1;
print $out "<li xml:id=\"$theid\">\n"
}
sub get_chapter_num {
if ($in_appendix == 0) {
return "$chapter_num";
} else {
if ($chapter_num == 1) { return "A"; }
elsif ($chapter_num == 2) { return "B"; }
elsif ($chapter_num == 3) { return "C"; }
elsif ($chapter_num == 4) { return "D"; }
elsif ($chapter_num == 5) { return "E"; }
print "\n\n\nHUH? too many appendices ($chapter_num)\n\n\n";
$num_errors++;
}
}
sub open_subsubsection {
my $theid = shift;
my $name = shift;
close_subsubsection ();
$insubsubsection = 1;
$subsubsection_num = $subsubsection_num+1;
my $ch = get_chapter_num();
if ($theid ne "") {
print $out "\n<subsubsection xml:id=\"$theid\" number=\"$ch.$section_num.$subsection_num.$subsubsection_num\">\n"
} else {
print $out "\n<subsubsection number=\"$ch.$section_num.$subsection_num.$subsubsection_num\">\n"
}
print "(subsubsection >$name< label >$theid<)\n";
print $out "<title>$name</title>\n";
}
sub open_intro_subsubsection {
$insubsubsection = 2;
print "(intro subsubsection)\n";
print $out "\n<introduction>\n";
}
sub open_subsection {
my $theid = shift;
my $name = shift;
close_subsection ();
$insubsection = 1;
$subsection_num = $subsection_num+1;
$subsubsection_num = 0;
my $ch = get_chapter_num();
if ($theid ne "") {
print $out "\n<subsection xml:id=\"$theid\" number=\"$ch.$section_num.$subsection_num\">\n"
} else {
print $out "\n<subsection number=\"$ch.$section_num.$subsection_num\">\n"
}
print "(subsection >$name< label >$theid<)\n";
print $out "<title>$name</title>\n";
# Don't open an intro subsubsection, that's done manually with %mbxINTROSUBSUBSECTION
}
sub open_intro_subsection {
$insubsection = 2;
print "(intro subsection)\n";
print $out "\n<introduction>\n";
}
sub open_exercise_subsection {
close_subsection ();
$insubsection = 3;
$subsection_num = $subsection_num+1;
$subsubsection_num = 0;
my $ch = get_chapter_num();
print "(exercises subsection)\n";
print $out "\n<exercises number=\"$ch.$section_num.$subsection_num\">\n"
}
sub open_section {
my $theid = shift;
my $name = shift;
close_section ();
$insection = 1;
$section_num = $section_num+1;
$subsection_num = 0;
$subsubsection_num = 0;
$exercise_num = 0;
$thm_num = 0;
my $ch = get_chapter_num();
if ($theid ne "") {
print $out "\n<section xml:id=\"$theid\" number=\"$ch.$section_num\">\n"
} else {
print $out "\n<section number=\"$ch.$section_num\">\n"
}
print "(section >$name< label >$theid<)\n";
print $out "<title>$name</title>\n";
# Don't open an intro subsection, that's done manually with %mbxINTROSUBSECTION
#open_intro_subsection();
}
sub open_chapter {
my $theid = shift;
close_chapter ();
$inchapter = 1;
$chapter_num = $chapter_num+1;
$section_num = 0;
$subsection_num = 0;
$subsubsection_num = 0;
$exercise_num = 0;
$thm_num = 0;
$equation_num = 0;
$figure_num = 0;
$table_num = 0;
my $ch = get_chapter_num();
if ($in_appendix == 0) {
if ($theid ne "") {
print $out "\n<chapter xml:id=\"$theid\" number=\"$ch\">\n"
} else {
print $out "\n<chapter number=\"$ch\">\n"
}
} else {
if ($theid ne "") {
print $out "\n<appendix xml:id=\"$theid\" number=\"$ch\">\n"
} else {
print $out "\n<appendix number=\"$ch\">\n"
}
}
}
sub modify_id {
my $theid = shift;
$theid =~ s/^([0-9])/X$1/;
$theid =~ s/:/_/g;
$theid =~ s/\./_/g;
return $theid;
}
sub do_line_subs {
my $line = shift;
if ($line =~ s|~|<nbsp/>|g) {
print "substituted nbsps\n";
}
if ($line =~ s|---|—|g) {
print "substituted emdashes\n";
}
if ($line =~ s|--|–|g) {
print "substituted endashes\n";
}
if ($line =~ s|\\texorpdfstring\{([^}]*)\}\{([^}]*)\}|$1|g) {
print "substituted texorpdfstring\n";
}
##FIXME: should we do this?
#if ($line =~ s|-|‐|g) {
#print "substituted hyphens\n";
#}
return $line;
}
sub do_more_subs {
my $line = do_line_subs(shift);
while ($line =~ s!\\eqref\{(.*?)\}!<xref ref="%MBXID%" text="global"/>!s) {
my $theid = modify_id($1);
$line =~ s!%MBXID%!$theid!;
}
while ($line =~ s!\\ref\{(.*?)\}!<xref ref="%MBXID%" text="global"/>!s) {
my $theid = modify_id($1);
$line =~ s!%MBXID%!$theid!;
}
while ($line =~ s!\\(chapter|Chapter|appendix|Appendix|section|subsection|thm|lemma|prop|cor|defn|remark|table|figure|example|exercise)v?ref\{(.*?)\}!<xref ref="%MBXID%" text="type-global"/>!s) {
my $theid = modify_id($2);
$line =~ s!%MBXID%!$theid!;
}
$line =~ s|\$(.*?)\$|<m>$1</m>|gs;
$line =~ s|\\myquote\{(.*?)\}|<q>$1</q>|sg;
$line =~ s|\\index\{([^}]*)\}|<idx>$1</idx>|gs;
$line =~ s|\\myindex\{([^}]*)\}|$1<idx>$1</idx>|gs;
return $line;
}
sub print_line {
my $line = shift;
if ($inparagraph == 0 && $line =~ m/[^ \r\n\t]/) {
open_paragraph ();
}
$line = do_line_subs($line);
print "line -- >$line<\n";
print $out $line;
}
sub do_thmtitle_subs {
my $title = shift;
$title =~ s|\\href\{(.*?)\}\{(.*?)\}|<url href=\"$1\" visual=\"$1\">$2</url>|gs;
#FIXME: should check if multiple footnotes work
while ($title =~ s!\\footnote\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}!<fn>$1</fn>!s) {
;
}
$title = do_more_subs($title);
return $title;
}
sub get_exercise_number {
my $ch = get_chapter_num();
if ($insection) {
return "$ch.$section_num.$exercise_num";
} elsif ($inchapter) {
return "$ch.$exercise_num";
} else {
return "$exercise_num";
}
}
sub get_thm_number {
my $ch = get_chapter_num();
if ($insection) {
return "$ch.$section_num.$thm_num";
} elsif ($inchapter) {
return "$ch.$thm_num";
} else {
return "$thm_num";
}
}
sub get_figure_number {
my $ch = get_chapter_num();
if ($inchapter and not $ch eq "0") {
return "$ch.$figure_num";
} else {
return "$figure_num";
}
}
sub get_table_number {
my $ch = get_chapter_num();
if ($inchapter and not $ch eq "0") {
return "$ch.$table_num";
} else {
return "$table_num";
}
}
sub get_equation_number {
my $ch = get_chapter_num();
if ($inchapter and not $ch eq "0") {
return "$ch.$equation_num";
} else {
return "$equation_num";
}
}
sub get_size_of_svg {
my $thefile = shift;
$thesizestr = qx!cat $thefile | grep '^<svg ' | sed 's/^<[^>]*\\(width="[^"]*"\\) *\\(height="[^"]*"\\).*\$/\\1 \\2/'!;
# If units missing, add them
$thesizestr =~ s/"([0-9.]*)"/"\1px"/;
$thesizestr =~ s/"([0-9.]*)"/"\1px"/;
chomp($thesizestr);
print "the size string of $thefile >$thesizestr<\n";
return $thesizestr
}
sub ensure_mbx_svg_version {
my $thefile = shift;
print "ENSURE $thefile-mbx.svg\n";
if ((not -e "$thefile-mbx.svg") and (-e "$thefile.pdf")) {
print "MAKING $thefile-mbx.svg from PDF\n";
system("pdf2svg $thefile.pdf $thefile-mbx.svg");
}
}
#sub ensure_mbx_png_version {
# my $thefile = shift;
#
# print "ENSURE $thefile-mbx.png\n";
# if ((not -e "$thefile-mbx.png") and (-e "$thefile.pdf")) {
# print "MAKING $thefile-mbx.png from PDF\n";
# system("./pdftopng.sh $thefile.pdf $thefile-mbx.png 192");
# }
#}
sub do_displaymath_subs {
my $eqn = shift;
$eqn =~ s/\\displaybreak\[0\]//g;
$eqn =~ s/\\avoidbreak[\r\n]*//g;
$eqn =~ s/\\pagebreak\[[0-9]\][\r\n]*//g;
return $eqn;
}
sub read_paragraph {
my $para = "";
my $read_something = 0;
while(1) {
my $line = <$in>;
if ( ! defined $line) {
print "FOUND END OF FILE\n";
if (@ins) {
print "END OF input FILE\n))))\n\n";
close($in);
$in = pop @ins;
next;
} else {
# This shouldsn't happen, we should have found and end of document
print "END OF MAIN FILE\n))))\n\n";
print "ERROR: no \\end{document} found so faking it\n\n";
$num_errors++;
$para = $para . "\n\\end{document}";
}
}
chomp($line);
#things that should go into the mbx to be processed (not raw MBX
#as %mbx) but should be ignored by latex
$line =~ s/^%mbxlatex //;
if ($line =~ m/^%mbxFIXME/) {
printf("\n\n\nFOUND mbxFIXME!\n\n\n");
$num_errors++;
} elsif ($line =~ m/^%mbxSTARTIGNORE/) {
$mbxignore = 1;
} elsif ($line =~ m/^%mbxENDIGNORE/) {
$mbxignore = 0;
} elsif ($mbxignore == 0 &&
($line =~ m/^[ \t]*\\input[ \t][ \t]*(.*)$/ ||
$line =~ m/^[ \t]*\\input\{(.*)\}.*$/)) {
my $thefile = $1;
push @ins, $in;
undef $in;
print "\n((((\nFOUND \\input $thefile\n";
if ( ! open($in,'<', $thefile)) {
print "\n\nHUH???\n\nThere is an \\input $thefile ... but I can't open \"$thefile\"\n\n))))\n\n";
$num_errors++;
$in = pop @ins;
}
} elsif ($line =~ s/^%mbx[ \t](.*)$/\\mbx ${1}%ENDOFLINE%/) {
#do nothing here, will deal with this later
#protect the xml thingies from us
$line =~ s/>/%MBXGT%/g;
$line =~ s/</%MBXLT%/g;
$line =~ s/&/%MBXAMP%/g;
$para = $para . $line . "\n";
#FIXME: if it would be here it wouldn't work in the middle
#of a paragraph:
#This will only work right if the paragraphs are separated, that is if it is
#in the middle of a paragraph it put the %mbx line in the wrong place
#my $thembxline = $1;
##FIXME: this is a terrible hack, but the only way I can get
##hardcoded number stuff to work
##This will only work right if the paragraphs are separated, that is if it is
##in the middle of a paragraph it will get the numbers wrong.
#while ($thembxline =~ m/%MBXEQNNUMBER%/) {
#$equation_num = $equation_num+1;
#$the_num = get_equation_number ();
#$thembxline =~ s/%MBXEQNNUMBER%/$the_num/;
#}
#print $out "$thembxline\n";
} elsif ($line =~ s/^%mbxCLOSEPARAGRAPH[ \t]*$/\\mbxCLOSEPARAGRAPH/) {
#do nothing here, will deal with this later
$para = $para . $line . "\n";
} elsif ($line =~ s/^%mbxCLOSEITEM[ \t]*$/\\mbxCLOSEITEM/) {
#do nothing here, will deal with this later
$para = $para . $line . "\n";
} elsif ($line =~ m/^\\documentclass/ ||
$line =~ m/^\\usepackage/ ||
$line =~ m/^\\addcontentsline/ ||
$line =~ m/^\\markboth/) {
# do nothing
;
} elsif ($line =~ m/^%mbxCLOSECHAPTER/) {
close_chapter ();
} elsif ($line =~ m/^%mbxINTROSUBSUBSECTION/) {
open_intro_subsubsection ();
} elsif ($line =~ m/^%mbxINTROSUBSECTION/) {
open_intro_subsection ();
} elsif ($mbxignore == 0) {
my $newline = 1;
if ($line =~ m/^%/ || $line =~ m/[^\\]%/) {
$newline = 0;
}
$line =~ s/^%.*$//;
$line =~ s/([^\\])%.*$/$1/;
if ($line =~ m/^[ \t]*$/ && $newline) {
if ($read_something) {
$para = $para . $line; # . " ";
last;
}
}
$read_something = 1;
if ($newline) {
$para = $para . $line . "\n";
} else {
$para = $para . $line;
}
}
}
#Do simple substitutions, (these are incomplete, just the ones I actually used at some point)
$para =~ s/\\"\{o\}/ö/g;
$para =~ s/\\"o/ö/g;
$para =~ s/\\\^o/ô/g;
$para =~ s/\\"i/ï/g;
$para =~ s/\\c\{S\}/Ş/g;
$para =~ s/\\u\{g\}/ğ/g;
$para =~ s/\\v\{r\}/ř/g;
$para =~ s/\\c\{c\}/ç/g;
$para =~ s/\\'e/é/g;
$para =~ s/\\'\{e\}/é/g;
$para =~ s/\\`e/è/g;
$para =~ s/\\`\{e\}/è/g;
$para =~ s/\\`a/à/g;
$para =~ s/\\`\{a\}/à/g;
$para =~ s/\\'a/á/g;
$para =~ s/\\'o/ó/g;
$para =~ s/\\'i/í/g;
$para =~ s/\{\\i\}/ı/g;
$para =~ s/\\'\{i\}/í/g;
$para =~ s/\\'E/É/g;
$para =~ s/\\'\{E\}/É/g;
$para =~ s/\\S([^a-zA-Z])/§$1/g;
$para =~ s/&/&/g;
$para =~ s/>/>/g;
$para =~ s/</</g;
#strip leading and trailing spaces
#$para =~ s/^ *//;
#$para =~ s/[ \n]*$//;
#Also strip some nonsensical spaces
#$para =~ s/[ \n](\\end{(exercise|example|thm|equation|align|equation\*|align\*)})/$1/g;
return $para;
}
@cltags = ();
while(1)
{
if ($para eq "") {
$para = read_paragraph ();
}
#print "\n\nparagraph: [[[$para]]]\n";
if ($para =~ m/^\\end\{document\}/) {
last;
#copy whitespace
} elsif ($para =~ s/^([ \n\r\t])//) {
print $out "$1";
} elsif ($para =~ s/^\\mbx[ \t](.*?)%ENDOFLINE%[ \n]*//) {
my $thembxline = $1;
$thembxline =~ s/%MBXGT%/>/g;
$thembxline =~ s/%MBXLT%/</g;
$thembxline =~ s/%MBXAMP%/&/g;
print $out "$thembxline\n";
} elsif ($para =~ s/^\\mbxCLOSEPARAGRAPH[ \n]*//) {
close_paragraph();
} elsif ($para =~ s/^\\mbxCLOSEITEM[ \n]*//) {
close_item();
} elsif ($para =~ s/^\$([^\$]+)\$//) {
my $line = $1;
open_paragraph_if_not_open ();
print $out "<m>$line</m>";
} elsif ($para =~ s/^\\chapter\*\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}[ \n]*//) {
#FIXME: un-numbered
my $name = do_line_subs($1);
my $theid;
if ($para =~ s/^\\label\{([^}]*)\}[ \n]*//) {
$theid = modify_id($1);
} else {
$theid = "";
}
$name =~ s|\$(.*?)\$|<m>$1</m>|gs;
$chapter_num = $chapter_num-1; #hack
open_chapter($theid);
print "(chapter >$name< label >$theid<)\n";
print $out "<title>$name</title>\n";
print "PARA:>$para<\n";
} elsif ($para =~ s/^\\chapter\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}[ \n]*//) {
my $name = do_line_subs($1);
my $theid;
if ($para =~ s/^\\label\{([^}]*)\}[ \n]*//) {
$theid = modify_id($1);
} else {
$theid = "";
}
$name =~ s|\$(.*?)\$|<m>$1</m>|gs;
open_chapter($theid);
print "(chapter >$name< label >$theid<)\n";
print $out "<title>$name</title>\n";
} elsif ($para =~ s/^\\section\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}[ \n]*//) {
my $name = do_line_subs($1);
my $theid;
if ($para =~ s/^\\label\{([^}]*)\}[ \n]*//) {
$theid = modify_id($1);
} else {
$theid = "";
}
$name =~ s|\$(.*?)\$|<m>$1</m>|gs;
open_section($theid,$name);
} elsif ($para =~ s/^\\subsection\{Exercises\}[ \n]*//) {
open_exercise_subsection();
} elsif ($para =~ s/^\\subsection\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}[ \n]*//) {
my $name = do_line_subs($1);
my $theid;
if ($para =~ s/^\\label\{([^}]*)\}[ \n]*//) {
$theid = modify_id($1);
} else {
$theid = "";
}
$name =~ s|\$(.*?)\$|<m>$1</m>|gs;
open_subsection($theid,$name);
} elsif ($para =~ s/^\\subsubsection\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}[ \n]*//) {
my $name = do_line_subs($1);
my $theid;
if ($para =~ s/^\\label\{([^}]*)\}[ \n]*//) {
$theid = modify_id($1);
} else {
$theid = "";
}
$name =~ s|\$(.*?)\$|<m>$1</m>|gs;
open_subsubsection($theid,$name);
# this assumes sectionnotes come in their own $para
} elsif ($para =~ s/^\\sectionnotes\{//s) {
print "(SECTIONNOTES start)\n";
open_paragraph_if_not_open ();
print $out "<em>Note: ";
push @cltags, "sectionnotes";
} elsif ($para =~ s/^\\setcounter\{exercise\}\{(.*?)\}[ \n\t]*//s) {
$exercise_num=$1;
} elsif ($para =~ s/^\\href\{([^}]*)\}\{([^}]*)\}//) {
open_paragraph_if_not_open ();
print "(link $1 $2)\n";
print $out "<url href=\"$1\" visual=\"$1\">$2</url>";
} elsif ($para =~ s/^\\url\{([^}]*)\}//) {
open_paragraph_if_not_open ();
print "(url $1)\n";
print $out "<url href=\"$1\" />";
} elsif ($para =~ s/^\\cite\{([^}]*)\}//) {
open_paragraph_if_not_open ();
$id=modify_id($1);
print "(cite $id ($1))\n";
print $out "<xref ref=\"biblio-$id\"/>";
} elsif ($para =~ s/^\\index\{([^}]*)\}//) {
# opening a paragraph leads to some empty paragraphs
# at the beginninng of subsections/sections
#open_paragraph_if_not_open ();
print "(index $1)\n";
my $index = do_line_subs($1);
my $sortby = "";
if ($index =~ s|^(.*)@(.*)$|$2|s) {
$sortby = $1;
}
$index =~ s|\$(.*?)\$|<m>$1</m>|sg;
if ($sortby eq "") {
$index =~ s|^(.*)!(.*)$|<h>$1</h><h>$2</h>|s;
print $out "<idx>$index</idx>";
} else {
if ($index =~ s|^(.*)!(.*)$|<h sortby="$sortby">$1</h><h>$2</h>|s) {
print $out "<idx>$index</idx>";
} else {
print $out "<idx><h sortby=\"$sortby\">$index</h></idx>";
}
}
} elsif ($para =~ s/^\\myindex\{([^}]*)\}//) {
open_paragraph_if_not_open ();
print "(myindex $1)\n";
my $index = do_line_subs($1);
$index =~ s|\$(.*?)\$|<m>$1</m>|sg;
print $out "$index<idx>$index</idx>";
} elsif ($para =~ s/^\\volIref\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}/$3/) {
printf "volIref using \"$3\"\n";
#this just replaces this with the second argument
} elsif ($para =~ s/^\\volIIref\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}\{([^{}]*|([^{}]*\{[^{}]*\}[^{}]*)*)\}/$3/) {
printf "volIIref using \"$3\"\n";
#this just replaces this with the second argument
} elsif ($para =~ s/^\\eqref\{([^}]*)\}//) {
open_paragraph_if_not_open ();
my $theid = modify_id($1);
print "(eqref $theid)\n";
print $out "<xref ref=\"$theid\" text=\"global\"/>";
} elsif ($para =~ s/^\\ref\{([^}]*)\}//) {
open_paragraph_if_not_open ();
my $theid = modify_id($1);
print "(ref $theid)\n";
print $out "<xref ref=\"$theid\" text=\"global\"/>";
} elsif ($para =~ s/^\\chapterref\{([^}]*)\}// ||
$para =~ s/^\\chaptervref\{([^}]*)\}// ||
$para =~ s/^\\Chapterref\{([^}]*)\}// ||
$para =~ s/^\\appendixref\{([^}]*)\}// ||
$para =~ s/^\\appendixvref\{([^}]*)\}// ||
$para =~ s/^\\Appendixref\{([^}]*)\}// ||
$para =~ s/^\\sectionref\{([^}]*)\}// ||
$para =~ s/^\\sectionvref\{([^}]*)\}// ||
$para =~ s/^\\subsectionref\{([^}]*)\}// ||
$para =~ s/^\\subsectionvref\{([^}]*)\}// ||
$para =~ s/^\\thmref\{([^}]*)\}// ||
$para =~ s/^\\thmvref\{([^}]*)\}// ||
$para =~ s/^\\lemmaref\{([^}]*)\}// ||
$para =~ s/^\\lemmavref\{([^}]*)\}// ||
$para =~ s/^\\propref\{([^}]*)\}// ||
$para =~ s/^\\propvref\{([^}]*)\}// ||
$para =~ s/^\\corref\{([^}]*)\}// ||
$para =~ s/^\\corvref\{([^}]*)\}// ||
$para =~ s/^\\remarkref\{([^}]*)\}// ||
$para =~ s/^\\remarkvref\{([^}]*)\}// ||
$para =~ s/^\\defnref\{([^}]*)\}// ||
$para =~ s/^\\defnvref\{([^}]*)\}// ||
$para =~ s/^\\tableref\{([^}]*)\}// ||
$para =~ s/^\\tablevref\{([^}]*)\}// ||
$para =~ s/^\\figureref\{([^}]*)\}// ||
$para =~ s/^\\figurevref\{([^}]*)\}// ||
$para =~ s/^\\exampleref\{([^}]*)\}// ||
$para =~ s/^\\examplevref\{([^}]*)\}// ||
$para =~ s/^\\exerciseref\{([^}]*)\}// ||
$para =~ s/^\\exercisevref\{([^}]*)\}//) {
my $theid = modify_id($1);
open_paragraph_if_not_open ();
print "(named ref $theid)\n";
print $out "<xref ref=\"$theid\" text=\"type-global\"/>";
} elsif ($para =~ s/^\\hyperref\[(.*?)\]\{(.*?)\}//) {
my $name = do_line_subs($2);
my $theid = modify_id($1);
open_paragraph_if_not_open ();
print "(hyperref $theid $name)\n";
print $out "<xref ref=\"$theid\" text=\"custom\">$name</xref>";
} elsif ($para =~ s/^\\hyperlink\{(.*?)\}\{(.*?)\}//) {
my $name = do_line_subs($2);
my $theid = modify_id($1);
open_paragraph_if_not_open ();
print "(hyperlink $theid $name)\n";
print $out "<xref ref=\"$theid\" text=\"custom\">$name</xref>";
} elsif ($para =~ s/^\\emph\{//) {
print "(em start)\n";
open_paragraph_if_not_open();
print $out "<em>";
push @cltags, "em";
} elsif ($para =~ s/^\\myquote\{//) {
print "(myquote start)\n";
open_paragraph_if_not_open();
print $out "<q>";
push @cltags, "myquote";
} elsif ($para =~ s/^\\textbf\{(.*?)\}//s) {
print "(textbf $1)\n";
open_paragraph_if_not_open ();
print $out "<alert>$1</alert>";
} elsif ($para =~ s/^\\texttt\{(.*?)\}//s) {
print "(texttt $1)\n";
open_paragraph_if_not_open ();
print $out "<c>$1</c>";
} elsif ($para =~ s/^\\unit\{(.*?)\}//s) {
print "(unit $1)\n";
open_paragraph_if_not_open ();
print $out "$1";
} elsif ($para =~ s/^\\unit\[(.*?)\]\{(.*?)\}//s) {
my $txt = $1;
my $unit = $2;
$txt =~ s|\$(.*?)\$|<m>$1</m>|gs;
print "(unit $txt $unit)\n";
open_paragraph_if_not_open ();
print $out "$txt $unit";
} elsif ($para =~ s/^\\unitfrac\{(.*?)\}\{(.*?)\}//s) {
print "(unitfrac $1/$2)\n";
open_paragraph_if_not_open ();
print $out "<m>\\nicefrac{\\text{$1}}{\\text{$2}}</m>";
} elsif ($para =~ s/^\\unitfrac\[(.*?)\]\{(.*?)\}\{(.*?)\}//s) {
my $txt = $1;
my $unitnum = $2;
my $unitden = $3;
$txt =~ s|\$(.*?)\$|<m>$1</m>|gs;
print "(unitfrac $txt $unitnum/$unitden)\n";
open_paragraph_if_not_open ();
print $out "$txt <m>\\nicefrac{\\text{$unitnum}}{\\text{$unitden}}</m>";
#FIXME: no notation index in HTML version yet
} elsif ($para =~ s/^\\glsadd\{(.*?)\}//s) {
print "(glsadd $1)\n";
} elsif ($para =~ s/^\\begin\{align\*\}[ \n]*//) {
print "(ALIGN*)\n";
if ($para =~ s/^(.*?)\\end\{align\*\}[ \n]*//s) {
my $eqn = do_displaymath_subs($1);
my $indexes = "";
while ($eqn =~ s/\\myindex\{(.*?)\}/$1/) {
$indexes = $indexes . "<idx>$1</idx>";
}
while ($eqn =~ s/\\index\{(.*?)\}//) {
$indexes = $indexes . "<idx>$1</idx>";
}
$indexes = do_line_subs($indexes);
#FIXME: Is wrapping in aligned all kosher?
print $out "<me>$indexes\n";
print $out "\\begin{aligned}\n";
print "(wrapping in aligned) EQ = $eqn\n";
print $out "$eqn";
print $out "\\end{aligned}\n";
print $out "</me>\n";
} else {
print "\n\n\nHUH?\n\n\nNo end align*!\n\n$para\n\n";
$num_errors++;
}
} elsif ($para =~ s/^\\begin\{align\}[ \n]*//) {
print "(ALIGN)\n";
if ($para =~ s/^(.*?)\\end\{align\}[ \n]*//s) {
my $eqn = do_displaymath_subs($1);
#$theid = "";
#if ($para =~ s/^ *\\label\{(.*?)\} *//) {
# $theid = $1;
#}
my $indexes = "";
while ($eqn =~ s/\\myindex\{(.*?)\}/$1/) {
$indexes = $indexes . "<idx>$1</idx>";
}
while ($eqn =~ s/\\index\{(.*?)\}//) {
$indexes = $indexes . "<idx>$1</idx>";
}
$indexes = do_line_subs($indexes);
print $out "<md>$indexes\n";
print $out "<mrow>\n";
#FIXME: this will mess up things with cases
# But currently I only have one single {align} with numbers
# that will need to get handled
$eqn =~ s|\\\\|</mrow>\n<mrow>\n|g;
print "EQ = $eqn\n";
print $out "$eqn";
print $out "</mrow>\n";
print $out "</md>\n";
} else {
print "\n\n\nHUH?\n\n\nNo end align!\n\n$para\n\n";
$num_errors++;
}
} elsif ($para =~ s/^\\begin\{multline\*\}[ \n]*//) {
print "(MULTLINE*)\n";
if ($para =~ s/^(.*?)\\end\{multline\*\}[ \n]*//s) {
my $eqn = do_displaymath_subs($1);
my $indexes = "";
while ($eqn =~ s/\\myindex\{(.*?)\}/$1/) {
$indexes = $indexes . "<idx>$1</idx>";
}
while ($eqn =~ s/\\index\{(.*?)\}//) {
$indexes = $indexes . "<idx>$1</idx>";
}
$indexes = do_line_subs($indexes);
print $out "<me latexenv=\"multline*\">$indexes\n";
print "EQ(multline*) = $eqn\n";
print $out "$eqn";
print $out "</me>\n";
} else {
print "\n\n\nHUH?\n\n\nNo end multline*!\n\n$para\n\n";
$num_errors++;
}
} elsif ($para =~ s/^\\begin\{multline\}[ \n]*//) {
print "(MULTLINE)\n";
if ($para =~ s/^(.*?)\\end\{multline\}[ \n]*//s) {
my $eqn = do_displaymath_subs($1);
my $theid = "";