mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-22 11:59:43 +01:00
misc/git-md-toc: make some useful improvements: min depth, max depth, use filename in anchor
This commit is contained in:
parent
fd14d6d789
commit
3fda530a9a
18
misc/git-md-toc
vendored
18
misc/git-md-toc
vendored
@ -109,6 +109,9 @@ my $toc_default_title = "Table of Content";
|
|||||||
my $toc_title;
|
my $toc_title;
|
||||||
my $toc_level = 1;
|
my $toc_level = 1;
|
||||||
my $update;
|
my $update;
|
||||||
|
my $max_depth = 6;
|
||||||
|
my $min_depth = 1;
|
||||||
|
my $use_filename = 0;
|
||||||
|
|
||||||
exit 1 unless GetOptions(
|
exit 1 unless GetOptions(
|
||||||
"h|help" => sub {
|
"h|help" => sub {
|
||||||
@ -123,6 +126,9 @@ exit 1 unless GetOptions(
|
|||||||
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,
|
||||||
|
"m|mindepth=i" => \$min_depth,
|
||||||
|
"f|filename" => \$use_filename,
|
||||||
);
|
);
|
||||||
|
|
||||||
# Hmm... No arguments. Let's take README.md or STDIN
|
# Hmm... No arguments. Let's take README.md or STDIN
|
||||||
@ -134,6 +140,8 @@ my $md_toc = "<!-- md-toc -->";
|
|||||||
my $md_toc_begin = "<!-- md-toc-begin -->";
|
my $md_toc_begin = "<!-- md-toc-begin -->";
|
||||||
my $md_toc_end = "<!-- md-toc-end -->";
|
my $md_toc_end = "<!-- md-toc-end -->";
|
||||||
|
|
||||||
|
my $filename;
|
||||||
|
|
||||||
foreach ( @ARGV ) {
|
foreach ( @ARGV ) {
|
||||||
my $orig_text;
|
my $orig_text;
|
||||||
{
|
{
|
||||||
@ -143,6 +151,8 @@ foreach ( @ARGV ) {
|
|||||||
close F;
|
close F;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$filename = $_;
|
||||||
|
|
||||||
my $clean_text = $orig_text;
|
my $clean_text = $orig_text;
|
||||||
|
|
||||||
# skip code fencing
|
# skip code fencing
|
||||||
@ -210,7 +220,13 @@ foreach ( @ARGV ) {
|
|||||||
|
|
||||||
$anchor .= ( 1 - $count{$anchor} or "" );
|
$anchor .= ( 1 - $count{$anchor} or "" );
|
||||||
|
|
||||||
push @toc, "$indent* [$title](#$anchor)";
|
if ($depth >= $min_depth - 1 and $depth <= $max_depth - 1) {
|
||||||
|
if ($use_filename) {
|
||||||
|
push (@toc, "$indent* [$title]($filename#$anchor)");
|
||||||
|
} else {
|
||||||
|
push (@toc, "$indent* [$title](#$anchor)");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
push @toc, $md_toc_end;
|
push @toc, $md_toc_end;
|
||||||
|
Loading…
Reference in New Issue
Block a user