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

Extra field (CSL note) is not parsed for addional variables #164

Open
3 tasks done
glorieux-f opened this issue Aug 7, 2023 · 1 comment
Open
3 tasks done

Extra field (CSL note) is not parsed for addional variables #164

glorieux-f opened this issue Aug 7, 2023 · 1 comment

Comments

@glorieux-f
Copy link
Contributor

glorieux-f commented Aug 7, 2023

  • I read the README and followed the instructions.
  • I am sure that the used CSL metadata follows the CSL schema.
  • I use a valid CSL stylesheet

Bug reports:

CSL format allow lots of variables: https://github.com/seboettg/citeproc-php/blob/develop/src/Util/Variables.php. Production clients for records, especially Zotero, has not a specific input for all variables. It is advised in Zotero forums to add such variables in the “Extra” field, exported as the "note" variable in CSL. It seems that citeproc-php do not parse the Zotero “Extra” field.

  • Reviewer, G. (n.d.). Review with structured fields [Genre A well known book, R. Author]. Journal.
  • Reviewer, G. (n.d.). Review, produced with zotero (extra fields) [Review of Review, produced with Zotero (extra field), R. Author]. Journal.

Bad Fix

Pre-parse the “note” variable before sending json to CiteProc.

Good fix

Correct the CSL export of Zotero if these practices are officially recommended. A post has been submitted
https://forums.zotero.org/discussion/106757/translators-csljson-export-csl-variables-from-extra-field

Nice fix

citeproc-php may parse the note field for known variables, like the CSL processor of Zotero.

Used CSL stylesheet:

apa.csl

Used CSL metadata

[
	{
		"id": "n1",
		"type": "article-journal",
		"container-title": "Journal",
		"note": "genre: Genre\nreviewed-title: A well known book",
		"title": "Review, produced with Zotero (extra field)",
		"author": [
			{
				"family": "Reviewer",
				"given": "Given"
			}
		],
		"reviewed-author": [
			{
				"family": "Author",
				"given": "Reviewed"
			}
		]
	},
	{
		"id": "n2",
		"type": "article-journal",
		"container-title": "Journal",
		"genre": "genre",
		"reviewed-title": "A well known book",
		"title": "Review with structured fields",
		"author": [
			{
				"family": "Reviewer",
				"given": "Given"
			}
		],
		"reviewed-author": [
			{
				"family": "Author",
				"given": "Reviewed"
			}
		]
	}
]
@glorieux-f
Copy link
Contributor Author

Bad fix, prepocess the note variable before submitting to citeproc-php

<?php
include "vendor/autoload.php";
use Seboettg\CiteProc\StyleSheet;
use Seboettg\CiteProc\CiteProc;
use Seboettg\CiteProc\Util\Variables;
// build dictionary of variables to extract from extra
$variables = array_flip(array_merge(Variables::NUMBER_VARIABLES, Variables::STANDARD_VARIABLE));
unset($variables['note']);
function extraVariables($item)
{
    global $variables;
    if (!isset($item->note)) return;
    $extra = $item->note;
    $note = '';
    // preg_split keep empty lines (not strtok)
    foreach(preg_split("/((\r?\n)|(\r\n?))/", $extra) as $line){
        $pos = strpos($line, ':');
        $var = trim(substr($line,0, $pos));
        if (isset($variables[$var])) {
            $item->{$var} = trim(substr($line, $pos + 1));
        }
        else {
            $note .= $line . "\n";
        }
    }
    $note = trim($note);
    if (!$note) {
        unset($item->note);
    }
    else {
        $item->note = $note;
    }
}


$data = json_decode($json);
foreach($data as $item) {
    extraVariables($item);
}
$style = StyleSheet::loadStyleSheet("apa");
$citeProc = new CiteProc($style, "en-US", $additionalMarkup);
echo $citeProc->render($data, "bibliography");

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

No branches or pull requests

1 participant