Discussion:
[referencer] A question about document types and document.get_field()
Christopher Belanger
2011-12-19 23:03:08 UTC
Permalink
Hi,

First off, I love Referencer, so thanks for all the hard work. As a
humanities student, I often find myself needing to compile
bibliographies. Obviously LaTeX is awesome for this, but if someone
wants me to e-mail them a list of references, it doesn't make sense to
make a new .tex file and generate a pdf.

So I've been working on a plugin to export plaintext citations from
Referencer to the clipboard. It's pretty simple, but if I finish it
I'll gladly share it.

However, I'm having problems with document.get_field(). In order to get
the bibliographic information, the plugin needs to get a bunch of
fields. The problem is that if it tries to get a field that doesn't
exist, rather than just returning a null value for that field, it throws
an exception and the plugin doesn't return anything at all.

I've gone through the source code, and this seems to happen on or around
line 774 of Document.c.

So, first question: is this a bug? If so, it seems like it would be
easy to just change that line from 'throw std::etc....' to 'return "";',
which would allow the plugin to keep chugging along with an empty variable.

Second question: documents have different "genres" or "types," such as
article, book, etc., and I know this determines which fields are
defined. However I can't find a direct way for a plugin to find out
what type a document is. Is there such a function I just can't find?
If so, that would allow me to make sure I only get the fields defined
for a document, and give a non-hacky way to format the citation output
properly (books being different from articles, etc.).

Many thanks,
- Chris Belanger
Mads Chr. Olesen
2011-12-20 09:43:12 UTC
Permalink
Post by Christopher Belanger
So I've been working on a plugin to export plaintext citations from
Referencer to the clipboard. It's pretty simple, but if I finish it
I'll gladly share it.
Please do :-)

I assume you're writing the plugin in Python?
Post by Christopher Belanger
However, I'm having problems with document.get_field(). In order to get
the bibliographic information, the plugin needs to get a bunch of
fields. The problem is that if it tries to get a field that doesn't
exist, rather than just returning a null value for that field, it throws
an exception and the plugin doesn't return anything at all.
I'm not intimately familiar with this part, but in Python you can catch
exceptions as in:

try:
title = doc.get_field("title")
except:
title = ""
--
Mads Chr. Olesen <mads at mchro.dk>
Christopher Belanger
2011-12-21 19:40:55 UTC
Permalink
Post by Mads Chr. Olesen
I assume you're writing the plugin in Python?
Thanks for the quick reply! It's in Python, yes. I am strictly a
novice/hobbyist programmer, so I really appreciate that Referencer is
relatively easy to extend like this.
Post by Mads Chr. Olesen
Post by Christopher Belanger
The problem is that if it tries to get a field that doesn't
exist, rather than just returning a null value for that field, it throws
an exception and the plugin doesn't return anything at all.
I'm not intimately familiar with this part, but in Python you can catch
title = doc.get_field("title")
title = ""
Thanks, I didn't know about that! That may work, but it would require
doing a check like that for every single doc.get_field. It seems like
it would be easier and probably more efficient to have it handled in the
source code by having the object return a null result to the plugin if
the field doesn't exist. This is, in fact, what it says will happen in
the "Writing Plugins for Referencer" document, so the fact that this
does not occur--and that it instead throws an exception that crashes the
plugin--probably qualifies as a bug.

I've made an (extremely simple!) modification to the source that causes
doc.get_field to act as the documentation says it should, which in turn
allows my plugin to run. Is there a way for me to submit the patch
generated by mercurial for consideration? The change is trivial, but as
a non-programmer, submitting a patch to an open-source project would be
exciting.

After looking more closely at PythonDocument.C, I see that there is no
command available to the plugin to determine the document type. This is
too bad, because it would make citation formatting a snap.

Best,
- Chris
Mads Chr. Olesen
2011-12-21 19:49:43 UTC
Permalink
Post by Christopher Belanger
I've made an (extremely simple!) modification to the source that causes
doc.get_field to act as the documentation says it should, which in turn
allows my plugin to run. Is there a way for me to submit the patch
generated by mercurial for consideration? The change is trivial, but as
a non-programmer, submitting a patch to an open-source project would be
exciting.
If you mail me your modified file I will integrate it. We are moving
Referencer development onto launchpad [1], and we should really do a new
release soon...
Post by Christopher Belanger
After looking more closely at PythonDocument.C, I see that there is no
command available to the plugin to determine the document type. This
is too bad, because it would make citation formatting a snap.
I have an email somewhere, from someone else, who also had problems with
the type of documents from a plugin (setting the type IIRC), so maybe it
needs to be made easier to deal with.

[1] https://code.launchpad.net/referencer
--
Mads Chr. Olesen <mads at mchro.dk>
Christopher Belanger
2011-12-21 20:15:17 UTC
Permalink
Post by Mads Chr. Olesen
Post by Christopher Belanger
I've made an (extremely simple!) modification to the source that causes
doc.get_field to act as the documentation says it should, which in turn
allows my plugin to run. Is there a way for me to submit the patch
generated by mercurial for consideration? The change is trivial, but as
a non-programmer, submitting a patch to an open-source project would be
exciting.
If you mail me your modified file I will integrate it. We are moving
Referencer development onto launchpad [1], and we should really do a new
release soon...
Please find attached my suggested changes! The first is quite small,
just causing doc.get_field("thefield") to return NULL if "thefield" is
not a defined field. This is in accordance with the Referencer
documentation.
Post by Mads Chr. Olesen
Post by Christopher Belanger
After looking more closely at PythonDocument.C, I see that there is no
command available to the plugin to determine the document type. This
is too bad, because it would make citation formatting a snap.
I have an email somewhere, from someone else, who also had problems with
the type of documents from a plugin (setting the type IIRC), so maybe it
needs to be made easier to deal with.
The second change I've proposed is the addition of a doc.get_type()
function, which simply returns the document type from within a plugin.
I can think of many reasons why this could be useful in a plugin, and no
reasons it could be harmful; setting document types from within a
plugin, on the other hand, seems like it could be iffy.

Everything compiles perfectly on my end, and the plugin-in-progess runs
without exceptions on my library of 349 documents (with metadata of
wildly varying quality).

This change will allow me to complete my plugin, which would help make
Referencer more appealing and useful for people who need to export
citation lists in plaintext--in other words, everyone who doesn't use LaTeX!

Thanks for your patience, and I will look at the launchpad page. I've
never contributed to this kind of project before, so apologies if I'm
being presumptuous or doing things not quite right.

Best,
- Chris
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch
URL: <http://icculus.org/pipermail/referencer/attachments/20111221/62625ab4/attachment.diff>
Mads Chr. Olesen
2012-10-31 03:00:14 UTC
Permalink
Post by Christopher Belanger
This change will allow me to complete my plugin, which would help make
Referencer more appealing and useful for people who need to export
citation lists in plaintext--in other words, everyone who doesn't use LaTeX!
Hi Christopher,

I know this is a very old thread, but I recently did a lot of work on
Referencer, including applying your patch.

You can find the code in
https://code.launchpad.net/~referencer-devs/referencer/trunk
(if on Ubuntu you can just do "bzr branch lp:referencer" to get a source
code checkout)

Did you manage to get your plugin working? If so I would be interested
in including it in Referencer.
--
Mads Chr. Olesen <mads at mchro.dk>
Loading...