File: build-pdf.pl

package info (click to toggle)
log4cplus 2.0.8-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,592 kB
  • sloc: cpp: 53,091; sh: 10,537; ansic: 1,845; python: 1,226; perl: 263; makefile: 209; xml: 85; objc: 59
file content (139 lines) | stat: -rwxr-xr-x 3,577 bytes parent folder | download | duplicates (2)
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
#! env perl

# This is a helper script for building log4cplus documentation PDF from several
# Markdown formatted files.

use strict;
use autodie qw(:all);
use File::Basename;
use File::Spec::Functions;
use File::Spec::Functions qw(rel2abs abs2rel);
use File::Glob qw(:globally);
use File::Slurp;
use Data::Dumper;
use Data::UUID;

my @FILES =
    ( 'README.md'
      , 'docs/examples.md'
      , 'docs/unicode.txt'
      , 'docs/release.txt'
      , 'AUTHORS'
      , 'LICENSE' );

my @PANDOC_1ST_STEP_SWITCHES =
    ( '--self-contained'
      , '--markdown-headings=atx'
      , '-f', 'markdown+smart'
      , '-t', 'markdown' );

my @PANDOC_2ND_STEP_SWITCHES =
    ( '--self-contained'
      , '--toc'
      , '--number-sections'
      # Qt4 / Win32 / MSVC has example that breaks listings on some versions of
      # TeXLive
      , '--listings'
      , '-f', 'markdown+smart'
      , '-t', 'latex',
      , '--pdf-engine=lualatex',
      , '--include-in-header=docs/latex-header.tex'
      , '--include-before-body=docs/latex-body.tex'
      , '-V', 'lang=en-US',
      , '-V', 'geometry:a4paper');

# pre-compute  various source information strings

my %sources = ();

foreach my $f (@FILES)
{
    my @parts = fileparse($f);
    my $rec = $sources{$f} = {};
    $rec->{file} = $parts[0];
    $rec->{abs_path} = rel2abs($f);
    $rec->{dir} = (fileparse($rec->{abs_path}))[1];
    $rec->{tmp_base} = $rec->{abs_path} . '.build-pdf';
    $rec->{md_tmp} = $rec->{tmp_base} . '.ref_rename_step';
    $rec->{cleanup_glob} = $rec->{abs_path} . '.*';
}

print Dumper(\%sources), "\n";

# unlink temporary files

foreach my $f (@FILES)
{
    my $rec = $sources{$f};
    my @junk = glob($rec->{cleanup_glob});
    print Dumper(\@junk), "\n";
    foreach my $file (@junk)
    {
        unlink $file;
    }
}

# pre-process Markdown to eliminate duplicate references

my $ug = new Data::UUID;

foreach my $f (@FILES)
{
    my $rec = $sources{$f};
    my $uuid = $ug->create();
    my $uuid_str = $ug->to_hexstring($uuid);
    my @args = (
        'pandoc', @PANDOC_1ST_STEP_SWITCHES,
        '--id-prefix=build-pdf:' . $uuid_str,
        '-o', $rec->{md_tmp},
        $rec->{abs_path});
    print "args: ", Dumper(@args), "\n";
    system(@args);
}

# produce LaTeX file containing contents of all documents

# collect all file parts
my @parts = ();
for my $f (@FILES)
{
    my $rec = $sources{$f};
    push @parts, abs2rel($rec->{md_tmp});
}

print Dumper(\@parts), "\n";

use constant README_MD_0_TEX => 'README.md.0.tex';
my @args = (
    'pandoc', @PANDOC_2ND_STEP_SWITCHES,
    '-o', README_MD_0_TEX,
    @parts);

print Dumper(\@args), "\n";

system(@args);

# process LaTeX source for non-Latin character ranges

# slurp file
my $contents = read_file(README_MD_0_TEX, binmode => ':utf8');
# search for character ranges and switch fonts for them
my $cjkCodePoint = qr/[\p{InCJKUnifiedIdeographs}]/u;
my $devanagariCodePoint = qr/[\p{Devanagari}]/u;
my $tamilCodePoint = qr/[\p{Tamil}]/u;
my $cyrilicCodePoint = qr/[\p{InCyrillic}\p{InCyrillicSupplementary}]/u;
$contents =~ s/(${cjkCodePoint}+)/{\\cjkfont{}$1}/g;
$contents =~ s/(${devanagariCodePoint}+)/{\\devanagarifont{}$1}/g;
$contents =~ s/(${tamilCodePoint}+)/{\\tamilfont{}$1}/g;
$contents =~ s/(${cyrilicCodePoint}+)/{\\cyrilicfont{}$1}/g;
use constant README_MD_TEX => 'README.md.tex';
write_file(README_MD_TEX, {binmode => ':utf8'}, $contents);

# produce PDF using latexmk utility to run the LuaLaTeX

@args = (
    'latexmk', '-gg', '-lualatex', README_MD_TEX);

print Dumper(\@args), "\n";

system(@args);