Skip to content

Fix IEEE preview does not display month #3983

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

Merged
merged 9 commits into from
May 2, 2018

Conversation

DevSiroukane
Copy link
Contributor

@DevSiroukane DevSiroukane commented Apr 26, 2018

Fix issue #3239 on maintable-beta branch

On month change from comboBox on OptionalFieldsTab, Month was not appear on IEEE style.
The issue get fixed.


  • Change in CHANGELOG.md described
  • Tests created for changes
  • Manually tested changed features in running JabRef
  • Screenshots added in PR description (for bigger UI changes)
  • Ensured that the git commit message is a good one
  • Check documentation status (Issue created for outdated help page at help.jabref.org?)

On month change from comboBox on OptionalFieldsTab:
1. Month was not appear on IEEE style.
2. Month was not written correctly on SourceTab.
both issues get fixed.
@DevSiroukane DevSiroukane changed the title Fix IEEE preview does not display month (#3239) Fix IEEE preview does not display month Apr 26, 2018
// create a copy of bibEntry
bibEntry = (BibEntry) bibEntry.clone();
// change month field from "#mon#" to "mon"
if((bibEntry.getFieldMap().get(FieldName.MONTH) != null) && !bibEntry.getFieldMap().get(FieldName.MONTH).isEmpty()){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually move this in the loop below. Has the advantage that you don't need to clone the entry.
And then you could try it using this way:

  if (FieldName.MONTH.equals(key)) {
                    bibEntry.getMonth().map(Month::getShortName).ifPresent(value -> bibTeXEntry.addField(new Key(key), new DigitStringValue(value)));
                }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test if this works?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or add LatexFieldFormatter fieldFormatter = new LatexFieldFormatter(...) in front of the for loop and then add .map(fieldFormatter ::format). In this way, we also support custom bibtex strings (and not only the predefined ones for the month field).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not work, as LatexFieldFormatter a) needs preferences object b) has only a two argument format method and does not implment the interface

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but it works "with obvious modifications" 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Siedlerchr I try ur suggestion but it didnt work.
I can also prefer your solution but instead of making this instruction inside if block

bibEntry.getMonth().map(Month::getShortName).ifPresent(value -> bibTeXEntry.addField(new Key(key), new DigitStringValue(value)));

I will keep the instruction that I made.

bibEntry.getFieldMap().put(FieldName.MONTH, bibEntry.getMonth().get().getShortName());

What do you think?

@@ -270,8 +270,8 @@ private void writeText(String text, int startPos, int endPos) {

private void writeStringLabel(String text, int startPos, int endPos,
boolean first, boolean last) {
putIn((first ? "" : " # ") + text.substring(startPos, endPos)
+ (last ? "" : " # "));
putIn((first ? "{" : " # ") + text.substring(startPos, endPos)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the reason for this change. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method were create only for "#%s#" forma I guess. The output should be "{%s}" to have this result for month

month = {mon}

not this

month = mon

check this on #3239

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh, I'm pretty sure that month = jan is the correct form and not month = {jan}. Maybe @koppor can clarify this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here what happen if you did not add {}.
image

as you can see the return of 2005 is in side {} but the return of month isnt it.

the method executed to return year = {2005}
https://github.com/DevSiroukane/jabref/blob/67387113513ce9529e08b62ffc430d13e24f0b4b/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java#L157

the method executed to return month = mon when month field is in this forma "#%s#".
https://github.com/DevSiroukane/jabref/blob/67387113513ce9529e08b62ffc430d13e24f0b4b/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java#L163

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we know that bibTeX forma should be

key = {value}

u can add a debug instruction here LatexFieldFormatter.java#L166

System.out.println(stringBuilder);

and see the out put after each change in any field. You will find that only month output is wrong if we miss the {} that I add on

LatexFieldFormatter.java#writeStringLabel(String text, int startPos, int endPos, boolean first, boolean last)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DevSiroukane You are confusing it here. If it's #month# it should be displayed without braces in the source. So the entry editor part was already correct.
Only when it's month -> {month}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Siedlerchr are you sure that month should not be between {} on source tab?

because I try to use generate entry using DOI (ex: 10.1109/TSG.2014.2346511) and the out put is this on source tab:

@Article{Rahbari-Asr2014,
  author    = {Navid Rahbari-Asr and Unnati Ojha and Ziang Zhang and Mo-Yuen Chow},
  title     = {Incremental Welfare Consensus Algorithm for Cooperative Distributed Generation/Demand Response in Smart Grid},
  journal   = {{IEEE} Transactions on Smart Grid},
  year      = {2014},
  volume    = {5},
  number    = {6},
  pages     = {2836--2845},
  month     = {nov},
  doi       = {10.1109/tsg.2014.2346511},
  publisher = {Institute of Electrical and Electronics Engineers ({IEEE})},
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the month does not need be in braces. Its a feature of bibtex called "Bibtex Strings", see e.g. http://www.bibtex.org/Format/.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Siedlerchr I think I should let source tab month appear as before

month = mon

I will remove the change as you said @tobiasdiez

// create a copy of bibEntry
bibEntry = (BibEntry) bibEntry.clone();
// change month field from "#mon#" to "mon"
if((bibEntry.getFieldMap().get(FieldName.MONTH) != null) && !bibEntry.getFieldMap().get(FieldName.MONTH).isEmpty()){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or add LatexFieldFormatter fieldFormatter = new LatexFieldFormatter(...) in front of the for loop and then add .map(fieldFormatter ::format). In this way, we also support custom bibtex strings (and not only the predefined ones for the month field).

@Siedlerchr
Copy link
Member

Please have a look at the Travis output, regarding the checkstyle, there is still some error

@@ -84,13 +85,22 @@ private void initialize(String newStyle, CitationStyleOutputFormat newFormat) th
* Converts the {@link BibEntry} into {@link CSLItemData}.
*/
private static CSLItemData bibEntryToCSLItemData(BibEntry bibEntry) {

// create a copy of bibEntry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment states the same as it is done in the code. So please "Remove Superfluous Comments" Java by Comparison.

You can also describe, why a clone is generated!

Copy link
Contributor Author

@DevSiroukane DevSiroukane Apr 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a good comment?

/**
 * Converts the {@link BibEntry} into {@link CSLItemData}.
 *
 * Clone bibEntry to make a save changes,
 * change month field from JabRefFormat <code>#mon#</> to ShortName <code>mon</code>
 * because CSL does not support JabRefFormat.
 */

Clone was generated because bibEntry is shared with many objects. So, to be sure that non of this objects may have issue after changing month field. I create a copy to change on it.

String citeKey = bibEntry.getCiteKeyOptional().orElse("");
BibTeXEntry bibTeXEntry = new BibTeXEntry(new Key(bibEntry.getType()), new Key(citeKey));

// Not every field is already generated into latex free fields
HTMLChars latexToHtmlConverter = new HTMLChars();
RemoveNewlinesFormatter removeNewlinesFormatter = new RemoveNewlinesFormatter();
for (String key : bibEntry.getFieldMap().keySet()) {
if (FieldName.MONTH.equals(key)) {
// change month field value from "#mon#" to "mon"
Copy link
Member

@koppor koppor Apr 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this hack is necessary at this place.

In case a field value is surrounded by #, JabRef stores the value as follows:

month = mar

and not

month = #mar#

See http://help.jabref.org/en/EntryEditor#field-consistency-checking for an explanation. So, I think, either the wrong method is used to get the month or there is something more wrong on JabRef: All other exports should have difficulties.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually necessary. The problem is that csl can not handle the JabRef special Syntax with the #. Therefore the value of the month field without the # is needed.

For export usually a latex formatter is applied which takes care of this

Copy link
Contributor Author

@DevSiroukane DevSiroukane Apr 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koppor As CSL is not part of our code we cannot inject instructions that handle #mon# forma.
The issue on CSL plugin is still open Bibtex month like #mar# not parsed #41

Copy link
Member

@tobiasdiez tobiasdiez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for you patients. The solution you suggests works, but is a bit more complicate than it need to. Below I outline a simpler approach. Could you please try to implement it. Thanks!

Moreover, please add a test to https://github.com/JabRef/jabref/blob/master/src/test/java/org/jabref/logic/citationstyle/CitationStyleTest.java which verifies that months are indeed successfully added.

if (FieldName.MONTH.equals(key)) {
bibEntry.getFieldMap().put(FieldName.MONTH, bibEntry.getMonth().get().getShortName());
}

bibEntry.getField(key)
.map(removeNewlinesFormatter::format)
.map(latexToHtmlConverter::format)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code changes above work but are more complicated than they need to. I think an easier solution is to extract the lambda expression in this line (ifPresent(value -> ...)) to a new method setFieldValue(bibTeXEntry, key, value). In this new method, you can then simply check if (FieldName.MONTH.equals(key)) { and use Month.parse to get the correct month value.

Copy link
Contributor Author

@DevSiroukane DevSiroukane Apr 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not notice that bibEntry and BibTeXEntry are different object. I think we do not need to clone bibEntry anymore.
Instead of create a new method setFieldValue(...) I thought of making if block direct on lamda expression. What do you think?

I execute this code and it work.

private static CSLItemData bibEntryToCSLItemData(BibEntry bibEntry) {
    String citeKey = bibEntry.getCiteKeyOptional().orElse("");
    BibTeXEntry bibTeXEntry = new BibTeXEntry(new Key(bibEntry.getType()), new Key(citeKey));

    // Not every field is already generated into latex free fields
    HTMLChars latexToHtmlConverter = new HTMLChars();
    RemoveNewlinesFormatter removeNewlinesFormatter = new RemoveNewlinesFormatter();
    for (String key : bibEntry.getFieldMap().keySet()) {
        bibEntry.getField(key)
                .map(removeNewlinesFormatter::format)
                .map(latexToHtmlConverter::format)
                .ifPresent(value -> {
                    if((FieldName.MONTH.equals(key)) && (value.matches("#[a-z]{3}#"))){
                        value = value.substring(1,4);
                    }
                    bibTeXEntry.addField(new Key(key), new DigitStringValue(value));
                });
    }
    return BIBTEX_CONVERTER.toItemData(bibTeXEntry);
}

About https://github.com/JabRef/jabref/blob/master/src/test/java/org/jabref/logic/citationstyle/CitationStyleTest.java I have no idea what I can do if you help me please. I am not used with test on java yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is also an idea, but I would not make the regex. If you have the field month, I would really get the shortname from the BibEntry then, so value = key.getMonth().getShortName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (String key : bibEntry.getFieldMap().keySet()) {
    bibEntry.getField(key)
            .map(removeNewlinesFormatter::format)
            .map(latexToHtmlConverter::format)
            .ifPresent(value -> {
                if(FieldName.MONTH.equals(key)){
                    value = bibEntry.getMonth().get().getShortName();
                }
                bibTeXEntry.addField(new Key(key), new DigitStringValue(value));
            });
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good, we are almost there. Please replace the value assignment line by

value = bibEntry.getMonth().map(Month::getShortName).orElse(value);

Background: the get method for an Optional throws an null pointer exception if the optional is empty. Thus, one has to check isPresent before using get or, alternatively, use orElse instead.

Please push the change to your branch and then we can merge!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just post my last request

Copy link
Member

@tobiasdiez tobiasdiez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for you contribution, and especially for you patience and the many quick follow-ups!

The code looks good to me and I'll hence merge it now!

@tobiasdiez tobiasdiez merged commit 147ffca into JabRef:maintable-beta May 2, 2018
@DrSiroukane
Copy link

Thank you, It's nice to work with all of you. I learn a lot.

Siedlerchr added a commit that referenced this pull request May 5, 2018
…rsectionnew

* upstream/maintable-beta: (88 commits)
  set look and feel to windows, aqua or nimbus for swing in case
  fix import
  remove look and feel
  New translations JabRef_en.properties (French) (#4009)
  Fix Look and Feel related issues (#4002)
  Fix statement in changelog
  [WIP] Add Text File Export for "Find Unlinked Files" (#3979)
  Fix IEEE preview does not display month  (#3983)
  Activate context menu on key press (#4004)
  Improve code layout
  Fix concurrent modification exception when adding entries to groups
  Fix build
  Typo
  Add fix
  Rename test
  Fix #3994 Duplicate unmodifiable list for sorting (#3996)
  Remove deprecated and unused method (#3993)
  Improvements around external file types (#3887)
  Migrate to native gradle test task (#3987)
  Do not run checkstyle as part of the gradle check task (#3985)
  ...
Siedlerchr added a commit that referenced this pull request May 10, 2018
…drop

* upstream/maintable-beta: (174 commits)
  Fix ACM fetcher import of entries (#4019)
  Reimplement tooltips for file and identifier columns (#4011)
  Add button-icon for union/intersection in the groups side panel (#3954)
  Update Dependencies (#4012)
  set look and feel to windows, aqua or nimbus for swing in case
  fix import
  remove look and feel
  New translations JabRef_en.properties (French) (#4009)
  Fix Look and Feel related issues (#4002)
  Fix statement in changelog
  [WIP] Add Text File Export for "Find Unlinked Files" (#3979)
  Fix IEEE preview does not display month  (#3983)
  Activate context menu on key press (#4004)
  Improve code layout
  Fix concurrent modification exception when adding entries to groups
  Fix build
  Typo
  Add fix
  Rename test
  Fix #3994 Duplicate unmodifiable list for sorting (#3996)
  ...

# Conflicts:
#	src/main/java/org/jabref/gui/actions/CleanupAction.java
#	src/main/java/org/jabref/gui/maintable/MainTable.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants