-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #397 from Inist-CNRS/bibtex
feat: 🎸 add [BIBParse]
- Loading branch information
Showing
9 changed files
with
188 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { StringDecoder } from 'string_decoder'; | ||
import BibtexParser from 'bib2json'; | ||
|
||
function BIBParse(data, feed) { | ||
if (!this.decoder) { | ||
this.decoder = new StringDecoder('utf8'); | ||
this.remainder = ''; | ||
this.counter = 0; | ||
this.parser = new BibtexParser((entry) => { | ||
feed.write(entry); | ||
}); | ||
} | ||
if (this.isLast()) { | ||
this.remainder += this.decoder.end(); | ||
if (this.remainder && this.counter > 1) { | ||
this.parser.parse(this.remainder); | ||
} | ||
return feed.close(); | ||
} | ||
let chunk; | ||
if (Buffer.isBuffer(data)) { | ||
chunk = this.decoder.write(data); | ||
} else if (typeof data === 'string') { | ||
chunk = data; | ||
} else { | ||
chunk = ''; | ||
} | ||
this.parser.parse(chunk); | ||
this.counter += 1; | ||
feed.end(); | ||
} | ||
|
||
/** | ||
* Take a `String` and split it at bibtext entry. | ||
* | ||
* Input: | ||
* | ||
* ```json | ||
* ["@article{my_article,\ntitle = {Hello world},\n", "journal = \"Some Journal\"\n"] | ||
* ``` | ||
* | ||
* Output: | ||
* | ||
* ```json | ||
* ["a", "b", "c", "d"] | ||
* ``` | ||
* | ||
* @name BIBParse | ||
* @returns {Object} | ||
*/ | ||
export default { | ||
BIBParse, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import from from 'from'; | ||
import ezs from '../../core/src'; | ||
import ezsBasics from '../src'; | ||
|
||
ezs.use(ezsBasics); | ||
|
||
describe('BIBParse', () => { | ||
it('should return a entry #1', (done) => { | ||
from(['@article{my_article,\ntitle = {Hello world},\n', 'journal = "Some Journal"\n}']) | ||
.pipe(ezs('BIBParse')) | ||
.on('error', (err) => done(err)) | ||
.on('data', (data) => { | ||
expect(typeof data).toBe('object'); | ||
expect(data.ObjectType).toBe('entry'); | ||
}) | ||
.on('end', () => { | ||
done(); | ||
}); | ||
}); | ||
it('should return a entry #2', (done) => { | ||
from(['@article{my_article,\ntitle = {Hello world},\njournal = "Some Journal"\n}']) | ||
.pipe(ezs('BIBParse')) | ||
.on('error', (err) => done(err)) | ||
.on('data', (data) => { | ||
expect(typeof data).toBe('object'); | ||
expect(data.ObjectType).toBe('entry'); | ||
}) | ||
.on('end', () => { | ||
done(); | ||
}); | ||
}); | ||
it('should return a entry #3', (done) => { | ||
from(['@article{my_article,\ntitle = {Hello world},\n', 1, 'journal = "Some Journal"\n}']) | ||
.pipe(ezs('BIBParse')) | ||
.on('error', (err) => done(err)) | ||
.on('data', (data) => { | ||
expect(typeof data).toBe('object'); | ||
expect(data.ObjectType).toBe('entry'); | ||
}) | ||
.on('end', () => { | ||
done(); | ||
}); | ||
}); | ||
it('should return a entry #3', (done) => { | ||
from([ | ||
Buffer.from('@article{my_article,\ntitle = {Hello world},\n'), | ||
Buffer.from('journal = "Some'), | ||
Buffer.from([0xE2]), | ||
Buffer.from([0x82]), | ||
Buffer.from([0xAC]), | ||
Buffer.from('Journal"\n}'), | ||
Buffer.from([0xC2]), | ||
]) | ||
.pipe(ezs('BIBParse')) | ||
.on('error', (err) => done(err)) | ||
.on('data', (data) => { | ||
expect(typeof data).toBe('object'); | ||
expect(data.ObjectType).toBe('entry'); | ||
}) | ||
.on('end', () => { | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should return no entry', (done) => { | ||
from(['@my_article,\ntitle = {Hello world},\n', 'journal = ']) | ||
.pipe(ezs('BIBParse')) | ||
.on('error', (err) => done(err)) | ||
.on('data', () => done(new Error('no way'))) | ||
.on('end', () => { | ||
done(); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3671,6 +3671,11 @@ [email protected]: | |
dependencies: | ||
duplexify "4.0.0" | ||
|
||
[email protected]: | ||
version "0.0.1" | ||
resolved "https://registry.yarnpkg.com/bib2json/-/bib2json-0.0.1.tgz#c33bcf6f35edb4647fd75ff76f25bd7999fc3f31" | ||
integrity sha512-6R9dpnE5FpUSH61Wq00XDcivsPAeH1+u+SeXevDh1eUHTfEiORRve5XpHTbCzwh1XytHotllY+ErUErbdxkFHg== | ||
|
||
big-integer@^1.6.17: | ||
version "1.6.51" | ||
resolved "https://registry.yarnpkg.com/big-integer/-/big-integer-1.6.51.tgz#0df92a5d9880560d3ff2d5fd20245c889d130686" | ||
|