Exemplo n.º 1
0
def readBibTexFileIntoTree(c, fn, p):
    '''Import a BibTeX file into a @bibtex tree.'''
    root = p.copy()
    g.es('reading:', fn)
    s = g.readFileIntoEncodedString(fn)
        # Read the encoded bytes for g.getEncodingAt()
    if not s or not s.strip():
        return
    encoding = g.getEncodingAt(p, s)
    s = g.toUnicode(s, encoding=encoding)
    aList, entries, strings = [], [], []
        # aList is a list of tuples (h,b).
    s = '\n' + ''.join([z.lstrip() for z in g.splitLines(s)])
    for line in s.split('\n@')[1:]:
        kind, rest = line[: 6], line[7:].strip()
        if kind == 'string':
            strings.append(rest[: -1] + '\n')
        else:
            i = min(line.find(','), line.find('\n'))
            h = '@' + line[: i]
            h = h.replace('{', ' ').replace('(', ' ').replace('\n', '')
            b = line[i + 1:].rstrip().lstrip('\n')[: -1]
            entries.append((h, b),)
    if strings:
        h, b = '@string', ''.join(strings)
        aList.append((h, b),)
    aList.extend(entries)
    for h, b in aList:
        p = root.insertAsLastChild()
        p.b, p.h = b, h
    root.expand()
    c.redraw()
Exemplo n.º 2
0
def readBibTexFileIntoTree(c, fn, p):
    '''Import a BibTeX file into a @bibtex tree.'''
    root = p.copy()
    g.es('reading:', fn)
    s = g.readFileIntoEncodedString(fn)
        # Read the encoded bytes for g.getEncodingAt()
    if not s or not s.strip():
        return
    encoding = g.getEncodingAt(p, s)
    s = g.toUnicode(s, encoding=encoding)
    aList, entries, strings = [], [], []
        # aList is a list of tuples (h,b).
    s = '\n' + ''.join([z.lstrip() for z in g.splitLines(s)])
    for line in s.split('\n@')[1:]:
        kind, rest = line[: 6], line[7:].strip()
        if kind == 'string':
            strings.append(rest[: -1] + '\n')
        else:
            i = min(line.find(','), line.find('\n'))
            h = '@' + line[: i]
            h = h.replace('{', ' ').replace('(', ' ').replace('\n', '')
            b = line[i + 1:].rstrip().lstrip('\n')[: -1]
            entries.append((h, b),)
    if strings:
        h, b = '@string', ''.join(strings)
        aList.append((h, b),)
    aList.extend(entries)
    for h, b in aList:
        p = root.insertAsLastChild()
        p.b, p.h = b, h
    root.expand()
    c.redraw()
Exemplo n.º 3
0
def writeTreeAsBibTex(c, fn, root):
    """Write root's *subtree* to bibFile."""
    strings, entries = [], []
    for p in root.subtree():
        h = p.h
        if h.lower() == '@string':
            strings.extend([('@string{%s}\n\n' % z.rstrip())
                for z in g.splitLines(p.b) if z.strip()])
        else:
            i = h.find(' ')
            kind, rest = h[: i].lower(), h[i + 1:].rstrip()
            if kind in entrytypes:
                entries.append('%s{%s,\n%s}\n\n' % (kind, rest, p.b.rstrip()))
    if strings or entries:
        g.es('writing:', fn)
        encoding=g.getEncodingAt(root)
        with open(fn, 'wb') as f:
            s = ''.join(strings + entries)
            f.write(g.toEncodedString(s,encoding=encoding))
Exemplo n.º 4
0
def writeTreeAsBibTex(c, fn, root):
    """Write root's *subtree* to bibFile."""
    strings, entries = [], []
    for p in root.subtree():
        h = p.h
        if h.lower() == '@string':
            strings.extend([('@string{%s}\n\n' % z.rstrip())
                for z in g.splitLines(p.b) if z.strip()])
        else:
            i = h.find(' ')
            kind, rest = h[: i].lower(), h[i + 1:].rstrip()
            if kind in entrytypes:
                entries.append('%s{%s,\n%s}\n\n' % (kind, rest, p.b.rstrip()))
    if strings or entries:
        g.es('writing:', fn)
        encoding=g.getEncodingAt(root)
        with open(fn, 'wb') as f:
            s = ''.join(strings + entries)
            f.write(g.toEncodedString(s,encoding=encoding))