summaryrefslogtreecommitdiff
path: root/main.go
blob: 66a2588a08758e5ca973974e52c306c980138643 (plain)
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
package main

import (
	"html/template"
	"os"
)

var urls = map[string]string{}
var names = map[string]string{}

func (doc Document) URL() string {
	if doc.ID == "" {
		return ""
	}
	key := doc.Vendor.Name + "\000" + doc.ID
	if _, have := urls[key]; !have {
		urls[key] = doc.Vendor.GetURL(doc.ID)
	}
	return urls[key]
}

func (doc Document) Name() string {
	if doc.ID == "" {
		var names []string
		for _, full := range doc.Fulls() {
			names = append(names, full.Name())
		}
		switch len(names) {
		case 0:
			return "???"
		case 1:
			return names[0]
		default:
			// BUG(lukeshu): Handle un-IDed Documents with
			// multiple IDed resellers.
			panic("TODO")
		}
	}
	key := doc.Vendor.Name + "\000" + doc.ID
	if _, have := names[key]; !have {
		names[key] = doc.Vendor.GetName(doc.ID, doc.URL())
	}
	return names[key]
}

func (doc Document) Fulls() []Document {
	var ret []Document
	for _, reseller := range doc.Resellers {
		if doc.ID != "" && reseller.Vendor.Name == doc.Vendor.Name && reseller.ID == doc.ID {
			continue
		}
		if reseller.Type == Full {
			ret = append(ret, reseller)
		}
	}
	return ret
}

func (doc Document) Patches() []Document {
	var ret []Document
	for _, reseller := range doc.Resellers {
		if doc.ID != "" && reseller.Vendor.Name == doc.Vendor.Name && reseller.ID == doc.ID {
			continue
		}
		if reseller.Type == Patch {
			ret = append(ret, reseller)
		}
	}
	return ret
}

func (doc Document) AsFull() *Document {
	if doc.Vendor.Name == "" && doc.ID == "" {
		return nil
	}
	ret := doc
	ret.Resellers = ret.Fulls()
	switch doc.Type {
	case Full:
		// Nothing to do
	case Patch:
		if len(ret.Resellers) == 0 {
			return nil
		}
		ret.Type = Full
		ret.ID = func() string {
			var ids []string
			for _, reseller := range ret.Resellers {
				if reseller.Vendor.Name == doc.Vendor.Name {
					ids = append(ids, reseller.ID)
				}
			}
			switch len(ids) {
			case 0:
				return ""
			case 1:
				return ids[0]
			default:
				panic("wut")
			}
		}()
		if ret.ID != "" {
			ret.Resellers = ret.Fulls()
		}
	default:
		panic("uhh")
	}
	return &ret
}

func (doc Document) AsPatch() *Document {
	if doc.Vendor.Name == "" && doc.ID == "" {
		return nil
	}
	ret := doc
	switch doc.Type {
	case Full:
		return nil
	case Patch:
		ret.Resellers = doc.Patches()
	default:
		panic("no")
	}
	return &ret
}

func (ed Edition) DocsOrdered() []Document {
	// This chould be O(n), but this niaeve implementation is
	// O(n^2).  It's OK, n is small.
	s := make([]Document, len(Vendors))
	for i, vnd := range Vendors {
		for _, doc := range ed.Docs {
			if doc.Vendor.Name == vnd.Name {
				s[i] = doc
			}
		}
	}
	return s
}

var tmpl = `{{define "document"}}{{if .}}
	{{if .URL}}<a href="{{.URL}}" title="{{.Name}}">{{.Name}}</a>{{else}}{{.Name}}{{end}}
	{{range .Resellers}}
	  <a href="{{.URL}}" title="{{.Name}}">({{.Vendor.Name}})</a>
	{{end}}
{{end}}{{end}}
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>POSIX Editions</title>
		<style>
			body { font-size: 8px; }
			table { border-collapse: collapse; }
			th, td { border: solid 1px black; }
			tr:not(:first-child):not(:nth-child(2)) th {
				font-family: monospace;
				text-align: left;
			}
		</style>
	</head>
	<body>
		<p>There's a typo in the "standardNumber" in IEEE
		   Xplore's records forfor the 2004 edition of 1003.1;
		   it says 2014 instead or 2004.  The actual document
		   says 2004 though.</p>
		<table>
			<caption><p>POSIX: C & Shell (1997-present)</p></caption>
			<tr><td rowspan=2></td>{{range .Vendors}}<th colspan=2>{{.Name}}</th>{{end}}</tr>
			<tr>{{range .Vendors}}<th>Full</th><th>Patch</th>{{end}}</tr>
			{{range .Editions}}<tr>
				<th>{{.Name}}</th>
				{{range .DocsOrdered}}
				  <td>{{template "document" .AsFull}}</td><td>{{template "document" .AsPatch}}</td>
				{{end}}
			</tr>{{end}}
		</table>
	</body>
</html>
`

func main() {
	err := template.Must(template.New("page").Parse(tmpl)).Execute(os.Stdout, map[string]interface{}{
		"Vendors":  Vendors,
		"Editions": Editions,
	})
	if err != nil {
		panic(err)
	}
}