| 1 | package Markdown::Pod::script; | ||
| 2 | # ABSTRACT: script to convert Markdown to POD | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.007'; | ||
| 8 | |||
| 9 | use Encode qw( encodings decode ); | ||
| 10 | use Getopt::Long; | ||
| 11 | use List::Util qw( first ); | ||
| 12 | use Markdown::Pod; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | |||
| 17 | bless { | ||
| 18 | encoding => 'utf8', | ||
| 19 | dialect => 'Standard', | ||
| 20 | verbose => undef, | ||
| 21 | argv => [], | ||
| 22 | @_, | ||
| 23 | }, $class; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub doit { | ||
| 27 | my $self = shift; | ||
| 28 | |||
| 29 | if ( my $action = $self->{action} ) { | ||
| 30 | $self->$action() and return 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | $self->show_help(1) | ||
| 34 | unless @{ $self->{argv} } || $self->{load_from_stdin}; | ||
| 35 | |||
| 36 | if ( $self->{encoding} ) { | ||
| 37 | my $found = first { $_ eq $self->{encoding} } Encode->encodings; | ||
| 38 | if ( !$found ) { | ||
| 39 | warn "cannot find such '$self->{encoding}' encoding\n"; | ||
| 40 | $self->show_help(1); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | my $m2p = Markdown::Pod->new; | ||
| 45 | |||
| 46 | if ( $self->{load_from_stdin} ) { | ||
| 47 | my $markdown = do { local $/; <STDIN>; }; | ||
| 48 | |||
| 49 | my $pod = $m2p->markdown_to_pod( | ||
| 50 | encoding => $self->{encoding}, | ||
| 51 | dialect => $self->{dialect}, | ||
| 52 | markdown => $markdown, | ||
| 53 | ); | ||
| 54 | print $self->{encoding} ? decode( $self->{encoding}, $pod ) : $pod; | ||
| 55 | } | ||
| 56 | |||
| 57 | for my $file ( @{ $self->{argv} } ) { | ||
| 58 | open my $fh, $file | ||
| 59 | or warn("cannot open $file: $!\n"), next; | ||
| 60 | my $markdown = do { local $/; <$fh>; }; | ||
| 61 | close $fh; | ||
| 62 | |||
| 63 | my $pod = $m2p->markdown_to_pod( | ||
| 64 | encoding => $self->{encoding}, | ||
| 65 | dialect => $self->{dialect}, | ||
| 66 | markdown => $markdown, | ||
| 67 | ); | ||
| 68 | print $self->{encoding} ? decode( $self->{encoding}, $pod ) : $pod; | ||
| 69 | } | ||
| 70 | |||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub env { | ||
| 75 | my ( $self, $key ) = @_; | ||
| 76 | return $ENV{ "PERL_MARKDOWN2POD_" . $key } || q{}; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub parse_options { | ||
| 80 | my $self = shift; | ||
| 81 | |||
| 82 | local @ARGV = @{ $self->{argv} }; | ||
| 83 | push @ARGV, split /\s+/, $self->env('OPT'); | ||
| 84 | push @ARGV, @_; | ||
| 85 | |||
| 86 | Getopt::Long::Configure("bundling"); | ||
| 87 | Getopt::Long::GetOptions( | ||
| 88 | 'e|encoding=s' => sub { $self->{encoding} = $_[1] }, | ||
| 89 | 'd|dialect=s' => sub { $self->{dialect} = $_[1] }, | ||
| 90 | 'v|verbose' => sub { $self->{verbose} = 1 }, | ||
| 91 | 'h|help' => sub { $self->{action} = 'show_help' }, | ||
| 92 | 'V|version' => sub { $self->{action} = 'show_version' }, | ||
| 93 | ); | ||
| 94 | |||
| 95 | if ( @ARGV == 0 || !-t STDIN ) { | ||
| 96 | $self->{load_from_stdin} = 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | $self->{argv} = \@ARGV; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub show_help { | ||
| 103 | my $self = shift; | ||
| 104 | |||
| 105 | if ( $_[0] ) { | ||
| 106 | die <<'USAGE'; | ||
| 107 | Usage: markdown2pod [OPTIONS] <file1> [ <file2> ... ] | ||
| 108 | |||
| 109 | Try `markdown2pod --help` for details. | ||
| 110 | USAGE | ||
| 111 | } | ||
| 112 | |||
| 113 | print <<"HELP"; | ||
| 114 | Usage: markdown2pod [OPTIONS] <file1> [ <file2> ... ] | ||
| 115 | |||
| 116 | OPTIONS: | ||
| 117 | -e,--encoding set markdown encoding | ||
| 118 | -d,--dialect set markdown dialect (Standard, Github, Theory) | ||
| 119 | -v,--verbose print more information | ||
| 120 | -h,--help print this message | ||
| 121 | -V,--version display software version | ||
| 122 | |||
| 123 | HELP | ||
| 124 | |||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub show_version { | ||
| 129 | print "markdown2pod (Markdown::Pod) version $Markdown::Pod::VERSION\n"; | ||
| 130 | return 1; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | __END__ | ||
| 136 | |||
| 137 | =pod | ||
| 138 | |||
| 139 | =encoding UTF-8 | ||
| 140 | |||
| 141 | =head1 NAME | ||
| 142 | |||
| 143 | Markdown::Pod::script - script to convert Markdown to POD | ||
| 144 | |||
| 145 | =head1 VERSION | ||
| 146 | |||
| 147 | version 0.007 | ||
| 148 | |||
| 149 | =head1 SYNOPSIS | ||
| 150 | |||
| 151 | my $app = Markdown::Pod::script->new; | ||
| 152 | $app->parse_options(@ARGV); | ||
| 153 | $app->doit or exit(1); | ||
| 154 | |||
| 155 | =head1 DESCRIPTION | ||
| 156 | |||
| 157 | This module contains script related functions. | ||
| 158 | |||
| 159 | =head1 METHODS | ||
| 160 | |||
| 161 | =head2 new | ||
| 162 | |||
| 163 | =head2 doit | ||
| 164 | |||
| 165 | =head2 env | ||
| 166 | |||
| 167 | =head2 parse_options | ||
| 168 | |||
| 169 | =head2 show_help | ||
| 170 | |||
| 171 | =head2 show_version | ||
| 172 | |||
| 173 | =head1 AUTHOR | ||
| 174 | |||
| 175 | 김도형 - Keedi Kim <keedi@cpan.org> | ||
| 176 | |||
| 177 | =head1 COPYRIGHT AND LICENSE | ||
| 178 | |||
| 179 | This software is copyright (c) 2018 by Keedi Kim. | ||
| 180 | |||
| 181 | This is free software; you can redistribute it and/or modify it under | ||
| 182 | the same terms as the Perl 5 programming language system itself. | ||
| 183 | |||
| 184 | =cut |