|
| 1 | +/* |
| 2 | +Copyright 2018 Joshua Vécsei |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "log" |
| 23 | + "os" |
| 24 | + "regexp" |
| 25 | + |
| 26 | + "github.com/urfave/cli" |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + app := cli.NewApp() |
| 31 | + app.Name = "UrldateToNote" |
| 32 | + app.Usage = "Bibtex urldate to note" |
| 33 | + |
| 34 | + app.Flags = []cli.Flag{ |
| 35 | + cli.StringFlag{ |
| 36 | + Name: "prefix, p", |
| 37 | + Value: "last visited at", |
| 38 | + Usage: "adds `PREFIX` to urldate", |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + app.Action = func(c *cli.Context) error { |
| 43 | + inputFile := "" |
| 44 | + outputFile := "new.bib" |
| 45 | + if c.NArg() > 1 { |
| 46 | + inputFile = c.Args().Get(0) |
| 47 | + outputFile = c.Args().Get(1) |
| 48 | + } else { |
| 49 | + log.Fatal("Error! Usage: urldatetonote input.bib output.bib") |
| 50 | + } |
| 51 | + prefix := c.String("prefix") |
| 52 | + bibFile, err := ioutil.ReadFile(inputFile) |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + urlDateRegexp, _ := regexp.Compile(`(?i)urldate = \{([0-9\.-/a-z]+)\}`) |
| 59 | + updatedBibtex := urlDateRegexp.ReplaceAllString(string(bibFile), "note = {"+prefix+" $1}") |
| 60 | + err = ioutil.WriteFile(outputFile, []byte(updatedBibtex), 777) |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + log.Fatal(err) |
| 64 | + } |
| 65 | + |
| 66 | + fmt.Printf("Successfully wrote new file %s", outputFile) |
| 67 | + |
| 68 | + return nil |
| 69 | + } |
| 70 | + app.Run(os.Args) |
| 71 | +} |
0 commit comments