mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-25 13:29:29 +01:00
misc/git-md-toc: fix indentation; skip README.md unless explicitly specified
This commit is contained in:
parent
a6c532d67e
commit
2afb5dc3f7
196
misc/git-md-toc
vendored
196
misc/git-md-toc
vendored
@ -114,18 +114,18 @@ my $min_depth = 1;
|
|||||||
my $use_filename = 0;
|
my $use_filename = 0;
|
||||||
|
|
||||||
exit 1 unless GetOptions(
|
exit 1 unless GetOptions(
|
||||||
"h|help" => sub {
|
"h|help" => sub {
|
||||||
pod2usage({ -verbose => 2, -noperldoc => 1 });
|
pod2usage({ -verbose => 2, -noperldoc => 1 });
|
||||||
},
|
},
|
||||||
|
|
||||||
"t|title:s" => sub {
|
"t|title:s" => sub {
|
||||||
$toc_title = $_[1] || $toc_default_title;
|
$toc_title = $_[1] || $toc_default_title;
|
||||||
},
|
},
|
||||||
"l|level=i" => sub {
|
"l|level=i" => sub {
|
||||||
( $toc_level = $_[1] ) =~ /^[1-6]$/
|
( $toc_level = $_[1] ) =~ /^[1-6]$/
|
||||||
or die "Integer expected in range [1..6]\n";
|
or die "Integer expected in range [1..6]\n";
|
||||||
},
|
},
|
||||||
"u|update" => \$update,
|
"u|update" => \$update,
|
||||||
"x|maxdepth=i" => \$max_depth,
|
"x|maxdepth=i" => \$max_depth,
|
||||||
"m|mindepth=i" => \$min_depth,
|
"m|mindepth=i" => \$min_depth,
|
||||||
"f|filename" => \$use_filename,
|
"f|filename" => \$use_filename,
|
||||||
@ -143,82 +143,86 @@ my $md_toc_end = "<!-- md-toc-end -->";
|
|||||||
my $filename;
|
my $filename;
|
||||||
|
|
||||||
foreach ( @ARGV ) {
|
foreach ( @ARGV ) {
|
||||||
my $orig_text;
|
my $orig_text;
|
||||||
{
|
{
|
||||||
local $/;
|
local $/;
|
||||||
open F, $_ or die "Unable to open for reading: $_: $!\n";
|
open F, $_ or die "Unable to open for reading: $_: $!\n";
|
||||||
$orig_text = <F>;
|
$orig_text = <F>;
|
||||||
close F;
|
close F;
|
||||||
};
|
};
|
||||||
|
|
||||||
$filename = $_;
|
$filename = $_;
|
||||||
|
|
||||||
my $clean_text = $orig_text;
|
# skip README.md unless it is the only file explicitly specified
|
||||||
|
next if @ARGV > 1 and $filename eq 'README.md';
|
||||||
|
|
||||||
# skip code fencing
|
my $clean_text = $orig_text;
|
||||||
$clean_text =~ s{
|
|
||||||
(?:\A|\n) [ \t]* ``` .*? \n [ \t]* ```
|
|
||||||
}{}msgx;
|
|
||||||
|
|
||||||
# skip non-empty TOC blocks
|
# skip code fencing
|
||||||
$clean_text =~ s{
|
$clean_text =~ s{
|
||||||
(?:\A|\n)
|
(?:\A|\n) [ \t]* ``` .*? \n [ \t]* ```
|
||||||
<!-- [ \t]+ md-toc-begin [ \t]+ --> [ \t\r]* \n
|
}{}msgx;
|
||||||
[\s\S]*? \n
|
|
||||||
<!-- [ \t]+ md-toc-end [ \t]+ --> [ \t\r]*
|
|
||||||
(?=\n)
|
|
||||||
}{}msgx;
|
|
||||||
|
|
||||||
my %count = ();
|
# skip non-empty TOC blocks
|
||||||
|
$clean_text =~ s{
|
||||||
|
(?:\A|\n)
|
||||||
|
<!-- [ \t]+ md-toc-begin [ \t]+ --> [ \t\r]* \n
|
||||||
|
[\s\S]*? \n
|
||||||
|
<!-- [ \t]+ md-toc-end [ \t]+ --> [ \t\r]*
|
||||||
|
(?=\n)
|
||||||
|
}{}msgx;
|
||||||
|
|
||||||
my @toc = ();
|
my %count = ();
|
||||||
|
|
||||||
push @toc, $md_toc_begin;
|
my @toc = ();
|
||||||
push @toc, "#" x $toc_level . " $toc_title" if $toc_title;
|
|
||||||
|
|
||||||
while ( $clean_text =~ m{
|
push @toc, $md_toc_begin;
|
||||||
(?:\A|\n)
|
push @toc, "#" x $toc_level . " $toc_title" if $toc_title;
|
||||||
[ ]{0,3}
|
|
||||||
(?:
|
|
||||||
# atx-style headers H1-H6
|
|
||||||
( [#]{1,6} ) [ \t]+ ( .+? ) (?: [ \t]+ [#]* )?
|
|
||||||
|
|
|
||||||
# setext-style headers H1
|
|
||||||
( \S[^\r\n]*? ) [ \t\r]* \n [ \t]* ( [=] )+
|
|
||||||
|
|
|
||||||
# setext-style header H2
|
|
||||||
( (?![-]+)|[^\r\n]+? ) [ \t\r]* \n [ \t]* ( [-] )+
|
|
||||||
)
|
|
||||||
[ \t\r]*
|
|
||||||
(?=\n)
|
|
||||||
}mgx ) {
|
|
||||||
my $depth;
|
|
||||||
my $indent;
|
|
||||||
my $title;
|
|
||||||
|
|
||||||
if ( $1 && $2 ) {
|
while ( $clean_text =~ m{
|
||||||
$depth = length($1) - 1;
|
(?:\A|\n)
|
||||||
$title = $2;
|
[ ]{0,3}
|
||||||
} elsif ( $4 && $3 ) {
|
(?:
|
||||||
$depth = 0;
|
# atx-style headers H1-H6
|
||||||
$indent = "";
|
( [#]{1,6} ) [ \t]+ ( .+? ) (?: [ \t]+ [#]* )?
|
||||||
$title = $3;
|
|
|
||||||
} elsif ( $6 && $5 ) {
|
# setext-style headers H1
|
||||||
$depth = 1;
|
( \S[^\r\n]*? ) [ \t\r]* \n [ \t]* ( [=] )+
|
||||||
$title = $5;
|
|
|
||||||
}
|
# setext-style header H2
|
||||||
|
( (?![-]+)|[^\r\n]+? ) [ \t\r]* \n [ \t]* ( [-] )+
|
||||||
|
)
|
||||||
|
[ \t\r]*
|
||||||
|
(?=\n)
|
||||||
|
}mgx ) {
|
||||||
|
|
||||||
next unless $title;
|
my $depth;
|
||||||
|
my $indent;
|
||||||
|
my $title;
|
||||||
|
|
||||||
$indent = " " x $depth;
|
if ( $1 && $2 ) {
|
||||||
|
$depth = length($1) - 1;
|
||||||
|
$title = $2;
|
||||||
|
} elsif ( $4 && $3 ) {
|
||||||
|
$depth = 0;
|
||||||
|
$indent = "";
|
||||||
|
$title = $3;
|
||||||
|
} elsif ( $6 && $5 ) {
|
||||||
|
$depth = 1;
|
||||||
|
$title = $5;
|
||||||
|
}
|
||||||
|
|
||||||
my $anchor = lc $title;
|
next unless $title;
|
||||||
$anchor =~ s/\s/-/g;
|
|
||||||
$anchor =~ s/[^\w-]//g;
|
|
||||||
|
|
||||||
$count{$anchor}++;
|
$indent = " " x $depth;
|
||||||
|
|
||||||
$anchor .= ( 1 - $count{$anchor} or "" );
|
my $anchor = lc $title;
|
||||||
|
$anchor =~ s/\s/-/g;
|
||||||
|
$anchor =~ s/[^\w-]//g;
|
||||||
|
|
||||||
|
$count{$anchor}++;
|
||||||
|
|
||||||
|
$anchor .= ( 1 - $count{$anchor} or "" );
|
||||||
|
|
||||||
if ($depth >= $min_depth - 1 and $depth <= $max_depth - 1) {
|
if ($depth >= $min_depth - 1 and $depth <= $max_depth - 1) {
|
||||||
if ($use_filename) {
|
if ($use_filename) {
|
||||||
@ -227,36 +231,36 @@ foreach ( @ARGV ) {
|
|||||||
push (@toc, "$indent* [$title](#$anchor)");
|
push (@toc, "$indent* [$title](#$anchor)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
push @toc, $md_toc_end;
|
push @toc, $md_toc_end;
|
||||||
|
|
||||||
my $toc = join "\n", @toc;
|
my $toc = join "\n", @toc;
|
||||||
|
|
||||||
unless ( $update ) {
|
unless ( $update ) {
|
||||||
print "$toc\n";
|
print "$toc\n";
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
$orig_text =~ s{
|
$orig_text =~ s{
|
||||||
(?: (\A) | [\r\n]+ )
|
(?: (\A) | [\r\n]+ )
|
||||||
(?:
|
(?:
|
||||||
<!-- [ \t]+ md-toc [ \t]+ --> [ \t\r]*
|
<!-- [ \t]+ md-toc [ \t]+ --> [ \t\r]*
|
||||||
|
|
|
|
||||||
<!-- [ \t]+ md-toc-begin [ \t]+ --> [ \t\r]* \n
|
<!-- [ \t]+ md-toc-begin [ \t]+ --> [ \t\r]* \n
|
||||||
(?: [\s\S]*? \n )*?
|
(?: [\s\S]*? \n )*?
|
||||||
<!-- [ \t]+ md-toc-end [ \t]+ --> [ \t\r]*
|
<!-- [ \t]+ md-toc-end [ \t]+ --> [ \t\r]*
|
||||||
)
|
)
|
||||||
(?: (\Z) | [\r\n]+ )
|
(?: (\Z) | [\r\n]+ )
|
||||||
}{
|
}{
|
||||||
( $1 // "\n\n" ) . $toc . "\n\n";
|
( $1 // "\n\n" ) . $toc . "\n\n";
|
||||||
}emgx;
|
}emgx;
|
||||||
|
|
||||||
warn "Updating $_\n";
|
warn "Updating $_\n";
|
||||||
|
|
||||||
open F, ">$_" or die "Unable to open for writing: $_: $!\n";
|
open F, ">$_" or die "Unable to open for writing: $_: $!\n";
|
||||||
print F $orig_text;
|
print F $orig_text;
|
||||||
close F;
|
close F;
|
||||||
}
|
}
|
||||||
|
|
||||||
# =========================================================================
|
# =========================================================================
|
||||||
|
Loading…
Reference in New Issue
Block a user