File: dh_mkdocs

package info (click to toggle)
python-mkdocs 1.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,812 kB
  • sloc: python: 14,346; javascript: 10,535; perl: 143; sh: 57; makefile: 30; xml: 11
file content (257 lines) | stat: -rwxr-xr-x 7,642 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/perl

# Copyright: 2018 Dmitry Shachnev <mitya57@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=head1 NAME

dh_mkdocs - helps with packaging MkDocs-generated documentation

=head1 SYNOPSIS

dh_mkdocs [S<I<debhelper options>>] [B<-X>I<item>] [B<-T>I<package> | B<--theme-package> I<package>]

=head1 DESCRIPTION

B<dh_mkdocs> is a helper program for packaging documentation generated
by MkDocs. More specifically:

=over 4

=item *

It replaces static files with symlinks to their packaged versions if possible.
For packages using non-standard themes, use the B<--theme-package> option,
for example:

    dh_mkdocs --theme-package mkdocs-bootstrap

B<dh_mkdocs> only symlinks non-empty files with identical content.

=item *

For the standard B<mkdocs> and B<readthedocs> themes, it replaces references to
the highlight.js library from CDN with references to the packaged version of
that library.

=item *

It adds the needed dependencies to B<${mkdocs:Depends}> substitution variable.
Please add this variable to the B<Depends:> field of your documentation
package.

=back

You can pass B<--with mkdocs> to L<dh(1)> to make it automatically call
B<dh_mkdocs> after B<dh_installdocs>.

=head1 OPTIONS

=over 4

=item B<-X>I<item>, B<--exclude> I<item>

Exclude files that contain I<item> anywhere in their filename from being
symlinked or edited.

This option can be specified multiple times.

=item B<-T>I<package>, B<--theme-package> I<package>

Scan the specified additional I<package> for files that can be used as
symlink targets.

This option can be specified multiple times.

=back

=cut

use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;
use Digest::file qw(digest_file);
use File::Find qw(find);
use File::Spec;
use IPC::Open2 qw(open2);

my @theme_packages;
my %package_dependencies;

sub add_theme_package {
    my ($option, $value) = @_;
    push @theme_packages, $value;
}

init(options => {
    'theme-package=s' => \&add_theme_package,
    'T=s' => \&add_theme_package,
});

my $mkdocs_directory_found = 0;

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $tmpdir = tmpdir($package);
    next unless -d $tmpdir;

    # in this hash, value 1 means versioned dependency, 0 means unversioned
    $package_dependencies{$package} = {};

    find({
        wanted => sub {
            return unless -d;
            return if -l;
            return if excludefile($_);
            return unless looks_like_mkdocs_directory($_);

            $mkdocs_directory_found = 1;
            fixup_highlightjs_usage($package, $_);
            if ($package ne 'mkdocs-doc') {
                symlink_static_files($package, $_);
            }
            write_substvars($package);
        },
        no_chdir => 1
    }, $tmpdir);
}

$mkdocs_directory_found or error('MkDocs directory not found');

sub looks_like_mkdocs_directory {
    my ($path) = @_;
    return 0 unless -f "$path/index.html";
    return 0 unless -f "$path/sitemap.xml";
    return 0 unless -f "$path/search/search_index.json";
    return 1;
}

sub fixup_highlightjs_usage {
    my ($package, $path) = @_;
    my $cdn_hljs = '(?:https:)?//cdnjs.cloudflare.com/ajax/libs/highlight.js/[\d.]+';
    my $pkg_hljs = 'file:///usr/share/javascript/highlight.js';

    find({
        wanted => sub {
            return unless -f;
            return unless /\.html$/s;
            my $filename = $_;

            open(my $in, '<', $_) or error("Failed to open $_ for read: $!");
            open(my $out, '>', "$_.new") or error("Failed to open $_.new for write: $!");
            while (<$in>) {
                if (m{$cdn_hljs/highlight.min.js}) {
                    $package_dependencies{$package}{'libjs-highlight.js'} = 0;
                }
                s{$cdn_hljs/highlight.min.js}{$pkg_hljs/highlight.min.js};
                s{$cdn_hljs/styles/github.min.css}{$pkg_hljs/styles/github.css};
                print $out $_ unless m{$cdn_hljs/languages/};
            }
            close($in);
            close($out);
            rename("$filename.new", $filename);
        },
        no_chdir => 1
    }, $path);
}

sub symlink_static_files {
    my ($package, $path) = @_;
    my %file_names_by_hash;
    my %package_names_by_hash;
    my @packages = (
        'fonts-lato',
        'fonts-font-awesome',
        'libjs-bootstrap4',
        'libjs-jquery',
        'libjs-lunr',
        'libjs-modernizr',
        'node-jquery',
        'mkdocs',
        'sphinx-rtd-theme-common'
    );
    push @packages, @theme_packages;

    foreach my $package_name (@packages) {
        my $stdout;
        my $pid = open2($stdout, undef, 'dpkg-query', '--listfiles', $package_name);
        if ($pid) {
            while (<$stdout>) {
                chomp;
                if (-f && -s && ! -l) {
                    my $digest = digest_file($_, 'SHA-1');
                    $file_names_by_hash{$digest} = $_;
                    $package_names_by_hash{$digest} = $package_name;
                }
            }
            close $stdout;
            waitpid($pid, 0);
        }
    }

    find({
        wanted => sub {
            return unless -f;
            return if -l;
            return if excludefile($_);

            my $digest = digest_file($_, 'SHA-1');
            if (exists $file_names_by_hash{$digest}) {
                my $target_path = $file_names_by_hash{$digest};
                my ($source_path) = $_ =~ m{debian/$package(/.*)};
                my $target_relpath = File::Spec->abs2rel($target_path, dirname($source_path));
                unlink($_) or error("Failed to delete $_: $!");
                symlink($target_relpath, $_) or error("Failed to create symbolic link: $!");
                my $dependency_name = $package_names_by_hash{$digest};
                $package_dependencies{$package}{$dependency_name} = 1;
            }
        },
        no_chdir => 1
    }, $path);
}

sub write_substvars {
    my ($package) = @_;
    my $dependencies = $package_dependencies{$package};

    while (my ($dependency, $versioned) = each %{$dependencies}) {
        if ($versioned) {
            my $stdout;
            my $pid = open2($stdout, undef,
                            'dpkg-query', '--show', '--showformat=${Version}', $dependency);
            waitpid($pid, 0);
            my $version = <$stdout>;
            $dependency = "$dependency (>= $version)";
        }
        addsubstvar($package, 'mkdocs:Depends', $dependency);
    }
}

=head1 SEE ALSO

L<debhelper(7)>, L<dh(1)>.

This program is meant to be used together with debhelper.

=head1 AUTHOR

Dmitry Shachnev <mitya57@debian.org>

=cut