Skip to content

Commit b060886

Browse files
committed
Use Inline::Python to interact with pygments
This is significantly faster when syntax highlighting but requires Inline::Python which is currently difficult to build. On my machine, htmlify with an external pygments takes around 22:20, while using it via Inline::Python took only 6:25.
1 parent 74326f7 commit b060886

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

htmlify.p6

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use Perl6::TypeGraph;
1313
use Perl6::TypeGraph::Viz;
1414
use Perl6::Documentable::Registry;
1515
use Pod::Convenience;
16+
use Inline::Python;
1617

1718
my $*DEBUG = False;
1819

@@ -708,19 +709,26 @@ sub pygmentize-code-blocks {
708709
say "pygmentize not found; code blocks will not be highlighted";
709710
return;
710711
}
712+
713+
my $py = Inline::Python.new();
714+
$py.run(q{
715+
import pygments.lexers
716+
import pygments.formatters
717+
p6lexer = pygments.lexers.get_lexer_by_name("perl6")
718+
htmlformatter = pygments.formatters.get_formatter_by_name("html")
719+
720+
def p6format(code):
721+
return pygments.highlight(code, p6lexer, htmlformatter)
722+
});
723+
711724
%*POD2HTML-CALLBACKS = code => sub (:$node, :&default) {
712725
for @($node.contents) -> $c {
713726
if $c !~~ Str {
714727
# some nested formatting code => we can't hilight this
715728
return default($node);
716729
}
717730
}
718-
my $basename = join '-', %*ENV<USER> // 'u', (^100_000).pick, 'pod_to_pyg.pod';
719-
my $tmp_fname = "$*TMPDIR/$basename";
720-
spurt $tmp_fname, $node.contents.join;
721-
LEAVE try unlink $tmp_fname;
722-
my $command = "pygmentize -l perl6 -f html < $tmp_fname";
723-
return qqx{$command};
731+
return $py.call('__main__', 'p6format', $node.contents.join);
724732
}
725733
}
726734

0 commit comments

Comments
 (0)