-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddr_latex.php
152 lines (139 loc) · 4.87 KB
/
ddr_latex.php
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
<?php
declare(strict_types=1);
include_once(__DIR__ . '/vendor/autoload.php');
use Psr\Log\LogLevel;
use Oeuvres\Kit\{Filesys, Log};
use Oeuvres\Kit\Logger\{LoggerCli};
use Oeuvres\Teinte\Format\{Tei};
// direct call as CLI
if (
php_sapi_name() == 'cli'
&& isset($argv[0])
&& realpath($argv[0]) == realpath(__FILE__)
) {
Rougemont::cli();
}
class Rougemont
{
/** Object to load Tei for exports */
static $tei;
/** Diectory where LaTeX should work */
static $work_dir;
public static function cli()
{
global $argv;
Log::setLogger(new LoggerCli(LogLevel::DEBUG));
if (!isset($argv[1])) {
die("usage: php ddr_latex.php livres/*.xml\n");
}
// drop $argv[0], $argv[1…] should be file
array_shift($argv);
self::$work_dir = __DIR__ . "/work/latex/";
self::$tei = new Tei();
// loop on arguments to get files of globs
foreach ($argv as $glob) {
foreach (glob($glob) as $tei_file) {
self::pdf($tei_file);
}
}
}
public static function bookurl($tei_file)
{
$tei_name = pathinfo($tei_file, PATHINFO_FILENAME);
list($tei_name) = explode("_", $tei_name);
$path = realpath($tei_file);
$bookurl = "https://unige.ch/rougemont/";
$folder = basename(dirname($path));
if (strpos($folder, 'livres') !== false) {
$bookurl .= "livres/".$tei_name.'/';
}
else if (strpos($folder, 'articles') !== false) {
$journal = explode('ddr-', $tei_name, 2)[1];
$bookurl .= "articles/".$journal.'/';
}
else if (strpos($folder, 'corr') !== false) {
$bookurl .= "correspondances/".$tei_name.'/';
}
}
public static function pdf($tei_file)
{
$bookurl = self::bookurl($tei_file);
// load TEI
self::$tei->load($tei_file);
$filename = pathinfo($tei_file, PATHINFO_FILENAME);
// Tex will need a lot of files, generate latex in a tmp dir
$dir = self::$work_dir . strtok($filename, '_');
Filesys::mkdir($dir);
$oldPath = getcwd();
chdir($dir); // change working directory
// booklet
$latex_file = "$dir/{$filename}_105x297.tex";
self::$tei->toUri(
'latex',
$latex_file,
[
'template' => __DIR__ . "/latex/rougemont_booklet.tex",
'latex.xsl' => __DIR__ . "/latex/rougemont_latex.xsl",
]
);
exec("latexmk -lualatex -quiet -f " . $latex_file);
// to build the booklet, apply a tex script to pdf
$tex = file_get_contents(__DIR__ .'/vendor/oeuvres/xsl/tei_latex/booklet_bind.tex');
// set the pdf file to transform (relative path)
$tex = str_replace('{thepdf}', "{{$filename}_105x297}", $tex);
// write the tex to process
$dst_booklet = "$dir/{$filename}_booklet";
file_put_contents("$dst_booklet.tex", $tex);
// works with -pdf only
exec("latexmk -pdf -quiet -f $dst_booklet.tex");
// A4 2 cols
/*
$latex_file = "$dir/$filename.tex";
self::$tei->toUri(
'latex',
$latex_file,
[
'template' => __DIR__ . "/rougemont_a4col2.tex",
'latex.xsl' => __DIR__ . "/rougemont_latex.xsl",
'bookurl' => $bookurl,
]
);
exec("latexmk -lualatex -quiet -f " . $latex_file);
*/
/*
// booklet
$latex_file = "$dir/{$filename}_105x297.tex";
self::$tei->toUri(
'latex',
$latex_file,
[
'template' => __DIR__ . "/piaget_booklet.tex",
'latex.xsl' => __DIR__ . "/piaget_latex.xsl",
]
);
exec("latexmk -lualatex -quiet -f " . $latex_file);
// to build the booklet, apply a tex script to pdf
$tex = file_get_contents(__DIR__ .'/vendor/oeuvres/xsl/tei_latex/booklet_bind.tex');
// set the pdf file to transform (relative path)
$tex = str_replace('{thepdf}', "{{$filename}_105x297}", $tex);
// write the tex to process
$dst_booklet = "$dir/{$filename}_booklet";
file_put_contents("$dst_booklet.tex", $tex);
// works with -pdf only
exec("latexmk -pdf -quiet -f $dst_booklet.tex");
// A5 for screen
$latex_file = "$dir/{$filename}_a5.tex";
self::$tei->toUri(
'latex',
$latex_file,
[
'template' => __DIR__ . "/piaget_a5.tex",
'latex.xsl' => __DIR__ . "/piaget_latex.xsl",
]
);
// exec("latexmk -xelatex -quiet -f " . $latex_file);
exec("latexmk -lualatex -quiet -f " . $latex_file);
*/
chdir($oldPath);
}
}