This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
210 lines (180 loc) · 5.14 KB
/
build.go
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
package main
import (
"fmt"
"log"
"net/http"
"path/filepath"
"strings"
"golang.org/x/net/html"
"github.com/bmaupin/go-epub"
"github.com/bmaupin/go-htmlutil"
)
const (
effectiveGoCoverImg = "assets/covers/effective-go.png"
effectiveGoFilename = "Effective Go.epub"
effectiveGoSectionTag = "h2"
effectiveGoTitle = "Effective Go"
effectiveGoTitleFilename = "title.xhtml"
effectiveGoUrl = "https://golang.org/doc/effective_go.html"
epubCSSFile = "assets/styles/epub.css"
preFontFile = "assets/fonts/SourceCodePro-Regular.ttf"
)
type epubSection struct {
title string
filename string
nodes []html.Node
}
func main() {
err := buildEffectiveGo()
if err != nil {
log.Printf("Error building Effective Go: %s", err)
}
}
func buildEffectiveGo() error {
resp, err := http.Get(effectiveGoUrl)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
panic(err)
}
}()
doc, err := html.Parse(resp.Body)
if err != nil {
log.Fatalf("Parse error: %s", err)
}
pageNode := htmlutil.GetFirstHtmlNode(doc, "main", "id", "page")
containerNode := htmlutil.GetFirstHtmlNode(pageNode, "div", "class", "container")
footerNode := htmlutil.GetFirstHtmlNode(containerNode, "div", "id", "footer")
footerNode = reformatEffectiveGoFooter(footerNode)
sections := []epubSection{}
sectionFilename := effectiveGoTitleFilename
// Don't add a title so it doesn't get added to the TOC
section := &epubSection{
filename: effectiveGoTitleFilename,
}
internalLinks := make(map[string]string)
// Iterate through each child node
for node := containerNode.FirstChild; node != nil; node = node.NextSibling {
// If we find the section tag
if node.Type == html.ElementNode && node.Data == effectiveGoSectionTag {
// Add the previous section to the slice of sections
sections = append(sections, *section)
sectionTitle := node.FirstChild.Data
sectionFilename = titleToFilename(sectionTitle)
// Start a new section
section = &epubSection{
filename: sectionFilename,
title: sectionTitle,
}
}
if node == footerNode {
// Add the footer node to the title page since it contains license information
sections[0].nodes = append(sections[0].nodes, *node)
} else {
// Append the current node to the current section
section.nodes = append(section.nodes, *node)
}
// Map internal links to their section filename
for _, idNode := range htmlutil.GetAllHtmlNodes(node, "", "id", "") {
for _, attr := range idNode.Attr {
if attr.Key == "id" {
internalLinks[attr.Val] = fmt.Sprintf("%s#%s", sectionFilename, attr.Val)
}
}
}
}
// Make sure the last section gets added
sections = append(sections, *section)
e := epub.NewEpub(effectiveGoTitle)
effectiveGoCoverImgPath, err := filepath.Abs(effectiveGoCoverImg)
if err != nil {
return err
}
e.SetCover(effectiveGoCoverImgPath, "")
epubCSSPath, err := e.AddCSS(epubCSSFile, "")
if err != nil {
return err
}
_, err = e.AddFont(preFontFile, "")
if err != nil {
return err
}
// Iterate through each section and add it to the EPUB
for _, section := range sections {
sectionContent := ""
for _, sectionNode := range section.nodes {
// Fix internal links so they work after splitting page into sections
for _, linkNode := range htmlutil.GetAllHtmlNodes(§ionNode, "a", "", "") {
for i, attr := range linkNode.Attr {
if attr.Key == "href" && strings.HasPrefix(attr.Val, "#") {
linkNode.Attr[i].Val = internalLinks[attr.Val[1:]]
}
}
}
nodeContent, err := htmlutil.HtmlNodeToString(§ionNode)
if err != nil {
return err
}
sectionContent += nodeContent
}
_, err := e.AddSection(sectionContent, section.title, section.filename, epubCSSPath)
if err != nil {
return err
}
}
err = e.Write(effectiveGoFilename)
if err != nil {
return err
}
return nil
}
func titleToFilename(title string) string {
title = strings.ToLower(title)
title = strings.Replace(title, " ", "-", -1)
return fmt.Sprintf("%s.xhtml", title)
}
func reformatEffectiveGoFooter(footerNode *html.Node) *html.Node {
newBrNode := func() *html.Node {
return &html.Node{
Type: html.ElementNode,
Data: "br",
}
}
for node := footerNode.FirstChild; node != nil; node = node.NextSibling {
// Double all <br> elements for styling
if node.Type == html.ElementNode && node.Data == "br" {
footerNode.InsertBefore(newBrNode(), node)
} else if node.Type == html.TextNode && strings.Contains(node.Data, "page") {
node.Data = strings.Replace(node.Data, "page", "book", -1)
}
}
footerNode.InsertBefore(
newBrNode(),
footerNode.FirstChild)
footerNode.InsertBefore(
newBrNode(),
footerNode.FirstChild)
sourceLinkNode := &html.Node{
Type: html.ElementNode,
Data: "a",
Attr: []html.Attribute{
html.Attribute{
Key: "href",
Val: effectiveGoUrl,
}},
}
sourceLinkNode.AppendChild(&html.Node{
Type: html.TextNode,
Data: effectiveGoUrl,
})
footerNode.InsertBefore(sourceLinkNode, footerNode.FirstChild)
footerNode.InsertBefore(
&html.Node{
Type: html.TextNode,
Data: "Source: ",
},
footerNode.FirstChild)
return footerNode
}