-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhtml.go
153 lines (136 loc) · 3.98 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
143
144
145
146
147
148
149
150
151
152
153
// This is Free Software covered by the terms of the MIT license.
// See LICENSE file for details.
// Copyright 2017 by Intevation GmbH
package main
import (
"bufio"
"encoding/xml"
"fmt"
"io"
)
type xhtml struct {
out *bufio.Writer
}
func (x *xhtml) writeString(s string) error {
_, err := x.out.WriteString(s)
return err
}
func (x *xhtml) str(s string) func(*node) error {
return func(*node) error { return x.writeString(s) }
}
func (x *xhtml) tag(name string) func(*node) error {
return x.str("<" + name + "/>")
}
func (x *xhtml) open(name string) func(*node) error {
return x.str("<" + name + ">")
}
func (x *xhtml) close(name string) func(*node) error {
return x.str("</" + name + ">")
}
func (x *xhtml) element(name string) *visitor {
return &visitor{x.open(name), x.close(name)}
}
func (x *xhtml) heading(level int) *visitor {
op, cl := fmt.Sprintf("<h%d>", level), fmt.Sprintf("</h%d>", level)
return &visitor{
enter: func(n *node) error {
x.writeString(op)
x.text(n)
return x.writeString(cl)
},
}
}
func (x *xhtml) text(n *node) error {
enc := xml.NewEncoder(x.out)
txt, ok := n.value.(string)
if !ok {
return nil
}
if err := enc.EncodeToken(xml.CharData(txt)); err != nil {
return err
}
return enc.Flush()
}
func (x *xhtml) link(n *node) error {
href, _ := n.value.(string)
enc := xml.NewEncoder(x.out)
if err := enc.EncodeToken(xml.StartElement{
Name: xml.Name{Local: "a"},
Attr: []xml.Attr{{Name: xml.Name{Local: "href"}, Value: href}},
}); err != nil {
return err
}
return enc.Flush()
}
func (x *xhtml) image(n *node) error {
img := n.value.(*image)
enc := xml.NewEncoder(x.out)
attr := []xml.Attr{{Name: xml.Name{Local: "src"}, Value: img.src}}
if img.alt != "" {
attr = append(attr, xml.Attr{Name: xml.Name{Local: "alt"}, Value: img.alt})
}
if err := enc.EncodeToken(xml.StartElement{
Name: xml.Name{Local: "img"},
Attr: attr,
}); err != nil {
return err
}
return enc.Flush()
}
const (
xhtmlHeader = xml.Header +
`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"` +
` "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">` + "\n" +
`<html version="-//W3C//DTD XHTML 1.1//EN"` +
` xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"` +
` xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"` +
` xsi:schemaLocation="http://www.w3.org/1999/xhtml` +
` http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd">` + "\n" +
"<body>\n"
xhtmlFooter = "\n</body>\n</html>\n"
)
func exportXHTML(doc *document, out io.Writer, standalone bool) error {
x := xhtml{out: bufio.NewWriter(out)}
if standalone {
x.writeString(xhtmlHeader)
}
err := doc.traverse(map[nodeType]*visitor{
orderedListNode: x.element("ol"),
unorderedListNode: x.element("ul"),
listItemNode: x.element("li"),
textNode: &visitor{enter: x.text},
boldNode: x.element("strong"),
italicsNode: x.element("i"),
underlinedNode: x.element("em"),
strikeNode: x.element("del"),
superscriptNode: x.element("sup"),
subscriptNode: x.element("sub"),
tableNode: x.element("table"),
tableRowNode: x.element("tr"),
tableCellNode: x.element("td"),
tableHeaderRowNode: x.element("tr"),
tableHeaderCellNode: x.element("th"),
heading1Node: x.heading(1),
heading2Node: x.heading(2),
heading3Node: x.heading(3),
heading4Node: x.heading(4),
heading5Node: x.heading(5),
heading6Node: x.heading(6),
paragraphNode: x.element("p"),
lineBreakNode: &visitor{enter: x.tag("br")},
escapeNode: &visitor{enter: x.text},
noWikiNode: &visitor{x.str("<pre><tt>"), x.str("</tt></pre>")},
noWikiInlineNode: x.element("tt"),
// placeholderNode not supported, yet.
imageNode: &visitor{x.image, x.close("img")},
linkNode: &visitor{x.link, x.close("a")},
horizontalLineNode: &visitor{enter: x.tag("hr")},
})
if err != nil {
return err
}
if standalone {
x.writeString(xhtmlFooter)
}
return x.out.Flush()
}