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 support for [text](@cite key) syntax #64

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Here I cite `Mayer2012`, a useful article, via `[Mayer2012](@cite)`: [Mayer2012]

I also cite `useful_proof`, a book you might not want to read, with `[useful_proof](@cite)` [useful_proof](@cite).

Use e.g. `[arbitrary text](@cite Mayer2012)` to cite a reference with an [arbitrary text](@cite Mayer2012).

Some TeX diacritical marks are supported, e.g. [Heun1900](@cite).

Clicking on the citations takes you to the bibliography.
Expand Down
4 changes: 3 additions & 1 deletion src/DocumenterCitations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ end

Foo.

[Mayer2012](@cite)
# Reference

* [Mayer, Krechetnikov, Phys. Rev. E 85, 046117 (2012)](@cite Mayer2012)
"""
struct Example end

Expand Down
19 changes: 15 additions & 4 deletions src/citations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ function expand_citation(elem, page, doc)
end

function expand_citation(link::Markdown.Link, meta, page, doc)
link.url !== "@cite" && return false

occursin("@cite", link.url) || return false
if length(link.text) === 1 && isa(link.text[1], String)
citation_name = link.text[1]
if link.url == "@cite" # citation format: [key](@cite)
citation_name = link.text[1]
else # citation format: [text](@cite key)
if (m = match(r"^@cite\s*([^\s},]+)\s*$", link.url)) ≢ nothing
citation_name = m[1]
else
error("Invalid citation: [$(link.text)]($(link.url))")
end
end
@info "Expanding citation: $citation_name."

if haskey(doc.plugins[CitationBibliography].bib, citation_name)
Expand All @@ -39,7 +46,11 @@ function expand_citation(link::Markdown.Link, meta, page, doc)
anchor = Anchors.anchor(headers, entry.id)
path = relpath(anchor.file, dirname(page.build))
authors = xnames(entry) |> tex2unicode
link.text = authors * " (" * xyear(entry) * ")"
if link.url == "@cite"
link.text = authors * " (" * xyear(entry) * ")"
else
# keep original link.text
end
link.url = string(path, Anchors.fragment(anchor))
return true
else
Expand Down