# -*- coding: utf-8 -*-
##
## $Id$
##
## This file is part of CDS Invenio.
## Copyright (C) 2002, 2003, 2004, 2005, 2006 CERN.
##
## CDS Invenio 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 2 of the
## License, or (at your option) any later version.
##
## CDS Invenio 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 CDS Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""BibFormat element - Prints English and French abstract.
"""

__revision__ = "$Id$"

from invenio import bibformat_utils

def format(bfo, prefix_en, prefix_fr, suffix_en, suffix_fr, limit, 
           extension_en="[...] ",extension_fr="[...] ", contextual="no",
           highlight='no'):
    """ Prints the abstract of a record in english
    and then french
    
    @param prefix_en a prefix for english abstract (printed only if english abstract exists)
    @param prefix_fr a prefix for french abstract (printed only if french abstract exists)
    @param limit the maximum number of sentences of the abstract to display (for each language)
    @param extension_en a text printed after english abstracts longer than parameter 'limit'
    @param extension_fr a text printed after french abstracts longer than parameter 'limit'
    @param suffix_en a suffix for english abstract(printed only if english abstract exists)
    @param suffix_fr a suffix for french abstract(printed only if french abstract exists)
    @parmm contextual if 'yes' prints sentences the most relative to user search keyword (if limit < abstract)
    @param highlight if 'yes' highlights words from user search keyword
    """
    out = ''
    
    abstract_en = bfo.fields('520__a')
    abstract_en.extend(bfo.fields('520__b'))
    abstract_en = "<br/>".join(abstract_en)
    
    abstract_fr = bfo.fields('590__a')
    abstract_fr.extend(bfo.fields('590__b'))
    abstract_fr = "<br/>".join(abstract_fr)

    if contextual == 'yes' and limit != "" and \
           limit.isdigit() and int(limit) > 0:
        context_en = bibformat_utils.get_contextual_content(abstract_en,
                                                            bfo.search_pattern,
                                                            max_lines=int(limit))
        #FIXME add something like [...] before and after
        #contextual sentences when not at beginning/end of abstract
        #if not abstract_en.strip().startswith(context_en[0].strip()):
        #    out += '[...]'
        abstract_en = "<br/>".join(context_en)
        
        context_fr = bibformat_utils.get_contextual_content(abstract_fr,
                                                            bfo.search_pattern,
                                                            max_lines=int(limit))
        abstract_fr = "<br/>".join(context_fr)
    
    if len(abstract_en) > 0 :

        out += prefix_en

        if limit != "" and limit.isdigit():
            print_extension = False
            s_abstract = abstract_en.split(".")

            if int(limit) < len(s_abstract):
                print_extension = True
                s_abstract = s_abstract[:int(limit)]

            for sentence in s_abstract:
                out += sentence+ "."

            if print_extension:
                out += " "+extension_en

        else:
            out += abstract_en

        out += suffix_en
    
    if len(abstract_fr) > 0 :

        out += prefix_fr

        if limit != "" and limit.isdigit():
            print_extension = False
            s_abstract = abstract_fr.split(".")

            if int(limit) < len(s_abstract):
                print_extension = True
                s_abstract = s_abstract[:int(limit)]
        
            for sentence in s_abstract:
                out += sentence + "."

            if print_extension:
                out += " "+extension_fr

        else:
            out += abstract_fr
        
        out += suffix_fr

    if highlight == 'yes':
        out = bibformat_utils.highlight(out, bfo.search_pattern)

    return out


@@ -1125,16 +1125,36 @@
 ght='no'
+, print_lang='en,fr'
 ):%0A    %22
@@ -1191,39 +1191,126 @@
 cord
- in english%0A    and then french
+. By default prints English and French versions.%0A%0A    Printed languages can be chosen with the 'print_lang' parameter.
 %0A   
@@ -2149,24 +2149,165 @@
     
-%22%22%22%0A    out = ''
+@param print_lang the comma-separated list of languages to print. Now restricted to 'en' and 'fr'%0A    %22%22%22%0A    out = ''%0A%0A    languages = print_lang.split(',')
 %0A   
@@ -3492,24 +3492,45 @@
 act_en) %3E 0 
+and 'en' in languages
 :%0A%0A        o
@@ -4068,16 +4068,37 @@
 fr) %3E 0 
+and 'fr' in languages
 :%0A%0A     

Fix to let format templates choose the language to print for the abstract.