Dear Ferran,
On 11/03/2010 10:33 AM, Ferran Jorba wrote:
I have a couple of collections where a particular Marc tag should
display a different text than the other collections.
[...]
I'll try to give some hints below, but the chosen strategy would in
general mostly depend on the context, such as how "exceptional" the
case occurs, and how much the formatting must differ. In the end the
main issue is just to find a good balance between flexibility and
cost of maintenance.
1. Create a completely different format template that duplicates the
same Default_HTML_detailed, but just changing the prefix value for my
tag.
The philosophy behind BibFormat would push for this solution: you have
different type of records, with a different type of formatting. Then
"wrap" your record in different templates.
(Note that we are not talking here of formatting the same record in a
different way depending on the current collection, for cases where a
record can belong to several collections)
As you mentioned this solution is not advantageous in terms of
maintenance of format templates. This solution would make sense
when the type of records and formatting differs "enough".
Let's move now directly to your third solution:
3. Trying to modify the prefix inside the bfe_*.py element, like that:
academic_collections = ['PROCUR', 'EXAMENS']
if (bfo.field('980__a') in academic_collections):
prefix = "<br/><span
class='etiqueta'><lang><en>Studies</en><es>Titulación</es><ca>Titulació</ca></lang>:</span><span
class='text'>"
But inspecting the code I see that the prefix is evaluated before
calling the bfe_*.py element, so my hack doesn't work.
Indeed, your bfe_*.py elements cannot simply override the `prefix' and
`suffix' parameters as they can do with `escape'. To have the prefix
taken into consideration you must include it in the output yourself:
[...]
# your code prepares some output in 'out' variable
out = ''
[...]
# when returning, add your custom my_prefix and my_suffix
# based on the current collection (assignment not shown here)
if out:
return my_prefix + out + my_suffix
Another problem remains with the evaluation of the <lang> tags in your
prefix (as these get evaluated before the evaluation of the elements.
This is something that could maybe be changed). You would need to build
the prefix by taking into consideration the current language:
[...]
my_prefix= "<span class='etiqueta'>%s</span>"
if bfo.lang == 'en':
my_prefix %= 'Studies'
elif bfo.lang == 'es':
my_prefix %= 'Titulació'
[...]
or use an internal function to process languages (not recommended):
[...]
from invenio.bibformat_engine import filter_languages
my_prefix = filter_languages(my_prefix, bfo.lang)
[...]
You can further refine the solution, by adding the different
prefixes and suffixes as parameters of the element to make your element
more flexible.
def format_element(bfo, my_prefix="", my_suffix="", \
my_acad_prefix="", my_acad_suffix=""):
[...]
if out:
if bfo.field('980__a') in academic_collections:
return my_acad_prefix + out + my_acad_suffix
else:
return my_prefix + out + my_suffix
(This is similar to bfe_abstract.py with its `prefix_en', `prefix_fr'
parameters)
There are several other possibilities and combinations. Let me just
mention another more flexible one, making use of knowledge bases to
determine the prefix/suffix, based on the collection:
def format_element(bfo, default_prefix="", default_suffix=""):
[...]
if out:
bfo.kb('my_template_prefix', bfo.field('980__a'), \
default=my_default_prefix) + \
out + \
bfo.kb('my_template_suffix', bfo.field('980__a'), \
default=my_default_suffix) + \
In BibFormat Knoweldges Bases admin pages, define two KBs named
"my_template_prefix" and "my_template_suffix", which map collection
names to prefixes and suffixes. Your template can then specify a
default prefix with parameter `default_prefix', which gets overridden
by the values in Knowledge Bases for some specified collections.
This solution is of course an overkill in some scenarios...
Best regards
--
Jerome Caffaro