| 1 | #!/usr/bin/env perl | ||
| 2 | # | ||
| 3 | # This file is part of Docbook::Convert. | ||
| 4 | # | ||
| 5 | # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>. | ||
| 6 | # | ||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||
| 9 | # | ||
| 10 | # Full license text is available at: | ||
| 11 | # | ||
| 12 | # <http://dev.perl.org/licenses/> | ||
| 13 | # | ||
| 14 | 2 | ▶ | use strict qw(vars); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 2 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 2 | ▶ | no warnings qw(utf8); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | |||
| 19 | # External modules | ||
| 20 | # | ||
| 21 | 2 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 2 | ▶ | use Getopt::Long; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | Getopt::Long::Configure('no_ignore_case'); | ||
| 24 | 2 | ▶ | use Pod::Usage; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 2 | ▶ | use FindBin qw($RealBin $Script); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 2 | ▶ | use File::Find; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 2 | ▶ | use Cwd qw(cwd abs_path); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | 0 | ▶ | use Docbook::Convert; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 29 | use Docbook::Convert::Util; | ||
| 30 | use Docbook::Convert::Constant; | ||
| 31 | |||
| 32 | |||
| 33 | # Used for debugging only | ||
| 34 | # | ||
| 35 | use Data::Dumper; | ||
| 36 | $Data::Dumper::Indent=1; | ||
| 37 | $Data::Dumper::Terse=1; | ||
| 38 | |||
| 39 | |||
| 40 | # Version Info, must be all one line for MakeMaker, CPAN. | ||
| 41 | # | ||
| 42 | $VERSION='1.012'; | ||
| 43 | |||
| 44 | |||
| 45 | # Run main routine | ||
| 46 | # | ||
| 47 | exit ${&main(&getopt(\@ARGV)) || die 'unknown error'}; | ||
| 48 | |||
| 49 | |||
| 50 | #=================================================================================================== | ||
| 51 | |||
| 52 | |||
| 53 | sub getopt { | ||
| 54 | |||
| 55 | |||
| 56 | # Parse options and dispatch to actual work routine below | ||
| 57 | # | ||
| 58 | my $argv_ar=shift(); | ||
| 59 | |||
| 60 | |||
| 61 | # Get options | ||
| 62 | # | ||
| 63 | my %opt; | ||
| 64 | |||
| 65 | |||
| 66 | # Converts some constant settings into settable options | ||
| 67 | # | ||
| 68 | my @getopt_constant=map {$GETOPT_CONSTANT_HR->{$_} ? $_ . $GETOPT_CONSTANT_HR->{$_} : $_} | ||
| 69 | keys %{$GETOPT_CONSTANT_HR}; | ||
| 70 | |||
| 71 | |||
| 72 | # Get command line options | ||
| 73 | # | ||
| 74 | GetOptions( | ||
| 75 | \%opt, | ||
| 76 | @{$GETOPT_AR}, | ||
| 77 | @getopt_constant, | ||
| 78 | 'pandoc', | ||
| 79 | ) || pod2usage(2); | ||
| 80 | pod2usage(-verbose => 99, -sections => 'SYNOPSIS|Options', -exitval => 1) if $opt{'help'}; | ||
| 81 | pod2usage(-verbose => 2) if $opt{'man'}; | ||
| 82 | $opt{'version'} && do { | ||
| 83 | print "$Script version: $VERSION\n"; | ||
| 84 | exit 0 | ||
| 85 | }; | ||
| 86 | |||
| 87 | |||
| 88 | # Turn on debug and verbosity if needed | ||
| 89 | # | ||
| 90 | ($VERBOSE, $DEBUG)=@opt{qw(verbose debug)}; | ||
| 91 | |||
| 92 | |||
| 93 | # Debug | ||
| 94 | # | ||
| 95 | $opt{'dumpopt'} && do { | ||
| 96 | CORE::print STDERR Dumper([sort @{$GETOPT_AR}, @getopt_constant]); | ||
| 97 | exit 0; | ||
| 98 | }; | ||
| 99 | |||
| 100 | |||
| 101 | # Recursing ? | ||
| 102 | # | ||
| 103 | if (exists $opt{'recurse'} || $opt{'recursedir'}) { | ||
| 104 | my $dn=$opt{'recursedir'} || cwd(); | ||
| 105 | $dn=Cwd::abs_path($dn); | ||
| 106 | my @fn; | ||
| 107 | my $wanted_cr=sub { | ||
| 108 | return unless -f (my $fn=$File::Find::name); | ||
| 109 | return if $fn=~/\/\._/; # Mac temp file | ||
| 110 | push @fn, $fn if ($fn=~/\Q$XMLSUFFIX\E$/); | ||
| 111 | }; | ||
| 112 | find($wanted_cr, $dn); | ||
| 113 | @fn || | ||
| 114 | #return err("no files with '%s' suffix found when recursing directory '$dn'", $XMLSUFFIX); | ||
| 115 | return msg("no files with '%s' suffix found when recursing directory '$dn'", $XMLSUFFIX); | ||
| 116 | $opt{'infile'}=\@fn; | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | # Get infile | ||
| 121 | # | ||
| 122 | unless (@{$opt{'infile'}}) { | ||
| 123 | $opt{'infile'}=@{$argv_ar} ? $argv_ar : [\*STDIN]; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | # Set handler | ||
| 128 | # | ||
| 129 | $opt{'handler'} ||= ( | ||
| 130 | ($opt{'markdown'} && 'markdown') || | ||
| 131 | $HANDLER_DEFAULT | ||
| 132 | ); | ||
| 133 | |||
| 134 | |||
| 135 | # Done | ||
| 136 | # | ||
| 137 | return \%opt; | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | sub main { | ||
| 143 | |||
| 144 | # Passed a list of options and convert file/input stream | ||
| 145 | # | ||
| 146 | my $opt_hr=shift(); | ||
| 147 | foreach my $fn (@{$opt_hr->{'infile'}}) { | ||
| 148 | my $output; | ||
| 149 | if ($opt_hr->{'pandoc'}) { | ||
| 150 | die "--pandoc requires a filename\n" if ref($fn); | ||
| 151 | require Docbook::Convert::Pandoc; | ||
| 152 | $output=Docbook::Convert::Pandoc->new()->convert_file($fn); | ||
| 153 | } | ||
| 154 | elsif (ref($fn) eq 'GLOB') { | ||
| 155 | $output=Docbook::Convert->process($fn, $opt_hr); | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | msg("processing file: $fn"); | ||
| 159 | $output=Docbook::Convert->process_file($fn, $opt_hr); | ||
| 160 | } | ||
| 161 | if (my $out_fn=$opt_hr->{'outfile'}) { | ||
| 162 | if ($out_fn=~/^\.(.*)$/) { | ||
| 163 | |||
| 164 | # Output file option is something like '.foo'. Means add '.foo' to input file name | ||
| 165 | # and make that the output file name | ||
| 166 | # | ||
| 167 | my $ext=$1; | ||
| 168 | ($out_fn=$fn)=~s/\Q$XMLSUFFIX\E$//; | ||
| 169 | $out_fn.=".${ext}"; | ||
| 170 | } | ||
| 171 | my $out_fh=IO::File->new($out_fn, O_WRONLY | O_CREAT | O_TRUNC) || | ||
| 172 | return err("unable to write to output file $out_fn, $!"); | ||
| 173 | print $out_fh $output; | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | print $output; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | return \undef; | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | __END__ | ||