You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.7 KiB
72 lines
1.7 KiB
2 years ago
|
#!/usr/bin/perl
|
||
|
|
||
|
# Script taken from https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex
|
||
|
# Manually adapt for sphinxdoc generated pdf
|
||
|
use 5.024;
|
||
|
use strict;
|
||
|
use warnings qw(all);
|
||
|
use IPC::Run3;
|
||
|
use LaTeX::Encode;
|
||
|
use Encode qw(decode);
|
||
|
|
||
|
my @levels
|
||
|
= qw(chapter section subsection subsubsection paragraph subparagraph);
|
||
|
my @counters;
|
||
|
|
||
|
my ($data_enc, $data);
|
||
|
run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, \$data_enc;
|
||
|
$data = decode('UTF-8', $data_enc, Encode::FB_CROAK);
|
||
|
|
||
|
my @latex_bm;
|
||
|
|
||
|
my $bm;
|
||
|
foreach (split(/\n/, $data)) {
|
||
|
/^Bookmark/ or next;
|
||
|
if (/^BookmarkBegin$/) {
|
||
|
add_latex_bm($bm) if $bm;
|
||
|
$bm = {};
|
||
|
} elsif (/^BookmarkLevel: (\d+)$/a) {
|
||
|
++$counters[$1 - 1];
|
||
|
$#counters = $1 - 1;
|
||
|
$bm->{number} = join(q{.}, @counters);
|
||
|
$bm->{level} = $1 - 1;
|
||
|
} elsif (/^BookmarkTitle: (.+)$/) {
|
||
|
# In Sphinx title include a utf-8 icon of a link, so remove it
|
||
|
my $title = substr($1, 0, -1);
|
||
|
$bm->{title} = latex_encode($title);
|
||
|
} elsif (/^BookmarkPageNumber: (\d+)$/a) {
|
||
|
$bm->{page} = $1;
|
||
|
} else {
|
||
|
die "Unknown Bookmark tag in $_\n";
|
||
|
}
|
||
|
}
|
||
|
add_latex_bm($bm) if $bm;
|
||
|
|
||
|
print <<LATEX;
|
||
|
\\documentclass{report}
|
||
|
\\title{Guide de montage Vhéliotech v1.0}
|
||
|
\\begin{document}
|
||
|
${ \join('', @latex_bm) }
|
||
|
\\end{document}
|
||
|
LATEX
|
||
|
|
||
|
exit 0;
|
||
|
|
||
|
sub add_latex_bm {
|
||
|
my $bm = shift;
|
||
|
# Don't extract top level title and subtitle with level > 2
|
||
|
if ($bm->{level} == 0 || $bm->{level} > 2) {
|
||
|
return;
|
||
|
}
|
||
|
# Make sections become chapters
|
||
|
my $level = $levels[$bm->{level} - 1];
|
||
|
# Remove the first number because it refers to the root title (ie always 1.X)
|
||
|
my $number = substr($bm->{number}, 2);
|
||
|
my $title = $bm->{title};
|
||
|
my $page = $bm->{page};
|
||
|
|
||
|
push @latex_bm, <<LINE;
|
||
|
\\contentsline {$level}{\\numberline {$number}$title}{$page}%
|
||
|
LINE
|
||
|
}
|