Repository navigation
Expand file tree
/
Copy pathparser_structural.go
More file actions
494 lines (469 loc) · 17.7 KB
/
Copy pathparser_structural.go
File metadata and controls
494 lines (469 loc) · 17.7 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package main
import (
"fmt"
"path/filepath"
"strings"
)
func blockquoteFenceHandler() lineHandler {
return lineHandler{
match: func(line string, insideBlock bool) bool { return reBlockQuote.MatchString(line) },
handle: func(_ *parseContext, line string, _ bool) (string, bool) {
if inBlockType > 0 {
logMsg(LogVerbose, "blockQuote schließen")
inBlockType = 0
return "</blockquote>\n", true
}
matches := reBlockQuote.FindStringSubmatch(line)
blocktype := "code"
inBlockType = BLOCKTYPE_CODE
if len(matches) == 2 && matches[1] != "" {
blocktype = strings.ToLower(matches[1])
switch blocktype {
case "cite":
inBlockType = BLOCKTYPE_CITE
case "note":
inBlockType = BLOCKTYPE_NOTE
case "info":
inBlockType = BLOCKTYPE_INFO
case "warn":
inBlockType = BLOCKTYPE_WARN
}
}
logMsg(LogVerbose, "blockQuote opening: %s", blocktype)
return fmt.Sprintf("<blockquote class=\"%s\">\n", blocktype), true
},
}
}
func blockquoteContentHandler() lineHandler {
return lineHandler{
match: func(line string, insideBlock bool) bool {
return inBlockType != BLOCKTYPE_NONE && !insideBlock
},
handle: func(ctx *parseContext, line string, _ bool) (string, bool) {
if inBlockType == BLOCKTYPE_CODE {
logMsg(LogVerbose, "blockQuote CODE line")
return fmt.Sprintf("%s<br/>\n", line), true
}
logMsg(LogVerbose, "blockQuote non CODE but parsed line")
return fmt.Sprintf("%s<br/>\n", parseLine(ctx, line, true)), true
},
}
}
func chapterHandler() lineHandler {
return lineHandler{
match: func(line string, insideBlock bool) bool { return reChapter.MatchString(line) },
handle: func(ctx *parseContext, line string, _ bool) (string, bool) {
if currentChapterTitle != "" {
appendPendingFootnotes(ctx)
addChapter(ctx, currentChapterTitle, currentChapterNumber[1], currentChapterContent)
}
matches := reChapter.FindStringSubmatch(line)
currentChapterTitle = parseLine(ctx, matches[2], true)
currentChapterContent.Reset()
currentChapterNumber[1]++
filename := fmt.Sprintf("xhtml/chapter_%05d.xhtml", currentChapterNumber[1])
ctx.currentChapterFile = filename
// The first real chapter marks where body content begins.
if !startReadingSet {
ctx.book.SetStartReading(filename)
startReadingSet = true
}
currentNavpoint[1] = ctx.book.AddNavpoint(currentChapterTitle, filename, 10)
firstparagraph = true
return fmt.Sprintf("<h1 id=\"label1_%d\">%s</h1>\n", currentChapterNumber[1], parseLine(ctx, matches[2], true)), true
},
}
}
// anchorDefHandler renders {#id} as an invisible span with that id.
// Matches inside backtick code spans are left untouched.
func anchorDefHandler() lineHandler {
return lineHandler{
match: func(line string, _ bool) bool { return matchOutsideBackticks(line, reAnchorDef) },
handle: func(ctx *parseContext, line string, insideBlock bool) (string, bool) {
out := replaceOutsideBackticks(line, reAnchorDef, func(sub []string) string {
return fmt.Sprintf(`<span id="%s"></span>`, sub[1])
})
return parseLine(ctx, out, insideBlock), true
},
}
}
// anchorLinkHandler renders [text](#id) as an internal epub hyperlink.
// Matches inside backtick code spans are left untouched.
func anchorLinkHandler() lineHandler {
return lineHandler{
match: func(line string, _ bool) bool { return matchOutsideBackticks(line, reAnchorLink) },
handle: func(ctx *parseContext, line string, insideBlock bool) (string, bool) {
out := replaceOutsideBackticks(line, reAnchorLink, func(sub []string) string {
href := resolveAnchorHref(ctx, sub[2], ctx.currentChapterFile)
return fmt.Sprintf(`<a href="%s">%s</a>`, href, sub[1])
})
return parseLine(ctx, out, insideBlock), true
},
}
}
// indexEntryHandler renders %[displayTerm](indexname) or %[displayTerm](indexname|canonical)
// as a classed span with a stable id. The display term appears in the text; the canonical
// term (after |) is used as the index key for grouping variants like singular/plural.
// Matches inside backtick code spans are left untouched.
func indexEntryHandler() lineHandler {
return lineHandler{
match: func(line string, _ bool) bool { return matchOutsideBackticks(line, reIndexEntry) },
handle: func(ctx *parseContext, line string, insideBlock bool) (string, bool) {
out := replaceOutsideBackticks(line, reIndexEntry, func(sub []string) string {
displayTerm, indexName, canonical := sub[1], sub[2], sub[3]
if canonical == "" {
canonical = displayTerm
}
key := indexName + "\x00" + canonical
seq := indexCounters[key]
indexCounters[key]++
htmlID := fmt.Sprintf("idx-%s-%s-%d", sanitizeID(indexName), sanitizeID(canonical), seq)
return fmt.Sprintf(`<span id="%s" class="index-entry" epub:type="index-term">%s</span>`, htmlID, displayTerm)
})
return parseLine(ctx, out, insideBlock), true
},
}
}
// indexOutputHandler renders %index[name] or %index[name](Title) as a new chapter.
// Entries are grouped by their canonical term; each group shows the canonical term
// as a label followed by links to every occurrence in the text.
func indexOutputHandler() lineHandler {
return lineHandler{
match: func(line string, _ bool) bool { return reIndexOutput.MatchString(line) },
handle: func(ctx *parseContext, line string, _ bool) (string, bool) {
sub := reIndexOutput.FindStringSubmatch(line)
indexName, title := sub[1], sub[2]
if title == "" {
title = indexName
}
entries, ok := indexes[indexName]
if !ok || len(entries) == 0 {
logMsg(LogDefault, "WARNING: no index entries found for %q", indexName)
return "", true
}
// Flush current chapter before starting the index chapter.
if currentChapterTitle != "" {
appendPendingFootnotes(ctx)
addChapter(ctx, currentChapterTitle, currentChapterNumber[1], currentChapterContent)
currentChapterTitle = ""
currentChapterContent.Reset()
}
currentChapterNumber[1]++
filename := fmt.Sprintf("xhtml/chapter_%05d.xhtml", currentChapterNumber[1])
ctx.currentChapterFile = filename
// Group entries by canonical term, preserving first-seen order.
type group struct {
canonical string
entries []indexEntry
}
seen := map[string]int{}
var groups []group
for _, e := range entries {
if idx, exists := seen[e.canonical]; exists {
groups[idx].entries = append(groups[idx].entries, e)
} else {
seen[e.canonical] = len(groups)
groups = append(groups, group{canonical: e.canonical, entries: []indexEntry{e}})
}
}
// indexHref returns the correct href for an index entry link.
// In AZW3 mode all chapters form one document, so just use #id.
indexHref := func(e indexEntry) string {
if ctx.azw3Mode || e.chapterFile == filename {
return "#" + e.htmlID
}
return "../" + e.chapterFile + "#" + e.htmlID
}
var body strings.Builder
if ctx.azw3Mode {
body.WriteString(fmt.Sprintf("<section>\n<h1 id=\"label1_%d\">%s</h1>\n<ul class=\"index-list\">\n", currentChapterNumber[1], title))
} else {
body.WriteString(fmt.Sprintf("<section epub:type=\"index\">\n<h1 id=\"label1_%d\">%s</h1>\n<ul epub:type=\"index-entry-list\" class=\"index-list\">\n", currentChapterNumber[1], title))
}
for i, g := range groups {
if len(g.entries) == 1 {
e := g.entries[0]
if ctx.azw3Mode {
body.WriteString(fmt.Sprintf(" <li><span>%s</span> <a href=\"%s\">1</a></li>\n", g.canonical, indexHref(e)))
} else {
body.WriteString(fmt.Sprintf(" <li epub:type=\"index-entry\"><span epub:type=\"index-term\">%s</span> <a epub:type=\"index-locator\" href=\"%s\">1</a></li>\n", g.canonical, indexHref(e)))
}
} else {
// Multiple occurrences: list canonical term once, link each occurrence.
if ctx.azw3Mode {
body.WriteString(fmt.Sprintf(" <li><span class=\"index-canonical\">%s</span>\n <ul>\n", g.canonical))
} else {
body.WriteString(fmt.Sprintf(" <li epub:type=\"index-entry\"><span epub:type=\"index-term\" class=\"index-canonical\">%s</span>\n <ul epub:type=\"index-locator-list\">\n", g.canonical))
}
for j, e := range g.entries {
body.WriteString(fmt.Sprintf(" <li><a href=\"%s\">%d</a></li>\n", indexHref(e), j+1))
}
body.WriteString(" </ul>\n </li>\n")
}
_ = i
}
body.WriteString("</ul>\n</section>\n")
var htmlContent string
if ctx.azw3Mode {
// AZW3: plain fragment only, no document wrapper.
htmlContent = body.String()
} else {
var customCSSLinks string
for _, p := range ctx.customCSSPaths {
customCSSLinks += "\n\t\t<link rel=\"stylesheet\" href=\"../" + p + "\"/>"
}
htmlContent = `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>` + title + `</title>` + customCSSLinks + `
</head>
<body>
` + body.String() + `
</body>
</html>`
}
if _, err := ctx.book.AddXHTML(filename, title, htmlContent, 10); err != nil {
logMsg(LogDefault, "ERROR: writing index chapter %s: %v", filename, err)
}
currentNavpoint[1] = ctx.book.AddNavpoint(title, filename, 10)
logMsg(LogDefault, "Add index %q (%s) as %s", indexName, title, filename)
return "", true
},
}
}
// tocOutputHandler renders %toc or %toc(Title) as a generated table of
// contents chapter at the position of the command. The optional title makes
// localization easy, e.g. %toc(Inhaltsverzeichnis). The TOC lists every
// chapter and subchapter of the whole book (collected in Pass 1), nested by
// heading level, and works identically for EPUB and AZW3.
func tocOutputHandler() lineHandler {
return lineHandler{
match: func(line string, _ bool) bool { return reTocOutput.MatchString(line) },
handle: func(ctx *parseContext, line string, _ bool) (string, bool) {
sub := reTocOutput.FindStringSubmatch(line)
title := sub[1]
if title == "" {
title = "Table of Contents"
}
if len(tocEntries) == 0 {
logMsg(LogDefault, "WARNING: %%toc found but the book has no chapters")
return "", true
}
// Flush current chapter before starting the TOC chapter.
if currentChapterTitle != "" {
appendPendingFootnotes(ctx)
addChapter(ctx, currentChapterTitle, currentChapterNumber[1], currentChapterContent)
currentChapterTitle = ""
currentChapterContent.Reset()
}
currentChapterNumber[1]++
filename := fmt.Sprintf("xhtml/chapter_%05d.xhtml", currentChapterNumber[1])
ctx.currentChapterFile = filename
// tocHref returns the link target for a heading. In AZW3 mode all
// chapters form one document, so links are plain #label anchors
// (resolved to exact positions by the KF8 writer). In EPUB mode
// chapters link to their file, subchapters to file#label.
tocHref := func(e tocEntry) string {
if ctx.azw3Mode {
return "#" + e.label
}
if e.level == 1 {
return "../" + e.chapterFile
}
return "../" + e.chapterFile + "#" + e.label
}
var body strings.Builder
body.WriteString(fmt.Sprintf("<section>\n<h1 id=\"label1_%d\">%s</h1>\n", currentChapterNumber[1], title))
body.WriteString("<ol class=\"toc-list\">\n")
level := 1
openLi := false
for _, e := range tocEntries {
if e.level > level {
// Nest deeper inside the currently open list item.
for level < e.level {
body.WriteString("\n<ol>\n")
level++
}
openLi = false
} else {
if openLi {
body.WriteString("</li>\n")
}
for level > e.level {
body.WriteString("</ol>\n</li>\n")
level--
}
}
body.WriteString(fmt.Sprintf("<li><a href=\"%s\">%s</a>", tocHref(e), parseLine(ctx, e.title, true)))
openLi = true
}
if openLi {
body.WriteString("</li>\n")
}
for level > 1 {
body.WriteString("</ol>\n</li>\n")
level--
}
body.WriteString("</ol>\n</section>\n")
var htmlContent string
if ctx.azw3Mode {
// AZW3: plain fragment only, no document wrapper.
htmlContent = body.String()
} else {
var customCSSLinks string
for _, p := range ctx.customCSSPaths {
customCSSLinks += "\n\t\t<link rel=\"stylesheet\" href=\"../" + p + "\"/>"
}
htmlContent = `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>` + title + `</title>` + customCSSLinks + `
</head>
<body>
` + body.String() + `
</body>
</html>`
}
if _, err := ctx.book.AddXHTML(filename, title, htmlContent, 10); err != nil {
logMsg(LogDefault, "ERROR: writing TOC chapter %s: %v", filename, err)
}
currentNavpoint[1] = ctx.book.AddNavpoint(title, filename, 10)
firstparagraph = true
logMsg(LogDefault, "Add table of contents %q as %s", title, filename)
return "", true
},
}
}
func headlineHandler() lineHandler {
return lineHandler{
match: func(line string, insideBlock bool) bool { return reHeadlines.MatchString(line) },
handle: func(ctx *parseContext, line string, _ bool) (string, bool) {
matches := reHeadlines.FindStringSubmatch(line)
chapterLevel := strings.Count(matches[1], "#")
currentChapterNumber[chapterLevel]++
currentChapterLabel := fmt.Sprintf("label%d_%d", chapterLevel, currentChapterNumber[chapterLevel])
firstparagraph = true
if currentNavpoint[chapterLevel-1] != nil {
anchorname := fmt.Sprintf("xhtml/chapter_%05d.xhtml#%s", currentChapterNumber[1], currentChapterLabel)
currentNavpoint[chapterLevel] = currentNavpoint[chapterLevel-1].AddNavpoint(parseLine(ctx, matches[2], true), anchorname, 0)
logMsg(LogVerbose, "Add subchapter %s as %s", matches[2], anchorname)
} else {
logMsg(LogVerbose, "Subchapter %s outside chapter", matches[2])
}
return fmt.Sprintf("<h%d id=\"%s\">%s</h%d>\n", chapterLevel, currentChapterLabel, parseLine(ctx, matches[2], true), chapterLevel), true
},
}
}
func metaHandler() lineHandler {
return lineHandler{
match: func(line string, insideBlock bool) bool { return reMeta.MatchString(line) },
handle: func(ctx *parseContext, line string, _ bool) (string, bool) {
matches := reMeta.FindStringSubmatch(line)
if len(matches) < 2 {
logMsg(LogDefault, "Error setting meta %s to %s", matches[1], matches[2])
currentChapterContent.WriteString("<p>" + line + "</p>\n")
return "", true
}
switch matches[1] {
case "title":
ctx.book.SetTitle(matches[2])
case "author":
ctx.book.AddAuthor(matches[2])
case "series":
if err := ctx.book.SetSeries(matches[2]); err != nil {
logMsg(LogDefault, "ERROR: Add series to %s: %v", matches[2], err)
}
case "set":
if err := ctx.book.SetSet(matches[2]); err != nil {
logMsg(LogDefault, "ERROR: Add set to %s: %v", matches[2], err)
}
case "entry":
if err := ctx.book.SetEntryNumber(matches[2]); err != nil {
logMsg(LogDefault, "ERROR: Add entry number to %s: %v", matches[2], err)
}
case "uuid":
if err := ctx.book.SetUUID(matches[2]); err != nil {
logMsg(LogDefault, "ERROR: Set UUID to %s: %v", matches[2], err)
}
case "language":
if err := ctx.book.AddLanguage(matches[2]); err != nil {
logMsg(LogDefault, "ERROR: Add language to %s: %v", matches[2], err)
}
case "date":
ctx.book.AddDate(matches[2])
case "rights":
ctx.book.AddRights(matches[2])
case "source":
ctx.book.AddSource(matches[2])
case "relation":
ctx.book.AddRelation(matches[2])
case "type":
ctx.book.AddType(matches[2])
case "quotes":
quotes := strings.Split(matches[2], ",")
if len(quotes) != 4 {
logMsg(LogDefault, "ERROR: quotes definition has to have 4 values seperated by a colon %s %v", matches[2], quotes)
} else {
laquo = quotes[0]
raquo = quotes[1]
lsaquo = quotes[2]
rsaquo = quotes[3]
}
}
return "", true
},
}
}
func coverHandler() lineHandler {
return lineHandler{
match: func(line string, insideBlock bool) bool { return reCover.MatchString(line) },
handle: func(ctx *parseContext, line string, _ bool) (string, bool) {
matches := reCover.FindStringSubmatch(line)
if err := addCover(ctx.book, matches[1], ctx.baseDir, *generateCover); err != nil {
logMsg(LogDefault, "Error including image %s with URI %s: %v", matches[0], filepath.Join(ctx.baseDir, matches[1]), err)
}
return "", true
},
}
}
func imageHandler() lineHandler {
return lineHandler{
match: func(line string, insideBlock bool) bool { return reImage.MatchString(line) },
handle: func(ctx *parseContext, line string, _ bool) (string, bool) {
transformed := reImage.ReplaceAllStringFunc(line, func(match string) string {
matches := reImage.FindStringSubmatch(match)
if len(matches) < 2 {
logMsg(LogDefault, "Error including %s with URI %s", matches[0], matches[2])
return match
}
firstparagraph = true
currentImageId++
currentImage := fmt.Sprintf("img/image_%05d%s", currentImageId, filepath.Ext(matches[2]))
imageID, err := ctx.book.AddImageFile(filepath.Join(ctx.baseDir, matches[2]), currentImage)
if err != nil {
logMsg(LogDefault, "Error including image %s with URI %s: %v", matches[0], filepath.Join(ctx.baseDir, matches[2]), err)
return match
}
logMsg(LogVerbose, "Including image %s: %s", imageID, currentImage)
imgSrc := "../" + currentImage
if strings.Contains(imageID, "kindle:") {
imgSrc = imageID
}
return fmt.Sprintf(`<img title="%s" alt="%s" src="%s"/>`, matches[4], matches[1], imgSrc)
})
return "<div>" + parseLine(ctx, transformed, true) + "</div>\n", true
},
}
}
func dividerHandler() lineHandler {
firstparagraph = true
return staticResult(reDivider, "<hr/>\n")
}
func pagebreakHandler() lineHandler {
firstparagraph = true
return staticResult(rePagebreak, "<MBP:PAGEBREAK/>\n")
}