-
Notifications
You must be signed in to change notification settings - Fork 34
/
tex4ebook.4ht
473 lines (420 loc) · 15.2 KB
/
tex4ebook.4ht
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
%\NewConfigure{ncxtable}{}
%Configure date meta info
\def\:zeropadtwo#1{%
\ifnum #1<10 0\fi
#1% Then output the argument
}% --- \zeroPadTwo ---
\def\time:round:corr{\ifnum\the\numexpr\the\time-\the\time/60*60\relax<0 1\else 0\fi}
\def\:iso:date{%
\the\year-%
\:zeropadtwo{\the\month}-%
\:zeropadtwo{\the\day}%
%Timestamp will be zero seconds
T\:zeropadtwo{\the\numexpr(\the\time/60)-\time:round:corr\relax}:%
\:zeropadtwo{\the\numexpr\the\time-((\the\time/60)-\time:round:corr)*60\relax}:%
00Z}
%General configuration
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">}}
\NewConfigure{EpubVersion}{1}
\Configure{EpubVersion}{2.0}
\NewConfigure{UniqueIdentifier}{1}
\Configure{UniqueIdentifier}{http://example.com/\jobname}
% allow configuration of the document language. babel's main language is used by default
% use this only if it doesn't work
\NewConfigure{DocumentLanguage}[1]{\def\GetLanguage{#1}}
% definitions of commands ignored in \author
\def\a:@author{}
\NewConfigure{@author}[1]{\concat:config\a:@author{#1}}
\NewConfigure{OpfScheme}{1}
\Configure{OpfScheme}{URI}
\NewConfigure{OpfMetadata}[1]{
\ifx\relax#1\relax%
\edef\a:OpfMetadata{}
\else
\edef\a:OpfMetadata{\a:OpfMetadata\HCode{\Hnewline}#1}
\fi
}
\Configure{OpfMetadata}{}
% This commands add filenames to opf item list and spine
\edef\opf:htmlfiles{}
\newcount\opf:htmlid
\def\opf:registerfilename#1{%
\ifcsdef{opf:entry#1}{}{%
\global\advance\opf:htmlid by1\relax
%\ifcsdef{opf:entry#1}{}{\csxdef{opf:entry#1}{Neni opf}}
\csxdef{opf:entry#1}{file\the\opf:htmlid}
\xdef\opf:htmlfiles{\opf:htmlfiles, #1}
}
}
\newcommand\OpfRegisterFile[1][\FileName]{\opf:registerfilename{#1}}
\NewConfigure{OpfManifest}[1]{%
\ifx\relax#1\relax%
\edef\a:OpfManifest{}
\else
\edef\a:OpfManifest{\a:OpfManifest\HCode{\Hnewline}#1}
\fi
}
\Configure{OpfManifest}{}
% This is for add properties like toc or cover image in opf manifest
% Property must be added from document usinf \opf:add:property, current output filename is used
\NewConfigure{OpfItemProperties}{2}
\Configure{OpfItemProperties}{}{}
\def\opf:properties#1{%
\ifcsdef{opf:properties:#1}{
\a:OpfItemProperties\csuse{opf:properties:#1}\b:OpfItemProperties%
}{}%
}
\def\opf:add:property#1{%
\ifcsdef{opf:properties:\FileName #1}{}{%
\csxdef{opf:properties:\FileName #1}{#1}%
\ifcsdef{opf:properties:\FileName}{%
\edef\:opf:prop:tmp{\csuse{opf:properties:\FileName}}
\csxdef{opf:properties:\FileName}{\:opf:prop:tmp\space #1}
}{\csxdef{opf:properties:\FileName}{#1}}
}
}
% enable simple configuration from the .cfg file
\newcommand\OpfAddProperty[1]{\opf:add:property{#1}}
%Set property only once
\def\opf:set:property#1{}
% This code will refister \maketitle page to the opf file list
\let\oldmktl\a:mktl
\def\a:mktl{\oldmktl\opf:registerfilename{\FileName}}
% guide element is used in epub2, it is similar to epub3 properties
% in epub3, it is deprecated, but it can be present
\edef\opf:guide{\HCode{<guide>\Hnewline}}
% register file to the guide. first argument is optional filename, current
% output name is used when empty. second is title and third type
\NewConfigure{OpfGuide}{4}
\newcommand\OpfGuide[3][]{%
\ifx\relax#1\relax \let\opf:guide:file\FileName\else\def\opf:guide:file{#1}\fi
\edef\opf:guide{\opf:guide\a:OpfGuide#3\b:OpfGuide#2\c:OpfGuide\opf:guide:file\d:OpfGuide}
}
\Configure{OpfGuide}{\HCode{<reference type="}}{\HCode{" title="}}{\HCode{" href="}}{\HCode{" />\Hnewline}}
% is NavMap used anywhere?
\NewConfigure{NavMap}{2}
\NewConfigure{NavSection}{4}
%Interface for setting xmlns
\edef\t4ht:xmlns{}
\NewConfigure{xmlns}[2]{%
\ifx\relax#2\relax%
\edef\t4ht:xmlns{}%
\else%
%\def\:prefix:xmlns{\ifx\relax#1\relax\else:#1\fi}
\edef\t4ht:xmlns{\t4ht:xmlns xmlns\ifx\relax#1\relax\else :#1\fi="#2"}%
\fi%
}
\NewConfigure{CoverImage}{2}
\Configure{CoverImage}{%
\ifvmode\IgnorePar\fi\EndP\HCode{<div class="cover-image">\Hnewline}
}{\HCode{</div>\Hnewline}\par}
\def\CoverMetadata#1{%
\special{t4ht+@File: #1}%
\Configure{OpfMetadata}
{\HCode{<meta name="cover" content="cover-image" />\Hnewline}}
\Configure{OpfManifest}
{\HCode{<item id="cover-image"
href="#1" media-type="\a:CoverMimeType" />}}
}
\renewcommand\coverimage[2][]{%
\edef\CoverImageName{#2}%
\CoverMetadata{#2}
\a:CoverImage%
\includegraphics[#1]{#2}%
\b:CoverImage%
}
\renewcommand\epubpage{\a:epubpage}
\NewConfigure{epubpage}{1}
\Configure{epubpage}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="newpage"></div>}}
\Css{.newpage{page-break-before:always;}}
\NewConfigure{CoverMimeType}{1}
\Configure{CoverMimeType}{image/png}
\TocAt-{part,chapter,section,subsection,subsubsection}
\Configure{crosslinks*}{}
\NewConfigure{NcxDoctype}{1}
\Configure{NcxDoctype}{\HCode{<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"\Hnewline
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">\Hnewline}}
%Get sanitized value of \title
\ifTag{TITLE+}{\def\Title{\LikeRef{TITLE+}}}{}
% If title contains macros, SafeTitle should be configured
% in the custom cfg file
\NewConfigure{SafeTitle}[1]{\gdef\Title{#1}}
% add main html as first file in the OPF
\opf:registerfilename{\jobname.\:html} %
% register all sectioning commands in the OPF
\def\patch:sec:register#1{%
\csgappto{c:#1}{\OpfRegisterFile}%
}
\patch:sec:register{part}
\patch:sec:register{chapter}
\patch:sec:register{section}
\patch:sec:register{subsection}
\patch:sec:register{subsubsection}
\patch:sec:register{likechapter}
\patch:sec:register{likesection}
\patch:sec:register{likesubsection}
\patch:sec:register{likesubsubsection}
\patch:sec:register{appendix}
\patch:sec:register{appendixsec}
\patch:sec:register{appendixsubsec}
\patch:sec:register{appendixsubsubsec}
\:CheckOption{epub}
\if:Option
%\Configure{crosslinks+}{}{}{}{}
% add explicit table of contents to OPF
\let\old:toc\a:tableofcontents
\def\a:tableofcontents{\old:toc\OpfRegisterFile}
\Configure{()}{$}{$}
\fi
\:CheckOption{kindle} \if:Option
\fi
%%
% We must test, it tableofcontents is empty, if yes, we must take special care to navmap
\newbool{tocnoempty}
\newcount\navpoint
\def\stepnavpoint{\advance\navpoint by1\relax\the\navpoint}
% define points for breaking section units to html pages
% we need to break long documents to smaller chunks, because e-readers are
% generally pretty weak devices
% use no-cut option if you don't want to break sections, or if you
% use your own cutting scheme in your .cfg file
\:CheckOption{no-cut}\if:Option\else
\ifdef\chapter{%
%\CutAt{chapter}\CutAt{likechapter}
% copied from tex4ht-html4.tex
\CutAt{part,appendix}
\CutAt{chapter,likechapter,appendix,part}
\CutAt{likechapter,appendix,part}
\CutAt{appendix,chapter,likechapter,part}
}{%
%\CutAt{section}\CutAt{likesection}}
\CutAt{section,likesection,part}
\CutAt{likesection,part}
}
\fi
% Commands for support of hiearchical table of contents
% Support for: chapter, section, subsection
\def\resettoclevels#1{%
\def\do##1{\csgdef{nav##1finish}{}}%
\docsvlist{#1}%
}
\def\usetoclevels#1{%
\def\do##1{\csuse{nav##1finish}}%
\docsvlist{#1}%
}
\def\finishtoclevel#1{%
\csgdef{nav#1finish}{\d:NavSection}%
}
\def\closelevels#1{%
\expandafter\usetoclevels\expandafter{#1}%
\expandafter\resettoclevels\expandafter{#1}%
}%
% 1 - section type 2 - closed sections
\def\navsection#1#2{%
\ConfigureToc{#1}%
{\expandafter\closelevels\expandafter{#2}%
\def\curr:sect:type{#1}\a:NavSection\Ncx:Mark}%
{\c:NavSection}%
{}%
{\b:NavSection%
\expandafter\finishtoclevel\expandafter{#1}%
}
}
% define closing groups for all levels defined in \Configure{resettoclevels}
\def\tmp:processlevel#1{%
\bgroup%
\csxdef{tmp:resettoclevels#1}{}%
\def\do##1{\csxdef{tmp:resettoclevels#1}{\csname tmp:resettoclevels#1\endcsname ##1,like##1,\appendix:type{##1}}}%
\def\tmp:parse##1#1##2\nil{%
\docsvlist{#1##2}%
}
\expandafter\tmp:parse\a:resettoclevels\nil%
\egroup%
\navsection{#1}{\csname tmp:resettoclevels#1\endcsname}%
% define hierarchy also for starred sections
\navsection{like#1}{\csname tmp:resettoclevels#1\endcsname}%
% and for appendices
\ifcsdef{appendix:type:#1}{\navsection{\csuse{appendix:type:#1}}{\csname tmp:resettoclevels#1\endcsname}}{}%
}
% define children levels for each sectioning level
\def\confnavsections{%
\def\do##1{%
\tmp:processlevel{##1}%
}
\expandafter\docsvlist\expandafter{\a:resettoclevels}
}
% As reported by Nat Kuhn on http://tex.stackexchange.com/q/209312/2891
% zeroes were printed as section numbers when secnumdepth counter was set to negative value
% so just strip then out
% \TitleMark does similar thing, but this solution is simpler, I hope it works correctly
\def\Ncx:Mark#1{%
\edef\:temp{#1}%
\def\z@ro{0}%
\ifx\:temp\z@ro\else#1\fi%
}
% to create correct hiearchical NCX for appendices, we register corresponding appendix types
% for various section types
\def\appendix:type#1{\ifcsdef{appendix:type:#1}{\csuse{appendix:type:#1},}{}}
\def\def:appendix:type#1#2{\csdef{appendix:type:#1}{#2}}
\def:appendix:type{chapter}{appendix}
\def:appendix:type{section}{appendixsec}
\def:appendix:type{subsection}{appendixsubsec}
\def:appendix:type{subsubsection}{appendixsubsubsec}
%
\def\ncx:head{%
\HCode{<head>\Hnewline}%
\HCode{<meta name="dtb:uid" content="\a:UniqueIdentifier"/>\Hnewline}%
\HCode{<meta name="dtb:depth" content="3"/>\Hnewline}%
\HCode{<meta name="dtb:totalPageCount" content="0"/>\Hnewline}%
\HCode{<meta name="dtb:maxPageNumber" content="0"/>\Hnewline}%
\HCode{</head>\Hnewline}%
}
\def\ncx:title{%
\HCode{<docTitle>\Hnewline<text>}\Title\HCode{</text>\Hnewline</docTitle>\Hnewline}%
}
% Get filename from tableofcontents anchor
\def\ncx:hfile#1{\LikeRef{)F\LikeRef{)Q#1}F-}}
\ExplSyntaxOn
% get filename for the section label
% sometimes, TeX4ht returns list of file numbers for label. we must use just
% the first number. we use the LaTeX 3 sequence list to get it.
\tl_new:N\ncx:hfilename
\def\ncx:newhfile#1{
\cs_if_exist:cTF{cw:)Q#1}{%
% cw:)Q#1 is csname of tag from the xref file. we convert it to sequence
\seq_set_from_clist:Nc\l_tmpa_seq{cw:)Q#1}%
% get first item and put it to a token list
\seq_get_left:NN \l_tmpa_seq \l_tmpa_tl%
% \RefFileNumber returns file name for the given file number
% \ncx:hfilename contains the filename for later use
\tl_set:Nx \ncx:hfilename {\RefFileNumber{\l_tmpa_tl}}%
}{% This shouldn't happen?
\tl_set:Nn \ncx:hfilename {}%
}
}
\ExplSyntaxOff
% define toc levels which should be included in the NCX file
\NewConfigure{resettoclevels}{1}
\Configure{resettoclevels}{part,appendix,chapter,section,subsection,subsubsection,paragraph}
\def\:tempa{%
\EndP%
\HtmlParOff%
\setcounter{tocdepth}{3}%
\special{t4ht>\jobname.ncx}%
% We don't want crosslinks in xml
% Basic sctructure of the ncx file
\NoFonts%
{\HCode{<?xml version="1.0" encoding="utf-8"?>\Hnewline}}%
{\a:NcxDoctype}%
{\Tg<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">}{}%
% To print document map, we customize the tableofcontents. We don't want TOC title,
% so:
\let\contentsname=\empty%
\Configure{tableofcontents}{\boolfalse{tocnoempty}\Tg<navMap>}{%
\usetoclevels{part,appendix,chapter,likechapter,appendixsec,section,likesection,subsection,appendixsubsec,likesubsection,subsubsection,likesubsubsection,appendixsubsubsec,subsubsubsection,paragraph}%
\ifbool{tocnoempty}{}%
{\HCode{<navPoint id="mainentry" playOrder="1">%
<navLabel><text>Document</text></navLabel>%
<content src="\jobname.\:html" />%
</navPoint>}}%
\Tg</navMap>}{}{}{}%
% We need to configure TocLink
% in navmapsrc is link to the file and anchor, where chapter or section is located
\def\navmapsrc{}%
\Configure{TocLink}{%
\ncx:newhfile{##2}%
\def\navmapsrc{\ncx:hfilename\:sharp ##2}%
\opf:registerfilename{\ncx:hfilename}%
##4%
}
% Configuraion of entries
\expandafter\resettoclevels\expandafter{\a:resettoclevels}%
\confnavsections%
\HtmlParOff%
\Configure{toTocLink}{}{}%
\Configure{NavSection}{\booltrue{tocnoempty}\HCode{\Hnewline<navPoint id="navPoint-}%
\stepnavpoint\HCode{" playOrder="}%
\the\navpoint\HCode{">\Hnewline<navLabel>\Hnewline<text><navmark type="\curr:sect:type">}%
}{\HCode{</text>\Hnewline%
</navLabel>\Hnewline}%
\HCode{<content src="\navmapsrc" />}%
}{\HCode{</navmark>}}{\HCode{</navPoint>\Hnewline}}%
% Meta inf
\ncx:head%
% Book title
\ncx:title%
\TableOfContents[part,appendix,chapter,likechapter,appendixsec,section,likesection,appendixsubsec,subsection,likesubsection,subsubsection,likesubsubsection]%
%Hack to get close tag working
\HCode{</ncx>}%
\EndNoFonts
\special{t4ht<\jobname.ncx}
\HtmlParOn
}
\let\ncxtable\:tempa
%\else
%\fi
\def\print:opf:scheme#1{ opf:scheme="#1"}
\def\:tempa{%
% French language definition makes the : character active, which can cause compilation error
\ifdefined\XeTeXcharclass\XeTeXcharclass `\: \z@\fi%
\bgroup
\setcounter{tocdepth}{3}
% Basic sctructure of the opf file
\special{t4ht>content.opf}
\NoFonts
\HtmlParOff
{\HCode{<?xml version="1.0" encoding="utf-8"?>\Hnewline}}
\HCode{<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="dcidid"
version="\a:EpubVersion">\Hnewline}
{\HCode{<metadata\Hnewline xmlns:dc="http://purl.org/dc/elements/1.1/"\Hnewline xmlns:opf="http://www.idpf.org/2007/opf">\Hnewline}}
{\HCode{<dc:title>}\Title\HCode{</dc:title>\Hnewline}}
{\HCode{<dc:language>}\GetLanguage\HCode{</dc:language>\Hnewline}}
%Add interface for such things
{\HCode{<dc:identifier id="dcidid" \print:opf:scheme\a:OpfScheme>\a:UniqueIdentifier}\HCode{</dc:identifier>\Hnewline}}
\def\and{\HCode{</dc:creator>\Hnewline<dc:creator>}}
{\HCode{<dc:creator>}{\a:@author\Author}\HCode{</dc:creator>\Hnewline}}
%{\HCode{<dc:contributor>\HP:file</dc:contributor>\Hnewline}}
{\HCode{<dc:date>}\:iso:date\HCode{</dc:date>}}
\a:OpfMetadata
\Tg</metadata>
% To print document map, we customize the tableofcontents. We don't want TOC title,
% so:
\let\contentsname=\empty
%\Configure{tableofcontents}{\Tg<navMap>}{\usetoclevels{part,chapter,section,subsection,subsubsection}\Tg</navMap>}{}{}{}
% We need to configure TocLink
% in navmapsrc is link to the file and anchor, where chapter or section is located
\def\navmapsrc{}
\HCode{\Hnewline<manifest>\Hnewline}
\HCode{<item id="ncx" href="\jobname.ncx" media-type="application/x-dtbncx+xml" />\Hnewline}
\HCode{<item id="stylesheet" href="\jobname.css" media-type="text/css"/>\Hnewline}
\renewcommand\do[1]{\HCode{<item id="\csuse{opf:entry##1}" \opf:properties{##1} href="##1" media-type="application/xhtml+xml" />\Hnewline}}
\expandafter\docsvlist\expandafter{\opf:htmlfiles}
\a:OpfManifest
%\Tg<manifest>
\special{t4ht<content.opf}
\special{t4ht>content-part2.opf}
\HCode{</manifest>\Hnewline}
\renewcommand\do[1]{\HCode{<itemref idref="\csuse{opf:entry##1}" />\Hnewline}}
\HCode{\Hnewline<spine toc="ncx">\Hnewline}
\expandafter\docsvlist\expandafter{\opf:htmlfiles}
% Next line is pattern to be replaced by lua code
\HCode{\Hnewline$\string{spine\string}\Hnewline}
\HCode{</spine>\Hnewline}
% insert guide
\opf:guide
\HCode{</guide>}
% Hack to get close tag working
\HCode{</package>}
\special{t4ht<content-part2.opf}
\HtmlParOn
\EndNoFonts
\egroup
%Hack!!
%\Configure{BODY}{}{\Tg</body>}
%\Configure{HTML}{}{\HCode{\Hnewline</html>\Hnewline}}%<!-- -->\Hnewline}}
}
\let\opftable\:tempa
\:CheckOption{epub3}
\if:Option
\input tex4ebook-epub3.4ht
\fi