Skip to content

Commit 9315c59

Browse files
author
Joshua Vécsei
committed
init
1 parent 5873c37 commit 9315c59

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Replace BibTex urldate with note
2+
3+
Automatically replaces the BibTex urldates with note attributes for all BibTex entries to show the date in predefined templates which do not support the urldate attribute by default.
4+
5+
6+
7+
```latex
8+
@phdthesis{key,
9+
author = {name},
10+
title = {title},
11+
url = {http://example.com},
12+
urldate = {02/18/2018}
13+
}
14+
```
15+
16+
Will be converted to
17+
18+
```latex
19+
@phdthesis{key,
20+
author = {name},
21+
title = {title},
22+
url = {http://example.com},
23+
note = {last visited at 02/18/2018}
24+
}
25+
```
26+
27+
The prefix can be passed as an argument to the command-line interface.
28+
29+
30+
31+
## Usage
32+
33+
`urldatetonote -h`
34+
35+
### Example
36+
37+
`urldatetonote --prefix "last visited at" input.bib output.bib`
38+
39+
The prefix is an optional argument at defaults to "last visited at".
40+
41+
42+
43+
## Build
44+
45+
`go build urldatetonote.go`
46+
47+
or
48+
49+
`go install` to install the binary to your `bin` directory.

urldatetonote.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)