Skip to content

Commit

Permalink
Implement printing initials in xnames
Browse files Browse the repository at this point in the history
Implement `xnames(entry; names=:last)` to print first name initials (as
stated in the documentation) and `xnames(entry; names=:lastonly)` to
print last names only.

Add an option `and` to use put " and " between the last two names.

Closes Humans-of-Julia#48
  • Loading branch information
goerz committed Mar 6, 2023
1 parent 1409c3c commit b737627
Show file tree
Hide file tree
Showing 8 changed files with 405 additions and 137 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ BibParser = "13533e5b-e1c2-4e57-8cef-cac5e52f6474"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"

[compat]
Expand Down
1 change: 1 addition & 0 deletions src/Bibliography.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import BibParser: BibTeX, CFF
import DataStructures
import DataStructures.OrderedSet
import FileIO
import Unicode

export export_bibtex, import_bibtex
export export_cff, import_cff
Expand Down
33 changes: 28 additions & 5 deletions src/staticweb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ function xtitle(entry)
return isdefined(entry, :title) ? entry.title : get(entry.fields, "title", "")
end


function _initial(name)
initial = ""
_name = Unicode.normalize(strip(name))
if length(_name) > 0
initial = "$(_name[1])."
for part in split(_name, "-")[2:end]
initial *= "-$(part[1])."
end
end
return initial
end

"""
xnames(entry, editors = false; names = :full)
Expand All @@ -32,29 +45,39 @@ Format the name of an `Entry` for web export.
# Arguments:
- `entry`: an entry
- `editors`: `true` if the name describes editors
- `names`: :full (full names) or :last (last names + first name abbreviation)
- `names`: :full (full names), :last (last names + first name initials), or :lastonly (last names only)
- `and`: `true` if the last two names should be delimited with " and "
"""
function xnames(
entry,
editors=false;
names=:full, # Current options: :last, :full
names=:full,
and=false,
)
# forces the names to be editors' name if the entry are Proceedings
if !editors && entry.type ["proceedings"]
return xnames(entry, true)
end
entry_names = editors ? entry.editors : entry.authors

if names == :last
if names == :full
parts = map(s -> [s.first, s.middle, s.particle, s.last, s.junior], entry_names)
elseif names == :last
parts = map(s -> [_initial(s.first), _initial(s.middle), s.particle, s.last, s.junior], entry_names)
elseif names == :lastonly
parts = map(s -> [s.particle, s.last, s.junior], entry_names)
else
parts = map(s -> [s.first, s.middle, s.particle, s.last, s.junior], entry_names)
error("Invalid names=$(repr(names)) not in :full, :last, :lastonly")
end

entry_names = map(parts) do s
return join(filter(!isempty, s), " ")
end
str = join(entry_names, ", ")
if and
str = join(entry_names, ", ", " and ")
else
str = join(entry_names, ", ")
end
return replace(str, r"[\n\r ]+" => " ") # TODO: make it cleaner (is replace still necessary)
end

Expand Down
27 changes: 15 additions & 12 deletions test/staticweb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ using ReferenceTests

@testset "staticweb helpers" for file in ["test.bib"] #, "xampl.bib"] #, "ignace_ref.bib"]
bib = Bibliography.import_bibtex("../examples/$file")
result = ""
for (key, val) in bib
result *= """
key: $(key)
title: '$(Bibliography.xtitle(val))'
names: '$(Bibliography.xnames(val))'
in: '$(Bibliography.xin(val))'
year: '$(Bibliography.xyear(val))'
link: '$(Bibliography.xlink(val))'
file: '$(Bibliography.xfile(val))'
for names in [:full, :last, :lastonly]
result = ""
and = (names == :full)
for (key, val) in bib
result *= """
key: $(key)
title: '$(Bibliography.xtitle(val))'
names: '$(Bibliography.xnames(val; names=names, and=and))'
in: '$(Bibliography.xin(val))'
year: '$(Bibliography.xyear(val))'
link: '$(Bibliography.xlink(val))'
file: '$(Bibliography.xfile(val))'
"""
"""
end
@test_reference "$file-$names.txt" result
end
@test_reference "$file.txt" result
end
120 changes: 120 additions & 0 deletions test/test.bib-full.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
key: CiteEprint
title: 'Automatic Cost Function Learning with Interpretable Compositional Networks'
names: 'Florian Richoux and Jean-François Baffier'
in: 'arXiv:2002.09811 [cs.AI].'
year: '2020'
link: 'https://arxiv.org/abs/2002.09811'
file: 'files/CiteEprint.pdf'

key: CitekeyArticle
title: 'The independence of the continuum hypothesis'
names: 'P. J. Cohen'
in: 'Proceedings of the National Academy of Sciences, 50(6), 1143--1148, 1963.'
year: '1963'
link: ''
file: 'files/CitekeyArticle.pdf'

key: CitekeyBook
title: 'Classical mechanics: the theoretical minimum'
names: 'Leonard Susskind and George Hrabovsky'
in: 'Penguin Random House, New York, NY, 2014.'
year: '2014'
link: ''
file: 'files/CitekeyBook.pdf'

key: CitekeyBooklet
title: 'Canoe tours in {S}weden'
names: 'Maria Swetla'
in: 'Distributed at the Stockholm Tourist Office, 2015.'
year: '2015'
link: ''
file: 'files/CitekeyBooklet.pdf'

key: CitekeyInbook
title: 'Photosynthesis'
names: 'Lisa A. Urry, Michael L. Cain, Steven A. Wasserman, Peter V. Minorsky and Jane B. Reece'
in: 'Campbell Biology, 187--221, Pearson, New York, NY.'
year: '2016'
link: ''
file: 'files/CitekeyInbook.pdf'

key: CitekeyIncollection
title: 'Flow Cytometry: The Glass Is Half Full'
names: 'Howard M. Shapiro'
in: 'In Flow Cytometry Protocols, editors, Teresa S. Hawley, Robert G. Hawley, 1--10. Springer, New York, NY, 2018.'
year: '2018'
link: ''
file: 'files/CitekeyIncollection.pdf'

key: CitekeyInproceedings
title: 'Studying mobile context-aware social services in the wild'
names: 'Paul Holleis, Matthias Wagner and Johan Koolwaaij'
in: ' In Proc. of the 6th Nordic Conf. on Human-Computer Interaction, NordiCHI, 207--216, New York, NY, 2010. ACM.'
year: '2010'
link: ''
file: 'files/CitekeyInproceedings.pdf'

key: CitekeyManual
title: '{R}: A Language and Environment for Statistical Computing'
names: 'R Core Team'
in: 'R Foundation for Statistical Computing, Vienna, Austria, 2018.'
year: '2018'
link: ''
file: 'files/CitekeyManual.pdf'

key: CitekeyMastersthesis
title: 'Spin structure of the nucleon in the asymptotic limit'
names: 'Jian Tang'
in: 'Master's thesis, Massachusetts Institute of Technology, Cambridge, MA, 1996.'
year: '1996'
link: ''
file: 'files/CitekeyMastersthesis.pdf'

key: CitekeyMisc
title: 'Pluto: The 'Other' Red Planet'
names: 'NASA'
in: '\url{https://www.nasa.gov/nh/pluto-the-other-red-planet}, 2015. Accessed: 2018-12-06.'
year: '2015'
link: ''
file: 'files/CitekeyMisc.pdf'

key: CitekeyPhdthesis
title: 'Relaxation Effects for Coupled Nuclear Spins'
names: 'Robert Charles Rempel'
in: 'PhD thesis, Stanford University, Stanford, CA, 1956.'
year: '1956'
link: ''
file: 'files/CitekeyPhdthesis.pdf'

key: CitekeyProceedings
title: 'Proceedings of the 17th International Conference on Computation and Natural Computation, Fontainebleau, France'
names: 'Susan Stepney, Sergey Verlan'
in: 'Volume 10867 of Lecture Notes in Computer Science, Cham, Switzerland, 2018. Springer.'
year: '2018'
link: ''
file: 'files/CitekeyProceedings.pdf'

key: CitekeyTechreport
title: '{W}asatch {S}olar {P}roject Final Report'
names: 'Vicki Bennett, Kate Bowman and Sarah Wright'
in: 'Technical Report DOE-SLC-6903-1, Salt Lake City Corporation, Salt Lake City, UT, 2018.'
year: '2018'
link: ''
file: 'files/CitekeyTechreport.pdf'

key: CitekeyUnpublished
title: 'Evolution: a revised theory'
names: 'Mohinder Suresh'
in: 'Well, a note, 2006.'
year: '2006'
link: ''
file: 'files/CitekeyUnpublished.pdf'

key: baffier2017experimental
title: 'Experimental Study of Compressed Stack Algorithms in Limited Memory Environments'
names: 'Jean-François Baffier, Yago Diez and Matias Korman'
in: 'arXiv:1706.04708 [cs.DS].'
year: '2017'
link: 'https://arxiv.org/abs/1706.04708'
file: 'files/baffier2017experimental.pdf'

120 changes: 120 additions & 0 deletions test/test.bib-last.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
key: CiteEprint
title: 'Automatic Cost Function Learning with Interpretable Compositional Networks'
names: 'F. Richoux, J.-F. Baffier'
in: 'arXiv:2002.09811 [cs.AI].'
year: '2020'
link: 'https://arxiv.org/abs/2002.09811'
file: 'files/CiteEprint.pdf'

key: CitekeyArticle
title: 'The independence of the continuum hypothesis'
names: 'P. J. Cohen'
in: 'Proceedings of the National Academy of Sciences, 50(6), 1143--1148, 1963.'
year: '1963'
link: ''
file: 'files/CitekeyArticle.pdf'

key: CitekeyBook
title: 'Classical mechanics: the theoretical minimum'
names: 'L. Susskind, G. Hrabovsky'
in: 'Penguin Random House, New York, NY, 2014.'
year: '2014'
link: ''
file: 'files/CitekeyBook.pdf'

key: CitekeyBooklet
title: 'Canoe tours in {S}weden'
names: 'M. Swetla'
in: 'Distributed at the Stockholm Tourist Office, 2015.'
year: '2015'
link: ''
file: 'files/CitekeyBooklet.pdf'

key: CitekeyInbook
title: 'Photosynthesis'
names: 'L. A. Urry, M. L. Cain, S. A. Wasserman, P. V. Minorsky, J. B. Reece'
in: 'Campbell Biology, 187--221, Pearson, New York, NY.'
year: '2016'
link: ''
file: 'files/CitekeyInbook.pdf'

key: CitekeyIncollection
title: 'Flow Cytometry: The Glass Is Half Full'
names: 'H. M. Shapiro'
in: 'In Flow Cytometry Protocols, editors, Teresa S. Hawley, Robert G. Hawley, 1--10. Springer, New York, NY, 2018.'
year: '2018'
link: ''
file: 'files/CitekeyIncollection.pdf'

key: CitekeyInproceedings
title: 'Studying mobile context-aware social services in the wild'
names: 'P. Holleis, M. Wagner, J. Koolwaaij'
in: ' In Proc. of the 6th Nordic Conf. on Human-Computer Interaction, NordiCHI, 207--216, New York, NY, 2010. ACM.'
year: '2010'
link: ''
file: 'files/CitekeyInproceedings.pdf'

key: CitekeyManual
title: '{R}: A Language and Environment for Statistical Computing'
names: 'R. C. Team'
in: 'R Foundation for Statistical Computing, Vienna, Austria, 2018.'
year: '2018'
link: ''
file: 'files/CitekeyManual.pdf'

key: CitekeyMastersthesis
title: 'Spin structure of the nucleon in the asymptotic limit'
names: 'J. Tang'
in: 'Master's thesis, Massachusetts Institute of Technology, Cambridge, MA, 1996.'
year: '1996'
link: ''
file: 'files/CitekeyMastersthesis.pdf'

key: CitekeyMisc
title: 'Pluto: The 'Other' Red Planet'
names: 'NASA'
in: '\url{https://www.nasa.gov/nh/pluto-the-other-red-planet}, 2015. Accessed: 2018-12-06.'
year: '2015'
link: ''
file: 'files/CitekeyMisc.pdf'

key: CitekeyPhdthesis
title: 'Relaxation Effects for Coupled Nuclear Spins'
names: 'R. C. Rempel'
in: 'PhD thesis, Stanford University, Stanford, CA, 1956.'
year: '1956'
link: ''
file: 'files/CitekeyPhdthesis.pdf'

key: CitekeyProceedings
title: 'Proceedings of the 17th International Conference on Computation and Natural Computation, Fontainebleau, France'
names: 'Susan Stepney, Sergey Verlan'
in: 'Volume 10867 of Lecture Notes in Computer Science, Cham, Switzerland, 2018. Springer.'
year: '2018'
link: ''
file: 'files/CitekeyProceedings.pdf'

key: CitekeyTechreport
title: '{W}asatch {S}olar {P}roject Final Report'
names: 'V. Bennett, K. Bowman, S. Wright'
in: 'Technical Report DOE-SLC-6903-1, Salt Lake City Corporation, Salt Lake City, UT, 2018.'
year: '2018'
link: ''
file: 'files/CitekeyTechreport.pdf'

key: CitekeyUnpublished
title: 'Evolution: a revised theory'
names: 'M. Suresh'
in: 'Well, a note, 2006.'
year: '2006'
link: ''
file: 'files/CitekeyUnpublished.pdf'

key: baffier2017experimental
title: 'Experimental Study of Compressed Stack Algorithms in Limited Memory Environments'
names: 'J.-F. Baffier, Y. Diez, M. Korman'
in: 'arXiv:1706.04708 [cs.DS].'
year: '2017'
link: 'https://arxiv.org/abs/1706.04708'
file: 'files/baffier2017experimental.pdf'

Loading

0 comments on commit b737627

Please sign in to comment.