| 1 | #! /bin/false | ||
| 2 | |||
| 3 | # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>, | ||
| 4 | # all rights reserved. | ||
| 5 | |||
| 6 | # This program is free software: you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation; either version 3 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | |||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | |||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | package Qgoda::Command::Markdown; | ||
| 20 | $Qgoda::Command::Markdown::VERSION = 'v0.9.2'; | ||
| 21 | use strict; | ||
| 22 | |||
| 23 | use Locale::TextDomain qw(qgoda); | ||
| 24 | |||
| 25 | use Qgoda; | ||
| 26 | use Qgoda::CLI; | ||
| 27 | use Qgoda::Util qw(read_file); | ||
| 28 | |||
| 29 | use base 'Qgoda::Command'; | ||
| 30 | |||
| 31 | sub _getDefaults { processor => 'Markdown' } | ||
| 32 | |||
| 33 | sub _getOptionSpecs { | ||
| 34 | processor => 'processor=s' | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _run { | ||
| 38 | my ($self, $args, $global_options, %options) = @_; | ||
| 39 | |||
| 40 | $args = ['-'] if !@$args; | ||
| 41 | my $count = 0; | ||
| 42 | map { ++$count if '-' eq $_ } @$args; | ||
| 43 | Qgoda::CLI->commandUsageError(markdown => | ||
| 44 | __"can only read once from standard input") | ||
| 45 | if $count > 1; | ||
| 46 | |||
| 47 | $global_options->{quiet} = 1; | ||
| 48 | delete $global_options->{verbose}; | ||
| 49 | $global_options->{log_stderr} = 1; | ||
| 50 | |||
| 51 | my $q = Qgoda->new($global_options); | ||
| 52 | my ($processor) = $q->_getProcessors($options{processor}); | ||
| 53 | if (!$processor) { | ||
| 54 | die __x("error instantiating processor '{processor}'.\n", | ||
| 55 | processor => $options{processor}); | ||
| 56 | } | ||
| 57 | |||
| 58 | foreach my $arg (@$args) { | ||
| 59 | $self->__processFile($processor, $arg); | ||
| 60 | } | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub __processFile { | ||
| 66 | my ($self, $processor, $filename) = @_; | ||
| 67 | |||
| 68 | my $markdown; | ||
| 69 | if ('-' eq $filename) { | ||
| 70 | $filename = __("[standard input]"); | ||
| 71 | $markdown = join '', <STDIN>; | ||
| 72 | } else { | ||
| 73 | $markdown = read_file $filename; | ||
| 74 | die __x("unable to open '{filename}' for reading: {error}!\n", | ||
| 75 | filename => $filename, error => $!) | ||
| 76 | if !defined $markdown; | ||
| 77 | } | ||
| 78 | |||
| 79 | print $processor->process($markdown); | ||
| 80 | |||
| 81 | return $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; | ||
| 85 | |||
| 86 | =head1 NAME | ||
| 87 | |||
| 88 | qgoda markdown - Run text through a Markdown processor | ||
| 89 | |||
| 90 | =head1 SYNOPSIS | ||
| 91 | |||
| 92 | qgoda markdown [<global options>] | ||
| 93 | [--processor=<Markdown processor>] [<file>...] | ||
| 94 | |||
| 95 | Try 'qgoda --help' for a description of global options. | ||
| 96 | |||
| 97 | =head1 DESCRIPTION | ||
| 98 | |||
| 99 | Runs the specified files through the Markdown processor and prints the | ||
| 100 | result to standard output. If no files are specified, or '-' is specified, | ||
| 101 | standard input is read. | ||
| 102 | |||
| 103 | This command does not aim to compete with other Markdown command-line tools. | ||
| 104 | Its purpose is to allow testing Markdown snippets with the Markdown processors | ||
| 105 | in use within Qgoda. | ||
| 106 | |||
| 107 | =head1 OPTIONS | ||
| 108 | |||
| 109 | =over 4 | ||
| 110 | |||
| 111 | =item -p, --processor=<processor> | ||
| 112 | |||
| 113 | Specify the Markdown processor to use, defaults to 'Markdown' which uses | ||
| 114 | L<Text::Markdown> for rendering. The other option is 'Hoedown' if | ||
| 115 | L<Text::Markdown::Hoedown> is installed. | ||
| 116 | |||
| 117 | =item -h --help | ||
| 118 | |||
| 119 | Show this help page and exit. | ||
| 120 | |||
| 121 | =back | ||
| 122 | |||
| 123 | =head1 SEE ALSO | ||
| 124 | |||
| 125 | L<Text::Markdown>(3pm), L<Text::Markdown::Hoedown>(3pm), qgoda(1), perl(1) | ||
| 126 | |||
| 127 | =head1 QGODA | ||
| 128 | |||
| 129 | Part of L<Qgoda|http://www.qgoda.net/>. |