aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@gmail.com>2019-01-06 22:36:38 -0300
committerMatias Linares <matiaslina@gmail.com>2019-01-06 22:36:38 -0300
commit43d04f7bcfaef32462651633df52736802b9665e (patch)
tree72e4f4478f3e53158a1e846b6b7f76974e872c56
parent5244d952da00984d226c334b4c1a7d5090b7cda5 (diff)
downloadperl6-pygments-43d04f7bcfaef32462651633df52736802b9665e.tar.gz
Don't segfault generating docs
-rw-r--r--lib/Pygments.pm646
1 files changed, 29 insertions, 17 deletions
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<perl.py>.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<terminal>,
+ :linenos(True)
+ ).say;
- say $formatted-code;
+Also it can be used with C<Pod::To::HTML>:
+
+ use Pygments;
+
+ # Set the pod code callback to use pygments before *use* it
+ my %*POD2HTML-CALLBACKS;
+ %*POD2HTML-CALLBACKS<code> = 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 <matiaslina@gmail.com>
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