-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhtml.go
146 lines (127 loc) · 3.67 KB
/
html.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
package main
import (
"fmt"
"io"
"sort"
"strings"
"github.com/nickng/bibtex"
)
func sortByYear(yearToEntries map[string][]string) []string {
keys := make([]string, 0, len(yearToEntries))
for k := range yearToEntries {
keys = append(keys, k)
}
sort.Sort(sort.Reverse(sort.StringSlice(keys)))
return keys
}
func appendIfNotEmpty(slice []string, s string) []string {
if s != "" {
return append(slice, s)
}
return slice
}
func makeBib(to io.Writer, bibEntries []bibEntry) {
yearToEntries := make(map[string][]string)
for _, entry := range bibEntries {
y := entry.Fields["year"].String()
yearToEntries[y] = append(yearToEntries[y], makeBibEntry(&entry))
}
sortedYears := sortByYear(yearToEntries)
for _, year := range sortedYears {
fmt.Fprintln(to, "<ul>")
for _, entry := range yearToEntries[year] {
fmt.Fprint(to, entry)
}
fmt.Fprintln(to, "</ul>")
}
}
func makeBibEntry(entry *bibEntry) string {
s := []string{
fmt.Sprintf("<li id='%s'>", entry.CiteName),
`<div>`,
makeBibEntryTitle(entry),
`</div>`,
`<div>`,
makeBibEntryAuthors(entry),
`</div>`,
`<span class="other">`,
makeBibEntryMisc(entry),
`</span>`,
`</li>`,
}
return strings.Join(s, "\n")
}
func makeBibEntryTitle(entry *bibEntry) string {
// Paper title is on the left side.
title := []string{
`<span class="paper">`,
decodeTitle(entry.Fields["title"].String()),
`</span>`,
}
// Icons are on the right side.
icons := makeIcons(entry)
return strings.Join(append(title, icons...), "\n")
}
func makeIcons(entry *bibEntry) []string {
var icons = []string{`<span class="icons">`}
// Not all references have a corresponding discussion (e.g., on net4people)
// but if they do, add an icon.
if field, ok := entry.Fields["discussion_url"]; ok {
s := fmt.Sprintf("<a href='%s'>", field.String()) +
`<img class="icon" title="Online discussion" src="assets/discussion-icon.svg" alt="Discussion icon">` +
`</a>`
icons = append(icons, s)
}
// Add icons that are always present.
icons = append(icons, []string{
fmt.Sprintf("<a href='%s'>", entry.Fields["url"].String()),
`<img class="icon" title="Download paper" src="assets/pdf-icon.svg" alt="Download icon">`,
`</a>`,
fmt.Sprintf("<a href='https://censorbib.nymity.ch/pdf/%s.pdf'>", entry.CiteName),
`<img class="icon" title="Download cached paper" src="assets/cache-icon.svg" alt="Cached download icon">`,
`</a>`,
fmt.Sprintf("<a href='https://github.com/NullHypothesis/censorbib/blob/master/references.bib#L%d'>", entry.lineNum),
`<img class="icon" title="Download BibTeX" src="assets/bibtex-icon.svg" alt="BibTeX download icon">`,
`</a>`,
fmt.Sprintf("<a href='#%s'>", entry.CiteName),
`<img class="icon" title="Link to paper" src="assets/link-icon.svg" alt="Paper link icon">`,
`</a>`,
}...)
return append(icons, `</span>`)
}
func makeBibEntryAuthors(entry *bibEntry) string {
s := []string{
`<span class="author">`,
decodeAuthors(entry.Fields["author"].String()),
`</span>`,
}
return strings.Join(s, "\n")
}
func makeBibEntryMisc(entry *bibEntry) string {
s := []string{}
s = appendIfNotEmpty(s, makeBibEntryVenue(entry))
s = appendIfNotEmpty(s, toStr(entry.Fields["year"]))
s = appendIfNotEmpty(s, toStr(entry.Fields["publisher"]))
return strings.Join(s, ", ")
}
func makeBibEntryVenue(entry *bibEntry) string {
var (
prefix string
bs bibtex.BibString
ok bool
)
if bs, ok = entry.Fields["booktitle"]; ok {
prefix = "In Proc. of: "
} else if bs, ok = entry.Fields["journal"]; ok {
prefix = "In: "
} else {
return "" // Some entries are self-published.
}
s := []string{
prefix,
`<span class="venue">`,
decodeProceedings(toStr(bs)),
`</span>`,
}
return strings.Join(s, "")
}