File: cite.go

package info (click to toggle)
addchain 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: sh: 428; makefile: 8
file content (61 lines) | stat: -rw-r--r-- 1,522 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package meta

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"strconv"
	"strings"

	"github.com/mmcloughlin/addchain/internal/print"
)

// CheckCitable checks whether a citation can be generated for this built
// version.
func (p *Properties) CheckCitable() error {
	if !p.IsRelease() {
		return errors.New("cannot cite non-release version")
	}
	return nil
}

// WriteCitation writes BibTeX citation for the most recent release to the given
// writer.
func (p *Properties) WriteCitation(w io.Writer) error {
	// Determine release time.
	date, err := p.ReleaseTime()
	if err != nil {
		return fmt.Errorf("release date: %w", err)
	}

	// Use tabwriter for field alignment.
	tw := print.NewTabWriter(w, 1, 4, 1, ' ', 0)

	field := func(key, value string) { tw.Linef("    %s\t=\t%s,", key, value) }
	str := func(key, value string) { field(key, "{"+value+"}") }

	tw.Linef("@misc{%s,", p.Name)
	str("title", p.Title())
	str("author", "Michael B. McLoughlin")
	field("year", strconv.Itoa(date.Year()))
	field("month", strings.ToLower(date.Month().String()[:3]))
	str("howpublished", "Repository \\url{"+p.RepositoryURL()+"}")
	str("version", p.ReleaseVersion)
	str("license", "BSD 3-Clause License")
	str("doi", p.DOI)
	str("url", p.DOIURL())
	tw.Linef("}")
	tw.Flush()

	return tw.Error()
}

// Citation returns a BibTeX citation for the most recent release.
func (p *Properties) Citation() (string, error) {
	buf := bytes.NewBuffer(nil)
	if err := p.WriteCitation(buf); err != nil {
		return "", err
	}
	return buf.String(), nil
}