@@ -4,6 +4,7 @@ package main
44import (
55 "flag"
66 "fmt"
7+ "io"
78 "log"
89 "os"
910
2324// Config holds a configuration for filtering rules.
2425type Config struct {
2526 BibType map [string ]struct {
26- Required []string
27- Remove []string
27+ Required []string
28+ Remove []string
29+ FieldsOrder []string `toml:"fields_order"`
2830 }
2931}
3032
@@ -55,14 +57,18 @@ func main() {
5557 if err != nil {
5658 log .Fatal (err )
5759 }
60+ keyOrderByType := make (map [string ][]string )
5861 if * config != "" {
5962 var conf Config
6063 if _ , err := toml .DecodeFile (* config , & conf ); err != nil {
6164 log .Fatalf ("Cannot read config: %s" , err )
6265 }
6366 filter (parsed , & conf )
67+ for name , bt := range conf .BibType {
68+ keyOrderByType [name ] = bt .FieldsOrder
69+ }
6470 }
65- fmt . Fprintf (writer , parsed . PrettyString () )
71+ prettyPrintOverridingOrder (writer , parsed , keyOrderByType )
6672}
6773
6874func filter (bib * bibtex.BibTex , conf * Config ) {
@@ -81,3 +87,13 @@ func filter(bib *bibtex.BibTex, conf *Config) {
8187 }
8288 }
8389}
90+
91+ func prettyPrintOverridingOrder (w io.Writer , parsed * bibtex.BibTex , keyOrderByType map [string ][]string ) {
92+ for _ , entry := range parsed .Entries {
93+ var opts []bibtex.PrettyStringOpt
94+ if order , specified := keyOrderByType [entry .Type ]; specified {
95+ opts = append (opts , bibtex .WithKeyOrder (order ))
96+ }
97+ fmt .Fprintf (w , entry .PrettyString (opts ... ))
98+ }
99+ }
0 commit comments