BibTeX.jl/src/bibitem.jl

42 lines
1.5 KiB
Julia
Raw Normal View History

"""
BibItem{S}(data::Dict{String,String})
A bibliography item in a bibTeX database, based on a dictionary of
strings to values. It is parameterized by a symbol `S` giving the
type of the item (`:article` etcetera). A `b::BibItem` supports
`b[key]` access to retrieve the data and in general acts like
a dictionary from `String` to `String`.
"""
struct BibItem{S} <: Associative{String,String}
data::Dict{String,String}
end
function BibItem!(data::Dict{String,String})
S = Symbol(pop!(data, "__type__"))
return BibItem{S}(data)
end
Base.similar(b::BibItem{S}) where {S} = BibItem{S}(Dict{String,String}())
Base.rehash!(b::BibItem, n=length(b.data)) = begin rehash!(b.data, n); b; end
Base.sizehint!(b::BibItem, n) = begin sizehint!(b.data, n); b; end
Base.empty!(b::BibItem) = begin empty!(b.data); b; end
Base.copy(b::BibItem{S}) where {S} = BibItem{S}(copy(b.data))
Base.get(b::BibItem, k::AbstractString, default) = get(b.data, String(k), default)
Base.getindex(b::BibItem, k::AbstractString) = getindex(b.data, String(k))
function Base.setindex!(b::BibItem, v::AbstractString, k::AbstractString)
b.data[String(k)] = String(v)
return b
end
Base.start(b::BibItem) = start(b.data)
Base.done(b::BibItem, i) = done(b.data, i)
Base.next(b::BibItem, i) = next(b.data, i)
Base.length(b::BibItem) = length(b.data)
function Base.show{S}(io::IO, b::BibItem{S})
print(io, "BibItem{:$S}(", length(b), " entries)")
end
# TODO: add Base.show text/plain and text/markdown for formatted citation