File Coverage

blib/lib/Text/Markup/Rest.pm
Criterion Covered Total %
statement 17 26 65.3
branch 0 4 0.0
condition n/a
subroutine 6 8 75.0
pod n/a
total 23 38 60.5


line stmt bran cond sub pod time code
1             package Text::Markup::Rest;
2              
3 1     1   2775 use 5.8.1;
  1         4  
4 1     1   3 use strict;
  1         2  
  1         16  
5 1     1   3 use warnings;
  1         1  
  1         38  
6 1     1   3 use Text::Markup;
  1         33  
  1         16  
7 1     1   3 use Text::Markup::Cmd;
  1         1  
  1         50  
8 1     1   4 use File::Basename;
  1         1  
  1         393  
9              
10             our $VERSION = '0.41';
11              
12             sub import {
13             # Replace the regex if passed one.
14 0 0   0     Text::Markup->register( rest => $_[1] ) if $_[1];
15             }
16              
17             # Find Python or die.
18             my $PYTHON = find_cmd(
19             [WIN32 ? 'python3.exe' : 'python3'],
20             '--version',
21             );
22              
23             # We have python, let's find out if we have docutils.
24             exec_or_die(
25             qq{Missing required Python "docutils" module},
26             $PYTHON, '-c', 'import docutils',
27             );
28              
29             # We ship with our own rst2html that's lenient with unknown directives.
30             my $RST2HTML = File::Spec->catfile(dirname(__FILE__), 'rst2html_lenient.py');
31              
32             exec_or_die(
33             "$RST2HTML will not execute",
34             $PYTHON, $RST2HTML, '--test-patch',
35             );
36              
37             # Optional arguments to pass to rst2html
38             my @OPTIONS = qw(
39             --no-raw
40             --no-file-insertion
41             --stylesheet=
42             --cloak-email-address
43             --no-generator
44             --quiet
45             );
46              
47             # Options to improve rendering of Sphinx documents
48             my @SPHINX_OPTIONS = qw(
49             --dir-ignore toctree
50             --dir-ignore highlight
51             --dir-ignore index
52             --dir-ignore default-domain
53              
54             --dir-nested note
55             --dir-nested warning
56             --dir-nested versionadded
57             --dir-nested versionchanged
58             --dir-nested deprecated
59             --dir-nested seealso
60             --dir-nested hlist
61             --dir-nested glossary
62              
63             --dir-notitle code-block
64              
65             --dir-nested module
66             --dir-nested function
67             --output-encoding utf-8
68             );
69             # note: domains directive (last 2 options) incomplete
70              
71             sub parser {
72 0     0     my ($file, $encoding, $opts) = @_;
73 0           my $html = do {
74 0           my $fh = open_pipe(
75             $PYTHON, $RST2HTML,
76             @OPTIONS, @SPHINX_OPTIONS,
77             '--input-encoding', $encoding,
78             $file
79             );
80 0           local $/;
81 0           <$fh>;
82             };
83              
84             # Make sure we have something.
85 0 0         return undef if $html =~ m{\s+}ms;
86              
87             # Alas, --no-generator does not remove the generator meta tag. :-(
88 0           $html =~ s{^\s*]+>\n}{}ms;
89              
90 0           return $html;
91             }
92              
93             1;
94             __END__