| 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; |