summaryrefslogtreecommitdiff
path: root/ref.go
blob: ba30c79751ccc185348b7f866b18a2b3c681353e (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
package main

import (
	"fmt"
	"log"
	"os"
	"strings"
	"sort"
	"io"
)

const (
	bufsize = 512
	prefix  = `.\"s `
	end = `.\"e`
	section = `.\"ยง`
	tableHead = ".TS\nexpand;\nl1w(1.5i) l1w(4.5i)."
	tableFoot = ".TE"
	mac = ".ref"
	delim = "()"
	extraPrefix = `\fC`
	extraSuffix = `\fP`
)

type Reference struct {
	name  string
	contents string
	index int
}

func getRefTxt(lines []string) string {
	var txt string
	for _, line := range lines {
		if strings.HasPrefix(line, end) {
			break
		}
		txt += line + "\n"
	}
	return string(txt)
}

func findIndex(refs []Reference, name string) int {
	for _, ref := range refs {
		if ref.name == name {
			return ref.index
		}
	}
	return -1
}

func checkLetter(ch rune) bool {
	if ch >= 'A' && ch <= 'Z' ||
		ch >= 'a' && ch <= 'z' {
		return true
	}
	return false
}

func checkAllCaps(word string) bool {
	for _, ch := range word {
		if !checkLetter(ch) {
			continue
		}
		if ch < 'A' || ch > 'Z' {
			return false
		}
	}
	return true
}
// Typographic niceties.
func extraFmt(ref string) string {
	words := strings.Split(ref, " ")
	if checkAllCaps(words[0]) {
		words[0] = extraPrefix + words[0] + extraSuffix
	}
	words[1] = extraPrefix + words[1] + extraSuffix

	return words[0] + " " + words[1]
}

func main() {
	in := os.Stdin
	if len(os.Args) > 1 {
		var err error
		in, err = os.Open(os.Args[1])
		if err != nil {
			log.Fatal(err)
		}
	}

	bytes, err := io.ReadAll(in)
	if err != nil {
		log.Fatal(err)
	}
	contents := string(bytes)

	/* Iterate through the contents of the file twice:
	Once to find and index all references.
	Again to refer to each reference. */
	var refs []Reference
	div := -1
	lines := strings.Split(contents, "\n")
	for i, line := range lines {
		if line == section {
			div = i
		}
	}
	// If no split, quietly return contents.
	if div == -1 {
		fmt.Print(contents)
		return
	}

	for i, line := range lines[div:] {
		if strings.HasPrefix(line, prefix) {
			var ref Reference
			ref.name = line[len(prefix):]
			// i is relative to the lines after div.
			ref.contents = getRefTxt(lines[div+i+1:])
			refs = append(refs, ref)
		}
	}

	/* Sort references alphabetically. */
	sort.Slice(refs, func(i, j int) bool {
		return strings.Compare(refs[i].name, refs[j].name) < 0
	})

	for i := range refs {
		refs[i].index = i
	}

	// Second pass.
	for _, line := range lines[:div] {
		if strings.HasPrefix(line, mac) {
			var name string
			var start, end int
			for i := range line {
				if line[i] == delim[0] {
					start = i+1
				}
				if line[i] == delim[1] {
					end = i
					break
				}
			}
			name = line[start:end]
			index := findIndex(refs, name)
			fmt.Printf(".post.url #%d \"%s\"\n\n", index,
				extraFmt(line[start-1:]))
		} else {
			fmt.Println(line)
		}
	}

	// Format References into AMS Style bibiography.
	/* As it turns out, in order to be referenced individually,
	each reference needs its own table. */
	for _, ref := range refs {
		fmt.Printf(".post.name %d\n", ref.index)
		fmt.Println(tableHead)
		fmt.Printf("T{\n%c%s%c\nT}", delim[0], extraFmt(ref.name),
			delim[1])
		fmt.Printf("\tT{\n%s\nT}\n", ref.contents)
		fmt.Println(tableFoot)
	}
}