Пример #1
0
def format(bfo, width="50"):
    """
    Prints a full BibTeX record.

    'width' must be bigger than or equal to 30.
    This format element is an example of large element, which does
    all the formatting by itself

    @param width the width (in number of characters) of the record
    """
    out = "@"
    width = int(width)
    if width < 30:
        width = 30

    name_width = 21
    value_width = width-name_width
    recID = bfo.control_field('001')

    #Print entry type
    import invenio.bibformat_elements.bfe_collection as bfe_collection
    collection = bfe_collection.format(bfo=bfo, kb="DBCOLLID2BIBTEX")
    if collection == "":
        out += "article"
        collection = "article"
    else:
        out += collection

    out += "{"

    #Print BibTeX key
    #
    key = ''
    for external_keys in bfo.fields("035"):
        if external_keys['9'] == "SPIRESTeX" and external_keys['z']:
            key = external_keys['z']
    if not key:
        #contruct key in spires like way  need to store an make unique
        ####FIXME
        key = bfo.field("100a").split(' ')[0].lower() + ":" + \
              bfo.field("269c").split('-')[0] + \
              chr((recID % 26) + 97) + chr(((recID / 26) % 26) + 97)
    out += key + ','

        #If author cannot be found, print a field key=recID
    import invenio.bibformat_elements.bfe_INSPIRE_authors as bfe_authors
    authors = bfe_authors.format(bfo=bfo,
                                 limit="5",
                                 separator=" and ",
                                 extension=" and others",
                                 collaboration = "no",
                                 print_links="no",
                                 name_last_first = "yes")
    if authors == "":
        out += format_bibtex_field("key",
                                   recID,
                                   name_width,
                                   value_width)
    else:
        out += format_bibtex_field("author",
                                   authors,
                                   name_width,
                                   value_width)

    #Print editors
    import invenio.bibformat_elements.bfe_editors as bfe_editors
    editors = bfe_editors.format(bfo=bfo, limit="10",
                                 separator=" and ",
                                 extension="",
                                 print_links="no")
    out += format_bibtex_field("editor",
                               editors,
                               name_width,
                               value_width)

    #Print title
    import invenio.bibformat_elements.bfe_INSPIRE_title as bfe_title
    title = bfe_title.format(bfo=bfo)
    out += format_bibtex_field("title",
                               '{' + title + '}',
                               name_width,
                               value_width)

    #Print institution
    if collection ==  "techreport":
        publication_name = bfo.field("269__b")
        out += format_bibtex_field("institution",
                                   publication_name,
                                   name_width, value_width)

    #Print organization
    if collection == "inproceedings" or collection == "proceedings":
        organization = []
        organization_1 = bfo.field("260__b")
        if organization_1 != "":
            organization.append(organization_1)
        organization_2 = bfo.field("269__b")
        if organization_2 != "":
            organization.append(organization_2)
        out += format_bibtex_field("organization",
                                   ". ".join(organization),
                                   name_width,
                                   value_width)

    #Print publisher
    if collection == "book" or \
           collection == "inproceedings" \
           or collection == "proceedings":
        publishers = []
        import invenio.bibformat_elements.bfe_publisher as bfe_publisher
        publisher = bfe_publisher.format(bfo=bfo)
        if publisher != "":
            publishers.append(publisher)
        publication_name = bfo.field("269__b")
        if publication_name != "":
            publishers.append(publication_name)
        imprint_publisher_name = bfo.field("933__b")
        if imprint_publisher_name != "":
            publishers.append(imprint_publisher_name)
        imprint_e_journal__publisher_name = bfo.field("934__b")
        if imprint_e_journal__publisher_name != "":
            publishers.append(imprint_e_journal__publisher_name)

        out += format_bibtex_field("publisher",
                                   ". ".join(publishers),
                                   name_width,
                                   value_width)


    #Print collaboration
    collaborations = bfo.fields("710__g")
    out += format_bibtex_field("collaboration",
                               ", ".join(collaborations),
                               name_width,
                               value_width)


    #Print school
    if collection == "phdthesis":
        university = bfo.field("502__b")

        out += format_bibtex_field("school",
                                   university,
                                   name_width,
                                   value_width)

    #Print address
    if collection == "book" or \
           collection == "inproceedings" or \
           collection == "proceedings" or \
           collection == "phdthesis" or \
           collection == "techreport":
        addresses = []
        publication_place = bfo.field("260__a")
        if publication_place != "":
            addresses.append(publication_place)
        publication_place_2 = bfo.field("269__a")
        if publication_place_2 != "":
            addresses.append(publication_place_2)
        imprint_publisher_place = bfo.field("933__a")
        if imprint_publisher_place != "":
            addresses.append(imprint_publisher_place)
        imprint_e_journal__publisher_place = bfo.field("934__a")
        if imprint_e_journal__publisher_place != "":
            addresses.append(imprint_e_journal__publisher_place)

        out += format_bibtex_field("address",
                                   ". ".join(addresses),
                                   name_width,
                                   value_width)


    #Print journal
    if collection == "article":
        journals = []
        host_title = bfo.field("773__p")
        if host_title != "":
            journals.append(host_title)
        journal = bfo.field("909C4p")
        if journal != "":
            journals.append(journal)

        out += format_bibtex_field("journal",
                                   ". ".join(journals),
                                   name_width,
                                   value_width)



    #Print number
    if collection == "techreport" or \
           collection == "article":
        numbers = []
        host_number = bfo.field("773__n")
        if host_number != "":
            numbers.append(host_number)
        number = bfo.field("909C4n")
        if number != "":
            numbers.append(number)
        out += format_bibtex_field("number",
                                   ". ".join(numbers),
                                   name_width,
                                   value_width)


    #Print volume
    if collection == "article" or \
           collection == "book":
        volumes = []
        host_volume = bfo.field("773__v")
        if host_volume != "":
            volumes.append(host_volume)
        volume = bfo.field("909C4v")
        if volume != "":
            volumes.append(volume)

        out += format_bibtex_field("volume",
                                   ". ".join(volumes),
                                   name_width,
                                   value_width)

    #Print series
    if collection == "book":
        series = bfo.field("490__a")
        out += format_bibtex_field("series",
                                   series,
                                   name_width,
                                   value_width)

    #Print pages
    if collection == "article" or \
           collection == "inproceedings":
        pages = []
        host_pages = bfo.field("773c")
        if host_pages != "":
            pages.append(host_pages)
            nb_pages = bfo.field("909C4c")
            if nb_pages != "":
                pages.append(nb_pages)
                phys_pagination = bfo.field("300__a")
                if phys_pagination != "":
                    pages.append(phys_pagination)

        out += format_bibtex_field("pages",
                                   ". ".join(pages),
                                   name_width,
                                   value_width)


    #Print doi
    if collection == "article":
        dois = bfo.fields("773__a")
        out += format_bibtex_field("doi",
                                   ", ".join(dois),
                                   name_width,
                                   value_width)


    #Print year
    year = bfo.field("773__y")
    if year == "":
        year = get_year(bfo.field("269__c"))
        if year == "":
            year = get_year(bfo.field("260__c"))
            if year == "":
                year = get_year(bfo.field("502__c"))
                if year == "":
                    year = get_year(bfo.field("909C0y"))

    out += format_bibtex_field("year",
                               year,
                               name_width,
                               value_width)

    #Print note
    note = bfo.field("500__a")
    out += format_bibtex_field("note",
                               note,
                               name_width,
                               value_width)

    #Print eprint
    import invenio.bibformat_elements.bfe_INSPIRE_arxiv as bfe_arxiv

    eprints = bfe_arxiv.get_arxiv(bfo, category = "no")

    if eprints:
        out += format_bibtex_field("eprint",
                                   eprints[0],
                                   name_width,
                                   value_width)
        out += format_bibtex_field("archivePrefix",
                                   "arXiv",
                                   name_width,
                                   value_width)
        cats = bfe_arxiv.get_cats(bfo)
        if cats:
            out += format_bibtex_field("primaryClass",
                                       cats[0],
                                       name_width,
                                       value_width)


    #other report numbers
    numbers=[]
    primary_report_numbers = bfo.fields('037_a')
    additional_report_numbers = bfo.fields('088_a')
    report_numbers = primary_report_numbers
    report_numbers.extend(additional_report_numbers)
    for number in report_numbers:
        if number <> eprints[0]:
            numbers.append(number)
    if numbers:
        out += format_bibtex_field("reportNumber",
                                   ", ".join(numbers),
                                   name_width,
                                   value_width)

    out +="\n}"

    return out
Пример #2
0
def format(bfo, width="50"):
    """
    Prints a full BibTeX record.

    'width' must be bigger than or equal to 30.
    This format element is an example of large element, which does
    all the formatting by itself

    @param width: the width (in number of characters) of the record
    """
    out = "@"
    width = int(width)
    if width < 30:
        width = 30

    name_width = 19
    value_width = width - name_width
    recID = bfo.control_field("001")

    # Print entry type
    import invenio.bibformat_elements.bfe_collection as bfe_collection

    collection = bfe_collection.format(bfo=bfo, kb="DBCOLLID2BIBTEX")
    if collection == "":
        out += "article"
    else:
        out += collection

    out += "{"

    # Print BibTeX key
    #
    # Try to have: author_name:recID
    # If author_name cannot be found, use primary_report_number
    # If primary_report_number cannot be found, use additional_report_number
    # If additional_report_number cannot be found, use title:recID
    # If title cannot be found, use only recID
    #
    # The construction of this key is inherited from old BibTeX format
    # written in EL, in old BibFormat.
    key = recID
    author = bfo.field("100__a")
    if author != "":
        key = get_name(author) + ":" + recID
    else:
        author = bfo.field("700__a")
        if author != "":
            key = get_name(author) + ":" + recID
        else:
            primary_report_number = bfo.field("037__a")
            if primary_report_number != "":
                key = primary_report_number
            else:
                additional_report_number = bfo.field("088__a")
                if additional_report_number != "":
                    key = primary_report_number
                else:
                    title = bfo.field("245__a")
                    if title != "":
                        key = get_name(title) + ":" + recID
    out += key + ","

    # Print authors
    # If author cannot be found, print a field key=recID
    import invenio.bibformat_elements.bfe_authors as bfe_authors

    authors = bfe_authors.format(bfo=bfo, limit="", separator=" and ", extension="", print_links="no")
    if authors == "":
        out += format_bibtex_field("key", recID, name_width, value_width)
    else:
        out += format_bibtex_field("author", authors, name_width, value_width)

    # Print editors
    import invenio.bibformat_elements.bfe_editors as bfe_editors

    editors = bfe_editors.format(bfo=bfo, limit="", separator=" and ", extension="", print_links="no")
    out += format_bibtex_field("editor", editors, name_width, value_width)

    # Print title
    import invenio.bibformat_elements.bfe_title as bfe_title

    title = bfe_title.format(bfo=bfo, separator=". ")
    out += format_bibtex_field("title", title, name_width, value_width)

    # Print institution
    if collection == "techreport":
        publication_name = bfo.field("269__b")
        out += format_bibtex_field("institution", publication_name, name_width, value_width)

    # Print organization
    if collection == "inproceedings" or collection == "proceedings":
        organization = []
        organization_1 = bfo.field("260__b")
        if organization_1 != "":
            organization.append(organization_1)
        organization_2 = bfo.field("269__b")
        if organization_2 != "":
            organization.append(organization_2)
        out += format_bibtex_field("organization", ". ".join(organization), name_width, value_width)

    # Print publisher
    if collection == "book" or collection == "inproceedings" or collection == "proceedings":
        publishers = []
        import invenio.bibformat_elements.bfe_publisher as bfe_publisher

        publisher = bfe_publisher.format(bfo=bfo)
        if publisher != "":
            publishers.append(publisher)
        publication_name = bfo.field("269__b")
        if publication_name != "":
            publishers.append(publication_name)
        imprint_publisher_name = bfo.field("933__b")
        if imprint_publisher_name != "":
            publishers.append(imprint_publisher_name)
        imprint_e_journal__publisher_name = bfo.field("934__b")
        if imprint_e_journal__publisher_name != "":
            publishers.append(imprint_e_journal__publisher_name)

        out += format_bibtex_field("publisher", ". ".join(publishers), name_width, value_width)

    # Print journal
    if collection == "article":
        journals = []
        host_title = bfo.field("773__p")
        if host_title != "":
            journals.append(host_title)
        journal = bfo.field("909C4p")
        if journal != "":
            journals.append(journal)

        out += format_bibtex_field("journal", ". ".join(journals), name_width, value_width)

    # Print school
    if collection == "phdthesis":
        university = bfo.field("502__b")

        out += format_bibtex_field("school", university, name_width, value_width)

    # Print address
    if (
        collection == "book"
        or collection == "inproceedings"
        or collection == "proceedings"
        or collection == "phdthesis"
        or collection == "techreport"
    ):
        addresses = []
        publication_place = bfo.field("260__a")
        if publication_place != "":
            addresses.append(publication_place)
        publication_place_2 = bfo.field("269__a")
        if publication_place_2 != "":
            addresses.append(publication_place_2)
        imprint_publisher_place = bfo.field("933__a")
        if imprint_publisher_place != "":
            addresses.append(imprint_publisher_place)
        imprint_e_journal__publisher_place = bfo.field("934__a")
        if imprint_e_journal__publisher_place != "":
            addresses.append(imprint_e_journal__publisher_place)

        out += format_bibtex_field("address", ". ".join(addresses), name_width, value_width)

    # Print number
    if collection == "techreport" or collection == "article":
        numbers = []
        primary_report_number = bfo.field("037__a")
        if primary_report_number != "":
            numbers.append(primary_report_number)
        additional_report_numbers = bfo.fields("088__a")
        additional_report_numbers = ". ".join(additional_report_numbers)
        if additional_report_numbers != "":
            numbers.append(additional_report_numbers)
        host_number = bfo.field("773__n")
        if host_number != "":
            numbers.append(host_number)
        number = bfo.field("909C4n")
        if number != "":
            numbers.append(number)
        out += format_bibtex_field("number", ". ".join(numbers), name_width, value_width)

    # Print volume
    if collection == "article" or collection == "book":
        volumes = []
        host_volume = bfo.field("773__v")
        if host_volume != "":
            volumes.append(host_volume)
        volume = bfo.field("909C4v")
        if volume != "":
            volumes.append(volume)

        out += format_bibtex_field("volume", ". ".join(volumes), name_width, value_width)

    # Print series
    if collection == "book":
        series = bfo.field("490__a")
        out += format_bibtex_field("series", series, name_width, value_width)

    # Print pages
    if collection == "article" or collection == "inproceedings":
        pages = []
        host_pages = bfo.field("773c")
        if host_pages != "":
            pages.append(host_pages)
        nb_pages = bfo.field("909C4c")
        if nb_pages != "":
            pages.append(nb_pages)
        phys_pagination = bfo.field("300__a")
        if phys_pagination != "":
            pages.append(phys_pagination)

        out += format_bibtex_field("pages", ". ".join(pages), name_width, value_width)

    # Print month
    month = get_month(bfo.field("269__c"))
    if month == "":
        month = get_month(bfo.field("260__c"))
        if month == "":
            month = get_month(bfo.field("502__c"))

    out += format_bibtex_field("month", month, name_width, value_width)

    # Print year
    year = get_year(bfo.field("269__c"))
    if year == "":
        year = get_year(bfo.field("260__c"))
        if year == "":
            year = get_year(bfo.field("502__c"))
            if year == "":
                year = get_year(bfo.field("909C0y"))

    out += format_bibtex_field("year", year, name_width, value_width)

    # Print note
    note = bfo.field("500__a")
    out += format_bibtex_field("note", note, name_width, value_width)

    out += "\n}"

    return out