Repository: jalvesaq/zotcite
Branch: master
Commit: fbd2dcc6b5c9
Files: 32
Total size: 173.3 KB
Directory structure:
gitextract_o02z_1cb/
├── .gitignore
├── LICENSE
├── README.md
├── doc/
│ └── zotcite.txt
├── ftplugin/
│ ├── bib_zotcite.lua
│ ├── markdown_zotcite.lua
│ ├── pandoc_zotcite.lua
│ ├── quarto_zotcite.lua
│ ├── rmd_zotcite.lua
│ ├── rnoweb_zotcite.lua
│ ├── tex_zotcite.lua
│ ├── typst_zotcite.lua
│ └── vimwiki_zotcite.lua
├── lua/
│ └── zotcite/
│ ├── bib.lua
│ ├── config.lua
│ ├── get.lua
│ ├── hl.lua
│ ├── init.lua
│ ├── lsp.lua
│ ├── seek.lua
│ ├── utils.lua
│ └── zotero.lua
├── plugin/
│ └── zotcite.lua
├── scripts/
│ ├── abntfix.py
│ ├── apafix
│ ├── odt2md.py
│ ├── pdfnotes.py
│ ├── pdfnotes2.py
│ └── zotid2bbtcitkey.py
├── selene.toml
├── stylua.toml
└── vim.yml
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
scripts/__pycache__/
lua/zotcite/types
doc/tags
================================================
FILE: LICENSE
================================================
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
================================================
FILE: README.md
================================================
# Zotcite
> [!Note]
> Users of Vim and Neovim < 0.10.4 have to switch to the "vim" branch.
> [!Note]
> Users of Neovim < 0.11.5 have to switch to the "pynvim" branch.
> [!Note]
> Users of Zotero < 8 have to switch to zotcite 0.7 (tag v0.7) if using Better
> BibTeX citation keys.
> [!Note]
> Zotcite now uses conventional citation keys based on a citation template
> and no longer requires the `zotref` filter. Alternatively, it can use Better
> BibTeX citation keys. There is also an option to use the internal Zotero
> item keys and insert the "Author-Year" part of the citation key as virtual
> text. If you have documents using the old system, and don't want to manually
> convert then, you can use the "pynvim" branch.
_Zotcite_ is a Neovim plugin that provides integration with Zotero for
Markdown, Quarto, Rmd, vimwiki, Typst, LaTeX, and Rnoweb file types. With
_zotcite_ you can:
- Do auto-completion of citation keys from Zotero database in
Markdown, RMarkdown, Quarto, LaTeX, Rnoweb, and Typst documents.
- Quickly see on the status bar information on the reference under the cursor.
- Open the PDF attachment of the reference associated with the citation key
under the cursor.
- Extract notes and highlighted text from PDF attachments of
references.
- Extract Zotero notes and annotations from Zotero database.
## Installation
**Dependencies**
- Required:
- Zotero >= 5 if not using Better-BibTeX. For Better-BibTeX users, Zotero >=
8 is required.
- The `sqlite3` command line application.
- [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) as
well as the tree-sitter parser for `yaml`.
- Optional:
- [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) for an
alternative way of inserting citation keys.
- Python 3 is required if you are going to convert OpenDocument texts (`odt`)
to markdown or extract annotations inserted in a PDF document by a PDF
viewer other than Zotero. Please, see the Zotcite documentation for details.
Zotcite can be installed as any Neovim plugin. Below is an example of how to
install `zotcite` with [lazy.nvim](https://github.com/folke/lazy.nvim):
```lua
{
"jalvesaq/zotcite",
dependencies = {
"nvim-treesitter/nvim-treesitter",
"nvim-telescope/telescope.nvim",
},
config = function ()
require("zotcite").setup({
-- your options here (see doc/zotcite.txt)
})
end
},
```
> [!Note]
> You don't need to lazy load zotcite because it is a filetype plugin. This
> means that it already lazy loads its modules, which will be enabled only for
> the supported file types.
## Usage
Please, read the plugin's
[documentation](https://raw.githubusercontent.com/jalvesaq/zotcite/master/doc/zotcite.txt)
for further instructions.
## Screenshots
[See the Wiki](https://github.com/jalvesaq/zotcite/wiki/Screenshots)
## See also:
Zotcite's original Python code was based on the
[citation.vim](https://github.com/rafaqz/citation.vim) project.
Similar project: [telescope-zotero.nvim](https://github.com/jmbuhr/telescope-zotero.nvim)
================================================
FILE: doc/zotcite.txt
================================================
*zotcite.txt* Integration with Zotero
1. Overview |zotcite_overview|
2. Usage |zotcite_usage|
2.1. Commands |zotcite_commands|
2.2. Mappings |zotcite_mappings|
2.3. Compiling documents |zotcite_compiling_docs|
3. Customization |zotcite_customization|
3.1. Paths |zotcite_paths|
3.2. Change default maps |zotcite_change_mappings|
3.3. Zotcite engine |zotcite_engine|
3.4. Syntax highlighting |zotcite_highlighting|
3.5. File types |zotcite_filetypes|
3.6. Open attachment |zotcite_open_in_zotero|
3.7. Show errors of command to open attachment |zotcite_wait_attachment|
4. Troubleshooting |zotcite_troubleshooting|
==============================================================================
1. Overview *zotcite_overview*
Zotcite is a Vim plugin that provides integration with Zotero. Please, see a
description of its features, including screenshots at:
https://github.com/jalvesaq/zotcite
==============================================================================
2. Usage *zotcite_usage*
------------------------------------------------------------------------------
2.1. Commands *zotcite_commands*
You can use the command `:Zseek` to see what references have either a last
author's name or title matching the pattern that you are seeking for. Example:
>vim
:Zseek marx
Only the first word is used as the search pattern. Thus, the following command
would give the same result as the above one:
>vim
:Zseek marx anything
The Telescope picker is filled with references that include the searched
pattern as a substring in the author's names or the work's title, but
Telescope allows you to further fuzzy filter the results. If you want the
Telescope picker filled with Zotero's entire database, run `:Zeek` without
arguments. The same Telescope dialogue is also used by `:Zannotations`,
`:Zselectannotations`, `:Znote`, and `:Zpdfnote` (see below).
Zotcite can extract and insert into the markdown document (1) annotations that
you have made using Zotero's built-in PDF viewer, (2) notes that you have
attached to a Zotero entry, and (3) annotations inserted in a PDF by an
external PDF viewer.
To extract annotations made with Zotero's built-in PDF viewer, use the Vim
command `:Zannotations key`. If the PDF has page labels, Zotero will register
them as the page numbers; otherwise, Zotero will consider the first page of
the PDF as page 1 which in most cases will be wrong. You can fix this by
passing an integer number as a second argument to `:Zannotations`. For
example, if page 1 of a book is page 11 of its PDF, you will get the correct
page numbers if you do:
>vim
:Zannotations key -10
By default, the colon separating the year from the page is replaced by ", p.
". If you want to use the colon or any other string as the separator, set the
value of `year_page_sep` in your `config`. Example:
>lua
year_page_sep = ':'
If you want to extract annotations made with Zotero's built-in PDF viewer but
would prefer to make a selection of the annotations to be imported then use
the vim command `:Zselectannotations key`. After choosing the reference, a
second dialogue will appear with the annotations and notes (if you have made
notes). You can scroll through the annotations list and press `enter` on the
annotations you'd wish to import. Once finished making the selection you need
to press `Esc` to escape the annotation selection dialogue.
>vim
To extract notes from Zotero, use the Vim command `:Znote key`
Similarly, to extract annotations (notes and highlighted texts) that were
inserted into a PDF document by an external PDF viewer, use the Vim command
`:Zpdfnote`. The page numbers of the annotations might be wrong, so always
check them after the extraction. You have also to manually add italics and
other rich text formatting and put back hyphens incorrectly removed from the
end of the PDF lines.
To insert citation keys, you can press <C-X><C-B> in Insert mode. The sequence
of letters before the cursor will be used to filter the references (see
`:Zseek` above).
------------------------------------------------------------------------------
2.2. Mappings *zotcite_mappings*
In Insert mode, type the `@` letter and one or more letters of either the last
name of the first author or the reference title. The matching of citation keys
is case-insensitive.
In Vim's Insert mode, <c-x><c-b> inserts citation keys (requires Telescope).
In Vim's Normal mode, put the cursor over a citation key and press:
- <Leader>zo to open the reference's attachment as registered in Zotero's
database.
- <Leader>zi to see in the status bar the last name of all authors, the
year, and the title of the reference.
- <Leader>za to see all fields of a reference as stored by Zotcite.
- <leader>zb to insert the abstract (if available) into the current buffer.
- <Leader>zv to view the (pdf or html) document generated from the current
(Markdown, Rmd, or Quarto) document.
------------------------------------------------------------------------------
2.3. Compiling documents *zotcite_compiling_docs*
Note: Make backup of your bib files because they will be overwritten by
zotcite.
zotcite will automatically save the bib file indicated in the YAML header when
saving the document. The citation key will have the format `@ABCDEFGH`, and
the `Author-Year` complement will be inserted as a virtual text.
If you have an old markdown document with citations including the no longer
used `-Author-Year` real text, the Vim command below will delete the
`-Author-Year` complements (with no automatic way of getting them back):
>vim
:%s/@\([A-Z0-9]\{8}\)[-#][-:+_[:digit:][:lower:][:upper:]\u00FF-\uFFFF]\+/@\1/g
<
Note: make a backup of you Markdown document before running the command to
be able to revert the changes if anything goes wrong.
Convert from ODT ~
If you have an OpenDocument Text with citations inserted by the Zotero
extension and want to convert it to Markdown, you can use the `odt2md.py`
application from the `scripts` directory to help you convert the document.
Example:
>sh
/path/to/zotcite/scripts/odt2md.py Document.odt
Within Neovim, you can run the command `:Zodt2md` to convert the ODT document
and see the resulting Markdown document in a new tab:
>vim
:Zodt2md Document.odt
Note that the conversion is not perfect because the visible text from the
citation fields is not deleted. You have to delete them manually, keeping what
is needed, such as the page numbers that you have added manually. It is not
possible to automate this step because LibreOffice allows the manual edition
of the citation fields.
==============================================================================
3. Customization *zotcite_customization*
------------------------------------------------------------------------------
3.1. Paths |zotcite_paths|
*zotero_sqlite_path*
If Zotcite cannot find the path to Zotero's database (`zotero.sqlite`), put
the correct path in your `config`. Example:
>lua
zotero_sqlite_path = '~/.zotero/zotero.sqlite'
<
*tmpdir*
If Zotcite is trying to use a non-writable directory as its temporary and
cache directory, set the value of `tmpdir`. Example:
>lua
tmpdir = '/dev/shm/zotcite-user_name'
<
*tex_fallback_root*
When editing LaTeX or Rnoweb documents, Zotcite first tries to find
`\addbibresource{...}` in the current buffer, then in the file pointed by
`% !TeX root = ...`. If both checks fail, Zotcite looks for a fallback root
file at `bufdir .. "/" .. tex_fallback_root`.
The default is `"../main.tex"`. Example:
>lua
tex_fallback_root = "../main.tex"
<
Zotcite adds its `scripts` directory to the environment variable `PATH`. If
it's not correctly finding the directory, you can set it manually:
>lua
python_scripts_path = "/path/to/zotcite/scripts"
<
*zotcite_python_path*
Zotcite runs "python3" when extracting notes from PDF documents annotated by
PDF viewers other than Zotero (`:Zpdfnote`) and when converting ODT to
Markdown (`:Zodt2md`). If "python3" is not in your path or if you are running
Python from a virtual environment, you should set the correct path in your
zotcite config. Example:
>lua
python_path = "/home/user_name/.py3env/bin/python3"
<
------------------------------------------------------------------------------
3.2. Change default maps *zotcite_change_mappings*
Below are examples of how to change the available key bindings:
>lua
-- Insert citation key:
vim.keymap.set("i", "<c-b>", "<Plug>ZCite")
-- Open reference attachment:
vim.keymap.set("n", "<c-]>", "<Plug>ZOpenAttachment")
-- See basic information on the reference of a citation key:
vim.keymap.set("n", "<Leader>I", "<Plug>ZCitationInfo")
-- See how the reference of a citation key is stored by Zotcite:
vim.keymap.set("n", "<Leader>C", "<Plug>ZCitationCompleteInfo")
-- Extract the abstract into the current buffer from the citation under cursor:
vim.keymap.set("n", "<Leader>A", "<Plug>ZExtractAbstract")
-- View the (pdf or html) document generated from the
-- current (Markdown, Rmd, or Quarto) document:
vim.keymap.set("n", "<Leader>V", "<Plug>ZViewDocument")
<
------------------------------------------------------------------------------
3.3. Zotcite engine *zotcite_engine*
*zotcite_key_type*
You can use three different types of citation keys with Zotcite:
- Zotero keys.
- Keys based on the citation template.
- Better BibTeX citation keys ("Citation Key" field).
Zotero keys are sequences of random upper case letters and numbers with
length eight, such as `A1B2C3D4` or `ABCD1234`. These keys are generated by
Zotero.
Keys based on the citation template are generated by zotcite according to the
`citation_template` option explained below. Duplicates are fixed by appending
alphabetic letters `a`, `b`, `c`, and so on. The letter that is appended
follows the chronological order of addition of references to Zotero's
database. This type of citation key is similar to what you get from Better
BibTex.
The Better BibTeX citation keys are generated by Zotero's extension Better
BibTex.
Advantages of each type:
- Zotero key: It is guaranteed to be unique. You will never get a duplicate.
But they are meaningless for humans because it is not based on an author
name, reference title, or year of publication. As a workaround, zotcite
conceals the Zotero key and inserts a virtual text based on the citation
template. The problem is that the cursor movement becomes strange as it
jumps over the virtual text.
- Key based on the citation template: You see the real text of the citation
key, and you see the cursor moving as usual. The problem is that if you
delete from Zotero's database a reference that was resulting in duplicate
citation key, the letters appended to the remaining references will change
unless the deleted reference was the last one added.
- Better BibTeX citation key: Same advantages and disadvantages of citation
keys based on a template.
What is the best for you:
- If you are writing a book or paper alone (or if everyone is using Neovim
and zotcite) and the strange cursor movements isn't annoying to you,
then, the Zotero key is the best option.
- If you are writing alone (or if everyone is using Neovim and zotcite), but
you want normal cursor movements, and don't want to install Better BibTex,
then, a citation key based on the citation template is the best option.
- If you are writing with other people who use Better BibTeX, than, of
course, you have no choice but to use Better BibTeX keys too.
The type of citation key is defined with the option `key_type`. Valid values
are "template" (the default), "better-bibtex", and "zotero". Example:
>lua
key_type = "zotero",
<
You can also set a different value for each document. For a Markdown,
RMarkdown or Quarto document, add the key `zotcite-key-type` to the YAML
header. Example:
>yaml
zotcite-key-type: better-bibtex
<
For LaTeX, Rnoweb, or Tpyst, it should be within a commented line with the
comment string at the beginning of the line. Example for LaTeX:
>tex
% zotcite-key-type = zotero
<
and for Typst:
>typst
// zotcite-key-type = template
<
*citation_template*
When the `key_type` is "zotero", zotcite complements the citation key with a
virtual text in the format `Author_Year`. The citation key is the key
attributed by Zotero to the references stored in its database, `Author` is the
last name of the first author, and `Year` is the full year (four digits). You
can define whether it will include or not the first author's last name (in
either lower case `{author}` or title case `{Author}`), the last names of the
first three authors (either `{authors}` or `{Authors}`), the first word of the
work title (in lower case `{title}` or as registered in Zotero `{Title}`),
and the year (with either four digits `{Year}` or only two `{year}`). Example:
>lua
citation_template = '{Author}-{year}'
<
When Zotero's SQLite3 database is updated, zotcite copies it to its temporary
directory. While copying, the slowest operation is writing the copy of the
file to disk. If you have enough RAM, you may want to set `tmpdir` as a
directory in a file system mounted in virtual memory, as in the second example
above.
*exclude_fields*
If you want to exclude some of Zotero's fields from the bib file
generated by Zotcite, set the value of `exclude_fields` to a white-separated
list of fields to be excluded. For example, if you want to exclude the fields
"Extra" (which appears as `note` in the bib file) and "file" field, put
this in your `config`:
>lua
exclude_fields = "note file"
<
*sort_key*
By default, zotcite uses the field "dateModified" to sort the results
displayed by `:Zseek`. You can use a different field, such as "date", "title",
"alastnm" or other fields whose values are strings or numbers (do
`<Leader>za` over some citation keys to see the available fields):
>lua
sort_key = "date"
<
*Zotero_collection*
If you want to restrict the search for references to specific collections
while completing citation keys, set the value `collection` in the YAML header
of the Markdown document, as in the examples:
>
---
collection: ['New Paper']
---
---
collection: ['PhD Thesis', 'New Paper']
---
The Python script `pdfnote.py` imports the modules `PyQt5` and `popplerqt5`.
On Debian systems they can be installed with the command:
>sh
sudo apt install python3-pyqt5 python3-poppler-qt5
<
In any system, if using `pip`:
>sh
pip install poppler-qt5
pip install python-poppler-qt5
<
If you have trouble install `poppler-qt5` or `python-poppler-qt5` you can add
to your zotcite config:
>lua
pdf_extractor = "pdfnotes2.py",
<
The Python script `pdfnotes2.py` requires the `pymupdf` module, which can be
installed on Debian:
>sh
sudo apt install python3-pymupdf
<
or with `pip`:
>sh
pip install pymupdf
<
------------------------------------------------------------------------------
3.4. Syntax Highlighting *zotcite_highlighting*
Syntax highlighting customization is done with Vim variables.
*hl_cite_key*
If you want to disable Zotcite's syntax highlighting of citation keys, put in
your `config`:
>lua
hl_cite_key = false
<
*zotcite_conceallevel*
Zotcite will set the 'conceallevel' of the document if you set the value
`conceallevel` in its config:
>lua
conceallevel = 0
if `conceallevel` is a negative integer (the default), zotcite will do nothing.
------------------------------------------------------------------------------
3.5. File types *zotcite_filetypes*
Zotcite is a filetype plugin and is enabled only if the file type is
`markdown`, `pandoc`, `rmd`, `quarto`, `vimwiki`, `latex` or `rnoweb`. If you
want to disable it for some of these file types, you should set the value of
`filetypes` in your `config`. Example:
>lua
filetypes = { 'markdown', 'pandoc', 'rmd', 'vimwiki' }
------------------------------------------------------------------------------
3.6. Open attachment *zotcite_open_in_zotero*
Zotcite calls `vim.ui.open()` to open attachments. But you can change the
command in your config:
>lua
open_cmd = "my_open_script"
If you want <Plug>ZOpenAttachment to open PDF attachments in Zotero (as
opposed to your system's default PDF viewer), put the following in your
`config`:
>lua
open_in_zotero = true
Note that you'll need to have Zotero configured as the default app for
opening `zotero://` links. On Linux, assuming your Zotero installation
included a `zotero.desktop` file, you can do the following:
>sh
xdg-mime default zotero.desktop x-scheme-handler/zotero
------------------------------------------------------------------------------
3.7. Show errors of command to open attachment *zotcite_wait_attachment*
While opening an attachment, zotcite cannot catch errors because it doesn't
wait for the application to finish. If zotcite fails to open a PDF attachment,
you may want to temporarily set `wait_attachment` as `true` in your `config`:
>lua
wait_attachment = true
Then, Neovim should freeze until the attachment is closed, and, if the PDF
viewer finishes with non-zero status, anything output to the `stderr` will be
displayed as a warning message.
==============================================================================
4. Troubleshooting *zotcite_troubleshooting*
*:Zinfo*
If either the plugin does not work or you want easy access to the values of
some internal variables, do the following command:
>vim
:Zinfo
Note that, unless you are editing Quarto documents, Zotcite will not enable
omni completion if any of the buffer's lines matches the regular pattern
`^bibliography:.*\.bib` when the markdown file is read.
vim:tw=78:ts=8:ft=help:norl
================================================
FILE: ftplugin/bib_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: ftplugin/markdown_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: ftplugin/pandoc_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: ftplugin/quarto_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: ftplugin/rmd_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: ftplugin/rnoweb_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: ftplugin/tex_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: ftplugin/typst_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: ftplugin/vimwiki_zotcite.lua
================================================
require("zotcite.config").init()
================================================
FILE: lua/zotcite/bib.lua
================================================
local zwarn = require("zotcite").zwarn
local M = {}
local extract_addbibresource = function(lines)
for _, v in pairs(lines) do
if v:find("\\addbibresource%{") then
local bib = v:gsub("\\addbibresource%{", "")
bib = bib:gsub("}.*", "")
return bib
end
end
return nil
end
local read_lines = function(path)
if not path or vim.fn.filereadable(path) == 0 then return nil end
return vim.fn.readfile(path)
end
local find_root_file = function(lines)
for _, v in pairs(lines) do
local root = v:match("^%s*%%+%s*!%s*[Tt][Ee][Xx]%s+root%s*=%s*(.-)%s*$")
if root and root ~= "" then return root end
end
return nil
end
local resolve_path = function(base_dir, path)
if not path or path == "" then return path end
if path:match("^/") or path:match("^%a:[/\\]") then return path end
return vim.fn.fnamemodify(base_dir .. "/" .. path, ":p")
end
local find_tex_bib = function()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
local bufpath = vim.api.nvim_buf_get_name(0)
local bufdir = vim.fn.fnamemodify(bufpath, ":p:h")
local bib = extract_addbibresource(lines)
if bib then return resolve_path(bufdir, bib) end
local root = find_root_file(lines)
if root then
local rootpath = resolve_path(bufdir, root)
local rootlines = read_lines(rootpath)
if not rootlines then
zwarn("Failed to read TeX root: '" .. rootpath .. "'")
return nil
end
bib = extract_addbibresource(rootlines)
if not bib then
zwarn(
"Could not find the '\\addbibresource' command in TeX root '"
.. rootpath
.. "'"
)
end
local rootdir = vim.fn.fnamemodify(rootpath, ":p:h")
return resolve_path(rootdir, bib)
end
local tfr = require("zotcite.config").get_config().tex_fallback_root
local fallback_root = vim.fn.fnamemodify(bufdir .. "/" .. tfr, ":p")
local fallback_lines = read_lines(fallback_root)
bib = fallback_lines and extract_addbibresource(fallback_lines) or nil
if bib then
local rootdir = vim.fn.fnamemodify(fallback_root, ":p:h")
return resolve_path(rootdir, bib)
end
zwarn("Could not find the '\\addbibresource' command.")
return nil
end
local find_typst_bib = function()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
for _, v in pairs(lines) do
if v and v:find("#bibliography%(") then
return v:match('#bibliography%(%s*"(%S-)".*')
end
end
zwarn("Could not find the `#bibliography` identifier.")
return nil
end
local find_markdown_bib = function()
local ybib = require("zotcite.get").yaml_field("bibliography", 0)
if not ybib then
zwarn("Could not find 'bibliography' field in YAML header.")
return nil
end
if type(ybib) == "string" then return ybib end
local bib = nil
if type(ybib) == "table" then
for _, v in pairs(ybib) do
if type(v) == "string" then
bib = v
if v:find("zotcite.bib") then break end
else
zwarn('Invalid "bibliography" field: ' .. vim.inspect(bib))
return
end
end
end
return bib
end
local find_bib_fn = function()
if vim.bo.filetype == "typst" then return find_typst_bib() end
if vim.bo.filetype == "tex" or vim.bo.filetype == "rnoweb" then
return find_tex_bib()
end
return find_markdown_bib()
end
local get_typ_citations = function(kz)
local kp = kz and "<[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]>"
or "<[%w%-\192-\244\128-\191]+>"
local ckeys = {}
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
for _, v in pairs(lines) do
local i = 1
while true do
local s, e = v:find(kp, i)
if not s or not e then break end
table.insert(ckeys, v:sub(s + 1, e - 1))
i = e + 1
end
end
return ckeys
end
local get_md_citations = function(kz)
local kp = kz and "@[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]"
or "@[%w%-\192-\244\128-\191]+"
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
local ckeys = {}
for _, v in pairs(lines) do
local i = 1
while true do
local s, e = v:find(kp, i)
if not s or not e then break end
table.insert(ckeys, v:sub(s + 1, e))
i = e + 1
end
end
return ckeys
end
local get_tex_citations = function(kz)
local kp1 = "\\%w*cit.*{"
local kp2 = kz and "[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]"
or "[%w%-\192-\244\128-\191]+"
local ckeys = {}
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
for _, v in pairs(lines) do
local i = 1
while true do
local s, e = v:find(kp1, i)
if not s or not e then break end
local j = e
while true do
local s2, e2 = v:find(kp2, j)
if not s2 or not e2 then break end
table.insert(ckeys, v:sub(s2, e2))
j = e2 + 1
end
i = e + 1
end
end
return ckeys
end
M.update = function()
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local kz = kt == "zotero"
local ckeys
if vim.o.filetype == "tex" or vim.o.filetype == "rnoweb" then
ckeys = get_tex_citations(kz)
else
ckeys = get_md_citations(kz)
end
if vim.o.filetype == "typst" then
local ck2 = get_typ_citations(kz)
for _, v in pairs(ck2) do
table.insert(ckeys, v)
end
end
if #ckeys == 0 then return end
local bib = find_bib_fn()
if not bib then return end
require("zotcite.zotero").update_bib(ckeys, bib, kt, false)
vim.schedule(require("zotcite.hl").citations)
end
return M
================================================
FILE: lua/zotcite/config.lua
================================================
---@class ZotciteUserOpts
---Whether to syntax highlight citation keys
---@field hl_cite_key? boolean
---Reference field to as sorting key by :Zseek
---@field sort_key? string
---Value to set the Vim option 'conceallevel'
---@field conceallevel? -1 | 0 | 1 | 2 | 3
---Freeze Neovim after opening a PDF attachment
---@field wait_attachment? boolean
---Open PDF attachments in Zotero
---@field open_in_zotero? boolean
---Space separated list of file types
---for which Zotcite is enabled
---@field filetypes? string[]
---Absolute path to `python3` executable
---@field python_path? string
---Application to extract notes from PDF documents
---@field pdf_extractor? '"pdfnotes.py"' | '"pdfnotes2.py"'
---Template for citation keys
---@field citation_template? string
---Type of citation key (internal Zotero key or template based)
---@field key_type? '"template"' | '"better-bibtex"' | '"zotero"'
---Path to `zotero.sqlite`
---@field zotero_sqlite_path? string
---Path to attachments directory (if any)
---@field attach_dir? string
---Temporary directory
---@field tmpdir? string
---Directory of zotcite's Python scripts
---@field python_scripts_path? string
---Zotero encoding (by default, "utf-8" on Linux
---and "latin1" on Windows)
---@field zotero_encoding? string
---Fields to be excluded from Zotero references
---@field exclude_fields? string
---String used to separate the year from the
---page in references (by default, ", p. ")
---@field year_page_sep? string
---@field open_cmd? string Command to open attachments
---Space separated list of title words to be
---excluded from citation keys
---@field banned_words? string
---Relative path from buffer directory to a fallback TeX root file
---used when no `% !TeX root = ...` is found
---@field tex_fallback_root? string
---@type ZotciteUserOpts
local config = {
key_type = "template",
hl_cite_key = true,
sort_key = "dateModified",
conceallevel = -1,
wait_attachment = false,
open_in_zotero = false,
filetypes = {
"tex",
"markdown",
"pandoc",
"quarto",
"rmd",
"rnoweb",
"typst",
"vimwiki",
},
python_path = "python3",
pdf_extractor = "pdfnotes.py",
tex_fallback_root = "../main.tex",
}
local did_global_init = false
local first_buf
local key_type = {}
local zwarn = require("zotcite").zwarn
local M = {}
local b = {}
M.info = {}
M.show = function()
local info = {}
for k, v in pairs(config) do
table.insert(info, { k, "Identifier" })
table.insert(info, { " = ", "Operator" })
table.insert(info, { vim.inspect(v) .. "\n" })
end
vim.schedule(function() vim.api.nvim_echo(info, false, {}) end)
end
local set_buffer_key_type = function()
local bn = vim.api.nvim_get_current_buf()
if vim.tbl_contains({ "tex", "rnoweb", "typst" }, vim.bo.filetype) then
local ptrn = vim.bo.filetype == "typst" and "^%s*//" or "^%s*%%"
ptrn = ptrn .. "%s*zotcite%-key%-type%s*=%s*"
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
for _, v in pairs(lines) do
if v:find(ptrn) then
local kt = v:match(".*zotcite%-key%-type%s*=%s*(%S*)")
if vim.tbl_contains({ "zotero", "template", "better-bibtex" }, kt) then
key_type[bn] = kt
return
end
end
end
else
local kt = require("zotcite.get").yaml_field("zotcite-key-type", bn)
if kt and type(kt) == "string" then
if vim.tbl_contains({ "zotero", "template", "better-bibtex" }, kt) then
key_type[bn] = kt
return
end
end
end
key_type[bn] = config.key_type
end
M.get_key_type = function(bufnr) return key_type[bufnr] end
local update_config = function()
local zotcite = require("zotcite")
if zotcite.user_opts then
for k, v in pairs(zotcite.user_opts) do
config[k] = v
end
end
end
M.has_buffer = function(bufnr)
for _, v in pairs(b) do
if v.bufnr == bufnr then return v end
end
return nil
end
M.get_b = function() return b end
local set_path = function()
if not config.python_scripts_path then
config.python_scripts_path = debug.getinfo(1, "S").source:match("^@(.*)/lua.*")
.. "/scripts"
end
if vim.fn.has("win32") == 1 then
local zpath = config.python_scripts_path:gsub("/", "\\")
if not vim.env.PATH:find(zpath) then
vim.env.PATH = zpath .. ";" .. vim.env.PATH
end
else
if not vim.env.PATH:find(config.python_scripts_path) then
vim.env.PATH = config.python_scripts_path .. ":" .. vim.env.PATH
end
end
end
local global_init = function()
if vim.fn.executable("sqlite3") == 0 then
zwarn("`sqlite3` executable not found. Please, install it.", true)
return false
end
-- Get Zotero data
local t1 = vim.uv.hrtime()
require("zotcite.zotero").init()
local t2 = vim.uv.hrtime()
M.info["Zotero init time (ms)"] = math.floor(0.5 + (t2 - t1) / 1000000)
local info = require("zotcite.zotero").info()
if not info then
zwarn("Failed to get information from Zotero", true)
return false
end
require("zotcite.hl").citations()
require("zotcite.lsp").start()
set_path()
vim.env.RmdFile = vim.fn.expand("%:p")
vim.api.nvim_create_user_command(
"Zseek",
function(tbl)
require("zotcite.seek").refs(tbl.args, require("zotcite.seek").print)
end,
{ nargs = "?", desc = "Zotcite: seek references" }
)
vim.api.nvim_create_user_command(
"Znote",
function(tbl) require("zotcite.get").note(tbl.args) end,
{ nargs = "?", desc = "Zotcite: insert Zotero notes" }
)
vim.api.nvim_create_user_command(
"Zannotations",
function(tbl) require("zotcite.get").annotations(tbl.args, false) end,
{ nargs = "?", desc = "Zotcite: insert Zotero annotations" }
)
vim.api.nvim_create_user_command(
"Zselectannotations",
function(tbl) require("zotcite.get").annotations(tbl.args, true) end,
{ nargs = "?", desc = "Zotcite: insert Zotero annotations" }
)
vim.api.nvim_create_user_command(
"Zpdfnote",
function(tbl) require("zotcite.get").PDFNote(tbl.args) end,
{ nargs = "?", desc = "Zotcite: insert PDF annotations" }
)
vim.api.nvim_create_user_command(
"Zodt2md",
function(tbl) require("zotcite").ODTtoMarkdown(tbl.args) end,
{ nargs = 1, desc = "Zotcite: convert ODT to Markdown" }
)
vim.api.nvim_create_user_command("Zconfig", require("zotcite.config").show, {})
require("zotcite.get").collection_name(first_buf)
return true
end
M.init = function()
-- Disable for LSP popup windows
if vim.o.buftype == "nofile" then return end
-- Do this only once
if did_global_init then
set_buffer_key_type()
require("zotcite.hl").citations()
require("zotcite.lsp").start()
else
update_config()
set_buffer_key_type()
did_global_init = true
if vim.v.vim_did_enter == 0 then
vim.api.nvim_create_autocmd("VimEnter", {
callback = function() vim.schedule(global_init) end,
})
else
vim.schedule(global_init)
end
end
local bnr = vim.api.nvim_get_current_buf()
first_buf = bnr
if M.has_buffer(bnr) then return end
-- But repeat this for every buffer
if not vim.tbl_contains(config.filetypes, vim.bo[bnr].filetype) then return end
local create_map = function(m, p, s, c, d)
local opts = { silent = true, noremap = true, expr = false, desc = d }
if vim.fn.hasmapto(p, m) == 1 then
vim.api.nvim_buf_set_keymap(bnr, m, p, c, opts)
else
vim.api.nvim_buf_set_keymap(bnr, m, s, c, opts)
end
end
create_map(
"i",
"<Plug>ZCite",
"<C-X><C-B>",
"<Cmd>lua require('zotcite.get').citation()<CR>",
"Zotcite: insert citation"
)
create_map(
"n",
"<Plug>ZOpenAttachment",
"<Leader>zo",
"<Cmd>lua require('zotcite.get').open_attachment()<CR>",
"Zotcite: open attachment"
)
create_map(
"n",
"<Plug>ZViewDocument",
"<Leader>zv",
"<Cmd>lua require('zotcite.utils').view_document()<CR>",
"Zotcite: view document"
)
create_map(
"n",
"<Plug>ZCitationInfo",
"<Leader>zi",
"<Cmd>lua require('zotcite.get').reference_data('ayt')<CR>",
"Zotcite: show reference info (short)"
)
create_map(
"n",
"<Plug>ZCitationCompleteInfo",
"<Leader>za",
"<Cmd>lua require('zotcite.get').reference_data('raw')<CR>",
"Zotcite: show reference info (complete)"
)
create_map(
"n",
"<Plug>ZExtractAbstract",
"<leader>zb",
"<Cmd>lua require('zotcite.get').abstract()<CR>",
"Zotcite: Paste abstract note in current buffer"
)
if config.conceallevel >= 0 then vim.o.conceallevel = config.conceallevel end
vim.api.nvim_create_autocmd(
"InsertLeave",
{ buffer = bnr, callback = require("zotcite.hl").citations }
)
vim.api.nvim_create_autocmd(
"BufWritePre",
{ buffer = bnr, callback = require("zotcite.bib").update }
)
vim.api.nvim_create_autocmd("BufWritePost", {
buffer = bnr,
callback = function(ev) require("zotcite.get").collection_name(ev.buf) end,
})
end
M.get_config = function() return config end
M.inited = function() return did_global_init end
return M
================================================
FILE: lua/zotcite/get.lua
================================================
local config = require("zotcite.config").get_config()
local zwarn = require("zotcite").zwarn
local seek = require("zotcite.seek")
local zotero = require("zotcite.zotero")
local offset = 0
local pdfnote_data = {}
local sel_list = {}
local citation = {
start_col = 0,
end_col = 0,
}
local M = {}
---Convert key and path into valid path
---@param attachment table
---@return string | nil
local translate_zpath = function(attachment, real_path)
if
not real_path
and config.open_in_zotero
and (
attachment.path:lower():find("%.pdf$")
or attachment.path:lower():find("%.html$")
)
then
return "zotero://open-pdf/library/items/" .. attachment.key
end
local fpath = tostring(attachment.path)
if attachment.path:find("attachments:") then
-- The user has set Edit / Preferences / Files and Folders / Base directory for linked attachments
if not config.attach_dir then
zwarn(
"Are you using a base directory for linked attachments? "
.. "The config option `attach_dir` is not defined."
)
return nil
else
fpath =
attachment.path:gsub(".*attachments:", "/" .. config.attach_dir .. "/")
end
elseif attachment.path:find(":/") then
-- Absolute file path
fpath = attachment.path:gsub(".*:/", "/")
elseif attachment.path:find("storage:") then
-- Default path
fpath = config.zotero_sqlite_path:match("(.*)/.-")
.. attachment.path:gsub("storage:", "/storage/" .. attachment.key .. "/")
end
if vim.fn.filereadable(fpath) == 0 then
zwarn('Could not find "' .. fpath .. '"')
fpath = ""
end
return fpath
end
local pdf_path = function(key, cb, real_path)
local repl, err = zotero.get_attachment(key)
if not repl then
zwarn(err)
return
end
local fpaths = {}
local item
for _, v in pairs(repl) do
item = translate_zpath(v, real_path)
if item then table.insert(fpaths, item) end
end
if #repl == 1 then
return fpaths[1]
else
local idx = 1
local items = {}
sel_list = {}
for _, v in pairs(fpaths) do
item = v:gsub(".*/", "")
item = vim.fn.slice(item, 0, 60)
table.insert(items, item)
table.insert(sel_list, v)
idx = idx + 1
end
vim.schedule(function() vim.ui.select(items, {}, cb) end)
end
end
local is_valid_char = function(c)
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
if kt == "zotero" then
return (c >= "0" and c <= "9")
or (c >= "A" and c <= "z")
or (c >= "a" and c <= "z")
or c:byte(1, 1) > 127
end
return c:find("[%w%-\192-\244\128-\191]")
end
local citation_key_vt = function(line, pos)
pos = pos + 1
if line:sub(pos, pos) == "@" or line:sub(pos, pos) == "<" then
pos = pos + 1
elseif line:sub(pos, pos) == ">" then
pos = pos - 1
end
local i = pos
local j = pos
while i > 0 and is_valid_char(line:sub(i, i)) do
i = i - 1
end
while j <= #line and is_valid_char(line:sub(j, j)) do
j = j + 1
end
local key = line:sub(i + 1, j - 1)
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
if kt == "zotero" then
if #key == 8 then return key end
return ""
else
return key
end
end
M.citation_key = function()
local lnum = vim.api.nvim_win_get_cursor(0)[1]
local line = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1]
local pos = vim.api.nvim_win_get_cursor(0)[2]
return citation_key_vt(line, pos)
end
M.reference_data = function(btype)
local wrd = M.citation_key()
if wrd ~= "" then
local repl = zotero.get_ref_data(wrd)
if type(repl) ~= "table" then
zwarn("Citation key not found")
return
end
local info = {}
if btype == "raw" then
for k, v in pairs(repl) do
table.insert(info, { k, "Title" })
table.insert(info, { ": " .. vim.inspect(v):gsub("\n$", "") .. "\n" })
end
else
if repl.alastnm then
table.insert(info, { repl.alastnm .. " ", "Identifier" })
end
if repl.year then table.insert(info, { repl.year .. " ", "Number" }) end
if repl.title then table.insert(info, { repl.title, "Title" }) end
end
vim.schedule(function() vim.api.nvim_echo(info, false, {}) end)
end
end
local finish_citation = function(ref)
if not ref then return end
local rownr = vim.api.nvim_win_get_cursor(0)[1] - 1
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local cite = kt == "zotero" and ref.value.key or ref.value.cite
if not (vim.bo.filetype == "tex" or vim.bo.filetype == "rnoweb") then
cite = "@" .. cite
end
vim.api.nvim_buf_set_text(
0,
rownr,
citation.start_col,
rownr,
citation.end_col,
{ cite }
)
local colnr = citation.start_col + #cite
vim.api.nvim_win_set_cursor(0, { rownr + 1, colnr })
vim.api.nvim_feedkeys("a", "n", false)
vim.schedule(require("zotcite.hl").citations)
end
M.citation = function()
local argmt = ""
local line = vim.api.nvim_get_current_line()
local last = vim.api.nvim_win_get_cursor(0)[2]
citation.start_col = last
citation.end_col = last
local c = line:sub(last, last):lower()
if (c >= "a" and c <= "z") or c > "\127" then
local first = last - 1
while first > 0 and ((c >= "a" and c <= "z") or c > "\127") do
first = first - 1
c = line:sub(first, first):lower()
end
argmt = line:sub(first + 1, last):lower()
citation.start_col = first
end
seek.refs(argmt, finish_citation)
end
M.abstract = function()
local wrd = M.citation_key()
if wrd ~= "" then
local repl = zotero.get_ref_data(wrd)
if type(repl) ~= "table" then
zwarn("Citation key not found")
return
end
if repl.abstractNote then
vim.api.nvim_put({ repl.abstractNote }, "l", true, true)
else
zwarn("No abstract found associated with article")
end
end
end
local get_annotations = function(sel)
local key = sel.value.key
local repl = zotero.get_annotations(key, offset)
if not repl then zwarn("No annotation found.") end
return repl
end
local finish_annotations = function(sel)
if not sel then return end
local repl = get_annotations(sel)
if not repl then return end
local lnum = vim.api.nvim_win_get_cursor(0)[1]
vim.api.nvim_buf_set_lines(0, lnum, lnum, true, repl)
require("zotcite.hl").citations()
end
local finish_annotations_selection = function(sel)
if not sel then return end
local lang = "markdown"
if vim.bo.filetype == "typst" then lang = "typst" end
if vim.bo.filetype == "tex" or vim.bo.filetype == "noweb" then lang = "latex" end
local raw_annotations = get_annotations(sel)
if raw_annotations then
local grouped_annotations = {}
local current_group = {}
local last_was_quote = false
for _, line in ipairs(raw_annotations) do
if
(lang == "markdown" and line:sub(1, 1) == ">")
or (lang == "typst" and line:find("^#quote"))
or (lang == "latex" and line:find("^\\begin%{quote%}"))
then
if #current_group > 0 and not last_was_quote then
table.insert(current_group, line)
table.insert(grouped_annotations, table.concat(current_group, "\n"))
current_group = {}
else
if #current_group > 0 then
table.insert(
grouped_annotations,
table.concat(current_group, "\n")
)
current_group = {}
end
table.insert(current_group, line)
end
last_was_quote = true
else
if last_was_quote and #current_group > 0 then
table.insert(grouped_annotations, table.concat(current_group, "\n"))
current_group = {}
end
table.insert(current_group, line)
last_was_quote = false
end
end
if #current_group > 0 then
table.insert(grouped_annotations, table.concat(current_group, "\n"))
end
local selected_indices = {}
local function select_annotation()
local opts = {}
local w = vim.o.columns - 10
if w > 140 then w = 140 end
for i, annotation in ipairs(grouped_annotations) do
if not selected_indices[i] then
table.insert(opts, i .. ": " .. annotation:sub(1, w)) -- Show first chars
end
end
if #opts == 0 then
if #selected_indices > 0 then
local sel_annot = {}
for index in pairs(selected_indices) do
table.insert(sel_annot, grouped_annotations[index])
end
local lnum = vim.api.nvim_win_get_cursor(0)[1]
local txt = table.concat(sel_annot, "\n\n")
local lines = vim.split(txt, "\n")
vim.api.nvim_buf_set_lines(0, lnum, lnum, true, lines)
require("zotcite.hl").citations()
end
return
end
vim.ui.select(
opts,
{ prompt = "Select annotation (or cancel to finish)" },
function(choice)
if choice then
local index = tonumber(vim.split(choice, ":")[1])
if index then
selected_indices[index] = true
select_annotation() -- Ask for the next selection
end
else
if #selected_indices > 0 then
local annot = {}
for index in pairs(selected_indices) do
table.insert(annot, grouped_annotations[index])
end
local lnum = vim.api.nvim_win_get_cursor(0)[1]
vim.api.nvim_buf_set_lines(
0,
lnum,
lnum,
true,
vim.split(table.concat(annot, "\n\n"), "\n")
)
require("zotcite.hl").citations()
end
end
end
)
end
select_annotation()
end
end
M.annotations = function(ko, use_selection)
local argmt
if ko:find(" ") then
ko = vim.fn.split(ko)
argmt = ko[1]
if ko[2] then
local ko2 = tonumber(ko[2])
if ko2 then offset = ko2 end
end
else
argmt = ko
offset = 0
end
if use_selection then
seek.refs(argmt, finish_annotations_selection)
else
seek.refs(argmt, finish_annotations)
end
end
local finish_note = function(sel)
local key = sel.value.key
local repl = zotero.get_notes(key)
if not repl then
zwarn("No note found.")
else
local lines = vim.fn.split(repl, "\n")
local lnum = vim.api.nvim_win_get_cursor(0)[1]
vim.api.nvim_buf_set_lines(0, lnum, lnum, true, lines)
require("zotcite.hl").citations()
end
end
M.note = function(key) seek.refs(key, finish_note) end
local finish_pdfnote_2 = function(_, idx)
local fpath = sel_list[idx]
local lnum = vim.api.nvim_win_get_cursor(0)[1]
local key = pdfnote_data.citekey
local p = pdfnote_data.pg
if vim.fn.filereadable(fpath) == 0 then
zwarn('File not readable: "' .. fpath .. '"')
return
end
-- Determine which PDF extractor to use
local pdf_extractor = config.pdf_extractor or "pdfnotes.py"
vim.env.ZYearPageSep = config.year_page_sep
local notes = vim.system({
config.python_path,
config.python_scripts_path .. "/" .. pdf_extractor,
fpath,
key,
p,
}, { text = true }):wait()
if notes.code == 0 then
local lines = vim.fn.split(notes.stdout, "\n")
if vim.bo.filetype == "typst" then
local tlines = {}
for _, v in pairs(lines) do
v = v:gsub(
"^> (.-) %[@(%S-), (.-)%]$",
'#quote[%1] #cite(<%2>, supplement: "%3")'
)
table.insert(tlines, v)
end
vim.api.nvim_buf_set_lines(0, lnum, lnum, true, tlines)
elseif vim.bo.filetype == "tex" or vim.bo.filetype == "rnoweb" then
local tlines = {}
for _, v in pairs(lines) do
v = v:gsub(
"^> (.-) %[@(%S-), (.-)%]$",
"\\begin{quote}%1 \\cite[%3]{%2}\\end{quote}"
)
table.insert(tlines, v)
end
vim.api.nvim_buf_set_lines(0, lnum, lnum, true, tlines)
else
vim.api.nvim_buf_set_lines(0, lnum, lnum, true, lines)
end
require("zotcite.hl").citations()
elseif notes.code == 33 then
zwarn('Failed to load "' .. fpath .. '" as a valid PDF document.')
elseif notes.code == 34 then
zwarn("No annotations found.")
else
zwarn(notes.stderr)
end
end
local finish_pdfnote = function(sel)
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local key = kt == "zotero" and sel.value.key or sel.value.cite
local repl = zotero.get_ref_data(key)
if type(repl) ~= "table" then
zwarn("Citation key not found")
return
end
local citekey = "@"
if kt == "zotero" then
citekey = citekey .. sel.value.key
else
citekey = citekey .. sel.value.cite
end
local pg = "1"
if repl.pages and repl.pages:find("[0-9]-") then pg = repl.pages end
pdfnote_data = { citekey = citekey, pg = pg }
local apath = pdf_path(key, finish_pdfnote_2, true)
if type(apath) == "string" then
sel_list = { apath }
finish_pdfnote_2(nil, 1)
end
end
M.PDFNote = function(key) seek.refs(key, finish_pdfnote) end
--- Strip string from empty spaces and quotes
---@param line string The string to be stripped
---@return string
local get_yaml_string = function(line)
local str
if line:find("^'.*'$") then
str = line:match("^%s*'(.-)'%s*$")
elseif line:find('^%s*".*"%s*$') then
str = line:match('^%s*"(.-)"%s*$')
else
str = line:match("^%s*(.-)%s*$")
end
return str
end
--- Get the value of a YAML field
---@param field string Field name
---@param bn integer Buffer number
---@return string | string[] | nil
M.yaml_field = function(field, bn)
if vim.tbl_contains({ "tex", "rnoweb" }, vim.bo.filetype) then return nil end
local line1 = vim.api.nvim_buf_get_lines(bn, 0, 1, true)[1]
if line1 ~= "---" then return end
local lines = vim.api.nvim_buf_get_lines(bn, 0, -1, true)
local nlines = #lines
local i = 2
local value
while i < nlines do
if lines[i] == "---" then break end
if lines[i]:find(field .. ":") then
if lines[i]:find(field .. ":%s*$") then
-- multiline list
value = {}
i = i + 1
while lines[i]:find("^%s*%-") do
if lines[i] == "---" then break end
local line = lines[i]:match("^%s*%-%s*(.-)%s*$")
table.insert(value, get_yaml_string(line))
i = i + 1
end
if lines[i]:find("^%s*%w*:$") then
return lines[i]:match("^%s*(%w-):$")
end
elseif lines[i]:find(field .. ":%s%[.*%]%s*$") then
-- bracketed list in a single line
value = vim.split(lines[i]:match("^" .. field .. ":%s*%[(.-)%]%s*$"), ",")
for k, v in pairs(value) do
value[k] = get_yaml_string(v)
end
else
-- string
value = lines[i]:match("^%s*" .. field .. ":%s*(.-)%s*$")
value = get_yaml_string(value)
end
return value
end
i = i + 1
end
return nil
end
--- Get collection names from buffer and set them in zotero.lua
---@param bn integer Buffer number
M.collection_name = function(bn)
local newc = M.yaml_field("collection", bn)
if not newc then return end
if type(newc) == "string" then newc = { newc } end
zotero.set_collections(vim.api.nvim_buf_get_name(0), newc)
end
--- Insert echo lines into table
---@param info table The table with echo lines
---@param ttl string Section title
---@param lines string[] Lines to insert
local function insert_info(info, ttl, lines)
table.insert(info, { ttl .. "\n", "Statement" })
for k, v in pairs(lines) do
table.insert(info, { " " .. k, "Title" })
table.insert(info, { ": " })
local hl = type(v) == "string" and "String"
or type(v) == "number" and "Number"
or "Normal"
table.insert(info, { vim.inspect(v), hl })
table.insert(info, { "\n" })
end
end
M.zotero_info = function()
if
not vim.tbl_contains(config.filetypes, vim.bo.filetype)
and vim.bo.filetype ~= "bib"
then
zwarn("zotcite doesn't support " .. vim.bo.filetype .. " filetype.")
return
end
local out_lines = {}
insert_info(out_lines, "Init information", require("zotcite.config").info)
insert_info(out_lines, "Zotero information", require("zotcite.zotero").info())
local w = require("zotcite").get_warns()
if #w > 0 then
table.insert(out_lines, { "Know problems:\n", "Statement" })
for _, v in pairs(w) do
table.insert(out_lines, { v .. "\n", "WarningMsg" })
end
end
vim.schedule(function() vim.api.nvim_echo(out_lines, false, {}) end)
end
local finish_open_attachment = function(_, idx)
if idx then require("zotcite.utils").open(sel_list[idx]) end
end
M.open_attachment = function()
local key = M.citation_key()
local apath = pdf_path(key, finish_open_attachment, false)
if type(apath) == "string" then require("zotcite.utils").open(apath) end
end
return M
================================================
FILE: lua/zotcite/hl.lua
================================================
local config = require("zotcite.config").get_config()
local M = {}
--- Highlight citation key and add virtual text
---@param ns integer Namespace id
---@param i integer Line
---@param s integer Column
---@param e integer End column
---@param a string
local vt_citation = function(ns, i, s, e, c, a)
if not a then return end
local set_m = vim.api.nvim_buf_set_extmark
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
if kt == "zotero" then
a = a:gsub("%-", "_")
set_m(0, ns, i - 1, s - 1, { end_col = e, hl_group = "Ignore", conceal = "" })
set_m(
0,
ns,
i - 1,
c,
{ virt_text = { { a, "Identifier" } }, virt_text_pos = "inline" }
)
else
if vim.tbl_contains({ "tex", "rnoweb", "bib" }, vim.bo.filetype) then
set_m(0, ns, i - 1, s - 1, { end_col = e, hl_group = "Identifier" })
else
set_m(0, ns, i - 1, s - 1, { end_col = s, hl_group = "Ignore", conceal = "" })
set_m(0, ns, i - 1, s, { end_col = e, hl_group = "Identifier" })
end
end
end
local vt_citations_bib = function(lines, ns)
local set_m = vim.api.nvim_buf_set_extmark
local key = nil
local citekey = nil
local zotkey = nil
local zlnum = 0
local clnum = 0
local hl = "WarningMsg"
for k, v in pairs(lines) do
if v:find("^@%S*{.*,%s*$") then
key = v:match("^@%S*{(.*),%s*$")
elseif v:find("^%s*zotkey%s*=%s{%S*},") then
zotkey = v:match("^%s*zotkey%s*=%s{(%S*)},")
zlnum = k
elseif v:find("^%s*citekey%s*=%s{%S*},") then
citekey = v:match("^%s*citekey%s*=%s{(%S*)},")
clnum = k
end
if key and citekey and zotkey then
local grd = require("zotcite.zotero").get_ref_data
local kt =
require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local r = kt == "zotero" and grd(zotkey) or grd(citekey)
if not r or r.zotkey ~= zotkey then
local s, e = lines[zlnum]:find(zotkey)
if s and e then
set_m(0, ns, zlnum - 1, s - 1, { end_col = e, hl_group = hl })
end
end
if not r or r.citekey ~= citekey then
local s, e = lines[clnum]:find(citekey:gsub("%-", "%%-"))
if s and e then
set_m(0, ns, clnum - 1, s - 1, { end_col = e, hl_group = hl })
end
end
citekey = nil
zotkey = nil
key = nil
end
end
end
local vt_citations_md = function(ac, ns, lines)
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local kp = kt == "zotero"
and "@[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]"
or "@[%w%-\192-\244\128-\191]+"
local a = ""
for k, v in pairs(lines) do
local i = 1
local imax = #v
while i < imax do
local s, e = v:find(kp, i)
if not s or not e then break end
a = ac[v:sub(s + 1, e)]
vt_citation(ns, k, s, e, s, a)
i = e + 1
end
end
end
local vt_citations_typ = function(ac, ns, lines)
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local kp = kt == "zotero"
and "<[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]>"
or "<[%w%-\192-\244\128-\191]+>"
local a = ""
for k, v in pairs(lines) do
local i = 1
local imax = #v
while i < imax do
local s, e = v:find(kp, i)
if not s or not e then break end
a = ac[v:sub(s + 1, e - 1)]
vt_citation(ns, k, s, e, e, a)
i = e + 1
end
end
end
local vt_citations_tex = function(ac, ns, lines)
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local kp1 = "\\%w*cit.*{"
local kp2 = kt == "zotero"
and "[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]"
or "[%w%-\192-\244\128-\191]+"
local a = ""
for k, v in pairs(lines) do
local i = 1
local imax = #v
while i < imax do
local s, e = v:find(kp1, i)
if not s or not e then break end
local j = e
local l = v:find("%}", j)
if not l then l = 1000 end
while j < l do
local s2, e2 = v:find(kp2, j)
if not s2 or not e2 then break end
a = ac[v:sub(s2, e2)]
vt_citation(ns, k, s2, e2, e2, a)
j = e2 + 1
end
i = e + 1
end
end
end
M.citations = function()
if not config.hl_cite_key then return end
local ac = require("zotcite.zotero").get_all_citations()
local ns = vim.api.nvim_create_namespace("ZCitation")
vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
if not vim.tbl_contains({ "tex", "rnoweb", "bib" }, vim.bo.filetype) then
vt_citations_md(ac, ns, lines)
end
if vim.bo.filetype == "typst" then
vt_citations_typ(ac, ns, lines)
elseif vim.bo.filetype == "tex" or vim.bo.filetype == "rnoweb" then
vt_citations_tex(ac, ns, lines)
elseif vim.bo.filetype == "bib" then
vt_citations_bib(lines, ns)
end
end
return M
================================================
FILE: lua/zotcite/init.lua
================================================
local M = {}
local warns = {}
--- Warning
---@param msg string The message
---@param store boolean? Whether to store the message
M.zwarn = function(msg, store)
vim.notify(msg, vim.log.levels.WARN, { title = "zotcite" })
if store then table.insert(warns, msg) end
end
M.get_warns = function() return warns end
M.user_opts = nil
--- Setup
---@param opts? ZotciteUserOpts
M.setup = function(opts)
if opts then M.user_opts = opts end
end
return M
================================================
FILE: lua/zotcite/lsp.lua
================================================
local zwarn = require("zotcite").zwarn
local zotero = require("zotcite.zotero")
local last_line = 0
local z_ls = {
name = "zotero_ls",
client_id = nil,
initialized = false,
stopped = true,
}
local compl_region = true
--- Resolve selected menu item
---@param key string
local resolve = function(key)
local ref = zotero.get_ref_data(key)
if not ref then return nil end
local doc = ""
local ttl = ref.title and ref.title or nil
local etype = string.gsub(ref.etype, "([A-Z])", " %1")
etype = string.lower(etype)
doc = "**" .. ttl .. "**\n\n"
if ref.etype == "journalArticle" and ref.publicationTitle then
doc = doc .. "*" .. ref.publicationTitle .. "*\n\n"
elseif ref.etype == "bookSection" and ref.bookTitle then
doc = doc .. "In *" .. ref.bookTitle .. "*\n\n"
end
if ref.alastnm then doc = doc .. ref.alastnm end
if ref.year then
doc = doc .. " (" .. ref.year .. ") "
else
doc = doc .. " (????) "
end
return etype, doc
end
--- Fill completion menu
---@param lnum integer Line number
---@param char integer Cursor column
local complete = function(callback, lnum, char)
local line = vim.api.nvim_buf_get_lines(0, lnum, lnum + 1, true)[1]
local byte_idx = vim.fn.byteidx(line, char)
if byte_idx < 0 then byte_idx = #line end
local subline = line:sub(1, byte_idx)
local word
if vim.bo.filetype == "rnoweb" or vim.bo.filetype == "tex" then
word = subline:match(".*{.-(%S+)$")
else
word = subline:match(".*@(%S+)$")
end
if not word then
callback(nil, { isIncomplete = false, items = {} })
return
end
local compl_items = {}
local bnm = vim.api.nvim_buf_get_name(0)
if vim.fn.has("win32") == 1 then bnm = string.gsub(tostring(bnm), "\\", "/") end
local itms = zotero.get_match(word, bnm)
local text_edit_range = {
start = {
line = lnum,
character = char - 1,
},
["end"] = {
line = lnum,
character = char,
},
}
if itms then
for _, v in pairs(itms) do
local txt = string.format("%s (%s) %s", v.alastnm, v.year, v.title)
if vim.fn.strwidth(txt) > 58 then
txt = vim.fn.strcharpart(txt, 0, 58) .. "⋯"
end
local kt =
require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
table.insert(compl_items, {
label = txt,
kind = vim.lsp.protocol.CompletionItemKind.Variable,
textEdit = {
newText = kt == "zotero" and v.zotkey or v.citekey,
range = text_edit_range,
},
})
end
end
callback(nil, { isIncomplete = false, items = compl_items })
end
--- Hover implementation
---@param lnum integer Line number
---@param char integer Cursor column
---@return table
local hover = function(lnum, char)
local line = vim.api.nvim_buf_get_lines(0, lnum, lnum + 1, true)[1]
-- Find zotero key
local k = char
local pre
if vim.bo.filetype == "tex" or vim.bo.filetype == "rnoweb" then
pre = line:sub(1, k):match(".-([%w%-\192-\244\128-\191]-)$")
else
pre = line:sub(1, k):match(".*@(.*)")
end
if not pre then return {} end
local pos = line:sub(k + 1, -1):match("^(%S*).*")
if not pos then return {} end
local subline = pre .. pos
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local ktnz = kt ~= "zotero"
local key = ktnz and subline:match("^([%w%-\192-\244\128-\191]+)")
or subline:match(
"^([0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z])"
)
if not key then return {} end
local ttl, doc = resolve(key)
if not ttl then return {} end
local i, j
if ktnz then
i, j = line:find(key)
else
i, j = line:find(key .. "[%w%-0-9]*")
end
if i then
return {
range = {
start = {
line = lnum,
character = i - 1,
},
["end"] = {
line = lnum,
character = j,
},
},
contents = { kind = "markdown", value = ttl .. "\n\n---\n\n" .. doc },
}
end
return { contents = { kind = "markdown", value = ttl .. "\n\n---\n\n" .. doc } }
end
--- Get list of "zotero_ls" servers attached to current buffer
---@return integer[]
local function get_z_id()
local cids = vim.lsp.get_clients({ buffer = vim.api.nvim_get_current_buf() })
local ids = {}
for _, v in pairs(cids) do
if v.name == z_ls.name then table.insert(ids, v.id) end
end
return ids
end
--- This function receives 4 arguments: method, params, callback, notify_callback
local function lsp_request(method, params, callback, _)
if method == "textDocument/completion" then
if not compl_region then
callback(nil, { isIncomplete = false, items = {} })
return
end
vim.schedule(
function() complete(callback, params.position.line, params.position.character) end
)
elseif method == "completionItem/resolve" then
local key = params.textEdit.newText
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
if kt == "zotero" then key = key:gsub("%-.*", "") end
local ttl, doc = resolve(key)
if ttl then
params.detail = ttl
params.documentation = { kind = "zinfo", value = doc }
end
callback(nil, params)
elseif method == "textDocument/hover" then
if not compl_region then
callback(nil, { result = nil })
return
end
local res = hover(params.position.line, params.position.character)
if res then callback(nil, res) end
elseif method == "initialize" then
callback(nil, {
capabilities = {
textDocument = {
completion = { completionItem = { snippetSupport = false } },
},
hoverProvider = true,
completionProvider = {
-- would work only if we could reset the completion
-- triggerCharacters = { "@" },
resolveProvider = true,
},
},
})
elseif method == "shutdown" then
z_ls.stopped = true
local zid = get_z_id()
if #zid > 0 then
for _, v in pairs(zid) do
if vim.lsp.buf_is_attached(0, v) then vim.lsp.buf_detach_client(0, v) end
vim.lsp.get_client_by_id(v):stop()
end
end
else
zwarn(
string.format(
'LSP method "%s" not implemented. Received `params`:\n%s',
vim.inspect(method),
vim.inspect(params)
)
)
end
end
--- This function receives two arguments: method, params
local function lsp_notify(method, _)
if method == "initialized" then
z_ls.initialized = true
z_ls.stopped = false
end
end
-- Inspired by null-ls (see none-ls.nvim/lua/null-ls/rpc.lua)
local function lsp_start(_, _)
return {
request = lsp_request,
notify = lsp_notify,
is_closing = function() return z_ls.stopped end,
terminate = function() z_ls.stopped = true end,
}
end
--- Set the value of `compl_region` which indicates if the cursor is in a
--- region where the completion of Zotero's keys is meaningful for Rnoweb or
--- LaTeX documents
local function set_compl_region_rnw()
local curpos = vim.api.nvim_win_get_cursor(0)
if not curpos then
compl_region = false
return
end
local line = vim.api.nvim_buf_get_lines(0, curpos[1] - 1, curpos[1], true)[1]
local subline = line:sub(1, curpos[2])
local pre, pos = subline:match(".*(\\cite%S*{)(.*)")
if pre and not pos:find("}") then
compl_region = true
else
compl_region = false
end
end
--- Set the value of `compl_region` which indicates if the cursor is in a
--- region where the completion of Zotero's keys is meaningful for Markdown
--- documents
local function set_compl_region_md()
local lines = vim.api.nvim_buf_get_lines(vim.api.nvim_get_current_buf(), 0, -1, true)
local lnum = last_line
-- Check if we are within normal markdown text
compl_region = true
for i = lnum, 1, -1 do
if string.find(lines[i], "^```{") then
-- within code block
compl_region = false
break
else
if string.find(lines[i], "^```$") then
-- after a code block
break
else
if
string.find(lines[i], "^---$") or string.find(lines[i], "^%.%.%.$")
then
-- after or within YAML front matter
break
end
end
end
end
end
--- Call the appropriate function to set the value `compl_region`
local function on_cursor_move()
if vim.bo.filetype == "rnoweb" or vim.bo.filetype == "tex" then
set_compl_region_rnw()
end
local curpos = vim.api.nvim_win_get_cursor(0)
if not curpos then return end
local curline = curpos[1]
if curline == last_line then return end
last_line = curline
set_compl_region_md()
end
local M = {}
--- Start or enable the language server
function M.start()
-- TODO: remove this when nvim 0.12 is released
if not vim.lsp.config then return end
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
buffer = vim.api.nvim_get_current_buf(),
callback = on_cursor_move,
})
vim.api.nvim_create_autocmd("CompleteDone", {
buffer = vim.api.nvim_get_current_buf(),
callback = require("zotcite.hl").citations,
})
local config = require("zotcite.config").get_config()
vim.lsp.config(z_ls.name, { cmd = lsp_start, filetypes = config.filetypes })
if z_ls.client_id then
vim.lsp.enable(z_ls.name)
else
z_ls.client_id = vim.lsp.start({ name = z_ls.name, cmd = lsp_start })
end
end
return M
================================================
FILE: lua/zotcite/seek.lua
================================================
local config = require("zotcite.config").get_config()
local ns = vim.api.nvim_create_namespace("ZSeekPreview")
local zotero = require("zotcite.zotero")
local M = {}
local get_match = function(key)
local citeptrn = key:gsub(" .*", "")
local refs = zotero.get_match(citeptrn, vim.api.nvim_buf_get_name(0))
if #refs == 0 then
vim.schedule(
function() vim.api.nvim_echo({ { "No matches found." } }, false, {}) end
)
end
return refs
end
M.print = function(ref)
local msg = {
{ ref.value.alastnm, "Identifier" },
{ " " },
{ ref.value.year, "Number" },
{ " " },
{ ref.value.title, "Title" },
}
vim.schedule(function() vim.api.nvim_echo(msg, false, {}) end)
end
local format_preview = function(v)
local alist = {}
local authors
if v.author then
if #v.author > 5 then
authors = v.author[1][1] .. ", " .. v.author[1][2] .. " and others"
else
for _, n in pairs(v.author) do
table.insert(alist, n[1] .. ", " .. n[2])
end
authors = table.concat(alist, "; ")
end
else
authors = "?"
end
local year = v.year or "????"
local title = v.title or "????"
local ptitle = v.publicationTitle or "????"
local txt
local hl = { { g = "Identifier", s = 0, e = #authors } }
table.insert(hl, { g = "Number", s = hl[1].e + 1, e = hl[1].e + 1 + #year })
table.insert(hl, { g = "Title", s = hl[2].e + 1, e = hl[2].e + 1 + #title })
if v.etype == "journalArticle" then
txt = string.format(
"%s %s %s. %s.\n\n%s\n",
authors,
year,
title,
ptitle,
v.abstract or "No abstract available."
)
table.insert(hl, { g = "Include", s = hl[3].e + 2, e = hl[3].e + 2 + #ptitle })
elseif v.etype == "bookSection" then
txt = string.format(
"%s %s %s. In: %s.\n\n%s\n",
authors,
year,
title,
ptitle,
v.abstract or ""
)
table.insert(hl, { g = "Include", s = hl[3].e + 6, e = hl[3].e + 6 + #ptitle })
else
txt = string.format("%s %s %s.\n\n%s\n", authors, year, title, v.abstract or "")
end
return txt, hl
end
--- Use telescope to find and select a reference
---@param key string Pattern to search
---@param cb function Callback function
M.refs = function(key, cb)
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local sorters = require("telescope.config").values
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local previewers = require("telescope.previewers")
local entry_display = require("telescope.pickers.entry_display")
local mtchs = get_match(key)
local references = {}
local awidth = 2
local awlim = vim.o.columns - 140
if awlim < 20 then awlim = 20 end
if awlim > 60 then awlim = 60 end
for _, v in pairs(mtchs) do
if #v.alastnm > awidth then awidth = #v.alastnm end
table.insert(references, {
display = v.alastnm .. " " .. v.year .. " " .. v.title,
etype = v.etype,
sort_key = v[config.sort_key] or "0000-00-00 0000",
publicationTitle = v.publicationTitle
or v.bookTitle
or v.proceedingsTitle
or v.conferenceName
or v.programTitle
or v.blogTitle
or v.code
or v.dictionaryTitle
or v.encyclopediaTitle
or v.forumTitle
or v.websiteTitle
or v.seriesTitle,
author = v.author
or v.artist
or v.performer
or v.director
or v.composer
or v.sponsor
or v.contributor
or v.interviewee
or v.cartographer
or v.inventor
or v.podcaster
or v.presenter
or v.programmer
or v.recipient
or v.editor
or v.seriesEditor
or v.translator,
alastnm = v.alastnm,
year = v.year,
title = v.title,
abstract = v.abstractNote,
key = v.zotkey,
cite = v.citekey,
})
end
if awidth > awlim then awidth = awlim end
table.sort(references, function(a, b) return (a.sort_key > b.sort_key) end)
pickers
.new({}, {
prompt_title = "Search pattern",
results_title = "Zotero references",
finder = finders.new_table({
results = references,
entry_maker = function(entry)
local displayer = entry_display.create({
separator = " ",
items = {
{ width = awidth }, -- Author
{ width = 4 }, -- Year
{ remaining = true }, -- Title
},
})
return {
value = entry,
display = function(e)
return displayer({
{ e.value.alastnm, "Identifier" },
{ e.value.year, "Number" },
{ e.value.title, "Title" },
})
end,
ordinal = entry.display,
}
end,
}),
sorter = sorters.generic_sorter({}),
previewer = previewers.new_buffer_previewer({
define_preview = function(self, entry, _)
local bufnr = self.state.bufnr
local preview_text, hl = format_preview(entry.value)
vim.api.nvim_buf_set_lines(
bufnr,
0,
-1,
false,
vim.split(preview_text, "\n")
)
for _, h in pairs(hl) do
vim.hl.range(bufnr, ns, h.g, { 0, h.s }, { 0, h.e }, {})
end
end,
}),
attach_mappings = function(prompt_bufnr, map)
map({ "i", "n" }, "<CR>", function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
-- Handle the selected reference here
cb(selection)
end)
return true
end,
})
:find()
end
-- text wrapping in the preview window
vim.api.nvim_create_autocmd("User", {
pattern = "TelescopePreviewerLoaded",
callback = function()
vim.wo.wrap = true
vim.wo.linebreak = true
end,
})
return M
================================================
FILE: lua/zotcite/utils.lua
================================================
local config = require("zotcite.config").get_config()
local zwarn = require("zotcite").zwarn
local M = {}
M.ODTtoMarkdown = function(odt)
require("zotcite.config").set_path()
local mdf = vim.system({ config.python_path, "odt2md.py", odt }, { text = true })
:wait()
if mdf.code == 0 then
vim.cmd("tabnew " .. mdf.stdout)
else
zwarn(mdf.stderr:gsub("\n", " "))
end
end
---@return string
local get_output_ext = function()
if vim.tbl_contains({ "tex", "rnoweb", "typst" }, vim.bo.filetype) then
return "pdf"
end
local field = vim.bo.filetype == "quarto" and "format" or "output"
local fmt = require("zotcite.get").yaml_field(field, vim.api.nvim_get_current_buf())
if fmt then
if type(fmt) == "table" then
fmt = #fmt > 0 and tostring(fmt[1]) or "html"
else
fmt = tostring(fmt)
end
if fmt == "pdf_document" or fmt == "beamer" then
return "pdf"
elseif fmt == "odf_document" then
return "odt"
end
return fmt
end
return "html"
end
M.view_document = function()
local ext = get_output_ext()
local doc = vim.fn.expand("%:p:r") .. "." .. ext
if vim.fn.filereadable(doc) == 0 then
zwarn('File "' .. doc .. '" not found.')
return
end
M.open(doc)
end
M.open = function(fpath)
if config.wait_attachment then
local obj
local em
if config.open_cmd then
obj = vim.system({ config.open_cmd, fpath }, { text = true }):wait()
em = "Error running `" .. config.open_cmd .. ' "' .. fpath .. '"' .. "`"
else
obj = vim.ui.open(fpath):wait()
em = 'Error running `vim.ui.open("' .. fpath .. '")`'
end
if obj.code ~= 0 then
em = em .. ":\n exit code: " .. tostring(obj.code)
if obj.stdout and obj.stdout ~= "" then em = em .. "\n " .. obj.stdout end
if obj.stderr and obj.stderr ~= "" then em = em .. "\n " .. obj.stderr end
zwarn(em)
end
return
end
if config.open_cmd then
vim.system({ config.open_cmd, fpath }, { text = true })
else
vim.ui.open(fpath)
end
end
return M
================================================
FILE: lua/zotcite/zotero.lua
================================================
-- A lot of code was either adapted or plainly copied from citation_vim,
-- written by Rafael Schouten: https://github.com/rafaqz/citation.vim
-- Code and/or ideas were also adapted from zotxt, pypandoc, and pandocfilters.
-- To debug this code, create a /tmp/test.md file and do:
-- pandoc /tmp/test.md -F /full/path/to/zotcite/lua/zotcite/zotref.lua
-- We can't use the "vim" module because zotref will be called by external
-- tools. For the same reason, we can't use the "zotcite" module
local config = require("zotcite.config").get_config()
local cite_template
local banned_words = {}
local zcopy
local ztime
local exclude_fields = {}
local entry = {}
local collections = {}
local docs = {}
--- Create an object storing all references from ~/Zotero/zotero.sqlite
-- Conversion from zotero.sqlite to bib types
local _zbt = {
artwork = "Misc",
audioRecording = "Misc",
blogPost = "Misc",
book = "Book",
bookSection = "InCollection",
case = "Misc",
computerProgram = "Book",
conferencePaper = "InProceedings",
dictionaryEntry = "InCollection",
document = "TechReport",
email = "Misc",
encyclopediaArticle = "InCollection",
film = "Misc",
forumPost = "Misc",
hearing = "Misc",
instantMessage = "Misc",
interview = "Misc",
journalArticle = "Article",
letter = "Misc",
magazineArticle = "Article",
newspaperArticle = "Article",
note = "Misc",
podcast = "Misc",
presentation = "Misc",
radioBroadcast = "Misc",
statute = "Misc",
thesis = "Thesis",
tvBroadcast = "Misc",
videoRecording = "Misc",
}
-- Conversion from zotero.sqlite to bib fields
-- It's incomplete and accuracy isn't guaranteed!
local _zbf = {
abstractNote = "abstract",
accessDate = "urldate",
applicationNumber = "call-number",
archiveLocation = "archive_location",
artworkMedium = "medium",
artworkSize = "dimensions",
attachment = "file",
audioFileType = "medium",
blogTitle = "booktitle",
bookTitle = "booktitle",
callNumber = "call-number",
code = "booktitle",
codeNumber = "volume",
codePages = "pages",
codeVolume = "volume",
conferenceName = "event",
court = "authority",
date = "issued",
issueDate = "issued",
dictionaryTitle = "booktitle",
distributor = "publisher",
encyclopediaTitle = "booktitle",
extra = "note",
filingDate = "submitted",
forumTitle = "booktitle",
genre = "type",
history = "references",
institution = "publisher",
interviewMedium = "medium",
issue = "number",
issuingAuthority = "authority",
legalStatus = "status",
legislativeBody = "authority",
libraryCatalog = "source",
meetingName = "event",
numPages = "pages",
numberOfVolumes = "volume",
place = "address",
priorityNumbers = "issue",
proceedingsTitle = "booktitle",
programTitle = "booktitle",
programmingLanguage = "type",
publicationTitle = "booktitle",
reporter = "booktitle",
runningTime = "dimensions",
seriesNumber = "number",
session = "chapter-number",
shortTitle = "shorttitle",
system = "medium",
thesisType = "type",
university = "publisher",
url = "URL",
versionNumber = "version",
websiteTitle = "booktitle",
websiteType = "type",
}
local _creators = {
"editor",
"seriesEditor",
"translator",
"reviewedAuthor",
"artist",
"performer",
"composer",
"director",
"podcaster",
"cartographer",
"programmer",
"presenter",
"interviewee",
"interviewer",
"recipient",
"sponsor",
"inventor",
}
local zwarn = require("zotcite").zwarn
local function expand_tilde(path)
if path:match("^~(/.*)") then
local home = os.getenv("HOME")
if not home then home = os.getenv("USERPROFILE") end
if home then return path:gsub("~", home, 1) end
end
return path
end
--- Check if a directory is writable
---@param dir string The directory path
---@return boolean
local function is_directory(dir)
-- Can't use vim.uv.fs_access(dir, "rw") because it returns `true` for files
local fname = dir .. "/Test_If_Dir_Is_Writable_By_Zotcite"
local f = io.open(fname, "w")
if f then
f:close()
os.remove(fname)
return true
end
return false
end
-- Split a string 's' by a delimiter 'sep'
local function str_split(s, sep)
local results = {}
for part in string.gmatch(s, "([^" .. sep .. "]+)") do
table.insert(results, part)
end
return results
end
local M = {}
--- Reads profile.ini to find prefs.js and then find the values of `data_dir`
--- and `attachment_dir`.
---@return string?, string?
local function get_zotero_prefs()
local p1 = vim.env.APPDATA and vim.env.APPDATA .. "/Zotero/Zotero/profiles.ini"
or expand_tilde("~/.zotero/zotero/profiles.ini")
local zps = { p1, expand_tilde("~/Library/Application Support/Zotero/profiles.ini") }
local zp = nil
for _, p in pairs(zps) do
if vim.uv.fs_access(p, "r") then
zp = p
break
end
end
if not zp then
zwarn("Could not find Zotero's `profiles.ini`", true)
return
end
local adir
local zsql
local zotero_basedir = zp:match("^(.*)/.-")
local lines = vim.fn.readfile(zp)
for _, line in pairs(lines) do
if line:find("Path=") == 1 then
local zprofile = line:gsub("Path=", "")
local zprefs = zotero_basedir .. "/" .. zprofile .. "/" .. "prefs.js"
if vim.uv.fs_access(zprefs, "r") then
local prefs = vim.fn.readfile(zprefs)
for _, pref in pairs(prefs) do
if
pref:find("extensions.zotero.baseAttachmentPath")
and pref:find("extensions.zotero.baseAttachmentPath") > 0
then
adir = pref:match('.*", "(.*)".*\n')
end
if
pref:find("extensions.zotero.dataDir")
and pref:find("extensions.zotero.dataDir") > 0
then
local data_dir = pref:match('.*", "(.*)".*')
if vim.uv.fs_access(data_dir .. "/zotero.sqlite", "r") then
zsql = data_dir .. "/zotero.sqlite"
end
end
end
end
end
end
return adir, zsql
end
---Define which Zotero collections each markdown document uses
---@param d string The name of the markdown document
---@param cl string[] The list of collections to be searched for citation keys when seeking references for the document 'd'.
function M.set_collections(d, cl)
docs[d] = {}
for _, c in pairs(cl) do
if collections[c] then
table.insert(docs[d], c)
else
zwarn('Collection "' .. c .. '" not found in Zotero database.')
end
end
end
--- Check modification time of file
---@param path string File path
---@return integer
local function getmtime(path)
-- TODO: Check how frequently this function is called by different completion plugins
-- local time_now = vim.uv.gettimeofday()
-- vim.notify("time now: " .. tostring(time_now))
local fd = vim.uv.fs_open(path, "r", tonumber("0444", 8))
if fd then
local fstat = vim.uv.fs_fstat(fd)
vim.uv.fs_close(fd)
if fstat then return fstat.mtime.sec end
end
return 0
end
local function copy_zotero_data()
local zf = config.zotero_sqlite_path
if not zf then return end
ztime = getmtime(zf)
zcopy = config.tmpdir .. "/copy_of_zotero.sqlite"
local zcopy_time = getmtime(zcopy)
-- Make a copy of zotero.sqlite to avoid locks
if ztime > zcopy_time then vim.uv.fs_copyfile(zf, zcopy) end
end
--- Run sqlite3 and get the output
---@param query string The SQL query
---@param sqlfile string | nil The path to the SQL data base
---@return table | nil
local function get_sql_data(query, sqlfile)
local sf = sqlfile and sqlfile or zcopy
local obj = vim.system({ "sqlite3", "-json", sf, query }, { text = true }):wait(3000)
if obj.code ~= 0 then
if sf and obj.code == 11 then
vim.uv.fs_utime(sf, 0, 0)
copy_zotero_data()
end
if obj.stderr then zwarn(string.format("[%d] %s", obj.code, obj.stderr)) end
return nil
end
if not obj.stdout then return nil end
if obj.stdout == "" then return nil end
return vim.json.decode(obj.stdout)
end
local function get_collections()
collections = {}
local query = "SELECT collections.collectionName, collectionItems.itemID "
.. "FROM collections, collectionItems "
.. "WHERE collections.collectionID = collectionItems.collectionID"
local sql_data = get_sql_data(query)
if not sql_data then return end
for _, v in pairs(sql_data) do
if not collections[v.collectionName] then collections[v.collectionName] = {} end
table.insert(collections[v.collectionName], v.itemID)
end
end
local function get_items_key_type()
local query = "SELECT items.itemID, items.dateAdded, items.key, itemTypes.typeName"
.. " FROM items, itemTypes"
.. " WHERE items.itemTypeID = itemTypes.itemTypeID"
local sql_data = get_sql_data(query)
if not sql_data then return end
entry = {}
for _, v in pairs(sql_data) do
if
v.typeName ~= "attachment"
and v.typeName ~= "annotation"
and v.typeName ~= "note"
then
entry[v.itemID] =
{ zotkey = v.key, etype = v.typeName, added = v.dateAdded, alastnm = "" }
end
end
end
local function add_most_fields()
local query = "SELECT items.itemID, fields.fieldName, itemDataValues.value"
.. " FROM items, fields, itemData, itemDataValues"
.. " WHERE items.itemID = itemData.itemID"
.. " and itemData.fieldID = fields.fieldID"
.. " and itemData.valueID = itemDataValues.valueID"
local sql_data = get_sql_data(query)
if not sql_data then return end
for _, v in pairs(sql_data) do
if entry[v.itemID] then entry[v.itemID][v.fieldName] = v.value end
end
end
local function add_authors()
local query = "SELECT items.itemID, creatorTypes.creatorType, creators.lastName, creators.firstName"
.. " FROM items, itemCreators, creators, creatorTypes"
.. " WHERE items.itemID = itemCreators.itemID"
.. " and itemCreators.creatorID = creators.creatorID"
.. " and itemCreators.creatorTypeID = creatorTypes.creatorTypeID"
local sql_data = get_sql_data(query)
if not sql_data then return end
for _, v in pairs(sql_data) do
if entry[v.itemID] ~= nil then
if entry[v.itemID][v.creatorType] ~= nil then
table.insert(entry[v.itemID][v.creatorType], { v.lastName, v.firstName })
else
entry[v.itemID][v.creatorType] = { { v.lastName, v.firstName } }
end
-- Special field for citation seeking
if v.creatorType == "author" then
entry[v.itemID].alastnm = entry[v.itemID].alastnm .. ", " .. v.lastName
else
local sought = { "author" }
for _, c in pairs(_creators) do
if v.creatorType == c then
local flag = false
for _, s in pairs(sought) do
if entry[v.itemID][s] ~= nil then
flag = true
break
end
end
if not flag then
entry[v.itemID].alastnm = entry[v.itemID].alastnm
.. ", "
.. v.lastName
end
end
table.insert(sought, c)
end
end
end
end
for _, v in pairs(entry) do
v.alastnm = v.alastnm:gsub("^, ", "")
end
end
local function get_year(e)
local y
if e.date then y = e.date:match("^([0-9][0-9][0-9][0-9])") end
if not y and e.issueDate then y = e.issueDate:match("^([0-9][0-9][0-9][0-9])") end
if y then
return y
else
return "????"
end
end
local function calculate_ck(v, has_a, has_t, has_y)
local key = cite_template
if has_a then
local lastname = "No_author"
local lastnames = "No_author"
local creators = { "author" }
for _, x in pairs(_creators) do
table.insert(creators, x)
end
local n = 0
local lnms = {}
for _, c in pairs(creators) do
if v[c] then
lastname = v[c][1][1]
for _, ln in pairs(v[c]) do
table.insert(lnms, ln[1])
n = n + 1
end
break
end
end
if n > 3 then
lastnames = lnms[1] .. "-etal"
else
if n > 0 then lastnames = table.concat(lnms, "-") end
end
lastname = lastname:gsub("%W", "")
key = key:gsub("%{author%}", lastname:lower(), 1)
key = key:gsub("%{Author%}", lastname, 1)
key = key:gsub("%{authors%}", lastnames:lower(), 1)
key = key:gsub("%{Authors%}", lastnames, 1)
end
if has_t then
local ttl = v.title:gsub("[,;:\\.!?].*", "")
ttl = ttl:gsub("%s%s*", " ")
local titlew = ""
local ttl_ls = vim.split(ttl, " ")
for _, t in pairs(ttl_ls) do
if not vim.tbl_contains(banned_words, t:lower()) then
titlew = t
break
end
end
key = key:gsub("%{title%}", titlew:lower())
key = key:gsub("%{Title%}", titlew)
end
if has_y then
key = key:gsub("%{year%}", v.year:gsub("^[0-9][0-9]", ""))
key = key:gsub("%{Year%}", v.year)
end
-- Delete punctuation marks
key = key:gsub("[%./ -,:-@\\[-`{-~]", "")
return key
end
local function calculate_citekeys()
local ct = cite_template:lower()
local has_a = ct:find("author") and true or false
local has_t = ct:find("title") and true or false
local has_y = ct:find("year") and true or false
for _, v in pairs(entry) do
v.year = get_year(v)
if not v.title then v.title = "???" end
if not v.citekey then v.citekey = calculate_ck(v, has_a, has_t, has_y) end
end
end
local function get_sequence_string(n)
local alphabet = "abcdefghijklmnopqrstuvwxyz"
local s = ""
while n > 0 do
local remainder = (n - 1) % 26
s = string.sub(alphabet, remainder + 1, remainder + 1) .. s
n = math.floor((n - 1) / 26)
end
return s
end
local function add_citekeys()
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
if kt == "better-bibtex" then
local missing_ck = {}
for _, v in pairs(entry) do
if v.citationKey then
v.citekey = v.citationKey
else
table.insert(
missing_ck,
vim.inspect(v.author) .. " " .. vim.inspect(v.title)
)
end
end
if #missing_ck > 40 then
zwarn("Missing 'Citation Key' for " .. tostring(#missing_ck) .. " entries")
elseif #missing_ck > 0 then
zwarn("Missing 'Citation Key' for:\n" .. table.concat(missing_ck, "\n"))
end
end
calculate_citekeys()
if kt == "template" then
-- Fix duplicates
for k1, v1 in pairs(entry) do
local dup = { v1 }
for k2, v2 in pairs(entry) do
if k1 ~= k2 and v1.citekey == v2.citekey then table.insert(dup, v2) end
end
if #dup > 1 then
table.sort(dup, function(a, b) return a.added > b.added end)
local i = 1
for _, v in pairs(dup) do
v.citekey = v.citekey .. get_sequence_string(i)
i = i + 1
end
end
end
end
end
local function delete_items()
local sql_data = get_sql_data("SELECT itemID FROM deletedItems")
if not sql_data then return end
for _, v in pairs(sql_data) do
entry[v.itemID] = nil
for k, _ in pairs(collections) do
for i, c in pairs(collections[k]) do
if c == v.itemID then
table.remove(collections[k], i)
break
end
end
end
end
end
local function sanitize_markdown(s)
s = s:gsub("%[", "\\[")
s = s:gsub("%]", "\\]")
s = s:gsub("@", "\\@")
s = s:gsub("%*", "\\*")
s = s:gsub("_", "\\_")
return s
end
local function sanitize_latex(s)
s = s:gsub("\\", "TeXtBacKsLasH")
s = s:gsub("%%", "\\%%")
s = s:gsub("&", "\\&")
s = s:gsub("%$", "\\$")
s = s:gsub("#", "\\#")
s = s:gsub("_", "\\_")
s = s:gsub("{", "\\{")
s = s:gsub("}", "\\}")
s = s:gsub("~", "\\textasciitilde{}")
s = s:gsub("%^", "\\textasciicircum{}")
s = s:gsub("TeXtBacKsLasH", "\\textbackslash{}")
return s
end
local function sanitize(s)
if vim.tbl_contains({ "tex", "rnoweb" }, vim.bo.filetype) then
return sanitize_latex(s)
end
return sanitize_markdown(s)
end
---@param item table The Zotero item
---@param ktype string Type of citation key
local function get_bib_ref(item, ktype)
local e = {}
e = vim.tbl_extend("force", e, item)
-- Fix the type
if e["etype"] and _zbt and _zbt[e["etype"]] then
e["etype"] = e["etype"]:gsub(e["etype"], _zbt[e["etype"]])
end
-- Escape quotes of all fields
for f, v in pairs(e) do
if type(v) == "string" then e[f] = v:gsub('"', '\\"') end
end
-- Rename some fields
local ekeys = {}
for k, _ in pairs(e) do
table.insert(ekeys, k)
end
for _, f in pairs(ekeys) do
if _zbf[f] then
e[_zbf[f]] = e[f]
e[f] = nil
end
end
if e["etype"] == "Article" and e["booktitle"] then
e["journal"] = e["booktitle"]
e["booktitle"] = nil
end
if e["etype"] == "InCollection" and not e["editor"] then e["etype"] = "InBook" end
for _, aa in pairs({
"title",
"journal",
"booktitle",
"journalAbbreviation",
"address",
"publisher",
}) do
if e[aa] then
e[aa] = string.gsub(e[aa], '\\"', '"')
e[aa] = string.gsub(e[aa], "\\\\", "\1")
e[aa] = string.gsub(e[aa], "([&%%$#_%[%]{}])", "\\%1")
e[aa] = string.gsub(e[aa], "\1", "{\\\\textbackslash}")
e[aa] = string.gsub(e[aa], "<i>(.-)</i>", "\\\\textit{%1}")
e[aa] = string.gsub(e[aa], "<b>(.-)</b>", "\\\\textbf{%1}")
e[aa] = string.gsub(e[aa], "<sub>(.-)</sub>", "$_{\\\\textrm{%1}}$")
e[aa] = string.gsub(e[aa], "<sup>(.-)</sup>", "$^{\\\\textrm{%1}}$")
e[aa] = string.gsub(
e[aa],
'<span style="font%-variant:small%-caps;">(.-)</span>',
"\\\\textsc{%1}"
)
e[aa] = string.gsub(e[aa], '<span class="nocase">(.-)</span>', "{%1}")
e[aa] = string.gsub(e[aa], "\n", " ")
end
end
local ekey = ktype == "zotero" and e.zotkey or e.citekey
local ref = { "@" .. e["etype"] .. "{" .. ekey .. "," }
for _, aa in pairs({
"author",
"editor",
"contributor",
"translator",
"container-author",
}) do
if e[aa] then
local names = {}
for _, pair in pairs(e[aa]) do
local last, first = pair[1], pair[2]
table.insert(names, last .. ", " .. first)
end
table.insert(
ref,
" " .. aa .. " = {" .. table.concat(names, " and ") .. "},"
)
end
end
if e["issued"] then
local ds = e["issued"]
ds = ds:gsub(" .*", "")
local d = str_split(ds, "-")
if d[1] ~= "0000" then
table.insert(ref, " year = {" .. e["year"] .. "},")
if d[2] ~= "00" then table.insert(ref, " month = {" .. d[2] .. "},") end
if d[3] ~= "00" then table.insert(ref, " day = {" .. d[3] .. "},") end
end
end
if e["urldate"] then e["urldate"] = string.gsub(e["urldate"], " .*", "") end
if e["pages"] then
e["pages"] = string.gsub(e["pages"], "([0-9])-([0-9])", "%1--%2")
end
local dont = {
"etype",
"issued",
"abstract",
"collection",
"author",
"editor",
"contributor",
"translator",
"alastnm",
"container-author",
"year",
}
for _, v in pairs(exclude_fields or {}) do
table.insert(dont, v)
end
for f, val in pairs(e) do
local skip = false
for _, d in pairs(dont) do
if f == d then
skip = true
break
end
end
if not skip then
local v = tostring(val):gsub("\n", " ")
table.insert(ref, " " .. f .. " = {" .. v .. "},")
end
end
table.insert(ref, "}")
table.insert(ref, "")
return ref
end
--- Build the contents of a .bib file
---@param keys string[] List of Zotero keys
---@param ktype string Type of citation key
---@return table
local function get_bib(keys, ktype)
local ref = {}
for _, k in pairs(keys) do
for _, e in pairs(entry) do
local key = ktype == "zotero" and e.zotkey or e.citekey
if k == key then ref[k] = get_bib_ref(e, ktype) end
end
end
return ref
end
--- Build the contents of a .bib file
---@param zkeys string[] List of Zotero keys
---@param bibf string Name of bib file
---@param ktype string Type of citation key
---@param verbose boolean Whether to print debugging information or not
function M.update_bib(zkeys, bibf, ktype, verbose)
local bib = {}
local f = io.open(bibf, "r")
if f then
if verbose then zwarn('zotcite: updating "' .. tostring(bibf) .. '"\n') end
local key = nil
for line in f:lines() do
if string.sub(line, 1, 1) == "@" then
local clean_line = string.gsub(line, ",%s*$", "")
key = string.gsub(clean_line, "^@.*{", "")
bib[key] = {}
end
if key then table.insert(bib[key], line) end
end
f:close()
else
if verbose then zwarn('zotcite: writing "' .. tostring(bibf) .. '"\n') end
bib = get_bib(zkeys, ktype)
end
-- Replace existing references and add new ones
local newbib = get_bib(zkeys, ktype)
for k, v in pairs(newbib) do
bib[k] = v
end
f = io.open(bibf, "w")
if f then
for _, v in pairs(bib) do
f:write(table.concat(v, "\n") .. "\n")
end
f:close()
end
end
---Return list of attachments associated with the citation key
---@param key string The citation key
---@return table | nil, string
function M.get_attachment(key)
local attachments = {}
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local field = kt == "zotero" and "zotkey" or "citekey"
for k, _ in pairs(entry) do
if entry[k][field] == key then
local query = "SELECT items.itemID, items.key, itemAttachments.path"
.. " FROM items, itemAttachments"
.. " WHERE items.itemID = itemAttachments.itemID and itemAttachments.parentItemID = '"
.. k
.. "'"
local sql_data = get_sql_data(query)
if not sql_data then return nil, "SQL query failed" end
for _, v in pairs(sql_data) do
if type(v.path) == "string" then
table.insert(attachments, v)
else
zwarn("Path is not a string: " .. vim.inspect(v.path))
end
end
if #attachments == 0 then return nil, "No attachments found" end
return attachments, ""
end
end
return nil, "Citation key not found"
end
function M.get_all_citations()
-- Return a list of all [zotkey, citekey].
local res = {}
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
if kt == "zotero" then
for _, e in pairs(entry) do
res[e.zotkey] = e.citekey
end
else
for k, e in pairs(entry) do
if e.citekey and e.zotkey then
res[e.citekey] = e.zotkey
else
zwarn("Missing citation key for itemID " .. tostring(k))
end
end
end
return res
end
local get_ref_data_template = function(citekey)
for _, v in pairs(entry) do
if v.citekey == citekey then return v end
end
return nil
end
local get_ref_data_zotkey = function(zotkey)
for _, v in pairs(entry) do
if v.zotkey == zotkey then return v end
end
return nil
end
function M.get_ref_data(key)
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
if kt == "zotero" then
return get_ref_data_zotkey(key)
else
return get_ref_data_template(key)
end
end
local get_ft_lang = function()
if vim.tbl_contains({ "tex", "rnoweb" }, vim.bo.filetype) then
return "latex"
elseif vim.bo.filetype == "typst" then
return "typst"
end
return "markdown"
end
--- Get year-page separator
---@param lang string
---@return string
local function get_ypsep(lang)
local ypsep = config.year_page_sep
if not ypsep then
if lang == "markdown" then
ypsep = ", p. "
elseif lang == "typst" then
ypsep = "p. "
else
ypsep = "p.~"
end
end
return ypsep
end
local get_key_id = function(zotkey)
local key_id = nil
for k, v in pairs(entry) do
if v.zotkey == zotkey then
key_id = k
break
end
end
return key_id
end
--- Return user annotations made using Zotero's PDF viewer.
---@param key string
---@param offset integer
---@return string[] | nil
function M.get_annotations(key, offset)
local lang = get_ft_lang()
if not config.zotero_sqlite_path then return end
copy_zotero_data()
local key_id = get_key_id(key)
if not key_id then return end
local query = "SELECT itemAttachments.itemID, itemAttachments.parentItemID,"
.. "\n itemAnnotations.parentItemID, itemAnnotations.type,"
.. "\n itemAnnotations.position, itemAnnotations.pageLabel,"
.. "\n itemAnnotations.authorName, itemAnnotations.text,"
.. "\n itemAnnotations.comment"
.. "\nFROM itemAttachments, itemAnnotations"
.. "\nWHERE itemAttachments.parentItemID = "
.. key_id
.. " and itemAnnotations.parentItemID = itemAttachments.ItemID"
local sql_data = get_sql_data(query)
if not sql_data then return end
local kt = require("zotcite.config").get_key_type(vim.api.nvim_get_current_buf())
local ckey = kt == "zotero" and key or entry[key_id].citekey
-- Year-page separator
local s = get_ypsep(lang)
local notes = {}
for _, v in pairs(sql_data) do
local position = vim.json.decode(v.position)
local page
if v.pageLabel ~= vim.NIL then
page = v.pageLabel
else
page = tostring(position.pageIndex + offset)
end
table.insert(notes, {
p = page,
pos_page = position.pageIndex,
pos_x = position.rects[1][1],
pos_y = position.rects[1][2],
c = v.comment ~= vim.NIL and v.comment,
t = v.text ~= vim.NIL and v.text,
type = v.type,
})
end
table.sort(notes, function(a, b)
if a.pos_page ~= b.pos_page then
return a.pos_page < b.pos_page
else
return a.pos_y > b.pos_y
end
end)
local lines = {}
for _, v in pairs(notes) do
if v.c then
local txt = sanitize(v.c)
if lang == "latex" then
txt =
string.format("%s [comment on \\citet[%s%s]{%s}]", txt, s, v.p, ckey)
elseif lang == "typst" then
txt = string.format(
'%s [comment on #cite(<%s>, supplement: "%s%s")]',
txt,
ckey,
s,
v.p
)
else
txt = string.format("%s [comment on @%s%s%s]", txt, ckey, s, v.p)
end
table.insert(lines, txt)
table.insert(lines, "")
end
if v.t then
local txt = sanitize(v.t)
if lang == "latex" then
table.insert(lines, "\\begin{quote}")
txt = string.format("%s \\cite[%s%s]{%s}", txt, s, v.p, ckey)
table.insert(lines, txt)
table.insert(lines, "\\end{quote}")
elseif lang == "typst" then
txt = string.format(
'#quote[%s] #cite(<%s>, supplement: "%s%s")',
txt,
ckey,
s,
v.p
)
table.insert(lines, txt)
else
txt = string.format("> %s [@%s%s%s]", txt, ckey, s, v.p)
table.insert(lines, txt)
end
table.insert(lines, "")
end
end
local repl = {}
for _, v in pairs(lines) do
if v:find("\n") then
local vlist = vim.split(v, "\n")
for _, v2 in pairs(vlist) do
table.insert(repl, v2)
end
else
table.insert(repl, v)
end
end
return repl
end
--- Convert HTML to LaTeX
---@param notes string
local notes_to_tex = function(notes)
-- Year-page separator
local ypsep = get_ypsep("latex")
notes = notes:gsub("<h1>(.-)</h1>", "\\section{%1}\n")
notes = notes:gsub("<h2>(.-)</h2>", "\\subsection{%1}\n")
notes = notes:gsub("<h3>(.-)</h3>", "\\subsubsection{%1}\n")
notes = notes:gsub("<h4>(.-)</h4>", "\\subsubsection{%1}\n")
notes = notes:gsub("<h5>(.-)</h5>", "\\subsubsection{%1}\n")
notes = notes:gsub("<strong>(.-)</strong>", "\\textbf{%1}")
notes = notes:gsub("<b>(.-)</b>", "\\textbf{%1}")
notes = notes:gsub("<em>(.-)</em>", "\\em{%1}")
notes = notes:gsub("<i>(.-)</i>", "\\em{%1}")
notes = notes:gsub(
'<span style="text%-decoration: line%-through">(.-)</span>',
"\\strikethrough{%1}"
)
notes = notes:gsub(' rel="noopener noreferrer nofollow"', "")
notes = notes:gsub('<a href="(.-)">(.-)</a>', "\\href{%1}{%2}")
notes = notes:gsub('%(<span class="citation%-item">.-</span>%)', "")
notes = notes:gsub(
'<span class="citation" data%-citation=".-items%%2F(.-)%%.-locator%%22%%3A%%22(.-)%%22.-">.-</span>',
"\\cite[" .. ypsep .. "%2]{%1}"
)
notes = notes:gsub(
'<span class="citation" data%-citation=".-items%%2F(.-)%%.-">',
"\\cite{%1}"
)
notes = notes:gsub("<li>%s*(.-)%s*</li>", "\n \\item %1")
notes = notes:gsub("<ul>(.-)</ul>", "\n\\begin{itemize}%1\\end{itemize}\n")
notes = notes:gsub("<ol>(.-)</ol>", "\n\\begin{enumerate}%1\\end{enumerate}\n")
notes = notes:gsub(
"<blockquote>%s*(.-)%s*</blockquote>",
"\n\\begin{quote}\n%1\n\\end{quote}\n"
)
notes = notes:gsub('<pre class="math">(.-)</pre>', "\n%1\n")
notes = notes:gsub("<pre>(.-)</pre>", "\n\\begin{verbatim}\n%1\nend{verbatim}\n")
notes = notes:gsub("<br/>", "\\linebreak\n")
notes = notes:gsub("<br>", "\\linebreak\n")
notes = notes:gsub("<p>(.-)</p>", "%1\n\n")
notes = notes:gsub(' class="highlight" data%-annotation=".-"(.-)', "")
notes = notes:gsub("<div .->", "")
notes = notes:gsub("</div>", "")
notes = notes:gsub("<span>", "")
notes = notes:gsub("</span>", "")
notes = notes:gsub("\n\n\n", "\n\n")
return notes .. "\n"
end
--- Convert HTML to Markdown
---@param notes string
local notes_to_md = function(notes)
-- Year-page separator
local ypsep = get_ypsep("markdown")
notes = notes:gsub("<h1>(.-)</h1>", "# %1\n")
notes = notes:gsub("<h2>(.-)</h2>", "## %1\n")
notes = notes:gsub("<h3>(.-)</h3>", "### %1\n")
notes = notes:gsub("<h4>(.-)</h4>", "#### %1\n")
notes = notes:gsub("<h5>(.-)</h5>", "##### %1\n")
notes = notes:gsub("<strong>(.-)</strong>", "**%1**")
notes = notes:gsub("<b>(.-)</b>", "**%1**")
notes = notes:gsub("<em>(.-)</em>", "*%1*")
notes = notes:gsub("<i>(.-)</i>", "*%1*")
notes =
notes:gsub('<span style="text%-decoration: line%-through">(.-)</span>', "~~%1~~")
notes = notes:gsub(' rel="noopener noreferrer nofollow"', "")
notes = notes:gsub('<a href="(.-)">(.-)</a>', "[%2](%1)")
notes = notes:gsub('%(<span class="citation%-item">.-</span>%)', "")
notes = notes:gsub(
'<span class="citation" data%-citation=".-items%%2F(.-)%%.-locator%%22%%3A%%22(.-)%%22.-">.-</span>',
"[@%1" .. ypsep .. "%2]"
)
notes =
notes:gsub('<span class="citation" data%-citation=".-items%%2F(.-)%%.-">', "@%1")
notes = notes:gsub("<li>%s*(.-)%s*</li>", "\n - %1")
notes = notes:gsub("<ul>(.-)</ul>", "\n%1\n")
notes = notes:gsub("<ol>(.-)</ol>", "\n%1\n")
notes = notes:gsub("<blockquote>%s*(.-)%s*</blockquote>", "\n> %1\n")
notes = notes:gsub('<pre class="math">(.-)</pre>', "\n%1\n")
notes = notes:gsub("<pre>(.-)</pre>", "\n```\n%1\n```\n")
notes = notes:gsub("<br/>", "\n\n")
notes = notes:gsub("<br>", "\n\n")
notes = notes:gsub("<p>(.-)</p>", "%1\n\n")
notes = notes:gsub(' class="highlight" data%-annotation=".-"(.-)', "")
notes = notes:gsub("<div .->", "")
notes = notes:gsub("</div>", "")
notes = notes:gsub("<span>", "")
notes = notes:gsub("</span>", "")
notes = notes:gsub("\n\n\n", "\n\n")
return notes .. "\n"
end
--- Convert HTML to Typst
---@param notes string
local notes_to_typ = function(notes)
-- Year-page separator
local ypsep = get_ypsep("typst")
notes = notes:gsub("<h1>(.-)</h1>", "= %1\n")
notes = notes:gsub("<h2>(.-)</h2>", "== %1\n")
notes = notes:gsub("<h3>(.-)</h3>", "=== %1\n")
notes = notes:gsub("<h4>(.-)</h4>", "==== %1\n")
notes = notes:gsub("<h5>(.-)</h5>", "===== %1\n")
notes = notes:gsub("<strong>(.-)</strong>", "*%1*")
notes = notes:gsub("<b>(.-)</b>", "*%1*")
notes = notes:gsub("<em>(.-)</em>", "_%1_")
notes = notes:gsub("<i>(.-)</i>", "_%1_")
notes =
notes:gsub('<span style="text%-decoration: line%-through">(.-)</span>', "~~%1~~")
notes = notes:gsub(' rel="noopener noreferrer nofollow"', "")
notes = notes:gsub('<a href="(.-)">(.-)</a>', '#link("%1")[%2]')
notes = notes:gsub('%(<span class="citation%-item">.-</span>%)', "")
notes = notes:gsub(
'<span class="citation" data%-citation=".-items%%2F(.-)%%.-locator%%22%%3A%%22(.-)%%22.-">.-</span>',
'#cite(<%1>, supplement: "' .. ypsep .. '%2", form: "normal")'
)
notes =
notes:gsub('<span class="citation" data%-citation=".-items%%2F(.-)%%.-">', "@%1")
notes = notes:gsub("<li>%s*(.-)%s*</li>", "\n - %1")
notes = notes:gsub("<ul>(.-)</ul>", "\n%1\n")
notes = notes:gsub("<ol>(.-)</ol>", "\n%1\n")
notes = notes:gsub("<blockquote>%s*(.-)%s*</blockquote>", "\n#quote[%1]\n")
notes = notes:gsub('<pre class="math">(.-)</pre>', "\n%1\n")
notes = notes:gsub("<pre>(.-)</pre>", "\n```\n%1\n```\n")
notes = notes:gsub("<br/>", "\n\n")
notes = notes:gsub("<br>", "\n\n")
notes = notes:gsub("<p>(.-)</p>", "%1\n\n")
notes = notes:gsub(' class="highlight" data%-annotation=".-"(.-)', "")
notes = notes:gsub("<div .->", "")
notes = notes:gsub("</div>", "")
notes = notes:gsub("<span>", "")
notes = notes:gsub("</span>", "")
notes = notes:gsub("\n\n\n", "\n\n")
return notes .. "\n"
end
-- Return user notes from a reference.
---@param key string The Zotero key
---@return string | nil
function M.get_notes(key)
if not config.zotero_sqlite_path then return end
copy_zotero_data()
local key_id = get_key_id(key)
if not key_id then return end
local query = "SELECT parentItemID, note FROM itemNotes WHERE parentItemID = "
.. tostring(key_id)
local sql_data = get_sql_data(query)
if not sql_data then return end
local notes = ""
for _, v in pairs(sql_data) do
notes = notes .. v.note
end
if notes == "" then return nil end
local lang = get_ft_lang()
notes = sanitize(notes)
if lang == "typst" then
notes = notes_to_typ(notes)
elseif lang == "latex" then
notes = notes_to_tex(notes)
else
notes = notes_to_md(notes)
end
return notes
end
--- Return information useful for debugging the application
---@return table
function M.info()
local ntypes = {}
local n = 0
for _, v in pairs(entry) do
n = n + 1
if ntypes[v.etype] then
ntypes[v.etype] = ntypes[v.etype] + 1
else
ntypes[v.etype] = 1
end
end
local r = {
["attach_dir"] = config.attach_dir,
["zotero.sqlite"] = config.zotero_sqlite_path,
["tmpdir"] = config.tmpdir,
["docs"] = docs,
["citation template"] = cite_template,
["banned words"] = table.concat(banned_words, " "),
["excluded fields"] = table.concat(exclude_fields, ", "),
["n refs"] = n,
["types"] = ntypes,
}
return r
end
local function load_zotero_data()
if not config.zotero_sqlite_path then return end
copy_zotero_data()
get_collections()
get_items_key_type()
delete_items()
add_most_fields()
add_authors()
add_citekeys()
end
--- Find citation key and return list of completions
---@param ptrn string Search pattern
---@param d string Buffer name
---@return table
function M.get_match(ptrn, d)
if getmtime(config.zotero_sqlite_path) > ztime then load_zotero_data() end
local keys = {}
if docs[d] then
for _, c in pairs(docs[d]) do
for _, v in pairs(collections[c]) do
table.insert(keys, v)
end
end
end
if #keys == 0 then
for k, _ in pairs(entry) do
table.insert(keys, k)
end
end
-- priority level
local p1, p2, p3, p4, p5, p6 = {}, {}, {}, {}, {}, {}
ptrn = ptrn:lower()
for _, v in ipairs(keys) do
local e = entry[v]
if e.citekey:lower():find(ptrn) == 1 then
table.insert(p1, e)
elseif e.alastnm and e.alastnm[1] and e.alastnm[1][1]:lower():find(ptrn) == 1 then
table.insert(p2, e)
elseif e.title and e.title:lower():find(ptrn) == 1 then
table.insert(p3, e)
elseif e.citekey:lower():find(ptrn) and e.citekey:lower():find(ptrn) > 1 then
table.insert(p4, e)
elseif
e.alastnm
and e.alastnm[1]
and e.alastnm[1][1]:lower():find(ptrn)
and e.alastnm[1][1]:lower():find(ptrn) > 1
then
table.insert(p5, e)
elseif
e.title
and e.title:lower():find(ptrn)
and e.title:lower():find(ptrn) > 1
then
table.insert(p6, e)
end
end
local resp = {}
for _, v in pairs(p1) do
table.insert(resp, v)
end
for _, v in pairs(p2) do
table.insert(resp, v)
end
for _, v in pairs(p3) do
table.insert(resp, v)
end
for _, v in pairs(p4) do
table.insert(resp, v)
end
for _, v in pairs(p5) do
table.insert(resp, v)
end
for _, v in pairs(p6) do
table.insert(resp, v)
end
return resp
end
--- Get Zotero data
---@return boolean
function M.init()
-- Template for citation keys
cite_template = config.citation_template
if not cite_template then cite_template = "{Authors}-{Year}" end
-- Title words to be ignored
if config.banned_words then
banned_words = vim.split(config.banned_words, " ")
else
banned_words =
{ "a", "an", "the", "some", "from", "on", "in", "to", "of", "do", "with" }
end
-- Path to zotero.sqlite
if config.zotero_sqlite_path then
if not vim.uv.fs_access(config.zotero_sqlite_path, "r") then
local msg = 'Please, check if the config option `zotero_sqlite_path` is set correctly: "'
.. config.zotero_sqlite_path
.. '" not found.'
zwarn(msg)
return false
end
end
-- Path to attachments directory
if config.attach_dir and not is_directory(config.attach_dir) then
local msg = "Please, fix the value `attach_dir` in your config. The directory "
.. config.attach_dir
.. " is not writable."
zwarn(msg)
return false
end
if not config.zotero_sqlite_path then
local adir, zdir = get_zotero_prefs()
if adir and not config.attach_dir then config.attach_dir = adir end
if zdir then
config.zotero_sqlite_path = zdir
else
return false
end
end
-- Temporary directory
if not config.tmpdir then
if os.getenv("XDG_CACHE_HOME") then
config.tmpdir = os.getenv("XDG_CACHE_HOME") .. "/zotcite"
elseif os.getenv("APPDATA") then
config.tmpdir = os.getenv("APPDATA") .. "/zotcite"
elseif is_directory(expand_tilde("~/.cache")) then
config.tmpdir = expand_tilde("~/.cache/zotcite")
elseif is_directory(expand_tilde("~/Library/Caches")) then
config.tmpdir = expand_tilde("~/Library/Caches/zotcite")
else
config.tmpdir = "/tmp/.zotcite"
end
end
if not is_directory(config.tmpdir) then
if vim.fn.mkdir(config.tmpdir, "p", "0o700") == 0 then
zwarn("Error creating directory '" .. config.tmpdir .. "'")
return false
end
if not is_directory(config.tmpdir) then
zwarn(
'Please, either set or fix the value of `tmpdir` in your zotcite config: "'
.. config.tmpdir
.. '" is not writable.'
)
return false
end
end
-- Fields that should not be added to the bib entries:
local zexcl = config.exclude_fields
if not zexcl then
exclude_fields = {}
else
exclude_fields = str_split(zexcl, " ")
end
load_zotero_data()
return true
end
return M
================================================
FILE: plugin/zotcite.lua
================================================
local did_plugin = false
if did_plugin then return end
did_plugin = true
vim.api.nvim_create_user_command("Zinfo", "lua require('zotcite.get').zotero_info()", {})
================================================
FILE: scripts/abntfix.py
================================================
#!/usr/bin/env python3
"""
Pandoc filter to fix ABNT citations.
"""
import sys
import io
import json
def TitleCase(x):
if x in [ 'et', 'al.', 'a', 'an', 'and', 'in', 'the', 'de', 'do', 'da',
'dos', 'das', 'com', 'na', 'no', 'nas', 'nos', 'e', 'um', 'uma']:
return x
return x.title()
def WalkFix(x):
if isinstance(x, dict) and 't' in x.keys() and x['t'] == 'Cite' and x['c'][0][0]['citationMode']['t'] == 'AuthorInText':
ultimo = 1000
for s, vs in enumerate(x['c'][1]):
if vs['t'] == 'Str':
vs['c'] = TitleCase(vs['c'])
if vs['c'].find(';') > -1:
ultimo = s
vs['c'] = vs['c'].replace(";", ",")
if ultimo < 1000:
x['c'][1][ultimo]['c'] = x['c'][1][ultimo]['c'].replace(",", " e")
else:
if isinstance(x, dict) and 'c' in x.keys() and isinstance(x['c'], list):
for y in x['c']:
WalkFix(y)
else:
if isinstance(x, list):
for y in x:
WalkFix(y)
if __name__ == "__main__":
sys.stderr.write('Running abntfix...\n')
sys.stderr.flush()
# Get json from pandoc (stdin)
i = sys.stdin.read()
j = json.load(io.StringIO(i))
WalkFix(j['blocks'])
# Print the json representation of the new document
sys.stdout.write(json.dumps(j))
================================================
FILE: scripts/apafix
================================================
The R package rmdfiltr includes an ampersand replacement filter:
https://cran.r-project.org/web/packages/rmdfiltr/index.html
================================================
FILE: scripts/odt2md.py
================================================
#!/usr/bin/env python3
"""
Convert an ODT file into Markdown, converting citations into zotcite format.
"""
import sys
import subprocess
import re
import json
from pathlib import Path
from zotero import ZoteroEntries
if __name__ == "__main__":
cmd = ['pandoc', sys.argv[1], '-t', 'markdown']
doc = subprocess.run(cmd, capture_output=True, text=True).stdout
z = ZoteroEntries()
while True:
res = re.search(
r"\[\]{#ZOTERO_ITEM CSL_CITATION\s(.*?),\\\"schema\\\":\\\"[\w:/\.-]*?\\\"}\s\w+}\(.*?\)", doc, re.DOTALL)
if not res:
break
cit = res.group(1)
cit += "}"
cit = cit.replace("\\\"", "\"")
cit = cit.replace("\\\\", "\\")
try:
cit = json.loads(cit)
except Exception as e:
with open("debug.txt", "w") as fh:
fh.write(cit)
raise (e)
cit_new = "["
for cit_item in cit["citationItems"]:
cit_new += z.GetCitationById(cit_item["id"]) + ";"
cit_new = cit_new[:-1] + "]"
doc = doc[:res.start()] + cit_new + doc[res.end():]
inf = Path(sys.argv[1])
outf = inf.parent / (inf.stem+".md")
with open(outf, "w") as fh:
fh.write(doc)
================================================
FILE: scripts/pdfnotes.py
================================================
#!/usr/bin/env python3
"""
Adapted from code written by Hamed MP, published at
https://gist.github.com/HamedMP/03440cca542ee7ae279175b78499fabf
"""
import sys
import os
import re
try:
import PyQt5
except ImportError:
sys.stderr.write("Please, install the Python3 module PyQt5")
sys.exit(1)
try:
import popplerqt5
except ImportError:
sys.stderr.write("Please, install the Python3 module python-poppler-qt5")
sys.exit(1)
def main():
doc = popplerqt5.Poppler.Document.load(sys.argv[1])
if doc is None:
sys.exit(33)
if len(sys.argv) > 2:
citekey = sys.argv[2]
else:
citekey = ''
# Sometimes, the page labels are spurious. So the priority is:
# 1. given page range; 2. page label; 3. given starting page.
page1 = 1
has_pg_range = False
if len(sys.argv) > 3:
pg = sys.argv[3]
if pg.find("-"):
has_pg_range = True
pIni = int(re.sub("-.*", "", pg))
pEnd = int(re.sub(".*-", "", pg))
nPgs = pEnd - pIni + 1
# Some documents have 1 or 2 extra presentation pages. Ignore them.
if doc.numPages() <= nPgs and (nPgs - doc.numPages()) < 2:
page1 = pEnd - doc.numPages() + 1
else:
page1 = int(sys.argv[3])
if os.getenv('ZYearPageSep') is None:
ypsep = ', p. '
else:
ypsep = os.getenv('ZYearPageSep')
notes = []
# Count the pages because not all page labels can be easily converted into
# numbers (examples: A1 and III)
pnum = 0
for i in range(doc.numPages()):
page = doc.page(i)
pnum += 1
pgnum = str(i + page1)
if not has_pg_range and page.label():
pgnum = page.label()
annotations = page.annotations()
(pwidth, pheight) = (page.pageSize().width(), page.pageSize().height())
if annotations:
for a in annotations:
if isinstance(a, popplerqt5.Poppler.Annotation):
# Guess the annotation's column for pages with two columns
if a.boundary().topRight().x() < 0.6:
c = 1
else:
c = 2
# Get the y coordinate to to print the annotations in the
# correct order
y = a.boundary().topRight().y()
if a.contents():
txt = a.contents() + ' [annotation'
if a.author():
txt = txt + ' by ' + a.author()
if citekey:
txt = txt + ' on ' + citekey
txt = txt + ypsep + pgnum + ']\n'
# Decrease the value of y to ensure that the comment
# on a highlighted text will be printed before the
# highlighted text itself
notes.append([pnum, c, y - 0.0000001, txt])
if isinstance(a, popplerqt5.Poppler.HighlightAnnotation):
quads = a.highlightQuads()
txt = ''
for quad in quads:
rect = (quad.points[0].x() * pwidth,
quad.points[0].y() * pheight,
quad.points[2].x() * pwidth,
quad.points[2].y() * pheight)
bdy = PyQt5.QtCore.QRectF()
bdy.setCoords(*rect)
txt = txt + str(page.text(bdy)) + '\n'
txt = txt.replace('-\n', '')
txt = txt.replace('\n', ' ')
txt = re.sub('^ *', '', txt)
txt = re.sub(' *$', '', txt)
if txt:
txt = '> ' + txt + ' ['
if citekey:
txt = txt + citekey
txt = txt + ypsep + pgnum + ']\n'
notes.append([pnum, c, y, txt])
if notes:
snotes = sorted(notes, key=lambda x: (x[0], x[1], x[2]))
for n in snotes:
print(n[3])
else:
sys.exit(34)
if __name__ == "__main__":
main()
================================================
FILE: scripts/pdfnotes2.py
================================================
#!/usr/bin/env python3
"""
PDF annotation extraction tool for zotcite using PyMuPDF (fitz)
"""
import sys
import os
import re
try:
import fitz # PyMuPDF
except ImportError:
sys.stderr.write("Please install the Python3 module pymupdf: pip install pymupdf")
sys.exit(1)
def main():
# Check arguments
if len(sys.argv) < 2:
sys.stderr.write("Usage: pdfnotes.py PDF_FILE [CITEKEY] [PAGE_OFFSET]\n")
sys.exit(1)
# Load the PDF file
try:
doc = fitz.open(sys.argv[1])
except Exception as e:
sys.stderr.write(f"Error opening PDF: {e}\n")
sys.exit(33)
# Get the citation key if provided
if len(sys.argv) > 2:
citekey = sys.argv[2]
else:
citekey = ''
# Get page offset
page1 = 1
has_pg_range = False
if len(sys.argv) > 3:
pg = sys.argv[3]
try:
# Check if it's a page range with hyphen
if "-" in pg:
has_pg_range = True
pIni = int(re.sub("-.*", "", pg))
pEnd = int(re.sub(".*-", "", pg))
nPgs = pEnd - pIni + 1
# Some documents have 1 or 2 extra presentation pages. Ignore them.
if doc.page_count <= nPgs and (nPgs - doc.page_count) < 2:
page1 = pEnd - doc.page_count + 1
else:
# Try to convert to an integer
try:
page1 = int(pg)
except ValueError:
# Not a valid page number, might be a Zotero key passed by mistake
sys.stderr.write(f"Warning: Third argument '{pg}' is not a valid page number. Using default page 1.\n")
page1 = 1
except Exception as e:
# Catch any other errors in page number parsing
sys.stderr.write(f"Warning: Error parsing page number: {e}. Using default page 1.\n")
page1 = 1
# Get year-page separator from environment
ypsep = os.getenv('ZYearPageSep')
if ypsep is None:
ypsep = ', p. '
notes = []
# Process each page
for page_idx in range(doc.page_count):
page = doc[page_idx]
pnum = page_idx + 1
# Handle page numbering
pgnum = str(page_idx + page1)
# Try to use page labels if available and not using a range
if not has_pg_range:
try:
# Get page label if available (Roman numerals, etc.)
if hasattr(doc, "get_page_labels"):
label = doc.get_page_labels()[page_idx]
if label and label.strip():
pgnum = label
except:
pass # Fallback to numeric page
# Get annotations
annots = page.annots()
if not annots:
continue
# Page dimensions for positioning
pwidth, _ = page.rect.width, page.rect.height
# Process annotations on this page
for annot in annots:
# Get annotation type
annot_type = annot.type[1]
# Get coordinates for sorting (top of annotation)
rect = annot.rect
x, y = rect.x0, rect.y0
# Guess column (for dual-column documents)
c = 1 if x < (pwidth / 2) else 2
# Get contents (annotation text)
contents = annot.info.get("content", "")
# Get author if available
author = annot.info.get("title", "")
# Process text comments/notes
if contents and annot_type in ["Text", "FreeText", "Note"]:
txt = contents + ' [annotation'
if author:
txt += ' by ' + author
if citekey:
txt += ' on ' + citekey
txt += ypsep + pgnum + ']\n'
# Add to notes list (with slight y-offset to ensure comments come before highlights)
notes.append([pnum, c, y - 0.0000001, txt])
# Process highlighted text
if annot_type in ["Highlight", "Underline"]:
# Extract text from the highlighted region
text = ""
try:
# Use word extraction so that only words mostly inside the highlight are included.
words = page.get_text("words")
for word in words:
if len(word) >= 5: # Ensure all required fields are present
word_rect = fitz.Rect(word[:4])
inter_rect = rect & word_rect # Compute the intersection rectangle
# Only add the word if more than 50% of its area is inside the annotation
if word_rect.get_area() > 0 and (inter_rect.get_area() / word_rect.get_area()) > 0.5:
text += word[4] + " "
except Exception:
try:
all_text = page.get_text("text")
if all_text:
text = "[Highlighted text could not be extracted precisely]"
except Exception:
pass
# Clean up the text
if text:
text = text.replace('-\n', '')
text = text.replace('\n', ' ')
text = re.sub(r'^\s*', '', text)
text = re.sub(r'\s*$', '', text)
text = re.sub(r'\s+', ' ', text)
if text.strip():
txt = '> ' + text + ' ['
if citekey:
txt += citekey
txt += ypsep + pgnum + ']\n'
notes.append([pnum, c, y, txt])
# Close the document
doc.close()
# Check if we found any notes
if notes:
# Sort notes by page, column, and y-position
snotes = sorted(notes, key=lambda x: (x[0], x[1], x[2]))
for n in snotes:
print(n[3], end='\n')
else:
sys.stderr.write(f"No annotations found in the PDF: {sys.argv[1]}\n")
sys.exit(34)
if __name__ == "__main__":
main()
================================================
FILE: scripts/zotid2bbtcitkey.py
================================================
#!/usr/bin/env python3
# Convert Zotero IDs into Better BibTeX (BBT) citation keys in a Markdown document
# These are the citation keys exported by Zotero File->Export Library with the "Better BibLaTeX" format
# and can be used in Quarto by setting the `bibliography:` YAML parameter to the exported .bib file
import requests
import sys
import json
import re
def zotid2citkey(zotid):
# Convert a Zotero ID into a Better BibTeX (BBT) citation key
#
# See
# https://github.com/retorquere/zotero-better-bibtex/discussions/3034#discussioncomment-11029462
url = "http://localhost:23119/better-bibtex/json-rpc"
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
data = {
"jsonrpc": "2.0",
"method": "item.citationkey",
"params": {"item_keys": [zotid]}
}
res = requests.post(url=url, headers=headers, data=json.dumps(data))
ress = list(res.json()["result"].items())
citkey = ress[0][1]
return (citkey)
fh = open(sys.argv[1], "r")
inf = fh.read()
fh.close()
ms = re.finditer(r"@\w+#[\w-]+(?=\]|;)", inf)
last_m = None
for m in ms:
if last_m is None:
outs = inf[0:m.start()]
else:
outs += inf[last_m.end():m.start()]
last_m = m
zotid = m.group(0)
if "#" in zotid:
zotid = zotid.split("#")[0]
zotid = re.sub("^@", "", zotid)
bbtid = zotid2citkey(zotid)
outs += "@" + bbtid
# Insert last part
outs += inf[last_m.end():]
with open(sys.argv[2], "w") as fh:
fh.write(outs)
================================================
FILE: selene.toml
================================================
# Set the base std to your custom library, which you'll put in std_dirs
std = "vim"
[rules]
multiple_statements = "allow"
[config]
# Point Selene to the directory containing your nvim.toml file.
# Assuming nvim.toml is in the same directory as selene.toml ('.').
std_dirs = [ "." ]
================================================
FILE: stylua.toml
================================================
column_width = 90
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
quote_style = "AutoPreferDouble"
call_parentheses = "Always"
collapse_simple_statement = "Always"
[sort_requires]
enabled = false
================================================
FILE: vim.yml
================================================
---
base: lua51
globals:
vim:
any: true
pandoc:
any: true
gitextract_o02z_1cb/ ├── .gitignore ├── LICENSE ├── README.md ├── doc/ │ └── zotcite.txt ├── ftplugin/ │ ├── bib_zotcite.lua │ ├── markdown_zotcite.lua │ ├── pandoc_zotcite.lua │ ├── quarto_zotcite.lua │ ├── rmd_zotcite.lua │ ├── rnoweb_zotcite.lua │ ├── tex_zotcite.lua │ ├── typst_zotcite.lua │ └── vimwiki_zotcite.lua ├── lua/ │ └── zotcite/ │ ├── bib.lua │ ├── config.lua │ ├── get.lua │ ├── hl.lua │ ├── init.lua │ ├── lsp.lua │ ├── seek.lua │ ├── utils.lua │ └── zotero.lua ├── plugin/ │ └── zotcite.lua ├── scripts/ │ ├── abntfix.py │ ├── apafix │ ├── odt2md.py │ ├── pdfnotes.py │ ├── pdfnotes2.py │ └── zotid2bbtcitkey.py ├── selene.toml ├── stylua.toml └── vim.yml
SYMBOL INDEX (5 symbols across 4 files) FILE: scripts/abntfix.py function TitleCase (line 11) | def TitleCase(x): function WalkFix (line 17) | def WalkFix(x): FILE: scripts/pdfnotes.py function main (line 24) | def main(): FILE: scripts/pdfnotes2.py function main (line 17) | def main(): FILE: scripts/zotid2bbtcitkey.py function zotid2citkey (line 13) | def zotid2citkey(zotid):
Condensed preview — 32 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (187K chars).
[
{
"path": ".gitignore",
"chars": 48,
"preview": "scripts/__pycache__/\nlua/zotcite/types\ndoc/tags\n"
},
{
"path": "LICENSE",
"chars": 35147,
"preview": " GNU GENERAL PUBLIC LICENSE\n Version 3, 29 June 2007\n\n Copyright (C) 2007 Free "
},
{
"path": "README.md",
"chars": 3186,
"preview": "# Zotcite\n\n> [!Note]\n> Users of Vim and Neovim < 0.10.4 have to switch to the \"vim\" branch.\n\n> [!Note]\n> Users of Neovim"
},
{
"path": "doc/zotcite.txt",
"chars": 18900,
"preview": "*zotcite.txt* Integration with Zotero\n\n\n 1. Overview "
},
{
"path": "ftplugin/bib_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "ftplugin/markdown_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "ftplugin/pandoc_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "ftplugin/quarto_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "ftplugin/rmd_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "ftplugin/rnoweb_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "ftplugin/tex_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "ftplugin/typst_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "ftplugin/vimwiki_zotcite.lua",
"chars": 33,
"preview": "require(\"zotcite.config\").init()\n"
},
{
"path": "lua/zotcite/bib.lua",
"chars": 6113,
"preview": "local zwarn = require(\"zotcite\").zwarn\n\nlocal M = {}\n\nlocal extract_addbibresource = function(lines)\n for _, v in pai"
},
{
"path": "lua/zotcite/config.lua",
"chars": 9820,
"preview": "---@class ZotciteUserOpts\n---Whether to syntax highlight citation keys\n---@field hl_cite_key? boolean\n---Reference field"
},
{
"path": "lua/zotcite/get.lua",
"chars": 19188,
"preview": "local config = require(\"zotcite.config\").get_config()\nlocal zwarn = require(\"zotcite\").zwarn\nlocal seek = require(\"zotci"
},
{
"path": "lua/zotcite/hl.lua",
"chars": 5568,
"preview": "local config = require(\"zotcite.config\").get_config()\n\nlocal M = {}\n\n--- Highlight citation key and add virtual text\n---"
},
{
"path": "lua/zotcite/init.lua",
"chars": 461,
"preview": "local M = {}\n\nlocal warns = {}\n\n--- Warning\n---@param msg string The message\n---@param store boolean? Whether to store t"
},
{
"path": "lua/zotcite/lsp.lua",
"chars": 10400,
"preview": "local zwarn = require(\"zotcite\").zwarn\nlocal zotero = require(\"zotcite.zotero\")\n\nlocal last_line = 0\nlocal z_ls = {\n "
},
{
"path": "lua/zotcite/seek.lua",
"chars": 7117,
"preview": "local config = require(\"zotcite.config\").get_config()\nlocal ns = vim.api.nvim_create_namespace(\"ZSeekPreview\")\nlocal zot"
},
{
"path": "lua/zotcite/utils.lua",
"chars": 2265,
"preview": "local config = require(\"zotcite.config\").get_config()\nlocal zwarn = require(\"zotcite\").zwarn\n\nlocal M = {}\n\nM.ODTtoMarkd"
},
{
"path": "lua/zotcite/zotero.lua",
"chars": 43309,
"preview": "-- A lot of code was either adapted or plainly copied from citation_vim,\n-- written by Rafael Schouten: https://github.c"
},
{
"path": "plugin/zotcite.lua",
"chars": 163,
"preview": "local did_plugin = false\nif did_plugin then return end\ndid_plugin = true\nvim.api.nvim_create_user_command(\"Zinfo\", \"lua "
},
{
"path": "scripts/abntfix.py",
"chars": 1402,
"preview": "#!/usr/bin/env python3\n\n\"\"\"\nPandoc filter to fix ABNT citations.\n\"\"\"\n\nimport sys\nimport io\nimport json\n\ndef TitleCase(x)"
},
{
"path": "scripts/apafix",
"chars": 125,
"preview": "The R package rmdfiltr includes an ampersand replacement filter:\nhttps://cran.r-project.org/web/packages/rmdfiltr/index."
},
{
"path": "scripts/odt2md.py",
"chars": 1244,
"preview": "#!/usr/bin/env python3\n\n\"\"\"\nConvert an ODT file into Markdown, converting citations into zotcite format.\n\"\"\"\n\nimport sys"
},
{
"path": "scripts/pdfnotes.py",
"chars": 4368,
"preview": "#!/usr/bin/env python3\n\n\"\"\"\nAdapted from code written by Hamed MP, published at\nhttps://gist.github.com/HamedMP/03440cca"
},
{
"path": "scripts/pdfnotes2.py",
"chars": 6249,
"preview": "#!/usr/bin/env python3\n\n\"\"\"\nPDF annotation extraction tool for zotcite using PyMuPDF (fitz)\n\"\"\"\n\nimport sys\nimport os\nim"
},
{
"path": "scripts/zotid2bbtcitkey.py",
"chars": 1549,
"preview": "#!/usr/bin/env python3\n\n# Convert Zotero IDs into Better BibTeX (BBT) citation keys in a Markdown document\n# These are t"
},
{
"path": "selene.toml",
"chars": 284,
"preview": "# Set the base std to your custom library, which you'll put in std_dirs\nstd = \"vim\"\n\n[rules]\nmultiple_statements = \"allo"
},
{
"path": "stylua.toml",
"chars": 211,
"preview": "column_width = 90\nline_endings = \"Unix\"\nindent_type = \"Spaces\"\nindent_width = 4\nquote_style = \"AutoPreferDouble\"\ncall_pa"
},
{
"path": "vim.yml",
"chars": 70,
"preview": "---\nbase: lua51\nglobals:\n vim:\n any: true\n pandoc:\n any: true\n"
}
]
About this extraction
This page contains the full source code of the jalvesaq/zotcite GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 32 files (173.3 KB), approximately 45.6k tokens, and a symbol index with 5 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.