forked from miekg/mmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2rfc.go
320 lines (285 loc) · 8.15 KB
/
xml2rfc.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
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
package mmark
import (
"bytes"
"fmt"
"sort"
"strconv"
"time"
)
// xml2rfc.go contains common code and variables that is shared
// between xml2rfcv[23].go.
var (
// These have been known to change, these are the current ones (2015-08-27).
CitationsBase = "https://xml2rfc.tools.ietf.org/public/rfc/"
// CitationsRFC is the URL where the citations for RFCs are.
CitationsRFC = CitationsBase + "bibxml/"
// CitationsANSI is the URL where the citations for ANSI documents are.
CitationsANSI = CitationsBase + "bibxml2/"
// CitationsID is the URL where the citations for I-Ds are.
CitationsID = CitationsBase + "bibxml3/"
// CitationsW3C is the URL where the citations for W3C documents are.
CitationsW3C = CitationsBase + "bibxml4/"
)
const (
referenceRFC = "reference.RFC."
referenceID = "reference.I-D.draft-"
referenceIDLatest = "reference.I-D."
referenceW3C = "reference."
referenceANSI = "reference."
ext = ".xml"
)
// referenceFile creates a .xml filename for the citation c.
// For I-D references like '[@?I-D.ietf-dane-openpgpkey#02]' it will
// create http://<CitationsID>/reference.I-D.draft-ietf-dane-openpgpkey-02.xml
// without an sequence number it becomes:
// http://<CitationsID>/reference.I-D.ietf-dane-openpgpkey.xml
func referenceFile(c *citation) string {
if len(c.link) < 4 {
return ""
}
switch {
case bytes.HasPrefix(c.link, []byte("RFC")):
return CitationsRFC + referenceRFC + string(c.link[3:]) + ext
case bytes.HasPrefix(c.link, []byte("I-D")):
seq := ""
if c.seq != -1 {
seq = "-" + fmt.Sprintf("%02d", c.seq)
return CitationsID + referenceID + string(c.link[4:]) + seq + ext
}
return CitationsID + referenceIDLatest + string(c.link[4:]) + ext
case bytes.HasPrefix(c.link, []byte("W3C")):
return CitationsW3C + referenceW3C + string(c.link) + ext
case bytes.HasPrefix(c.link, []byte("ANSI")):
fallthrough
case bytes.HasPrefix(c.link, []byte("CCITT")):
fallthrough
case bytes.HasPrefix(c.link, []byte("FIPS")):
fallthrough
case bytes.HasPrefix(c.link, []byte("IEEE")):
fallthrough
case bytes.HasPrefix(c.link, []byte("ISO")):
fallthrough
case bytes.HasPrefix(c.link, []byte("ITU")):
fallthrough
case bytes.HasPrefix(c.link, []byte("PKCS")):
return CitationsANSI + referenceANSI + string(c.link) + ext
}
return ""
}
// countCitationsAndSort returns the number of informative and normative
// references and a string slice with the sorted keys.
func countCitationsAndSort(citations map[string]*citation) (int, int, []string) {
keys := make([]string, 0, len(citations))
refi, refn := 0, 0
for k, c := range citations {
if c.typ == 'i' {
refi++
}
if c.typ == 'n' {
refn++
}
keys = append(keys, k)
}
sort.Strings(keys)
return refi, refn, keys
}
var entityConvert = map[byte][]byte{
'<': []byte("<"),
'>': []byte(">"),
'&': []byte("&"),
// '\'': []byte("'"),
// '"': []byte("""),
}
func writeEntity(out *bytes.Buffer, text []byte) {
for i := 0; i < len(text); i++ {
if s, ok := entityConvert[text[i]]; ok {
out.Write(s)
continue
}
out.WriteByte(text[i])
}
}
// sanitizeXML strips XML from a string.
func sanitizeXML(s []byte) []byte {
inTag := false
j := 0
for i := 0; i < len(s); i++ {
if s[i] == '<' {
inTag = true
continue
}
if s[i] == '>' {
inTag = false
continue
}
if !inTag {
s[j] = s[i]
j++
}
}
return s[:j]
}
// writeSanitizeXML strips XML from a string and writes
// to out.
func writeSanitizeXML(out *bytes.Buffer, s []byte) {
inTag := false
for i := 0; i < len(s); i++ {
if s[i] == '<' {
inTag = true
continue
}
if s[i] == '>' {
inTag = false
continue
}
if !inTag {
out.WriteByte(s[i])
}
}
}
// titleBlockTOMLAuthor outputs the author from the TOML title block.
func titleBlockTOMLAuthor(out *bytes.Buffer, a author) {
out.WriteString("<author")
if a.Role != "" {
out.WriteString(" role=\"")
writeEntity(out, []byte(a.Role))
out.WriteString("\"")
}
out.WriteString(" initials=\"")
writeEntity(out, []byte(a.Initials))
out.WriteString("\"")
out.WriteString(" surname=\"")
writeEntity(out, []byte(a.Surname))
out.WriteString("\"")
out.WriteString(" fullname=\"")
writeEntity(out, []byte(a.Fullname))
out.WriteString("\">\n")
abbrev := ""
if a.OrganizationAbbrev != "" {
abbrev = " abbrev=\"" + a.OrganizationAbbrev + "\""
}
out.WriteString("<organization")
writeEntity(out, []byte(abbrev))
out.WriteString(">")
writeEntity(out, []byte(a.Organization))
out.WriteString("</organization>\n")
out.WriteString("<address>\n")
out.WriteString("<postal>\n")
out.WriteString("<street>")
writeEntity(out, []byte(a.Address.Postal.Street))
out.WriteString("</street>\n")
for _, street := range a.Address.Postal.Streets {
out.WriteString("<street>")
writeEntity(out, []byte(street))
out.WriteString("</street>\n")
}
out.WriteString("<city>")
writeEntity(out, []byte(a.Address.Postal.City))
out.WriteString("</city>\n")
for _, city := range a.Address.Postal.Cities {
out.WriteString("<city>")
writeEntity(out, []byte(city))
out.WriteString("</city>\n")
}
out.WriteString("<code>")
writeEntity(out, []byte(a.Address.Postal.Code))
out.WriteString("</code>\n")
for _, code := range a.Address.Postal.Codes {
out.WriteString("<code>")
writeEntity(out, []byte(code))
out.WriteString("</code>\n")
}
out.WriteString("<country>")
writeEntity(out, []byte(a.Address.Postal.Country))
out.WriteString("</country>\n")
for _, country := range a.Address.Postal.Countries {
out.WriteString("<country>")
writeEntity(out, []byte(country))
out.WriteString("</country>\n")
}
out.WriteString("<region>")
writeEntity(out, []byte(a.Address.Postal.Region))
out.WriteString("</region>\n")
for _, region := range a.Address.Postal.Regions {
out.WriteString("<region>")
writeEntity(out, []byte(region))
out.WriteString("</region>\n")
}
out.WriteString("</postal>\n")
out.WriteString("<phone>" + a.Address.Phone + "</phone>\n")
out.WriteString("<email>" + a.Address.Email + "</email>\n")
out.WriteString("<uri>" + a.Address.Uri + "</uri>\n")
out.WriteString("</address>\n")
out.WriteString("</author>\n")
}
// titleBlockTOMLDate outputs the date from the TOML title block.
func titleBlockTOMLDate(out *bytes.Buffer, d time.Time) {
year := ""
if d.Year() > 0 {
year = " year=\"" + strconv.Itoa(d.Year()) + "\""
}
month := ""
if d.Month() > 0 {
month = " month=\"" + time.Month(d.Month()).String() + "\""
}
day := ""
if d.Day() > 0 {
day = " day=\"" + strconv.Itoa(d.Day()) + "\""
}
out.WriteString("<date" + year + month + day + "/>\n\n")
}
// titleBlockTOMLKeyword outputs the keywords from the TOML title block.
func titleBlockTOMLKeyword(out *bytes.Buffer, keywords []string) {
for _, k := range keywords {
out.WriteString("<keyword>" + k + "</keyword>\n")
}
}
// titleBlockTOMLPI returns "yes" or "no" or a stringified number
// for use as process instruction. If version is 3 they are returned
// as attributes for use *inside* the <rfc> tag.
func titleBlockTOMLPI(pi pi, name string, version int) string {
if version == 2 {
switch name {
case "toc":
return "<?rfc toc=\"" + yesno(pi.Toc, "yes") + "\"?>\n"
case "symrefs":
return "<?rfc symrefs=\"" + yesno(pi.Symrefs, "yes") + "\"?>\n"
case "sortrefs":
return "<?rfc sortrefs=\"" + yesno(pi.Sortrefs, "yes") + "\"?>\n"
case "compact":
return "<?rfc compact=\"" + yesno(pi.Compact, "yes") + "\"?>\n"
case "topblock":
return "<?rfc topblock=\"" + yesno(pi.Topblock, "yes") + "\"?>\n"
case "comments":
return "<?rfc comments=\"" + yesno(pi.Comments, "no") + "\"?>\n"
case "subcompact":
return "<?rfc subcompact=\"" + yesno(pi.Subcompact, "no") + "\"?>\n"
case "private":
return "<?rfc private=\"" + yesno(pi.Private, "") + "\"?>\n"
case "header":
if pi.Header == piNotSet {
return ""
}
return "<?rfc header=\"" + pi.Header + "\"?>\n"
case "footer":
if pi.Footer == piNotSet {
return ""
}
return "<?rfc footer=\"" + pi.Footer + "\"?>\n"
default:
printf(nil, "unhandled or unknown PI seen: %s", name)
return ""
}
}
// version 3
return ""
}
func yesno(s, def string) string {
if s == "" {
return def
}
if s == "yes" {
return "yes"
}
return "no"
}