Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into macsign
Browse files Browse the repository at this point in the history
* upstream/master:
  Fix order of fields in bib file (#6750)
  Squashed 'src/main/resources/csl-styles/' changes from eb0d37e..c8c6c6d
  Try to fix cleanup PR
  • Loading branch information
Siedlerchr committed Aug 19, 2020
2 parents b5fe951 + 9adfd5b commit c5a44c4
Show file tree
Hide file tree
Showing 6 changed files with 621 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cleanup_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
id: checksecrets
shell: bash
run: |
if [ "$BUILDJABREFPRIVATEKEY" == "" ]; then
if [ -z "$BUILDJABREFPRIVATEKEY" ]; then
echo ::set-output name=secretspresent::false
else
echo ::set-output name=secretspresent::true
Expand Down
42 changes: 29 additions & 13 deletions src/main/java/org/jabref/logic/bibtex/BibEntryWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Predicate;
Expand All @@ -21,7 +24,6 @@
import org.jabref.model.entry.field.BibField;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.OrFields;
import org.jabref.model.strings.StringUtil;

public class BibEntryWriter {
Expand Down Expand Up @@ -91,30 +93,44 @@ public void writeWithoutPrependedNewlines(BibEntry entry, Writer out, BibDatabas
*/
private void writeRequiredFieldsFirstRemainingFieldsSecond(BibEntry entry, Writer out,
BibDatabaseMode bibDatabaseMode) throws IOException {
// Write header with type and bibtex-key.
// Write header with type and bibtex-key
TypedBibEntry typedEntry = new TypedBibEntry(entry, bibDatabaseMode);
out.write('@' + typedEntry.getTypeForDisplay() + '{');

writeKeyField(entry, out);

TreeSet<Field> written = new TreeSet<>(Comparator.comparing(Field::getName));
Set<Field> written = new HashSet<>();
written.add(InternalField.KEY_FIELD);
int indentation = getLengthOfLongestFieldName(entry);

Optional<BibEntryType> type = entryTypesManager.enrich(entry.getType(), bibDatabaseMode);
if (type.isPresent()) {
// Write required fields first.
for (OrFields value : type.get().getRequiredFields()) {
for (Field field : value) {
writeField(entry, out, field, indentation);
written.add(field);
}
// Write required fields first
List<Field> requiredFields = type.get()
.getRequiredFields()
.stream()
.flatMap(Collection::stream)
.sorted(Comparator.comparing(Field::getName))
.collect(Collectors.toList());

for (Field field : requiredFields) {
writeField(entry, out, field, indentation);
}
// Then optional fields.
for (BibField field : type.get().getOptionalFields()) {
writeField(entry, out, field.getField(), indentation);
written.add(field.getField());

// Then optional fields
List<Field> optionalFields = type.get()
.getOptionalFields()
.stream()
.map(BibField::getField)
.sorted(Comparator.comparing(Field::getName))
.collect(Collectors.toList());

for (Field field : optionalFields) {
writeField(entry, out, field, indentation);
}

written.addAll(requiredFields);
written.addAll(optionalFields);
}
// Then write remaining fields in alphabetic order.
SortedSet<Field> remainingFields = entry.getFields()
Expand Down
Loading

0 comments on commit c5a44c4

Please sign in to comment.