From 43d04f7bcfaef32462651633df52736802b9665e Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 6 Jan 2019 22:36:38 -0300 Subject: Don't segfault generating docs --- lib/Pygments.pm6 | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'lib/Pygments.pm6') diff --git a/lib/Pygments.pm6 b/lib/Pygments.pm6 index 0244631..a303f18 100644 --- a/lib/Pygments.pm6 +++ b/lib/Pygments.pm6 @@ -4,8 +4,9 @@ unit class Pygments:ver<0.0.1>; use Inline::Python; my Inline::Python $py; +method !ip { + return $py if $py.defined; -INIT { $py .= new; $py.run: q:to/SETUP/; from pygments import highlight @@ -15,10 +16,12 @@ INIT { SETUP $py.run: %?RESOURCES.slurp; + + $py } method call($name, |c) { - $py.call('__main__', $name, |c) + self!ip.call('__main__', $name, |c) } method highlight(Str $code, $lexer = Any, :$formatter = 'html', *%options) is export { @@ -29,7 +32,7 @@ method highlight(Str $code, $lexer = Any, :$formatter = 'html', *%options) is ex }; my $f = $.formatter($formatter, |%options); - $py.call('pygments', 'highlight', $code, $l, $f) + self!ip.call('pygments', 'highlight', $code, $l, $f) } method formatter($name, *%options) is export { @@ -37,11 +40,11 @@ method formatter($name, *%options) is export { } method style(Str $name = 'default') { - $py.call('pygments.styles', 'get_style_by_name', $name) + self!ip.call('pygments.styles', 'get_style_by_name', $name) } method styles { - $py.run('list(STYLE_MAP.keys())', :eval).map: *.decode + self!ip.run('list(STYLE_MAP.keys())', :eval).map: *.decode } =begin pod @@ -49,9 +52,10 @@ method styles { =head1 NAME Pygments - Wrapper to python pygments library. - =head1 SYNOPSIS +Printing some code with a terminal formatter. + use Pygments; my $code = q:to/ENDCODE/; @@ -68,18 +72,27 @@ Pygments - Wrapper to python pygments library. # OUTPUT: 「I love Rust」 love => 「love」 lang => 「Rust」 ENDCODE - # Get a theme. - my $css = Pygments.css('manni'); - # Format a full html with line numbers and theme `manni` - my $formatted-code = Pygments.highlight( - $code, "perl6", - :linenos(True), - :style($css), - :full(True) - ); + Pygments.highlight( + $code, "perl6", :formatter, + :linenos(True) + ).say; - say $formatted-code; +Also it can be used with C: + + use Pygments; + + # Set the pod code callback to use pygments before *use* it + my %*POD2HTML-CALLBACKS; + %*POD2HTML-CALLBACKS = sub (:$node, :&default) { + Pygments.highlight($node.contents.join('\n'), "perl6", + :style(Pygments.style('emacs')), + :full) + }; + use Pod::To::HTML; + use Pod::Load; + + pod2html(load('some.pod6'.IO)).say =head1 DESCRIPTION @@ -121,5 +134,4 @@ Matias Linares Copyright 2019 Matias Linares This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0. - =end pod -- cgit v1.2.3-70-g09d2