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

process extra fields #3152

Closed
wants to merge 10 commits into from
Closed

process extra fields #3152

wants to merge 10 commits into from

Conversation

retorquere
Copy link
Contributor

Brings the extra-fields into the conversion process.

@retorquere retorquere closed this Sep 27, 2023
@retorquere retorquere reopened this Sep 27, 2023
@zoe-translates
Copy link
Collaborator

Hello @retorquere,

Am I right in understanding that this is intended to bring the records in the Extra field into the item object as properties, so those extra fields with proper BibTeX counterparts could get a chance of getting into the BibTeX record rather than the "notes" text?

@retorquere
Copy link
Contributor Author

That is correct.

@retorquere
Copy link
Contributor Author

Although come to think of it, I'd also have to pick up the CSL fields in extra.

@zoe-translates
Copy link
Collaborator

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).

@retorquere
Copy link
Contributor Author

I wonder if there's more succinct way to achieve this goal

There is, I'll push that tonight.

@retorquere
Copy link
Contributor Author

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.

@retorquere
Copy link
Contributor Author

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] || [])) {
Copy link
Collaborator

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).

Copy link
Contributor Author

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) {
Copy link
Collaborator

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).

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'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);
Copy link
Collaborator

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)

Copy link
Contributor Author

@retorquere retorquere Oct 12, 2023

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

@zoe-translates zoe-translates Oct 17, 2023

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)?

Copy link
Contributor Author

@retorquere retorquere Oct 17, 2023

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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 = {
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@dstillman
Copy link
Member

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.

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.

@retorquere
Copy link
Contributor Author

I'm with you on that. So, what now?

@retorquere retorquere closed this by deleting the head repository Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants