-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.pl
executable file
·54 lines (43 loc) · 1.12 KB
/
filter.pl
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
#!/usr/bin/env perl
use v5.14;
use Pandoc::Filter;
use Pandoc::Elements;
use Pandoc;
sub section_number {
state @section;
return Str '' if $_->match('.unnumbered');
my $h = shift;
my $level = $h->level;
$section[-1]++ if @section == $level;
pop @section while @section > $level;
push @section, 1 while @section < $level;
return Str join( '.', @section, '' ), Space;
}
my $headers = sub {
# TODO: set id by section number, e.g. '2.1'
Header $_->level + 2, $_->attr, [ section_number($_), @{ $_->content } ];
};
pandoc_filter Link => sub {
my ( $link, $format ) = @_;
my $url = $link->url;
$url =~ s{\.md$}{.$format};
$link->url($url);
return $link;
},
Header => $headers,
CodeBlock => sub {
return if $_->class ne 'include';
my @lines = split "\n", $_->content;
# TODO: relative links
return [
map {
-e $_
? do {
my $doc = pandoc->file($_);
$doc->transform( Header => $headers );
@{ $doc->content };
}
: Null
} @lines
];
};