Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep @Comment text in a bib file #1638

Merged
merged 2 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- [#1345](https://github.com/JabRef/jabref/issues/1345) Cleanup ISSN

### Fixed
- Fixed [#1632](https://github.com/JabRef/jabref/issues/1632) User comments (@Comment) with or without brackets are now kept
- Fixed [#1264](https://github.com/JabRef/jabref/issues/1264): S with caron does not render correctly
- LaTeX to Unicode converter now handles combining accents
- Fixed [#636](https://github.com/JabRef/jabref/issues/636): DOI in export filters
Expand Down
59 changes: 26 additions & 33 deletions src/main/java/net/sf/jabref/importer/fileformat/BibtexParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,22 +243,18 @@ private void parseAndAddEntry(String type) {
}

private void parseJabRefComment(Map<String, String> meta) throws IOException {
StringBuilder buffer = parseBracketedTextExactly();
/**
*
* Metadata are used to store Bibkeeper-specific
* information in .bib files.
*
* Metadata are stored in bibtex files in the format
*
* @comment{jabref-meta: type:data0;data1;data2;...}
*
* Each comment that starts with the META_FLAG is stored
* in the meta HashMap, with type as key. Unluckily, the
* old META_FLAG bibkeeper-meta: was used in JabRef 1.0
* and 1.1, so we need to support it as well. At least
* for a while. We'll always save with the new one.
*/
StringBuilder buffer = null;
try {
buffer = parseBracketedTextExactly();
} catch (IOException e) {
/* if we get an IO Exception here, than we have an unbracketed comment,
* which means that we should just return and the comment will be picked up as arbitrary text
* by the parser
*/
LOGGER.info("Found unbracketed comment");
return;
}

String comment = buffer.toString().replaceAll("[\\x0d\\x0a]", "");
if (comment.substring(0,
Math.min(comment.length(), MetaData.META_FLAG.length())).equals(
Expand Down Expand Up @@ -297,11 +293,8 @@ private void parseJabRefComment(Map<String, String> meta) throws IOException {

// custom entry types are always re-written by JabRef and not stored in the file
dumpTextReadSoFarToString();
} else {
// FIXME: user comments are simply dropped
// at least, we log that we ignored the comment
LOGGER.info("Dropped comment from database: " + comment);
}

}


Expand Down Expand Up @@ -329,14 +322,14 @@ private String dumpTextReadSoFarToString() {
// if there is no entry found, simply return the content (necessary to parse text remaining after the last entry)
if (indexOfAt == -1) {
return purgeEOFCharacters(result);
} else if(result.contains(SavePreferences.ENCODING_PREFIX)) {
} else if (result.contains(SavePreferences.ENCODING_PREFIX)) {
// purge the encoding line if it exists
int runningIndex = result.indexOf(SavePreferences.ENCODING_PREFIX);
while(runningIndex < indexOfAt) {
if(result.charAt(runningIndex) == '\n') {
while (runningIndex < indexOfAt) {
if (result.charAt(runningIndex) == '\n') {
break;
} else if(result.charAt(runningIndex) == '\r') {
if(result.charAt(runningIndex + 1) == '\n') {
} else if (result.charAt(runningIndex) == '\r') {
if (result.charAt(runningIndex + 1) == '\n') {
runningIndex++;
}
break;
Expand Down Expand Up @@ -413,10 +406,10 @@ private void skipSpace() throws IOException {

private void skipOneNewline() throws IOException {
skipSpace();
if(peek() == '\r') {
if (peek() == '\r') {
read();
}
if(peek() == '\n') {
if (peek() == '\n') {
read();
}
}
Expand Down Expand Up @@ -460,7 +453,7 @@ private int peek() throws IOException {
private int read() throws IOException {
int character = pushbackReader.read();

if(! isEOFCharacter(character)) {
if (!isEOFCharacter(character)) {
pureTextFromFile.offerLast((char) character);
}
if (character == '\n') {
Expand All @@ -474,7 +467,7 @@ private void unread(int character) throws IOException {
line--;
}
pushbackReader.unread(character);
if(pureTextFromFile.getLast() == character) {
if (pureTextFromFile.getLast() == character) {
pureTextFromFile.pollLast();
}
}
Expand Down Expand Up @@ -799,7 +792,7 @@ private String parseKey() throws IOException {
private StringBuffer parseBracketedText() throws IOException {
StringBuffer value = new StringBuffer();

consume('{','(');
consume('{', '(');

int brackets = 0;

Expand Down Expand Up @@ -834,18 +827,18 @@ private StringBuffer parseBracketedText() throws IOException {
}
}

consume('}',')');
consume('}', ')');

return value;
}

private boolean isClosingBracketNext () {
private boolean isClosingBracketNext() {
try {
int peek = peek();
boolean isCurlyBracket = peek == '}';
boolean isRoundBracket = peek == ')';
return isCurlyBracket || isRoundBracket;
} catch(IOException e) {
} catch (IOException e) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public String getUserComments() {

try {
// get the text before the entry
String prolog = parsedSerialization.substring(0, parsedSerialization.indexOf('@'));
String prolog = parsedSerialization.substring(0, parsedSerialization.lastIndexOf('@'));

// delete trailing whitespaces (between entry and text)
prolog = prolog.replaceFirst("\\s+$", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1556,4 +1556,79 @@ public void preserveEncodingPrefixInsideEntry() {
assertEquals(Collections.singletonList(expected), parsed);
}

@Test
public void parseBracketedComment() throws IOException {
String commentText = "@Comment{someComment}";
ParserResult result = BibtexParser.parse(new StringReader(commentText));

assertEquals(commentText, result.getDatabase().getEpilog());
}

@Test
public void parseRegularCommentBeforeEntry() throws IOException {
// @formatter:off
String bibtexEntry = "@Comment{someComment} " + OS.NEWLINE +
"@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
" Journal = {International Journal of Something}," + OS.NEWLINE +
" Note = {some note}," + OS.NEWLINE +
" Number = {1}" + OS.NEWLINE +
"}";
// @formatter:on

ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
Collection<BibEntry> entries = result.getDatabase().getEntries();
BibEntry entry = entries.iterator().next();

assertEquals(bibtexEntry, entry.getParsedSerialization());
}

@Test
public void parseCommentWithoutBrackets () throws IOException {
String commentText = "@Comment someComment";
ParserResult result = BibtexParser.parse(new StringReader(commentText));

assertEquals(commentText, result.getDatabase().getEpilog());
}

@Test
public void parseCommentWithoutBracketsBeforeEntry() throws IOException {
// @formatter:off
String bibtexEntry = "@Comment someComment " + OS.NEWLINE +
"@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
" Journal = {International Journal of Something}," + OS.NEWLINE +
" Note = {some note}," + OS.NEWLINE +
" Number = {1}" + OS.NEWLINE +
"}";
// @formatter:on

ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
Collection<BibEntry> entries = result.getDatabase().getEntries();
BibEntry entry = entries.iterator().next();

assertEquals(bibtexEntry, entry.getParsedSerialization());
}

@Test
public void parseCommentContainingEntries() throws IOException {
// @formatter:off
String bibtexEntry = "@Comment{@article{myarticle,}" + OS.NEWLINE +
"@inproceedings{blabla, title={the proceedings of blabla}; }" + OS.NEWLINE +
"} " + OS.NEWLINE +
"@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
" Journal = {International Journal of Something}," + OS.NEWLINE +
" Note = {some note}," + OS.NEWLINE +
" Number = {1}" + OS.NEWLINE +
"}";
// @formatter:on

ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
Collection<BibEntry> entries = result.getDatabase().getEntries();
BibEntry entry = entries.iterator().next();

assertEquals(bibtexEntry, entry.getParsedSerialization());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ @Preamble{preamble
@String{firstString = {my first string}}
@String{secondString = {}}

@Comment { this is a bracketed comment that should be preserved

Regardless auf how much space there is inbetween
}

@ARTICLE{1102917,
author = {E. Bardram},
title = {The trouble with login: on usability and computer security in ubiquitous
Expand All @@ -21,6 +26,8 @@ @ARTICLE{1102917
publisher = {Springer-Verlag}
}

@Comment this in an unbracketed comment that should be preserved as well

This is some arbitrary user comment that should be preserved

@InProceedings{1137631,
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/testbib/bibWithUserComments.bib
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ @Preamble{preamble
@String{firstString = {my first string}}
@String{secondString = {}}

@Comment { this is a bracketed comment that should be preserved

Regardless auf how much space there is inbetween
}

@ARTICLE{1102917,
author = {E. Bardram},
title = {The trouble with login: on usability and computer security in ubiquitous
Expand All @@ -21,6 +26,8 @@ @ARTICLE{1102917
publisher = {Springer-Verlag}
}

@Comment this in an unbracketed comment that should be preserved as well

This is some arbitrary user comment that should be preserved

@INPROCEEDINGS{1137631,
Expand Down