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