Skip to content

Commit

Permalink
Merge pull request #23 from nickng/bibtex-comment
Browse files Browse the repository at this point in the history
Bibtex comment support to make it play nice with JabRef
  • Loading branch information
nickng authored Dec 12, 2023
2 parents 443fee2 + 0bb5cd0 commit 381e70c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 77 deletions.
5 changes: 2 additions & 3 deletions bibtex.y
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var bib *BibTex // Only for holding current bib

%token tCOMMENT tSTRING tPREAMBLE
%token tATSIGN tCOLON tEQUAL tCOMMA tPOUND tLBRACE tRBRACE tDQUOTE tLPAREN tRPAREN
%token <strval> tBAREIDENT tIDENT
%token <strval> tBAREIDENT tIDENT tCOMMENTBODY
%type <bibtex> bibtex
%type <bibentry> bibentry
%type <bibtag> tag stringentry
Expand All @@ -47,8 +47,7 @@ bibentry : tATSIGN tBAREIDENT tLBRACE tBAREIDENT tCOMMA tags tRBRACE { $$ = NewB
| tATSIGN tBAREIDENT tLPAREN tBAREIDENT tCOMMA tags tRPAREN { $$ = NewBibEntry($2, $4); for _, t := range $6 { $$.AddField(t.key, t.val) } }
;

commententry : tATSIGN tCOMMENT tLBRACE longstring tRBRACE {}
| tATSIGN tCOMMENT tLPAREN longstring tRBRACE {}
commententry : tATSIGN tCOMMENT tCOMMENTBODY { }
;

stringentry : tATSIGN tSTRING tLBRACE tBAREIDENT tEQUAL longstring tRBRACE { $$ = &bibTag{key: $4, val: $6 } }
Expand Down
111 changes: 52 additions & 59 deletions bibtex.y.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions bibtex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestParser(t *testing.T) {

for _, ex := range examples {
t.Logf("Parsing example: %s", ex)
b, err := ioutil.ReadFile(ex)
b, err := os.ReadFile(ex)
if err != nil {
t.Errorf("Cannot read %s: %v", ex, err)
}
Expand All @@ -89,12 +89,13 @@ func TestMultiParse(t *testing.T) {
"example/simple.bib",
"example/simple.bib",
"example/simple.bib",
"example/simple2.bib", // simple but with comment
}

var bibs []*BibTex
for _, ex := range examples {
t.Logf("Parsing example: %s", ex)
b, err := ioutil.ReadFile(ex)
b, err := os.ReadFile(ex)
if err != nil {
t.Errorf("Cannot read %s: %v", ex, err)
}
Expand Down Expand Up @@ -122,7 +123,7 @@ func TestPrettyStringRoundTrip(t *testing.T) {

for _, ex := range examples {
// Read input.
b, err := ioutil.ReadFile(ex)
b, err := os.ReadFile(ex)
if err != nil {
t.Fatal(err)
}
Expand All @@ -148,7 +149,7 @@ func TestPrettyStringRoundTrip(t *testing.T) {

func TestUnexpectedAtSign(t *testing.T) {
// Tests correct syntax but scanning error
b, err := ioutil.ReadFile("example/unexpected-at-sign.badbib")
b, err := os.ReadFile("example/unexpected-at-sign.badbib")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -191,7 +192,7 @@ func AssertEntriesEqual(t *testing.T, a, b *BibEntry) {
}

func BenchmarkStringPerformance(b *testing.B) {
exampleFileBytes, err := ioutil.ReadFile("example/biblatex-examples.bib")
exampleFileBytes, err := os.ReadFile("example/biblatex-examples.bib")
if err != nil {
b.Fatal(err)
}
Expand Down
13 changes: 6 additions & 7 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// The package contains a simple parser and data structure to represent bibtex
// records.
//
// Supported syntax
// # Supported syntax
//
// The basic syntax is:
//
// @BIBTYPE{IDENT,
// key1 = word,
// key2 = "quoted",
// key3 = {quoted},
// }
// @BIBTYPE{IDENT,
// key1 = word,
// key2 = "quoted",
// key3 = {quoted},
// }
//
// where BIBTYPE is the type of document (e.g. inproceedings, article, etc.)
// and IDENT is a string identifier.
Expand All @@ -20,5 +20,4 @@
// found in the link below. If there are any problems, please file any issues
// with a minimal working example at the GitHub repository.
// http://maverick.inria.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html
//
package bibtex // import "github.com/nickng/bibtex"
16 changes: 16 additions & 0 deletions example/simple2.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@article{name,
year = 2016,
title = {SessionBasedBlah}
}

@comment{
this looks like an entry
but is actually a comment
even if the braces don't
balance it's still valid {}

@inproceedings{ng2014,
title = "Blah",
author = "Me",
booktitle = "ABCD2014"
}
Loading

0 comments on commit 381e70c

Please sign in to comment.