-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchapter.go
More file actions
59 lines (51 loc) · 1.53 KB
/
chapter.go
File metadata and controls
59 lines (51 loc) · 1.53 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
package mobi
import (
"bytes"
"fmt"
)
// Chapter lets you add sub-chapters to the book
type Chapter interface {
AddSubChapter(title string, text []byte) Chapter
}
type mobiChapter struct {
ID int
Parent int
Title string
RecordOffset int
LabelOffset int
Len int
HTML []byte
SubChapters []*mobiChapter
subChapter bool
}
// NewChapter adds a new chapter to the output MobiBook
func (w *mobiBuilder) NewChapter(title string, text []byte) Chapter {
w.chapters = append(w.chapters, mobiChapter{ID: w.chapterCount, Title: title, HTML: text})
w.chapterCount++
return &w.chapters[len(w.chapters)-1]
}
// AddSubChapter adds a sub-chapter to the Chapter and returns the parent chapter back again
func (w *mobiChapter) AddSubChapter(title string, text []byte) Chapter {
w.SubChapters = append(w.SubChapters, &mobiChapter{Parent: w.ID, Title: title, HTML: text, subChapter: true})
return w
}
// Number of sub-chapters in this chapter
func (w *mobiChapter) SubChapterCount() int {
return len(w.SubChapters)
}
func (w *mobiChapter) generateHTML(out *bytes.Buffer) {
//Add check for unsupported HTML tags, characters, clean up HTML
w.RecordOffset = out.Len()
Len0 := out.Len()
if !w.subChapter {
// main chapter, write TOC target
out.WriteString(fmt.Sprintf("<a name='%d' id='%d'></a>", w.ID, w.ID))
}
out.WriteString("<h1>" + w.Title + "</h1>")
out.Write(w.HTML)
out.WriteString("<mbp:pagebreak/>")
w.Len = out.Len() - Len0
for i := range w.SubChapters {
w.SubChapters[i].generateHTML(out)
}
}