| 1 | package Docbook::Convert::Pandoc; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # This file is part of Docbook::Convert. | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>. | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # Full license text is available at: | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # <http://dev.perl.org/licenses/> | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use strict qw(vars); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # Core modules | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use File::Find (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use File::Temp qw(tempdir tempfile); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # Non-core modules | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use IPC::Run3 qw(run3); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # Version information | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | $VERSION='1.012'; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | my ($class, $opt_hr)=@_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | return bless({%{$opt_hr || {}}}, $class); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opt_hr || {}
MC/DC100%1/1
$opt_hr || {}
$opt_hr
Truth table100%2/2
A
A= $opt_hr
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub command { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
command: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my ($self, @command)=@_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my ($output, $error); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | run3(\@command, \undef, \$output, \$error); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | die "command failed (@command): $error\n" if $?; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub convert_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
convert_file: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my ($self, $fn)=@_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | die "DocBook file not found: $fn\n" unless -f $fn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | my $source_fn=abs_path($fn); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $temporary_dn=tempdir(CLEANUP => 1); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $expanded_fn=File::Spec->catfile($temporary_dn, 'expanded.xml'); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $promoted_fn=File::Spec->catfile($temporary_dn, 'promoted.xml'); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $asset_dn=File::Spec->catdir(dirname(abs_path(__FILE__)), 'Pandoc'); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $self->command($self->{'xmllint'} || 'xmllint', '--nonet', '--xinclude', '--noent', | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'xmllint'} || 'xmllint'
MC/DC0%0/1
$self->{'xmllint'} || 'xmllint'
$self->{'xmllint'}
Truth table0%0/2
A
A= $self->{'xmllint'}
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | $source_fn, '--output', $expanded_fn); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $self->command($self->{'xsltproc'} || 'xsltproc', '--nonet', '-o', $promoted_fn, | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'xsltproc'} || 'xsltproc'
MC/DC0%0/1
$self->{'xsltproc'} || 'xsltproc'
$self->{'xsltproc'}
Truth table0%0/2
A
A= $self->{'xsltproc'}
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | File::Spec->catfile($asset_dn, 'promote-title-ids.xsl'), $expanded_fn); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $markdown=$self->command($self->{'pandoc'} || 'pandoc', '-f', 'docbook', | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'pandoc'} || 'pandoc'
MC/DC0%0/1
$self->{'pandoc'} || 'pandoc'
$self->{'pandoc'}
Truth table0%0/2
A
A= $self->{'pandoc'}
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | '-t', 'markdown-smart-auto_identifiers-simple_tables+pipe_tables-all_symbols_escapable', | ||||||||||||||||||||||||||||||||||||||||||
| 60 | '--wrap=none', | ||||||||||||||||||||||||||||||||||||||||||
| 61 | '--lua-filter='.File::Spec->catfile($asset_dn, 'admonition.lua'), | ||||||||||||||||||||||||||||||||||||||||||
| 62 | '--resource-path='.dirname($source_fn), $promoted_fn); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $markdown=~s/\{\.manual-page-new-tab\s+role="manual-page-new-tab"\}/{target="_blank" rel="noopener"}/g; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | return $markdown; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | sub discover_articles { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
discover_articles: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | my ($self, $root_dn)=@_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | $root_dn='doc' unless defined($root_dn) && length($root_dn); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $root_dn and length $root_dn
MC/DC0%0/2
defined $root_dn and length $root_dn
defined $root_dn
length $root_dn
Truth table33%1/3
A and B
A= defined $root_dnB= length $root_dn
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | return [] unless -d $root_dn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | my @article_fn; | |||||||||||||||||||||||||||||||||||||||||
| 74 | File::Find::find({ | ||||||||||||||||||||||||||||||||||||||||||
| 75 | no_chdir => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 31 | ▶ | wanted => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 31 | my $fn=$File::Find::name; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 31 | ▶ | if (-d $fn && $fn=~m{(?:^|/)(?:build|site|mkdocs|example|examples|images)$}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-d $fn and $fn =~ m[(?:^|/)(?:build|site|mkdocs|example|examples|images)$]
MC/DC100%2/2
-d $fn and $fn =~ m[(?:^|/)(?:build|site|mkdocs|example|examples|images)$]
-d $fn
$fn =~ m[(?:^|/)(?:build|site|mkdocs|example|examples|images)$]
Truth table100%3/3
A and B
A= -d $fnB= $fn =~ m[(?:^|/)(?:build|site|mkdocs|example|examples|images)$]
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 8 | $File::Find::prune=1; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 23 | ▶ | return unless -f $fn && !-l $fn && $fn=~/\.xml$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
-f $fn and not -l $fn and $fn =~ /\.xml$/i
-f $fn and not -l $fn
MC/DC66%2/3
-f $fn and not -l $fn and $fn =~ /\.xml$/i
-f $fn
not -l $fn
$fn =~ /\.xml$/i
Truth table75%3/4
A and not(B) and C
A= -f $fnB= not -l $fnC= $fn =~ /\.xml$/i
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 12 | ▶ | push @article_fn, $fn if $self->is_article($fn); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | }, $root_dn); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | return [sort @article_fn]; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | ▶ | sub convert_articles { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
convert_articles: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | my ($self, $root_dn)=@_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | my @changed_fn; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | foreach my $source_fn (@{$self->discover_articles($root_dn)}) { | |||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | (my $output_fn=$source_fn)=~s/\.xml$/.md/i; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | my $markdown=$self->convert_file($source_fn); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 6 | ▶ | my $existing=-f $output_fn ? $self->read_file($output_fn) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | next if defined($existing) && $existing eq $markdown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $existing and $existing eq $markdown
MC/DC50%1/2
defined $existing and $existing eq $markdown
defined $existing
$existing eq $markdown
Truth table66%2/3
A and B
A= defined $existingB= $existing eq $markdown
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | ▶ | die "refusing to overwrite symlink $output_fn\n" if -l $output_fn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | push @changed_fn, $output_fn; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 3 | ▶ | next if $self->{'dry_run'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | $self->write_file($output_fn, $markdown); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | return \@changed_fn; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 12 | ▶ | sub is_article { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_article: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 12 | my ($self, $fn)=@_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 12 | my $xml=$self->read_file($fn); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 12 | $xml=~s/^\xEF\xBB\xBF//; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 12 | $xml=~s/<\?.*?\?>//gs; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 12 | $xml=~s/<!--.*?-->//gs; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 12 | ▶ | return 0 unless $xml=~/<([A-Za-z_][A-Za-z0-9_.-]*(?::[A-Za-z_][A-Za-z0-9_.-]*)?)(?=[\s\/>])/s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 12 | my $root=$1; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 12 | $root=~s/^.*://; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 12 | return $root eq 'article'; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 16 | ▶ | sub read_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 16 | my ($self, $fn)=@_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 16 | ▶ | open(my $input_fh, '<', $fn) || die "unable to open $fn: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 16 | binmode($input_fh); | |||||||||||||||||||||||||||||||||||||||||
| 130 | 16 | local $/=undef; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 16 | my $text=<$input_fh>; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 16 | ▶ | close($input_fh) || die "unable to close $fn: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 16 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | sub write_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=6 SCAR=18.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | my ($self, $fn, $text)=@_; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | my ($output_fh, $temporary_fn)=tempfile('.docbook-XXXXXX', DIR => dirname($fn), UNLINK => 1); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | binmode($output_fh); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | ▶ | print {$output_fh} $text || die "unable to write $fn: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$text || die("unable to write ${fn}: $!\n")
MC/DC0%0/1
$text || die("unable to write ${fn}: $!\n")
$text
Truth table50%1/2
A
A= $text
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | ▶ | close($output_fh) || die "unable to close $fn: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | ▶ | my $mode=-f $fn ? (stat($fn))[2] & 07777 : 0644; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | ▶ | chmod($mode, $temporary_fn) || die "unable to set mode on $fn: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 2 | ▶ | rename($temporary_fn, $fn) || die "unable to replace $fn: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||||||||||||||||