Skip to content

Commit

Permalink
damn i got pretty far
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Feb 24, 2023
0 parents commit c60d59b
Show file tree
Hide file tree
Showing 13 changed files with 1,107 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"extends": [
"../../../.eslintrc.json"
],
"ignorePatterns": [
"!**/*"
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"rules": {}
},
{
"files": [
"*.ts",
"*.tsx"
],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"rules": {}
}
]
}
32 changes: 32 additions & 0 deletions .lib.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"target": "es2017",
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true
},

"externalHelpers": true,
"loose": true
},
"module": {
"type": "es6",
"strict": true,
"noInterop": true
},
"sourceMaps": true,
"exclude": [
"jest.config.ts",
".*.spec.tsx?$",
".*.test.tsx?$",
"./src/jest-setup.ts$",
"./**/jest-setup.ts$",
".*.js$"
]
}
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# csl-to-biblatex

Somewhat jank CSL-JSON to biblatex converter

## Contents

- [csl-to-biblatex](#csl-to-biblatex)
- [Contents](#contents)
- [What is this?](#what-is-this)
- [When should I use this?](#when-should-i-use-this)
- [Install](#install)
- [Use](#use)
- [API](#api)
- [Syntax tree](#syntax-tree)
- [Types](#types)
- [Compatibility](#compatibility)
- [Security](#security)
- [Related](#related)
- [Contribute](#contribute)
- [License](#license)

## What is this?

## When should I use this?

## Install

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). In Node.js (version 12.20+, 14.14+, or 16.0+), install with [yarn](https://classic.yarnpkg.com/en/docs/cli/add)

```sh
yarn add csl-to-biblatex
```

or with [npm](https://docs.npmjs.com/cli/install)

```sh
npm install csl-to-biblatex
```

## Use

## API

## Syntax tree

## Types

## Compatibility

## Security

## Related

## Contribute

## License

GPL-3.0-or-later © Thomas F. K. Jorna

[unified]: https://unifiedjs.com
[unifiedgh]: https://github.com/unifiedjs/unified
[xast-from-xml]: https://github.com/syntax-tree/xast-util-from-xml
[rehype]: https://github.com/rehypejs/rehype
[rejour]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour
[rejour-parse]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour/rejour-parse
[rejour-stringify]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour/rejour-stringify
[rejour-move-abstract]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour/rejour-move-abstract
[rejour-meta]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour/rejour-meta
[rejour-relatex]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour/rejour-relatex
[relatex]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/relatex
[relatex-parse]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/relatex/relatex-parse
[jast]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour/jast
[jast-util-to-texast]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour/jast-util-to-texast
[jastscript]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/rejour/jastscript
[texast]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/relatex/texast
[texast-util-to-latex]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/relatex/texast-util-to-latex
[hast]: https://github.com/syntax-tree/hast
[xast]: https://github.com/syntax-tree/xast
[mdast]: https://github.com/syntax-tree/mdast
[mdast-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
[latex-utensils]: https://github.com/tamuratak/latex-utensils
[latexjs]: https://github.com/latexjs/latexjs
[reoff]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/reoff
[reoff-parse]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/reoff/reoff-parse
[reoff-rejour]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/reoff/reoff-rejour
[ooxast]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/ooxast/ooxast
[ooxast]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/ooxast/ooxast-util-to-jast
[csl-to-biblatex]: https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/csl-to-biblatex
23 changes: 23 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const config = {
displayName: 'csl-to-biblatex',
preset: '../../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
useESM: true,
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': ['@swc/jest'],
},
transformIgnorePatterns: [],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../../coverage/libs/csl-to-biblatex',
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
}

export default config
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "csl-to-biblatex",
"version": "0.0.1",
"license": "GPL-3.0-or-later",
"repository": "https://github.com/TrialAndErrorOrg/parsers",
"author": "Thomas F. K. Jorna <[email protected]>",
"type": "module",
"description": "Somewhat jank CSL-JSON to biblatex converter",
"keywords": [
"unified",
"abstract",
"syntax",
"tree",
"ast"
]
}
51 changes: 51 additions & 0 deletions project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "citations-csl-to-biblatex",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/citations/csl-to-biblatex/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nrwl/js:swc",
"outputs": [
"{options.outputPath}"
],
"options": {
"outputPath": "dist/libs/citations/csl-to-biblatex",
"tsConfig": "libs/citations/csl-to-biblatex/tsconfig.lib.json",
"packageJson": "libs/citations/csl-to-biblatex/package.json",
"main": "libs/citations/csl-to-biblatex/src/index.ts",
"assets": [
"libs/citations/csl-to-biblatex/*.md"
]
}
},
"deploy": {
"executor": "ngx-deploy-npm:deploy",
"options": {
"access": "public"
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": [
"{options.outputFile}"
],
"options": {
"lintFilePatterns": [
"libs/citations/csl-to-biblatex/**/*.ts"
]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": [
"{workspaceRoot}/coverage/{projectRoot}"
],
"options": {
"jestConfig": "libs/citations/csl-to-biblatex/jest.config.ts",
"passWithNoTests": true
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/csl-to-biblatex'
7 changes: 7 additions & 0 deletions src/lib/csl-to-biblatex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { cslToBiblatex } from './csl-to-biblatex';

describe('cslToBiblatex', () => {
it('should work', () => {
expect(cslToBiblatex()).toEqual('csl-to-biblatex');
})
})
138 changes: 138 additions & 0 deletions src/lib/csl-to-biblatex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { Data as CSL, ItemType } from 'csl-json'
const biblatexCSLMap: {
source: Record<string, ItemType>
target: Record<ItemType, string>
} = {
source: {
article: 'article-journal',
book: 'book',
mvbook: 'book',
inbook: 'chapter',
bookinbook: 'book',
booklet: 'book',
collection: 'book',
mvcollection: 'book',
incollection: 'chapter',
dataset: 'dataset',
online: 'webpage',
patent: 'patent',
periodical: 'article-journal',
proceedings: 'book',
mvproceedings: 'book',
inproceedings: 'paper-conference',
reference: 'book',
mvreference: 'book',
inreference: 'entry',
report: 'report',
software: 'book',
thesis: 'thesis',
unpublished: 'manuscript',
artwork: 'graphic',
audio: 'song',
image: 'figure',
jurisdiction: 'legal_case',
legislation: 'legislation',
legal: 'treaty',
letter: 'personal_communication',
movie: 'motion_picture',
music: 'musical_score',
review: 'review',
video: 'motion_picture',
conference: 'paper-conference',
electronic: 'webpage',
mastersthesis: 'thesis',
phdthesis: 'thesis',
techreport: 'report',
www: 'webpage',
},
target: {
article: 'article',
'article-journal': 'article',
'article-magazine': 'article',
'article-newspaper': 'article',
bill: 'legislation',
book: 'book',
broadcast: 'audio',
chapter: 'inbook',
dataset: 'dataset',
entry: 'inreference',
'entry-dictionary': 'inreference',
'entry-encyclopedia': 'inreference',
figure: 'artwork',
graphic: 'artwork',
interview: 'audio',
legal_case: 'jurisdiction',
legislation: 'legislation',
manuscript: 'unpublished',
motion_picture: 'movie',
musical_score: 'music',
'paper-conference': 'inproceedings',
patent: 'patent',
personal_communication: 'letter',
post: 'online',
'post-weblog': 'online',
report: 'report',
review: 'review',
'review-book': 'review',
song: 'music',
speech: 'audio',
thesis: 'thesis',
treaty: 'legal',
webpage: 'online',
map: 'artwork',
pamphlet: 'booklet',
},
}

export function cslToBiblatex(csl: CSL[]) {
const texEntryMap = (key: string, value?: string) =>
value ? `${key} = {${value}}` : []
const toBibtex = (c: CSL[]) => {
return c
.map(
// prettier-ignore
(ref,index) =>
[
`@${(biblatexCSLMap.target )[ref.type] || 'article'}{${generateCiteKey(ref.id,index)}`,
// ${ref.author?.[0]?.family && ref.issued?.['date-parts']?.[0]?.[0] ? `${ref.author[0].family}${ref.issued['date-parts'][0][0]}` :ref['citation-key'] || ref.id}`,
texEntryMap('title ',ref.title),
texEntryMap('author ',ref.author
?.map(
(auth) =>
(auth.literal ||
`${(auth['dropping-particle'] || auth['non-dropping-particle'] || '')} ${auth.family}${auth.given ? `, ${auth.given}` : ''}${auth.suffix || ''}`).trim()
)
.join(' and ')),
texEntryMap('number ',`${ref.number||ref.issue||''}`),
texEntryMap('volume ',`${ref.volume||''}`),
texEntryMap('url ',ref.URL),
texEntryMap('doi ',ref.DOI),
texEntryMap('publisher ',ref.publisher),
texEntryMap('place ',ref['publisher-place']),
texEntryMap('year ',ref.issued?.literal ),
texEntryMap('date ',ref.issued?.['date-parts']?.[0]?.join('-')),
texEntryMap('pages ',ref.page),
texEntryMap('journal ',ref.source),

].flat().join(',\n ')+'\n}'
)
.join('\n\n\n') // + '\n}'
}
const bibtex = toBibtex(csl)

return bibtex
}

function generateCiteKey(id?: string, index?: number) {
if (!id) {
return `bib${index}`
}
if (!id.match(/\d/)) {
return id
}
if (id.slice(-4).match(/(\d{4}|\d{3}[a-z])/)) {
return id
}

return `bib${id.replace(/\D/g, '')}`
}
Loading

0 comments on commit c60d59b

Please sign in to comment.