-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFT.pm
executable file
·1472 lines (1241 loc) · 37.8 KB
/
AFT.pm
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
# Almost Free Text (AFT) Parser
#
# Copyright (C) 1996-2010 Todd A. Coram. All rights reserved.
#
# This perl script parses aft documents and produces output formatted according
# to an aft 'element' file. See aft-refman.aft for additional information.
#
use strict;
no strict "refs";
package AFT;
use English;
use vars qw ($VERSION $version $outputfile $element_type $author $title
$pre_process_line $my_print);
# Initializations of globals that consumers of this package may read/modify.
#
$author = '';
$title = '';
$VERSION="v5.098";
$version=
"Almost Free Text $VERSION; Copyright 1996-2010 Todd Coram.";
$element_type="bn-html"; # Default
$outputfile=''; # Output file
# You can supply your own pre-processor here... For now, here is a NOOP.
# This is of interest to apps that use AFT.pm as a package.
#
$pre_process_line = sub { local $_ = shift; my($fname,$lcnt) = @_; return $_; };
# You can supply your own print subroutine here.
#
$my_print = sub { print @_; };
# Package scoped variables.
#
my $aft_advert =
"This document was generated using {-AFT $VERSION\@http://www.maplefish.com/todd/aft.html-}";
my $autonumber =0; # prefix sections with nested numbers
my $verbose = 0; # Spew out lots of rambling commentary?
my $tabstop=8; # Default number of spaces constituting a tab.
my $holding_preamble = 1; # True if we have not outputted title/author.
# Holds file handle for table of contents output
#
my $tocout;
#
# Global State (modes). This controls the AFT state machine.
#
my $mode =
{
processing_input => 1, # Are we processing input or dumping results?
in_table => 0, # Are we in table mode?
need_table_headers => 0, # Used to keep track of table building.
eat_sep => 0, # Used to eat a table separator line.
in_quote => 0, # Are we in quote mode?
in_verb => 0, # Are we in verbatim mode?
in_blocked_verb => 0, # Are we in blocked verbatim mode?
in_filtered_verb => 0, # Are we in filtered verbatim mode?
in_para => 0, # Paragraph mode indicator
in_list_el => 0, # Are we inside of a list element?
cur_sect_level => 0, # Current section we are in.
in_sect => 0, # True if we ever go into sections...
};
# Cache (memo) variables. Not state, but artifact used by states.
#
my $table_caption = ''; # Holds current table's caption.
my @list_stack; # A stack of lists as we nest.
my @section_stack; # Keeps track of nesting sections.
my $section_number = Autonum->new();
my %index; # Hash of arrays (name -> [ ref, ... ])
my @note; # Holds collected 'endnotes'.
#
# Submodes for paragraph mode.
#
my $paragraph =
{
small => 0,
strong => 0,
emphasize => 0,
teletype => 0,
};
my $face =
{
"|" => "Teletype",
"''" => "Emphasis",
"_" => "Strong",
"~" => "Small",
};
# Pragma variables (set from inside documents)
#
my %pragma_prevar = (); # variables expanded before filtering.
my %pragma_ctl = (); # variables used for internal control
# Set up some convenient symbols %%
#
sub setup_symbols {
$pragma_prevar{'lb'} = $AFT_OUTPUT::elem{'LineBreak'};
$pragma_prevar{'sp'} = $AFT_OUTPUT::elem{'NBSPACE'};
$pragma_prevar{'bang'} = '!';
}
# Don't expand pragmas in verbatim mode.
#
$AFT_OUTPUT::pragma_ctl{expandinverbatim} = 'no';
# Ignore square brackets as hyperlink indicators
#
$AFT_OUTPUT::pragma_ctl{verbatimsquarebrackets} = 'no';
# Turn on/off pre and post filtering
#
$AFT_OUTPUT::pragma_ctl{prefilter} = 'yes';
$AFT_OUTPUT::pragma_ctl{postfilter} = 'yes';
# AFT functions
#
my @Functions = (
\&handle_blocked_verbatim, # Must be before first...
\&handle_comments, # Must be first
\&handle_title_preamble, # Must be second
\&handle_includes,
\&handle_image,
\&handle_ruler,
\&handle_sections,
\&handle_lists,
\&handle_centered_text,
\&handle_quoted_text,
\&handle_table,
\&handle_verbatim,
);
# Run AFT from the command line. (The normal way to invoke AFT)
#
sub main {
parse_command_line();
load_element_file(element_file());
setup_symbols();
if ($outputfile eq '') {
# Use first input filename to construct output filename.
#
$outputfile = $ARGV[0];
$outputfile =~ s/\.\w+$//; # remove last '.' and anything following.
$outputfile .= ".".lc($AFT_OUTPUT::elem{
defined $AFT_OUTPUT::elem{'EXT'} ? 'EXT' : 'ID'});
}
# Try and open output file and set it as the default output.
#
open(OUT, ">$outputfile") or die "Can't open $outputfile for output!\n";
select(OUT);
# Announce
#
print(STDERR
"$version\n Writing $element_type output into $outputfile using".
" $INC{element_file()}.\n") if $verbose;
begin();
# Process each file supplied on the command line.
#
foreach my $filename (@ARGV) {
process_file($filename);
}
end();
close(OUT);
close($tocout) if $tocout; # If we wrote a table of contents, close it.
if (my $post_processor = $AFT_OUTPUT::elem{'PostProcessor'}) {
print(STDERR "\nPost Processing with '$post_processor'\n")
and eval $post_processor
or die "Can't post process $outputfile $!\n";
}
exit 0;
}
sub begin {
# Initialize our state.
#
reset_states();
# Hold onto the preamble 'til we see if *Title and *Author info is present.
#
$holding_preamble = 1;
$mode->{processing_input} = 1;
}
# Run AFT on a file given its filename.
#
sub do_file {
my($filename) = @_;
load_element_file(element_file());
begin();
process_file($filename);
end();
}
# Run AFT on a single line supplied as a string (with a reference filename
# and line number-- both can be fake.)
#
sub do_string {
local $_ = shift;
my ($fname, $lcnt) = @_;
$_ = $pre_process_line->($_, $fname, $lcnt);
# Convert every $tabstop spaces into a tab... e.g. /\ {4}/
s/\ {$tabstop}/\t/g if (!$mode->{in_blocked_verb});
# Iterate through all functions until one satisfies the input.
#
foreach my $function (@Functions) {
return if ($function->($fname, $lcnt, $_));
}
# All non-tabbed, non-sectional, non-special lines end up here.
#
# Always reset states
# (take us out of whatever mode we may have been in).
#
reset_states();
# Now handle a special case... We need to detect blank lines to
# determine whether we should end paragraph mode.
#
reset_paragraph(), return if $_ eq '';
# Otherwise, if not in paragraph mode, enter paragraph mode now.
#
enter_paragraph() if !$mode->{in_para};
# and kick out the filtered line.
#
output(filter($_)."\n");
}
# Run AFT on a file given its file handle.
#
sub do_FH_file {
my($fh, $filename) = @_;
load_element_file(element_file());
begin();
process_FH_file($fh, $filename);
end();
}
sub output_preamble {
output($AFT_OUTPUT::file_preamble."\n",
title => $title, author => $author, version => $version);
}
sub output_postamble {
output($AFT_OUTPUT::file_postamble."\n", aft => filter($aft_advert));
}
sub output_indices {
return if (%index == 0); # no indexed words
if (defined($AFT_OUTPUT::elem{'PrintIndex'})) {
output($AFT_OUTPUT::elem{'PrintIndex'}."\n");
return;
}
output($AFT_OUTPUT::elem{'HorizontalLine'}."\n");
do_string("* Index");
do_string("\n");
foreach my $key (sort(keys %index)) {
next if ($key =~ /iNtErNaLNOTE/);
output($AFT_OUTPUT::elem{'LineBreak'});
output("${key} : ");
foreach my $target (@{$index{$key}}) {
do_string("[*($target)], ");
}
}
}
sub output_notes {
output($AFT_OUTPUT::elem{'HorizontalLine'}."\n");
my $count = 1;
foreach my $note (@note) {
do_string("=[(iNtErNaLNOTE$count)]=\n\\[[$count(REFiNtErNaLNOTE$count)]] - $note\n");
output($AFT_OUTPUT::elem{'LineBreak'});
$count++;
}
}
sub end {
# End all modes.
#
reset_states();
$mode->{processing_input} = 0;
# If we ever entered sections...
enter_section(0), output($AFT_OUTPUT::elem{'EndSectLevel1'}."\n")
if ($mode->{in_sect});
output_notes() if ($AFT_OUTPUT::elem{'NotesAtEnd?'} eq 'yes');
output_indices();
# End output file with Postamble..
#
output_postamble();
}
sub parse_command_line {
## Process the command line options.
#
my $usage=
"Usage:\n aft [--autonumber] [--verbose] [--output=file | --output=-]".
" [--type=output_type] infile ..";
use Getopt::Long;
GetOptions ("output=s" => \$outputfile, # output file name
"verbose!" => \$verbose, # output type (html, etc)
"type=s" => \$element_type, # output type (html, etc)
"autonumber!" => \$autonumber, # section numbers
"tabstop=i" => \$tabstop); # number of spaces = tab
print (STDERR "$version\n$usage\n"), exit 2 if (@ARGV == 0);
}
sub element_file {
return "aft-".$element_type.".pm";
}
# loadElementFile(file) - load the supplied element file name.
#
sub load_element_file {
my $elementfile = shift;
# This is more of an '#include' than a package import.
eval
{require $elementfile}; # Sets 3 variables in a subroutine
# called initElements() and adds 2 additional
# subroutines: prefilter() and postfilter().
die "Can't locate $elementfile. \n\t(I looked in: @INC)\n" if $@;
# Initialize elements;
#
AFT_OUTPUT::init_elements();
}
# processFile(fname) - Locate, open and process the supplied file.
#
sub process_file {
my($fname) = @_;
local *IN;
if (!open(*IN, $fname)) {
$fname .= ".aft"; # maybe we just got a a base name?
open(*IN, $fname) or ((warn "Can't open $fname: $!\n"), return -1);
}
# Do that voodoo that you do so well.
#
print (STDERR "\nProcessing $fname.\n[") if $verbose;
process_FH_file(*IN, $fname);
# Done with it, so close it.
#
close (*IN);
print (STDERR "]\nFinished processing $fname.\n") if $verbose;
return 0;
}
# processFH_File (fh,fname) - Process the supplied file by the handle.
#
sub process_FH_file {
my($fh, $fname) = @_;
my $lcnt = 0; # line count
my $continued_line = "";
LINE: while (<$fh>) {
$lcnt++;
chomp;
chop if /.*\r$/; # In case we are unix perl processing a MSDOS .aft file
$continued_line .= $1, next LINE if (/(.*)\\$/ and !$mode->{in_verb}); # collect continuations
do_string($continued_line.$_, $fname, $lcnt); # process complete line
$continued_line = "";
}
do_string($continued_line, $fname, $lcnt) if $continued_line;
}
##### Functions
#
# Handle comments and comment commands
#
sub handle_comments {
my $fname = shift; my $lcnt = shift; local($_) = @_;
# Handle Strike lines (X---)
#
/^X-{3,}([^\-]?.*)/ and do {
output($AFT_OUTPUT::elem{'StrikeLine'}."\n",line => $1);
return 1;
};
# Handle comments and comment commands (pragmas).
#
/^[C\#]-{3,}([^\-]?.*)/ and do {
# See if there is stuff we need to pass directly through the filters.
# #---PASS-'ID' text
#
$1 =~ /PASS-(\w+)\s+(.*)/ and do {
output($2) if ($AFT_OUTPUT::elem{'ID'} eq $1);
return 1;
};
# Set a pragma variable..
# #---SET[-ID] var=value
#
$1 =~ /SET(\s?|-\w+)\s*([^\=\ ]+)\s*=\s*(.*)/ and do {
# Special control variable
$AFT_OUTPUT::pragma_ctl{$2} = $3 if ($1 eq "-CONTROL");
if ("-$AFT_OUTPUT::elem{'ID'}" eq $1) {
$AFT_OUTPUT::pragma_postvar{$2} = $3;
} else {
set_prevar($2,$3) if ($1 !~ /^-/);
}
return 1;
};
# See if we need to adjust tabstop.
# #---TABSTOP=N
#
$1 =~ /TABSTOP=(\d+)/ and do {
$tabstop = $1;
print (STDERR "\n[$fname($lcnt):".
" TABSTOP set to $tabstop spaces.]\n");
return 1;
};
output($AFT_OUTPUT::elem{'CommentLine'}."\n",line => $1);
return 1; # regular comment
};
return 0; # no comments encountered
}
# Handle *Title, *Author and preamble output.
#
sub handle_title_preamble {
my $fname = shift; my $lcnt = shift; local($_) = @_;
# *Title:
#
/^\*Title:\s*(.*)$/ and do {
$title = filter($1);
return 1;
};
# *Author:
#
/^\*Author:\s*(.*)$/ and do {
$author = filter($1);
return 1;
};
# Output the preamble if we have been holding on to it.
#
if ($holding_preamble) {
return 1 if /^\s*$/; # empty line
$holding_preamble = 0;
output_preamble();
# Now print out title and author if they were collected.
# If *Title and *Author were the first two lines in the document,
# then we held the preamble until they were collected.
# Else we assume that they are not available, so we just print
# the preamble.
output($AFT_OUTPUT::elem{"Title"}."\n", title => $title) if $title;
output($AFT_OUTPUT::elem{"Author"}."\n", author => $author) if $author;
}
return 0;
}
# Handle *Insert:, *Include:, *File:,*See File and table of contents.
#
sub handle_includes {
my $fname = shift; my $lcnt = shift; local($_) = @_;
/^\*(Insert|See File|Include|File):\s*(\S+)/ and do {
process_file($2);
return 1;
};
# *TOC: (table of contents)
#
/^\*(TOC)/ and do {
# If there is no automatic table of contents markup, then generate
# an AFT style markup.
if ($AFT_OUTPUT::elem{$1} eq '') {
generate_t_oC($fname);
} else {
output($AFT_OUTPUT::elem{$1}."\n");
}
return 1;
};
return 0;
}
# Handle *Image: and it's variations.
#
sub handle_image {
my $fname = shift; my $lcnt = shift; local($_) = @_;
/^\*(Image|Image-left|Image-center|Image-right):\s*(\S+)/ and do {
output($AFT_OUTPUT::elem{$1}."\n", image =>$2);
return 1;
};
return 0;
}
# Handle ------
#
sub handle_ruler {
my $fname = shift; my $lcnt = shift; local($_) = @_;
/^\-{4,}/ and do {
output($AFT_OUTPUT::elem{'HorizontalLine'}."\n");
return 1;
};
return 0;
}
# Handle *, **, ***, ****, **** etc (sections) and
# ^*, ^**, ^***, ^**** (sections referencing TOC)
#
sub handle_sections {
my $fname = shift; my $lcnt = shift; local($_) = @_;
/^(\^*\*{1,7})\s*(.+?)\s*$/ and do {
my($sname) = $2;
if ($mode->{in_sect} eq 0) {
output($AFT_OUTPUT::elem{'BeginSectLevel1'}."\n");
$mode->{in_sect} = 1;
}
enter_section(length $1);
$section_number->incr(length $1);
my $number = $section_number->dotted();
my $full_sname = $sname;
$full_sname = "$number. $sname" if $autonumber;
print (STDERR "]\n[$full_sname ") if $verbose;
# print section name
#
output($AFT_OUTPUT::elem{$1}."\n", section => $sname,
text => filter($full_sname), number => $number);
# Save the section for the TOC file.
#
if (length($1) < 5) {
my($level) = $1;
$level =~ tr/*^/\t/d;
print ($tocout "$level"."* {-$full_sname\@$sname-}\n") if $tocout;
}
return 1;
};
return 0;
}
# List Mode
#
sub handle_lists {
my $fname = shift; my $lcnt = shift; local($_) = @_;
# Only do this if not in verbatim/quote mode and we parse one of the
# following:
# <tab>*
# <tab>[text]
# <tab>number.
# <tab>number)
# <tab>#)
# <tab>#.
#
(!$mode->{in_verb} and !$mode->{in_quote} and
(/^(\t{1,})(\*|\[.+\]|\#[.\)]|\d+[.\)])(.*)$/)) and do {
my $rest_of_line = $3;
my $list = '';
my ($le, $name);
my $new_level = length($1);
my $cur_list_level = scalar @list_stack;
my $current_list = '';
if ($cur_list_level gt 0) {
$current_list = $list_stack[$#list_stack];
}
if ($2 =~ /^\*/) {
$list = 'Bullet';
$le = prepare_output($AFT_OUTPUT::elem{'BulletListElement'});
} elsif ($2 =~ /^\[(.+)\]/) {
$name = $1,
$list = 'Named',
$le = prepare_output($AFT_OUTPUT::elem{'NamedListElement'},
name => filter($name));
} else {
$list = 'Numbered';
$le = prepare_output($AFT_OUTPUT::elem{'NumberedListElement'});
}
# Are we nesting yet?
#
while ($cur_list_level < $new_level) {
# Increase nest level
#
push(@list_stack,$list);
end_list_element();
output($AFT_OUTPUT::elem{'Start'.$list.'List'}."\n");
$cur_list_level++;
$current_list = $list;
}
while ($cur_list_level > $new_level) {
# Retreat to a previous level
#
end_list_element();
$current_list = pop(@list_stack);
output($AFT_OUTPUT::elem{'End'.$current_list.'List'}."\n");
$cur_list_level--;
$current_list = pop(@list_stack);
push(@list_stack, $current_list);
}
if ($list ne $current_list) {
# Changing horses... A new list type.
#
end_list_element();
$current_list = pop(@list_stack);
output($AFT_OUTPUT::elem{'End'.$current_list.'List'}."\n");
push(@list_stack,$list);
$current_list = $list;
output($AFT_OUTPUT::elem{'Start'.$list.'List'}."\n");
}
end_list_element();
$mode->{in_list_el} = 1;
output($le); # output element line
output(filter($rest_of_line));
return 1;
};
# Print a continuation of list element if in list mode and tabbed...
#
if (scalar(@list_stack) and /^\t\s*(.*)$/) {
output(' '.filter($1));
return 1;
}
end_list_element();
return 0;
}
# Terminate list element.
#
sub end_list_element {
if ($mode->{in_list_el}) {
output($AFT_OUTPUT::elem{'End'.$list_stack[$#list_stack].
'ListElement'}."\n");
$mode->{in_list_el} = 0;
}
}
# Handle centered text.
#
sub handle_centered_text {
my $fname = shift; my $lcnt = shift; local($_) = @_;
(!scalar(@list_stack) and !$mode->{in_verb} and
!$mode->{in_quote} and /^\t{2,}(.*)$/) and do {
reset_states();
output($AFT_OUTPUT::elem{'Center'}."\n", center => filter($1));
return 1;
};
return 0;
}
# Handle quoted text.
#
sub handle_quoted_text {
my $fname = shift; my $lcnt = shift; local($_) = @_;
(!$mode->{in_verb} and /^\t\#\s*(.*)$/) and do {
if (!$mode->{in_quote}) { # if we aren't in quote mode yet...
reset_states();
output($AFT_OUTPUT::elem{'StartQuote'}."\n");
$mode->{in_quote} = 1;
}
output(filter($1)."\n");
return 1;
};
return 0;
}
# Handle tables
#
sub handle_table {
my $fname = shift; my $lcnt = shift; local($_) = @_;
if ($AFT_OUTPUT::pragma_ctl{tableparser} eq 'new') {
return handle_new_table_parser($fname, $lcnt, $_);
}
# If not in verbatim or quote mode, try table...
#
(!$mode->{in_verb} and !$mode->{in_quote} and /^\t\!(.*)$/) and do {
my $ecnt; # Number of elements.
my @elements;
my $ftype;
# First thing is first... Are we in the table yet?
#
!$mode->{in_table} and do {
reset_states(); # start clean
$mode->{in_table} = 1;
# Don't really start the table yet. We need to know how many
# columns we will be dealing with. Expect table headers next
# time through.
#
$mode->{need_table_headers} = 1;
$1 =~ /([^\!]*)/;
# The first thing we got was a caption. Save it for later.
#
$table_caption = filter($1);
return 1;
};
# Separator line !--------!
#
if ($1 =~ /[\-]+!$/) {
return 1;
}
# We should be in Table mode now. The first thing we should do
# is split up columns into individual elements. Ignore bogus
# trailing column. If we got less than 2 elements, this ain't no
# table!
#
if (($ecnt = (@elements = split ("!", $1, 100)) - 1) < 2) {
print(STDERR
"\n$fname($lcnt): Weirdness in a table... not enough columns.\n");
return 1;
}
# Okay, if this is the 2nd time through then we are looking for
# table headers...
#
if ($mode->{need_table_headers}) {
# We got the column count ($ecnt) above, so we assume that
# it will stay consistent. If not, that's someone else's
# problem.
#
output($AFT_OUTPUT::elem{'StartTable'}."\n", columns =>$ecnt,
caption => $table_caption);
output($AFT_OUTPUT::elem{'TableCaption'}."\n",
caption => $table_caption);
$mode->{need_table_headers} = 0; # don't need them anymore
$ftype = $AFT_OUTPUT::elem{'TableHeader'}; # short hand
} else {
$ftype = $AFT_OUTPUT::elem{'TableElement'}; # short hand
}
output($AFT_OUTPUT::elem{'TableRowStart'});
# Now loop through each column element and spit it out.
#
foreach my $item (@elements) {
output($ftype, stuff => filter($item)) if $item;
}
# End of Table Row
#
output($AFT_OUTPUT::elem{'TableRowEnd'}."\n");
return 1;
};
return 0;
}
# Handle ''New Style'' tables
#
my @row_acc;
sub handle_new_table_parser {
my $fname = shift; my $lcnt = shift; local($_) = @_;
# If not in verbatim or quote mode, try table...
#
(!$mode->{in_verb} and !$mode->{in_quote} and /^\t\!(.*)$/) and do {
my $ecnt = 0; # Number of elements.
my @elements;
my $etype;
my $tline = $1;
# Protect \!
$tline =~ s/\\!/%bang%/g;
# First thing is first... Are we in the table yet?
#
!$mode->{in_table} and do {
reset_states(); # start clean
$mode->{in_table} = 1;
# Don't really start the table yet. We need to know how many
# columns we will be dealing with. Expect table headers next
# time through.
#
$mode->{need_table_headers} = 1;
if ($tline =~ /([^\!]*)/) { $tline = $1; }
# if ($1 =~ /([^\!]*)/) { $tline = $1; }
# The first thing we got was a caption. Save it for later.
#
$table_caption = filter($tline);
$mode->{eatOne} = 1;
return 1;
};
if ($tline =~ /[\-]+!$/ and $mode->{eatOne}) {
$mode->{eatOne} = 0;
return 1;
}
# Separator line !--------! means kick out previously accumulated row.
#
if ($tline =~ /[\-]+!$/) {
output($AFT_OUTPUT::elem{'TableRowStart'});
if ($mode->{need_table_headers}) {
$etype = $AFT_OUTPUT::elem{'TableHeader'}; # short hand
} else {
$etype = $AFT_OUTPUT::elem{'TableElement'}; # short hand
}
if (@row_acc) {
$mode->{need_table_headers} = 0 if $mode->{need_table_headers};
while (my $item = pop(@row_acc)) {
output($etype, stuff => filter($item)) if $item;
if (scalar(@row_acc) > 2) {
output($AFT_OUTPUT::elem{'TableElementSep'});
}
}
output($AFT_OUTPUT::elem{'TableRowEnd'}."\n");
}
return 1;
}
# otherwise...
# We should be in Table mode now. The first thing we should do
# is split up columns into individual elements. Ignore bogus
# trailing column. If we got less than 2 elements, this ain't no
# table!
#
if (($ecnt = (@elements = split ("!", $tline, 100)) - 1) < 2) {
print(STDERR
"\n$fname($lcnt): Weirdness in a table... not enough columns.\n");
return 1;
}
# Okay, if this is the 2nd time through then we are looking for
# table headers...
#
if ($mode->{need_table_headers}) {
# We got the column count ($ecnt) above, so we assume that
# it will stay consistent. If not, that's someone else's
# problem.
#
output($AFT_OUTPUT::elem{'StartTable'}."\n", columns =>$ecnt,
caption => $table_caption);
output($AFT_OUTPUT::elem{'TableCaption'}."\n",
caption => $table_caption);
}
# If just accumulating...
# Now loop through each column element and save it.
#
my $col = @elements;
foreach my $item (@elements) {
$row_acc[$col] .= $item if $item;
$col--;
}
return 1;
};
return 0;
}
# Handle verbatim issues.
#
sub handle_blocked_verbatim {
my $fname = shift; my $lcnt = shift; local($_) = @_;
if ($mode->{in_blocked_verb} or $mode->{in_filtered_verb}) {
handle_verbatim($fname,$lcnt,$_);
return 1;
}
return 0; # drop thru
}
sub handle_verbatim {
my $fname = shift; my $lcnt = shift; local($_) = @_;
# Check to see if we should get out of blocked/filtered verbatim mode.
#
/^\^>>/ and do {
# Get out of blocked and filtered verbatim mode
reset_states();
return 1;
};
# Verbatim Text (and yes, even Quoted Text continuations)
#
(/(^\t|^\^\<\<\w*)/ or
$mode->{in_blocked_verb} or $mode->{in_filtered_verb}) and do {
# First, are we starting fresh?
#
(!$mode->{in_verb} and !$mode->{in_quote}) and do {
reset_states(); # start clean
$mode->{in_verb} = 1;
# We are just entering the blocked verbatim mode,
# so just remember this and don't print this line.
#
if ($1 =~ /\^\<\</) {
if ($POSTMATCH =~ /[Ff]/) {
$mode->{in_filtered_verb} = 1;
output($AFT_OUTPUT::elem{'StartFilteredVerbatim'}."\n");
} else {
$mode->{in_blocked_verb} = 1;
output($AFT_OUTPUT::elem{'StartBlockedVerbatim'}."\n");
}
return 1;
} # else
output($AFT_OUTPUT::elem{'StartVerbatim'}."\n");
};
# In quote mode? Just kick out filtered text.
#
output(filter($POSTMATCH)."\n"), return 1 if $mode->{in_quote};
# We must be in a verbatim mode...
#
# Kill the first tab
#
s/^\t//g if (!($mode->{in_blocked_verb} or $mode->{in_filtered_verb}));
# Now change all tabs to 8 spaces.
#
s/\t/ /g if (!$mode->{in_blocked_verb});
# Can we really filter FilterVerbatim?
#
if ($mode->{in_filtered_verb} and
($AFT_OUTPUT::elem{'FullFilterFilteredVerbatim?'} =~ /[Yy]/)) {
output(filter($_)."\n");
} else {
if ($AFT_OUTPUT::elem{'PreFilterVerbatim?'} =~ /[Yy]/) {
output(AFT_OUTPUT::prefilter($_)."\n");
} else {
output($_."\n"); # output 'as is'
}
}
return 1;
};
return 0;