-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Preserve user comments in bib file #1471
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
Changes from 29 commits
d6d6721
cb85443
b50c3d6
2c39c89
b547703
ce56e23
fe79b41
ec589e8
c7f4694
3957680
399bcb9
14db4bf
392864e
9c23e94
21eac95
3206b29
a1c98c5
678c83e
7a3522a
51d82a2
1dca1a7
cee181f
6496190
afea9b0
dc4ae0f
ccddef0
6d1ac65
0972b51
b4b288a
67a4885
9000f1c
d970a4c
c1db48f
75d6262
98cbe98
72de4ce
e5e5c44
a8a7b20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,4 +152,28 @@ public String getParsedSerialization() { | |
public boolean hasChanged(){ | ||
return hasChanged; | ||
} | ||
|
||
/* | ||
* Returns user comments (arbitrary text before the string) if there are any. If not returns the empty string | ||
*/ | ||
public String getUserComments() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now an exact copy of BibEntry.getUserComments right? Maybe add a superclass BibItem containing this method and then BibEntry and BibString derive from this superclass (maybe there is even more common code which could be extracted to the super class) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to always slip into the role of your antagonist, but I am against creating a class hierarchy. In most cases, it makes the code harder to understand, and the code duplication savings are just not worth the effort. In addition, one has to adhere to the liskov substitution principle to create a good class hierarchy, but this is hard to do right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, having an experienced antagonist is probably the best way to learn something 😄 So now ignoring everything about the actual implementation and only speaking about "business objects": There are different items which can be contained in a bib file. For example My idea would be: BibString, BibEntry, BibComment all derive from BibItem. BibItem has methods to parse and write at least the form |
||
if(parsedSerialization != null) { | ||
|
||
try { | ||
// get the text before the string | ||
String prolog = parsedSerialization.substring(0, parsedSerialization.indexOf('@')); | ||
|
||
// delete trailing whitespaces (between string and text) | ||
prolog = prolog.replaceFirst("\\s+$", ""); | ||
// if there is any non whitespace text, write it with proper line separation | ||
if (prolog.length() > 0) { | ||
return prolog; | ||
} | ||
} catch(StringIndexOutOfBoundsException ignore) { | ||
// if this occurs a broken parsed serialization has been set, so just do nothing | ||
} | ||
} | ||
|
||
return ""; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -291,6 +291,82 @@ public void roundtrip() throws IOException { | |||
} | ||||
} | ||||
|
||||
@Test | ||||
public void roundtripWithUserComment() throws IOException { | ||||
Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); | ||||
Charset encoding = StandardCharsets.UTF_8; | ||||
ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); | ||||
|
||||
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); | ||||
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), | ||||
new Defaults(BibDatabaseMode.BIBTEX)); | ||||
|
||||
databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); | ||||
try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { | ||||
assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
public void roundtripWithUserCommentAndEntryChange() throws IOException { | ||||
Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); | ||||
Charset encoding = StandardCharsets.UTF_8; | ||||
ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); | ||||
|
||||
BibEntry entry = result.getDatabase().getEntryByKey("1137631"); | ||||
entry.setField("author", "Mr. Author"); | ||||
|
||||
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); | ||||
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), | ||||
new Defaults(BibDatabaseMode.BIBTEX)); | ||||
|
||||
databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); | ||||
|
||||
try (Scanner scanner = new Scanner(Paths.get("src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib"),encoding.name())) { | ||||
assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
public void roundtripWithUserCommentBeforeString() throws IOException { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like an exact copy of
|
||||
Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); | ||||
Charset encoding = StandardCharsets.UTF_8; | ||||
ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); | ||||
|
||||
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); | ||||
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), | ||||
new Defaults(BibDatabaseMode.BIBTEX)); | ||||
|
||||
databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); | ||||
try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { | ||||
assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { | ||||
Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); | ||||
Charset encoding = StandardCharsets.UTF_8; | ||||
ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); | ||||
|
||||
BibtexString string = result.getDatabase().getStringValues().iterator().next(); | ||||
if(string.getContent().isEmpty()) { | ||||
// do nothing | ||||
} else { | ||||
string.setContent("my first string"); | ||||
} | ||||
|
||||
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); | ||||
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), | ||||
new Defaults(BibDatabaseMode.BIBTEX)); | ||||
|
||||
databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); | ||||
|
||||
try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { | ||||
assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); | ||||
} | ||||
} | ||||
|
||||
@Test | ||||
public void writeSavedSerializationOfEntryIfUnchanged() throws IOException { | ||||
BibEntry entry = new BibEntry(); | ||||
|
@@ -543,4 +619,5 @@ public void writeEntriesInOriginalOrderWhenNoSaveOrderConfigIsSetInMetadata() th | |||
+ Globals.NEWLINE | ||||
, stringWriter.toString()); | ||||
} | ||||
|
||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is an install4j change part of this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I revert it later, sorry...