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

Add FileIO interface for .bib files #47

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ BibInternal = "2027ae74-3657-4b95-ae00-e2f7d55c3e64"
BibParser = "13533e5b-e1c2-4e57-8cef-cac5e52f6474"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"

[compat]
Expand Down
2 changes: 2 additions & 0 deletions src/Bibliography.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import BibParser: BibTeX, CFF
# Others
import DataStructures
import DataStructures.OrderedSet
import FileIO

export export_bibtex, import_bibtex
export export_cff, import_cff
Expand All @@ -24,6 +25,7 @@ include("bibtex.jl")
include("cff.jl")
include("csl.jl")
include("staticweb.jl")
include("fileio.jl")

"""
export_bibtex(target, bibliography)
Expand Down
30 changes: 30 additions & 0 deletions src/fileio.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
fileio_load(file; check)
fileio_load(stream; check)
The FileIO interface to import a BibTeX file or parse a BibTeX string and convert it to the internal bibliography format.
The `check` keyword argument can be set to `:none` (or `nothing`), `:warn`, or `:error` to raise appropriate logs.
"""
function fileio_load(file::FileIO.File{FileIO.format"BIB"}; check = :error)
BibParser.parse_file(file.filename; check)
end

function fileio_load(stream::FileIO.Stream{FileIO.format"BIB"}; check = :error)
BibParser.parse_entry(read(stream, String); check)
end

"""
fileio_save(file, data)
fileio_save(stream, data)
Export a bibliography as a BibTeX string to a file or stream.
"""
function fileio_save(file::FileIO.File{FileIO.format"BIB"}, data)
open(file, "w") do f
write(f, export_bibtex(data))
end
end

function fileio_save(stream::FileIO.Stream{FileIO.format"BIB"}, data)
write(stream, export_bibtex(data))
end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bibliography
using FileIO
using Test
using ReferenceTests

Expand Down Expand Up @@ -44,3 +45,4 @@ rm("demo_export.bib")
include("sort_bibliography.jl")
include("staticweb.jl")
include("cff.jl")
include("test-fileio.jl")
28 changes: 28 additions & 0 deletions test/test-fileio.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@testset "FileIO interface functions." begin
F = FileIO.File{FileIO.format"BIB"}
S = FileIO.Stream{FileIO.format"BIB"}

# Load, export and reload as file.
let
bib = Bibliography.fileio_load(F("test.bib"))
Bibliography.fileio_save(F("test-fileio.bib"), bib)
bib2 = Bibliography.fileio_load(F("test-fileio.bib"))
@test_broken bib == bib2
rm("test-fileio.bib")
end

# Load, export and reload as stream.
let
bib = open("test.bib") do f
Bibliography.fileio_load(S(f))
end
open("test-fileio.bib", "w") do f
Bibliography.fileio_save(S(f), bib)
end
bib2 = open("test-fileio.bib") do f
Bibliography.fileio_load(S(f))
end
@test_broken bib == bib2
rm("test-fileio.bib")
end
end