Skip to content

Commit

Permalink
fixes #1810
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed May 19, 2021
1 parent f9933de commit 96348ba
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 31 deletions.
5 changes: 3 additions & 2 deletions site/content/exporting/scripting.md
Expand Up @@ -58,14 +58,15 @@ But please fix your postscripts to test for the translator context.

The postscript should be a `javascript` snippet. You can access the data with following objects and methods:

- `reference` is the BibTeX reference you are building, and the reference has a number of fields.
- `item` is the Zotero item that's the source of the reference.
- `reference` is the BibTeX reference you are building, and the reference has a number of fields.

e.g. you can access the date in zotero item `item.date`.

- `reference.has` is a dictionary of fields for output.
- `reference.date` is the parsed and normalized version of `item.date`.

e.g. you can see whether the `year` field has been set by testing for `reference.has.year`
e.g. you can see whether the `year` field has been set by testing for `reference.has.year`, and when e.g. for a season-date only the year is exported in bibtex, you can find it in `reference.date.season`

- `reference.add` is the function to add or modify keys in `reference.has`. It accepts the following named parameters in the form of an object:

Expand Down
1 change: 1 addition & 0 deletions test/features/export.feature
Expand Up @@ -144,6 +144,7 @@ Scenario Outline: Export <references> references for BibTeX to <file>

Examples:
| file | references |
| Exporting "month = {season}" for BibTeX #1810 | 1 |
| bibtex does not export season dates | 1 |
| DOI not escaped using postscript #1803 | 1 |
| Using the Extra field in the exported Citation Key #1571 | 1 |
Expand Down
Expand Up @@ -364,7 +364,6 @@ assur\'ee par les services de traduction de l'Universit\'e McGill.},
title = {Explaining {{Behavior}}},
author = {Dretske, Fred},
year = 1993,
month = {summer},
volume = {32},
issn = {0012-2173},
groups = {Explaining Behavior (C-R et critiques)},
Expand Down Expand Up @@ -399,7 +398,6 @@ assur\'ee par les services de traduction de l'Universit\'e McGill.},
title = {Fred {{Dretske}} on the {{Explanatory Role}} of {{Semantic Content}}},
author = {Hassrick, Beth},
year = 1995,
month = {summer},
volume = {6},
pages = {59--66},
issn = {1072-1894},
Expand Down
@@ -0,0 +1,16 @@

@article{hinton88rht,
title = {Hopi {{Time}}},
author = {Hinton, Leanne},
year = 1988,
volume = {12},
pages = {361--364},
issn = {0095-182X (print); 1534-1828 (elec.)},
doi = {10.2307/1184426},
collaborator = {Malotki, Ekkehart},
journal = {American Indian Quarterly},
number = {4},
month = {fall}
}


@@ -0,0 +1,60 @@
{
"config": {
"id": "36a3b0b5-bad0-4a04-b79b-441c7cef77db",
"label": "BetterBibTeX JSON",
"localeDateOrder": "mdy",
"options": {
"exportNotes": true
},
"preferences": {
"autoAbbrev": true,
"autoExport": "idle",
"autoExportDelay": 7,
"bibtexURL": "url",
"citekeyFormat": "[authors1+.:replace=.etal,+:lower][shortyear][title:skipwords:lower:abbr]",
"exportTitleCase": false,
"keyConflictPolicy": "change",
"keyScope": "global",
"postscript": "Zotero.debug('date === ' + JSON.stringify(reference.date)); if (Translator.BetterBibTeX && reference.date.type === 'season') { reference.add({ name: 'month', value: ['', 'spring', 'summer', 'fall', 'winter'][reference.date.season] }) }",
"qualityReport": true,
"quickCopyMode": "citekeys",
"skipFields": "abstract,lccn,keywords,file,annotation,call-number"
}
},
"items": [
{
"DOI": "10.2307/1184426",
"ISSN": "0095-182X (print); 1534-1828 (elec.)",
"callNumber": "N",
"citationKey": "hinton88rht",
"creators": [
{
"creatorType": "author",
"firstName": "Leanne",
"lastName": "Hinton"
},
{
"creatorType": "reviewedAuthor",
"firstName": "Ekkehart",
"lastName": "Malotki"
}
],
"date": "1988 Autumn",
"extra": [
"Citation Key: hinton88rht"
],
"issue": "4",
"itemID": 1,
"itemType": "journalArticle",
"pages": "361-364",
"publicationTitle": "American Indian Quarterly",
"tags": [
{
"tag": "hopi language"
}
],
"title": "Hopi Time",
"volume": "12"
}
]
}
@@ -1,8 +1,8 @@

@article{ansoff1975,
title = {Managing {{Strategic Surprise}} by {{Response}} to {{Weak Signals}}},
author = {Ansoff, H. Igor},
year = 1975,
month = {winter},
volume = {18},
pages = {21--33},
publisher = {{University of California Press}},
Expand All @@ -12,3 +12,5 @@
journal = {California Management Review},
number = {2}
}


48 changes: 22 additions & 26 deletions translators/Better BibTeX.ts
Expand Up @@ -361,36 +361,32 @@ export function doExport(): void {
// #1541
if (ref.referencetype === 'inbook' && ref.has.author && ref.has.editor) delete ref.has.editor

if (item.date) {
const date = Zotero.BetterBibTeX.parseDate(item.date)
switch ((date || {}).type || 'verbatim') {
case 'verbatim':
ref.add({ name: 'year', value: item.date })
break
switch (ref.date.type) {
case 'verbatim':
ref.add({ name: 'year', value: ref.date.verbatim })
break

case 'interval':
if (date.from.month) ref.add({ name: 'month', value: months[date.from.month - 1], bare: true })
ref.add({ name: 'year', value: `${date.from.year}` })
break
case 'interval':
if (ref.date.from.month) ref.add({ name: 'month', value: months[ref.date.from.month - 1], bare: true })
ref.add({ name: 'year', value: `${ref.date.from.year}` })
break

case 'date':
if (date.month) ref.add({ name: 'month', value: months[date.month - 1], bare: true })
if (date.orig?.type === 'date') {
ref.add({ name: 'year', value: `[${date.orig.year}] ${date.year}` })
}
else {
ref.add({ name: 'year', value: `${date.year}` })
}
break
case 'date':
if (ref.date.month) ref.add({ name: 'month', value: months[ref.date.month - 1], bare: true })
if (ref.date.orig?.type === 'date') {
ref.add({ name: 'year', value: `[${ref.date.orig.year}] ${ref.date.year}` })
}
else {
ref.add({ name: 'year', value: `${ref.date.year}` })
}
break

case 'season':
ref.add({ name: 'year', value: date.year })
ref.add({ name: 'month', value: ['', 'spring', 'summer', 'fall', 'winter'][date.season] })
break
case 'season':
ref.add({ name: 'year', value: ref.date.year })
break

default:
log.debug('Unexpected date type', date)
}
default:
log.debug('Unexpected date type', { date: item.date, parsed: ref.date })
}

ref.add({ name: 'keywords', value: item.tags, enc: 'tags' })
Expand Down
3 changes: 3 additions & 0 deletions translators/bibtex/reference.ts
Expand Up @@ -6,6 +6,7 @@ declare const Zotero: any
import { Reference as Item } from '../../gen/typings/serialized-item'
import { Cache } from '../../typings/cache'
import type { Translators } from '../../typings/translators'
import type { ParsedDate } from '../../content/dateparser'

import { Translator } from '../lib/translator'

Expand Down Expand Up @@ -309,6 +310,7 @@ export class Reference {
public useprefix: boolean
public language: string
public english: boolean
public date: ParsedDate | { type: 'none' }

// patched in by the Bib(La)TeX translators
public fieldEncoding: Record<string, 'raw' | 'url' | 'verbatim' | 'creators' | 'literal' | 'latex' | 'tags' | 'attachments' | 'date'>
Expand Down Expand Up @@ -354,6 +356,7 @@ export class Reference {
constructor(item) {
this.item = item
this.packages = {}
this.date = item.date ? Zotero.BetterBibTeX.parseDate(item.date) : { type: 'none' }

if (!this.item.language) {
this.english = true
Expand Down

0 comments on commit 96348ba

Please sign in to comment.