Example #1
0
File: latex.py Project: RebUT/REBUT
def parseBib(filename, language):
    pp.ParserElement.setDefaultWhitespaceChars(" \n\t")
    entry = returnList(pp.Word('@', pp.alphanums) + sl('{') +
        pp.Word(pp.alphanums + "_") + sl(',') +
        CommaList(returnTuple(pp.Word(pp.alphanums) + sl('=') + pp.QuotedString('{', endQuoteChar = '}'))) +
        pp.Suppress(pp.Optional(',')) +
        sl('}'))
    r = (pp.ZeroOrMore(entry) | pp.Suppress('#' + pp.ZeroOrMore(pp.CharsNotIn('\n'))) + pp.StringEnd()).parseFile(filename)

    bibliography = QOpen(os.path.join(language, "bibliography.rst"), 'wt')
    print >>bibliography, "Bibliography"
    print >>bibliography, "============"
    print >>bibliography

    for _,e in sorted([(str(x[1]), x) for x in r]):
        (etype, tag, attrs) = str(e[0][1:]), str(e[1]), dict([(str(a), str(b)) for (a,b) in e[2]])
        
        representations = {
            'article' :         '$author, "$title". $journal $volume $number, pp $pages ($year)',
            'inproceedings' :   '$author "$title", $booktitle, $year',
            'misc' :            '$author "$title", $year',
            'techreport' :      '$author "$title", $edition, $edition ($year)',
        }
        if etype in representations:
            if 0:
                print >>bibliography, tag
                print >>bibliography, "^" * len(tag)
                print >>bibliography

            print >>bibliography, ".. [%s] %s" % (tag, Template(representations[etype]).safe_substitute(attrs))
            print >>bibliography
    bibliography.close()
Example #2
0
def parseBib(filename, language):
    pp.ParserElement.setDefaultWhitespaceChars(" \n\t")
    entry = returnList(
        pp.Word('@', pp.alphanums) + sl('{') + pp.Word(pp.alphanums + "_") +
        sl(',') + CommaList(
            returnTuple(
                pp.Word(pp.alphanums) + sl('=') +
                pp.QuotedString('{', endQuoteChar='}'))) +
        pp.Suppress(pp.Optional(',')) + sl('}'))
    r = (pp.ZeroOrMore(entry)
         | pp.Suppress('#' + pp.ZeroOrMore(pp.CharsNotIn('\n'))) +
         pp.StringEnd()).parseFile(filename)

    bibliography = QOpen(os.path.join(language, "bibliography.rst"), 'wt')
    print >> bibliography, "Bibliography"
    print >> bibliography, "============"
    print >> bibliography

    for _, e in sorted([(str(x[1]), x) for x in r]):
        (etype, tag, attrs) = str(e[0][1:]), str(e[1]), dict([
            (str(a), str(b)) for (a, b) in e[2]
        ])

        representations = {
            'article':
            '$author, "$title". $journal $volume $number, pp $pages ($year)',
            'inproceedings': '$author "$title", $booktitle, $year',
            'misc': '$author "$title", $year',
            'techreport': '$author "$title", $edition, $edition ($year)',
        }
        if etype in representations:
            if 0:
                print >> bibliography, tag
                print >> bibliography, "^" * len(tag)
                print >> bibliography

            print >> bibliography, ".. [%s] %s" % (
                tag, Template(representations[etype]).safe_substitute(attrs))
            print >> bibliography
    bibliography.close()