Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
avuserow committed Feb 16, 2015
1 parent 74326f7 commit b060886
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions htmlify.p6
Expand Up @@ -13,6 +13,7 @@ use Perl6::TypeGraph;
use Perl6::TypeGraph::Viz;
use Perl6::Documentable::Registry;
use Pod::Convenience;
use Inline::Python;

my $*DEBUG = False;

Expand Down Expand Up @@ -708,19 +709,26 @@ sub pygmentize-code-blocks {
say "pygmentize not found; code blocks will not be highlighted";
return;
}

my $py = Inline::Python.new();
$py.run(q{
import pygments.lexers
import pygments.formatters
p6lexer = pygments.lexers.get_lexer_by_name("perl6")
htmlformatter = pygments.formatters.get_formatter_by_name("html")
def p6format(code):
return pygments.highlight(code, p6lexer, htmlformatter)
});

%*POD2HTML-CALLBACKS = code => sub (:$node, :&default) {
for @($node.contents) -> $c {
if $c !~~ Str {
# some nested formatting code => we can't hilight this
return default($node);
}
}
my $basename = join '-', %*ENV<USER> // 'u', (^100_000).pick, 'pod_to_pyg.pod';
my $tmp_fname = "$*TMPDIR/$basename";
spurt $tmp_fname, $node.contents.join;
LEAVE try unlink $tmp_fname;
my $command = "pygmentize -l perl6 -f html < $tmp_fname";
return qqx{$command};
return $py.call('__main__', 'p6format', $node.contents.join);
}
}

Expand Down

0 comments on commit b060886

Please sign in to comment.