PrettyString pretty prints a BibTex.
()
| 211 | |
| 212 | // PrettyString pretty prints a BibTex. |
| 213 | func (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. |
| 250 | func stringformat(v string) string { |
no test coverage detected