| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright: 2018 Dmitry Shachnev <[email protected]> |
| 4 |
# |
| 5 |
# Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
# a copy of this software and associated documentation files (the |
| 7 |
# "Software"), to deal in the Software without restriction, including |
| 8 |
# without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
# distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
# permit persons to whom the Software is furnished to do so, subject to |
| 11 |
# the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included |
| 14 |
# in all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
|
| 24 |
=head1 NAME |
| 25 |
|
| 26 |
dh_mkdocs - helps with packaging MkDocs-generated documentation |
| 27 |
|
| 28 |
=head1 SYNOPSIS |
| 29 |
|
| 30 |
dh_mkdocs [S<I<debhelper options>>] [B<-X>I<item>] [B<-T>I<package> | B<--theme-package> I<package>] |
| 31 |
|
| 32 |
=head1 DESCRIPTION |
| 33 |
|
| 34 |
B<dh_mkdocs> is a helper program for packaging documentation generated |
| 35 |
by MkDocs. More specifically: |
| 36 |
|
| 37 |
=over 4 |
| 38 |
|
| 39 |
=item * |
| 40 |
|
| 41 |
It replaces static files with symlinks to their packaged versions if possible. |
| 42 |
For packages using non-standard themes, use the B<--theme-package> option, |
| 43 |
for example: |
| 44 |
|
| 45 |
dh_mkdocs --theme-package mkdocs-bootstrap |
| 46 |
|
| 47 |
B<dh_mkdocs> only symlinks non-empty files with identical content. |
| 48 |
|
| 49 |
=item * |
| 50 |
|
| 51 |
For the standard B<mkdocs> and B<readthedocs> themes, it replaces references to |
| 52 |
the highlight.js library from CDN with references to the packaged version of |
| 53 |
that library. |
| 54 |
|
| 55 |
=item * |
| 56 |
|
| 57 |
It adds the needed dependencies to B<${mkdocs:Depends}> substitution variable. |
| 58 |
Please add this variable to the B<Depends:> field of your documentation |
| 59 |
package. |
| 60 |
|
| 61 |
=back |
| 62 |
|
| 63 |
You can pass B<--with mkdocs> to L<dh(1)> to make it automatically call |
| 64 |
B<dh_mkdocs> after B<dh_installdocs>. |
| 65 |
|
| 66 |
=head1 OPTIONS |
| 67 |
|
| 68 |
=over 4 |
| 69 |
|
| 70 |
=item B<-X>I<item>, B<--exclude> I<item> |
| 71 |
|
| 72 |
Exclude files that contain I<item> anywhere in their filename from being |
| 73 |
symlinked or edited. |
| 74 |
|
| 75 |
This option can be specified multiple times. |
| 76 |
|
| 77 |
=item B<-T>I<package>, B<--theme-package> I<package> |
| 78 |
|
| 79 |
Scan the specified additional I<package> for files that can be used as |
| 80 |
symlink targets. |
| 81 |
|
| 82 |
This option can be specified multiple times. |
| 83 |
|
| 84 |
=back |
| 85 |
|
| 86 |
=cut |
| 87 |
|
| 88 |
use strict; |
| 89 |
use warnings; |
| 90 |
use Debian::Debhelper::Dh_Lib; |
| 91 |
use Digest::file qw(digest_file); |
| 92 |
use File::Find qw(find); |
| 93 |
use File::Spec; |
| 94 |
use IPC::Open2 qw(open2); |
| 95 |
|
| 96 |
my @theme_packages; |
| 97 |
my %package_dependencies; |
| 98 |
|
| 99 |
sub add_theme_package { |
| 100 |
my ($option, $value) = @_; |
| 101 |
push @theme_packages, $value; |
| 102 |
} |
| 103 |
|
| 104 |
init(options => { |
| 105 |
'theme-package=s' => \&add_theme_package, |
| 106 |
'T=s' => \&add_theme_package, |
| 107 |
}); |
| 108 |
|
| 109 |
my $mkdocs_directory_found = 0; |
| 110 |
|
| 111 |
foreach my $package (@{$dh{DOPACKAGES}}) { |
| 112 |
my $tmpdir = tmpdir($package); |
| 113 |
next unless -d $tmpdir; |
| 114 |
|
| 115 |
# in this hash, value 1 means versioned dependency, 0 means unversioned |
| 116 |
$package_dependencies{$package} = {}; |
| 117 |
|
| 118 |
find({ |
| 119 |
wanted => sub { |
| 120 |
return unless -d; |
| 121 |
return if -l; |
| 122 |
return if excludefile($_); |
| 123 |
return unless looks_like_mkdocs_directory($_); |
| 124 |
|
| 125 |
$mkdocs_directory_found = 1; |
| 126 |
fixup_highlightjs_usage($package, $_); |
| 127 |
if ($package ne 'mkdocs-doc') { |
| 128 |
symlink_static_files($package, $_); |
| 129 |
} |
| 130 |
write_substvars($package); |
| 131 |
}, |
| 132 |
no_chdir => 1 |
| 133 |
}, $tmpdir); |
| 134 |
} |
| 135 |
|
| 136 |
$mkdocs_directory_found or error('MkDocs directory not found'); |
| 137 |
|
| 138 |
sub looks_like_mkdocs_directory { |
| 139 |
my ($path) = @_; |
| 140 |
return 0 unless -f "$path/index.html"; |
| 141 |
return 0 unless -f "$path/sitemap.xml"; |
| 142 |
return 0 unless -f "$path/search/search_index.json"; |
| 143 |
return 1; |
| 144 |
} |
| 145 |
|
| 146 |
sub fixup_highlightjs_usage { |
| 147 |
my ($package, $path) = @_; |
| 148 |
my $cdn_hljs = '(?:https:)?//cdnjs.cloudflare.com/ajax/libs/highlight.js/[\d.]+'; |
| 149 |
my $pkg_hljs = 'file:///usr/share/javascript/highlight.js'; |
| 150 |
|
| 151 |
find({ |
| 152 |
wanted => sub { |
| 153 |
return unless -f; |
| 154 |
return unless /\.html$/s; |
| 155 |
my $filename = $_; |
| 156 |
|
| 157 |
open(my $in, '<', $_) or error("Failed to open $_ for read: $!"); |
| 158 |
open(my $out, '>', "$_.new") or error("Failed to open $_.new for write: $!"); |
| 159 |
while (<$in>) { |
| 160 |
if (m{$cdn_hljs/highlight.min.js}) { |
| 161 |
$package_dependencies{$package}{'libjs-highlight.js'} = 0; |
| 162 |
} |
| 163 |
s{$cdn_hljs/highlight.min.js}{$pkg_hljs/highlight.min.js}; |
| 164 |
s{$cdn_hljs/styles/github.min.css}{$pkg_hljs/styles/github.css}; |
| 165 |
print $out $_ unless m{$cdn_hljs/languages/}; |
| 166 |
} |
| 167 |
close($in); |
| 168 |
close($out); |
| 169 |
rename("$filename.new", $filename); |
| 170 |
}, |
| 171 |
no_chdir => 1 |
| 172 |
}, $path); |
| 173 |
} |
| 174 |
|
| 175 |
sub symlink_static_files { |
| 176 |
my ($package, $path) = @_; |
| 177 |
my %file_names_by_hash; |
| 178 |
my %package_names_by_hash; |
| 179 |
my @packages = ( |
| 180 |
'fonts-lato', |
| 181 |
'fonts-font-awesome', |
| 182 |
'libjs-bootstrap4', |
| 183 |
'libjs-jquery', |
| 184 |
'libjs-lunr', |
| 185 |
'libjs-modernizr', |
| 186 |
'node-jquery', |
| 187 |
'mkdocs', |
| 188 |
'sphinx-rtd-theme-common' |
| 189 |
); |
| 190 |
push @packages, @theme_packages; |
| 191 |
|
| 192 |
foreach my $package_name (@packages) { |
| 193 |
my $stdout; |
| 194 |
my $pid = open2($stdout, undef, 'dpkg-query', '--listfiles', $package_name); |
| 195 |
if ($pid) { |
| 196 |
while (<$stdout>) { |
| 197 |
chomp; |
| 198 |
if (-f && -s && ! -l) { |
| 199 |
my $digest = digest_file($_, 'SHA-1'); |
| 200 |
$file_names_by_hash{$digest} = $_; |
| 201 |
$package_names_by_hash{$digest} = $package_name; |
| 202 |
} |
| 203 |
} |
| 204 |
close $stdout; |
| 205 |
waitpid($pid, 0); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
find({ |
| 210 |
wanted => sub { |
| 211 |
return unless -f; |
| 212 |
return if -l; |
| 213 |
return if excludefile($_); |
| 214 |
|
| 215 |
my $digest = digest_file($_, 'SHA-1'); |
| 216 |
if (exists $file_names_by_hash{$digest}) { |
| 217 |
my $target_path = $file_names_by_hash{$digest}; |
| 218 |
my ($source_path) = $_ =~ m{debian/$package(/.*)}; |
| 219 |
my $target_relpath = File::Spec->abs2rel($target_path, dirname($source_path)); |
| 220 |
unlink($_) or error("Failed to delete $_: $!"); |
| 221 |
symlink($target_relpath, $_) or error("Failed to create symbolic link: $!"); |
| 222 |
my $dependency_name = $package_names_by_hash{$digest}; |
| 223 |
$package_dependencies{$package}{$dependency_name} = 1; |
| 224 |
} |
| 225 |
}, |
| 226 |
no_chdir => 1 |
| 227 |
}, $path); |
| 228 |
} |
| 229 |
|
| 230 |
sub write_substvars { |
| 231 |
my ($package) = @_; |
| 232 |
my $dependencies = $package_dependencies{$package}; |
| 233 |
|
| 234 |
while (my ($dependency, $versioned) = each %{$dependencies}) { |
| 235 |
if ($versioned) { |
| 236 |
my $stdout; |
| 237 |
my $pid = open2($stdout, undef, |
| 238 |
'dpkg-query', '--show', '--showformat=${Version}', $dependency); |
| 239 |
waitpid($pid, 0); |
| 240 |
my $version = <$stdout>; |
| 241 |
$dependency = "$dependency (>= $version)"; |
| 242 |
} |
| 243 |
addsubstvar($package, 'mkdocs:Depends', $dependency); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
=head1 SEE ALSO |
| 248 |
|
| 249 |
L<debhelper(7)>, L<dh(1)>. |
| 250 |
|
| 251 |
This program is meant to be used together with debhelper. |
| 252 |
|
| 253 |
=head1 AUTHOR |
| 254 |
|
| 255 |
Dmitry Shachnev <[email protected]> |
| 256 |
|
| 257 |
=cut |