This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
xhtml.go
142 lines (122 loc) · 3.23 KB
/
xhtml.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
package epub
import (
"encoding/xml"
"fmt"
)
const (
xhtmlDoctype = `<!DOCTYPE html>
`
xhtmlLinkRel = "stylesheet"
xhtmlTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title dir="auto"></title>
</head>
<body dir="auto"></body>
</html>
`
)
// xhtml implements an XHTML document
type xhtml struct {
xml *xhtmlRoot
}
// This holds the actual XHTML content
type xhtmlRoot struct {
XMLName xml.Name `xml:"http://www.w3.org/1999/xhtml html"`
XmlnsEpub string `xml:"xmlns:epub,attr,omitempty"`
Head xhtmlHead `xml:"head"`
Body xhtmlInnerxml `xml:"body"`
}
type xhtmlHead struct {
Title xhtmlTitle `xml:"title"`
Link *xhtmlLink
}
type xhtmlTitle struct {
XMLName xml.Name `xml:"title,omitempty"`
Dir string `xml:"dir,attr,omitempty"`
Value string `xml:",chardata"`
}
// The <link> element, used to link to stylesheets
// Ex: <link rel="stylesheet" type="text/css" href="../css/epub.css" />
type xhtmlLink struct {
XMLName xml.Name `xml:"link,omitempty"`
Rel string `xml:"rel,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
Href string `xml:"href,attr,omitempty"`
}
// This holds the content of the XHTML document between the <body> tags. It is
// implemented as a string because we don't know what it will contain and we
// leave it up to the user of the package to validate the content
type xhtmlInnerxml struct {
XML string `xml:",innerxml"`
Dir string `xml:"dir,attr,omitempty"`
}
// Constructor for xhtml
func newXhtml(body string) *xhtml {
x := &xhtml{
xml: newXhtmlRoot(),
}
x.setBody(body)
return x
}
// Constructor for xhtmlRoot
func newXhtmlRoot() *xhtmlRoot {
r := &xhtmlRoot{
Body: xhtmlInnerxml{Dir: "auto"},
}
err := xml.Unmarshal([]byte(xhtmlTemplate), &r)
if err != nil {
panic(fmt.Sprintf(
"Error unmarshalling xhtmlRoot: %s\n"+
"\txhtmlRoot=%#v\n"+
"\txhtmlTemplate=%s",
err,
*r,
xhtmlTemplate))
}
return r
}
func (x *xhtml) setBody(body string) {
x.xml.Body.XML = "\n" + body + "\n"
x.xml.Body.Dir = "auto"
}
func (x *xhtml) setCSS(path string) {
x.xml.Head.Link = &xhtmlLink{
Rel: xhtmlLinkRel,
Type: mediaTypeCSS,
Href: path,
}
}
func (x *xhtml) setTitle(title string) {
x.xml.Head.Title = xhtmlTitle{
Dir: "auto",
Value: title,
}
}
func (x *xhtml) setXmlnsEpub(xmlns string) {
x.xml.XmlnsEpub = xmlns
}
func (x *xhtml) Title() string {
return x.xml.Head.Title.Value
}
// Write the XHTML file to the specified path
func (x *xhtml) write(xhtmlFilePath string) {
xhtmlFileContent, err := xml.MarshalIndent(x.xml, "", " ")
if err != nil {
panic(fmt.Sprintf(
"Error marshalling XML for XHTML file: %s\n"+
"\tXML=%#v",
err,
x.xml))
}
// Add the doctype declaration to the output
xhtmlFileContent = append([]byte(xhtmlDoctype), xhtmlFileContent...)
// Add the xml header to the output
xhtmlFileContent = append([]byte(xml.Header), xhtmlFileContent...)
// It's generally nice to have files end with a newline
xhtmlFileContent = append(xhtmlFileContent, "\n"...)
if err := filesystem.WriteFile(xhtmlFilePath, []byte(xhtmlFileContent), filePermissions); err != nil {
panic(fmt.Sprintf("Error writing XHTML file: %s", err))
}
}