MCPcopy Create free account
hub / github.com/cogentcore/core / PrettyString

Method PrettyString

parse/languages/bibtex/bibtex.go:213–247  ·  view source on GitHub ↗

PrettyString pretty prints a BibTex.

()

Source from the content-addressed store, hash-verified

211
212// PrettyString pretty prints a BibTex.
213func (bib *BibTex) PrettyString() string {
214 var buf bytes.Buffer
215 for i, entry := range bib.Entries {
216 if i != 0 {
217 fmt.Fprint(&buf, "\n")
218 }
219 fmt.Fprintf(&buf, "@%s{%s,\n", entry.Type, entry.CiteName)
220
221 // Determine key order.
222 keys := []string{}
223 for key := range entry.Fields {
224 keys = append(keys, key)
225 }
226
227 priority := map[string]int{"title": -3, "author": -2, "url": -1}
228 sort.Slice(keys, func(i, j int) bool {
229 pi, pj := priority[keys[i]], priority[keys[j]]
230 return pi < pj || (pi == pj && keys[i] < keys[j])
231 })
232
233 // Write fields.
234 tw := tabwriter.NewWriter(&buf, 1, 4, 1, ' ', 0)
235 for _, key := range keys {
236 value := entry.Fields[key].String()
237 format := stringformat(value)
238 fmt.Fprintf(tw, " %s\t=\t"+format+",\n", key, value)
239 }
240 tw.Flush()
241
242 // Close.
243 buf.WriteString("}\n")
244 }
245 return buf.String()
246
247}
248
249// stringformat determines the correct formatting verb for the given BibTeX field value.
250func stringformat(v string) string {

Callers 2

LookupCiteMethod · 0.95
LookupCiteMethod · 0.95

Calls 7

StringMethod · 0.95
stringformatFunction · 0.85
FprintMethod · 0.80
SliceMethod · 0.80
WriteStringMethod · 0.80
StringMethod · 0.65
FlushMethod · 0.45

Tested by

no test coverage detected