Markdent/CLI.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 87 CC 8 SCAR 27.9
CC
8
Cov
0%
run
42.0
BUILD
12.0
BEGIN
2.0
CRAP 16.2
1 package Markdent::CLI;
2  
3 use strict;
4 use warnings;
5 use namespace::autoclean;
6  
7 our $VERSION = '0.39';
8  
9 use File::Slurper qw( read_text );
10  
11 # We need to make this a prereq so we have --help
12 use Getopt::Long::Descriptive;
13 use Markdent::Simple::Document;
14 use Markdent::Simple::Fragment;
15 use Markdent::Types;
16 use MooseX::Getopt::OptionTypeMap;
17  
18 use Moose;
19  
20 with 'MooseX::Getopt::Dashes';
21  
22 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
23 t('ExistingFile') => '=s',
24 );
25  
26 has file => (
27 is => 'ro',
28 isa => => t('ExistingFile'),
29 predicate => 'has_file',
30 documentation =>
31 'A Markdown file (or - for STDIN) to parse and turn into HTML. Conflicts with --text.',
32 );
33  
34 has text => (
35 is => 'ro',
36 isa => => t('Str'),
37 predicate => 'has_text',
38 documentation =>
39 'Markdown text to parse and turn into HTML. Conflicts with --file.',
40 );
41  
42 has title => (
43 is => 'ro',
44 isa => => t('Str'),
45 predicate => '_has_title',
46 documentation => 'The title for the created document. Optional.',
47 );
48  
49 has charset => (
50 is => 'ro',
51 isa => => t('Str'),
52 predicate => '_has_charset',
53 documentation => 'The charset for the created document. Optional.',
54 );
55  
56 has language => (
57 is => 'ro',
58 isa => => t('Str'),
59 predicate => '_has_language',
60 documentation => 'The language for the created document. Optional.',
61 );
62  
63 has dialects => (
64 is => 'ro',
65 isa => => t( 'ArrayRef', of => t('Str') ),
66 default => sub { [] },
67 documentation => 'One oe more dialects to use when parsing.',
68 );
69  
70 sub BUILD {
71 my $self = shift;
72  
73 die 'Cannot pass both --file and --text options'
74 if $self->has_file && $self->has_text;
75  
76 die 'You must pass a --file or --text parameter'
77 unless $self->has_file || $self->has_text;
78  
79 return;
80 }
81  
82 sub run {
83 my $self = shift;
84  
85 ## no critic (InputOutput::ProhibitExplicitStdin)
86 my $markdown
87 = $self->has_file
88 ? $self->file eq '-'
89 ? do { local $/ = undef; <STDIN> }
90 : read_text( $self->file )
91 : $self->text;
92  
93 my ( $class, %p ) = $self->_has_title
94 ? (
95 'Markdent::Simple::Document',
96 title => $self->title,
97 ( $self->_has_charset ? ( charset => $self->charset ) : () ),
98 ( $self->_has_language ? ( language => $self->language ) : () ),
99 )
100 : ('Markdent::Simple::Fragment');
101  
102 my $html = $class->new->markdown_to_html(
103 markdown => $markdown,
104 dialects => $self->dialects,
105 %p,
106 );
107  
108 ## no critic (InputOutput::RequireCheckedSyscalls)
109 print $html;
110 ## use critic;
111  
112 exit 0;
113 }
114  
115 __PACKAGE__->meta->make_immutable;
116  
117 1;