Devel/IPerl/NBConvert.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 99 CC 5 SCAR 24.8
CC
5
Cov
0%
to_pod
30.0
run
2.0
BEGIN
2.0
CRAP 12.0
1 package Devel::IPerl::NBConvert;
2 $Devel::IPerl::NBConvert::VERSION = '0.012';
3 use strict;
4 use warnings;
5  
6 use Getopt::Long;
7 use Path::Class;
8 use JSON::MaybeXS;
9 use Markdown::Pod;
10 use HTML::FromANSI;
11 use Moo;
12  
13 has ansi_css => ( is => 'ro',
14 default => sub {
15 'font-family: fixedsys, lucida console, terminal, vga, monospace; line-height: 1; letter-spacing: 0; font-size: 12pt'
16 });
17 has notebook_file => ( is => 'rw' );
18 has output_file => ( is => 'rw' );
19 has to_format => ( is => 'rw', default => sub { 'pod' } );
20 has output_to_stdout => ( is => 'rw' );
21  
22 sub run {
23 my ($self) = @_;
24 my $file = shift @ARGV;
25 $self->notebook_file( file($file) );
26  
27 my $data = decode_json( $self->notebook_file->slurp );
28  
29 my $output = $self->to_pod( $data );
30  
31 print $output;
32 }
33  
34 sub to_pod {
35 my ($self, $nb) = @_;
36 my $md2pod = Markdown::Pod->new;
37 my $ansi_css = $self->ansi_css;
38  
39 my $pod_string;
40  
41 for my $cell ( @{ $nb->{cells} } ) {
42 if( $cell->{cell_type} eq 'markdown' ) {
43 my $md = join '', @{ $cell->{source} };
44 $pod_string .= $md2pod->markdown_to_pod( markdown => $md );
45 } elsif( $cell->{cell_type} eq 'code' ) {
46 my $code = join '', @{ $cell->{source} };
47  
48 # move it over for code
49 $pod_string .= join '', map { s/^/ /r } @{ $cell->{source} };
50 $pod_string .= "\n\n";
51  
52 my @outputs = @{ $cell->{outputs} };
53 for my $output (@outputs) {
54 my $data = $output->{data};
55 if( exists $data->{"text/html"} ) {
56 # HTML preferred
57 $pod_string .= "=begin html\n\n";
58 my $html = join '', @{ $data->{"text/html"} };
59 $html =~ s/\n//g;
60 $pod_string .= "<p>$html</p>";
61 $pod_string .= "\n\n=end html\n\n";
62 } elsif( exists $data->{"text/plain"} ) {
63 $pod_string .= "=begin html\n\n";
64 local $HTML::FromANSI::Options{fill_cols} = 1; # fill all 80 cols
65 local $HTML::FromANSI::Options{font_face} = '';
66 local $HTML::FromANSI::Options{style} = '';
67 my $html = ansi2html( (join '', @{ $data->{"text/plain"} }) );
68 $html =~ s|^<tt>|<tt><span style='$ansi_css'>|;
69 $html =~ s|</tt>$|</span></tt>|;
70 $pod_string .= $html;
71 $pod_string .= "\n\n=end html\n\n";
72 }
73 }
74 }
75  
76 $pod_string .= "\n\n";
77 }
78  
79 return $pod_string;
80 }
81  
82 1;
83  
84 __END__