-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmd_tbl_fix
More file actions
executable file
·74 lines (63 loc) · 1.78 KB
/
md_tbl_fix
File metadata and controls
executable file
·74 lines (63 loc) · 1.78 KB
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
#!/usr/bin/perl
use warnings;
use strict;
use File::Basename;
# Finds tables that doc conversion leaves as --- line, header instead
# of header, --- line. Reverses them. Tries to insert | delims in each
# column based on the spaces between -- -- line. Sometimes gets it wrong.
# Deletes the table footer.
my $path = dirname( $0 ) . "/docs/simdocs/%s_doc.md";
my $n = @ARGV;
while( @ARGV ) {
my( $inb , @b);
my $fn = shift;
$fn = sprintf( $path, basename( $fn, ( qw(.md ) ) ) )
unless( $fn =~ m,/, );
open( my $f, '<', $fn ) or die( "$fn:$!\n" ) ;
printf STDERR ( "%s\n", $fn ) if( $n > 0 );
die( "$fn.bak exists\n" ) if( -f "$fn.bak" );
rename( $fn, "$fn.bak" ) || die( "rename: $!\n" );
open( my $of,">", $fn ) or die( "$fn.bak:$!\n" );
$inb = 1; # zero if partially done, will skip to marker
while( <$f> ) {
chomp;
if( !$inb && /^__STARTHERE__$/ ) {
$inb = 1;
next;
}
unless( $inb ) {
print $of ( "$_\n" );
next;
}
if( /^ *((----+)(?: (----+))+)/ ) {
if( @b ) {
pb($of, \@b );
@b = ();
} else {
push @b, $1;
}
} elsif( @b ) {
s/^ +//g;
push @b, $_;
} else {
print $of ( "$_\n" );
}
}
pb( $of, \@b ) if( @b );
close $of;
}
exit;
sub pb {
my( $of, $b ) = @_;
my( @col ) = split( / /, $b->[0] );
$_ = length foreach @col;
my $t = $b->[0]; $b->[0] = $b->[1]; $b->[1] = $t;
foreach my $l (@$b) {
my $r = '|';
foreach my $n (@col) {
$r .= substr( $l, 0, $n, '' ) . '|';
}
substr( $r, -1, 0, $l ) if( length $l );;
print $of ("$r\n" );
}
}