forked from ash/raku-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight.raku
executable file
·61 lines (46 loc) · 1.38 KB
/
highlight.raku
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
#!/usr/bin/env raku
my $output-root = 'docs';
scan-dir('_site');
sub scan-dir($root) {
say "Scanning $root";
for dir($root) -> $path {
# next if $path ~~ /^'_' | ^'.'/;
next if $path ~~ /^ '_site/docs/' /;
if $path.d {
scan-dir($path);
}
else {
process-file($path);
}
}
}
sub process-file($path) {
my $output-path = "$output-root/$path";
$output-path ~~ s/ '_site/' //;
# say $output-path;
my $dirname = $output-path.IO.dirname;
$dirname.IO.mkdir unless $dirname.IO.d;
if $path ~~ / 'index.html' / {
if $output-path.IO.f && ($path.IO.modified < $output-path.IO.modified) {
return;
}
# say "Processing $path";
my $html = $path.slurp;
$html ~~ s:g/'<pre><code class="language-raku">' (.*?) '</code></pre>'/{
highlight(~$0)
}/;
$output-path.IO.spurt: $html;
}
elsif $path !~~ / 'index.md' / {
$path.IO.copy: $output-path;
}
}
sub highlight($code is copy) {
$code ~~ s:g/'<'/</;
$code ~~ s:g/'>'/>/;
$code ~~ s:g/'&'/&/;
'/tmp/highlight.raku'.IO.spurt($code);
run '/usr/local/bin/pygmentize', '-f', 'html', '-l', 'raku', '-O', 'style=vs', '-o', '/tmp/highlight.raku.html', '/tmp/highlight.raku';
my $html = '/tmp/highlight.raku.html'.IO.slurp;
return $html;
}