File: footnotes.go

package info (click to toggle)
git-sizer 1.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 300 kB
  • sloc: sh: 98; makefile: 80
file content (46 lines) | stat: -rw-r--r-- 799 bytes parent folder | download
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
package sizes

import (
	"bytes"
	"fmt"
)

type Footnotes struct {
	footnotes []string
	indexes   map[string]int
}

func NewFootnotes() *Footnotes {
	return &Footnotes{
		indexes: make(map[string]int),
	}
}

func (f *Footnotes) CreateCitation(footnote string) string {
	if footnote == "" {
		return ""
	}

	index, ok := f.indexes[footnote]
	if !ok {
		index = len(f.indexes) + 1
		f.footnotes = append(f.footnotes, footnote)
		f.indexes[footnote] = index
	}
	return fmt.Sprintf("[%d]", index)
}

func (f *Footnotes) String() string {
	if len(f.footnotes) == 0 {
		return ""
	}

	buf := &bytes.Buffer{}
	buf.WriteByte('\n')
	for i, footnote := range f.footnotes {
		index := i + 1
		citation := fmt.Sprintf("[%d]", index)
		fmt.Fprintf(buf, "%-4s %s\n", citation, footnote)
	}
	return buf.String()
}