-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmd_fix
executable file
·51 lines (45 loc) · 1.27 KB
/
md_fix
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
#!/usr/bin/perl
use warnings;
use strict;
use File::Basename;
# converts ---- and === to ## and # (not tables, headings)
# Changes blockquotes to code blocks
my $path = dirname( $0 ) . "/docs/simdocs/%s_doc.md";
my $n = @ARGV;
while( @ARGV ) {
my( $ll, $inq );
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" );
while( <$f> ) {
chomp;
if( $inq ) {
if( $_ !~ s/^>\s*// ) {
$_ = "~~~";
$inq = 0;
}
} elsif( $_ =~ s/^>\s*// ) {
print $of ( $ll ) if( $ll );
$ll = "~~~\n";
$inq = 1;
}
if( /^[=\-]{3,}$/ ) {
if( $ll ) {
print $of ( (/=/? '#' : '##') . " $ll" );
undef $ll; undef $inq;
} else {
print $of ( "$_\n" );
}
next;
}
print $of ( $ll ) if( $ll );
$ll = "$_\n";
}
print $of ( $ll ) if( $ll );
close $of;
}