コード例 #1
0
def main(bibfiles, citations, format, output=sys.stdout):
    """
    Idea for the structure

    :param bibfiles: list of bibfiles
    :param citations: list of citations
    :param format: Output format (raw, latex or bibtex)
    :param output: Output file
    """
    if format == 'raw':
        unicode = True
    else:
        unicode = False

    #Load the bibtex
    entries = {}
    for bibfile in bibfiles:
        entries.update(get_bibtex_entries(bibfile, unicode))

    #Load configuration
    config = ConfigFormat(format)

    #Create a new dir with reqfields only
    new = dict()

    for entry in entries:
        citekey = entries[entry]['ID']
        #If the key is in the tex file
        if citekey in citations:
            tmp = dict()
            tmp['ENTRYTYPE'] = entries[entry]['ENTRYTYPE']
            try:
                req_field = config.get_reqfields(tmp['ENTRYTYPE'])
            except ValueError as e:
                output.write('% ' + str(e) + '\n')
            else:
                for field in entries[entry].keys():
                    #If the field is requested
                    if field in req_field:
                        tmp[field] = entries[entry][field]
                #Push the entry
                new[entry] = tmp

    #write it!
    if format == 'bibtex':
        from libcitebib.writer import write_bibtex
        write_bibtex(citations, new, output)
    elif format == 'latex' or format == 'raw':
        from libcitebib.writer import write_text
        write_text(citations, new, config, format, output)
    else:
        raise ValueError('Wrong format value')
コード例 #2
0
    def test_article(self):

        biblio = {'Carroll1986': {'author': [{'ID': 'CarrollBJ', 'name': 'Carroll, B J'}],
                     'journal': {'ID': 'Langmuir', 'name': 'Langmuir'},
                     'pages': '248 to 250', 'title': 'Equilibrium conformations of liquid drops on thin cylinders under forces of capillarity. A theory for the roll-up process',
                     'ENTRYTYPE': 'article', 'volume': '2', 'year': '1986'}}

        out = StringIO()
        style = 'author, journal (year)'
        write_text(biblio, style, out=out)
        result = out.getvalue().strip()

        expected = 'B. J. Carroll, Langmuir (1986)'
        self.assertEqual(result, expected)