-
Notifications
You must be signed in to change notification settings - Fork 770
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
process extra fields #3152
process extra fields #3152
Conversation
Hello @retorquere, Am I right in understanding that this is intended to bring the records in the |
That is correct. |
Although come to think of it, I'd also have to pick up the CSL fields in |
Ah, I see, thanks. I wonder if there's more succinct way to achieve this goal, because I see a lot of duplication in the added code, which I fear might be prone to error if someone else is to work on them in the future. It would also be great to have more comments about what the code does. (To be fair there's too few comments to begin with in those translators). |
There is, I'll push that tonight. |
BTW personally I think this is a general feature across translators perhaps better handled in the framework rather than in these translators, but I can't oversee the implications of doing that. |
Also BTW the BibTeX.js translator has a fair number of pre-existing linting errors/warnings. I'd be happy to address those, but I think that is better handled in a separate PR, or reviewing would become hard. |
BibLaTeX.js
Outdated
const rec = { raw: line }; | ||
const kv = line.match(/^([^:]+)\s*:\s*(.+)/); | ||
if (kv) { | ||
for (const field of (extra.field[field.field] || [])) { |
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.
This is an error (using the variable field
before the declaration const field
).
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.
You're right -- sorry about that, that's now corrected.
BibLaTeX.js
Outdated
} | ||
}; | ||
|
||
function parseExtraFields(item) { |
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 think I get what you mean here using the filter()
to sieve through the extra fields. But this is going to be confusing because it may not be obvious that we're relying on the side effect of modifying item
when calling this function. Please at least document this function (it's purpose and how it achieves that).
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've added comments, and renamed the function to better reflect what it does.
BibTeX.js
Outdated
@@ -1505,7 +1680,7 @@ function doExport() { | |||
var extra = extraFieldsToString(extraFields); // Make sure we join exactly with what we split | |||
if (extra) writeField("note", extra); |
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.
Here, you're writing what you have filtered from the original item's extra (extraFields
) back into the Bib(La)TeX note
field. I think this is why the generated export record has duplicate lines in the note
. You'll likely want to replace this with if (item.extra) writeField("note", item.extra);
outside the conditional if (extraFields)
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.
This isn't my code though, that was already present before my change, so I must assume that that is intentional. Personally I don't see a good reason for them to be there, but I don't know what the use-case behind this was.
I don't think they are duplicate though? They're reproduced there which I didn't expect, but not twice.
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.
They're reproduced there which I didn't expect, but not twice.
Have you tried resetting the translators in Zotero client after updating the code?
I did and I can reproduce the behaviour (duplicated fields from the extra).
The reason I suggest changing this part is that the code made sense before you implemented the filter-and-update step. It converted the extraFields
array back into a string so that it would be dumped into the note
field of the BibTeX output.
With your changes, the meaning of extraFields
has changed. It is no longer a 1-1 copy of the item.extra
record but converted to array; it now contains the relevant lines in item.extra
already converted to "proper" fields (and removed from the original item.extra
string). So the line
if (extra) writeField("note", extra);
dumps those already-converted fields into the note
field. That's why this part of the code should be changed after your other changes.
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.
Wouldn't resetting the translators reset them to the released version rather than my (still local) changes?
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.
Hm, sorry, I wasn't clear. What is your procedure for testing the updated doExport()
function? Do you do this in Scaffold (the do*
/lightening button) or in the main client (saving exported files)?
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.
Is scaffold really the only reliable way to test translators? I work from git and in the past having scaffold in the process was a bit of a pain.
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.
Can I even add export tests to run in scaffold?
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.
Alternatively, you can do live testing as follows: select an item in your library, go to Scaffold and click on the "do*/lightening" button, and inspect the output. This will execute the live code being edited even if it hasn't been saved.
I've ran this procedure and even though I'm of course still seeing extracted fields appear in the note field (which I'll change), I don't see them turning there up twice, which is what I thought you meant.
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.
Translators are updated, fields are no longer written back into the note field.
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.
It looks like scaffold trimmed (existing, not mine) lines with trailing whitespace, so the diff is larger than it used to be.
BibLaTeX.js
Outdated
rec.value = line.substr(splitAt + 1).trim(); | ||
} | ||
fields.push(rec); | ||
const extra = { |
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.
Here, it's probably necessary to clearly mark this object as a "global" (top-level to the translator script) with ALL_CAPS
and more descriptive name. There are too many variables/properties named "extra" or similar.
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.
done
Yeah, sorry to come in late here, and thanks so much for working on this, but this really doesn't belong in individual translators. If we want to say that translators should automatically get converted fields from Extra, we should just do that in the core, where we already have CSL mappings and code to extract built-in and CSL fields from Extra. Code duplication aside, these mappings are bound to just get out of date. |
I'm with you on that. So, what now? |
Brings the extra-fields into the conversion process.