From 893b4fb788975d339654c1309243fb3b4f6f90bf Mon Sep 17 00:00:00 2001 From: Philipp Falk Date: Tue, 20 Sep 2016 15:34:18 +0200 Subject: [PATCH] bibtex2mathhtml.py: work on split bibtex files --- bibtex.py | 65 ------------------------------------------------- bibtex2mathhtml.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 65 deletions(-) delete mode 100755 bibtex.py create mode 100755 bibtex2mathhtml.py diff --git a/bibtex.py b/bibtex.py deleted file mode 100755 index 1e81b13..0000000 --- a/bibtex.py +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env python3 - -import os -import tempfile -import subprocess -import sys - -def split_bibtex(dirname, filename, categories): - for cat in categories: - cmd = ['bib2bib', '-q', '-c', '$type="{}"'.format(cat), '--warn-error', - '-ob', os.path.join(dirname, '{}.bib'.format(cat)), - filename] - try: - subprocess.check_call(cmd) - except subprocess.CalledProcessError as e: - if e.returncode != 2: - raise e - return tmpdir - -def get_publications(dirname, category, basename): - filename = '{}.bib'.format(category) - if not os.path.exists(filename): - return (None, None) - cmd = ['bibtex2html', '-q', '--no-doc', '--no-header', - filename] - subprocess.check_call(cmd) - with open('{}.html'.format(filename.rsplit('.', 1)[0]), 'r') as f: - html = f.read().split('
')[0].strip().replace('{}_bib.html'.format(category), '{}_bib.html'.format(basename)) - with open('{}_bib.html'.format(filename.rsplit('.', 1)[0]), 'r') as f: - bib = f.read().split('
')[0].strip().split('\n\n', 2)[2] - return (html, bib) - -if __name__ == '__main__': - if len(sys.argv) != 2: - print('Usage: {} '.format(sys.argv[0]), file=sys.stderr) - sys.exit(1) - - filename = sys.argv[1] - basename = filename.rsplit('.', 1)[0] - categories = ['preprint', 'article', 'book', 'proceeding', 'thesis'] - catnames = ['Preprints', 'Zeitschriftenartikel', 'Bücher', 'Proceedings', 'Abschlussarbeiten'] - - outhtml = [] - outbibl = [] - outhtml.append('% use aam ueberschrift="Publikationen"') - outbibl.append('% use aam ueberschrift="BibTeX"') - with tempfile.TemporaryDirectory() as tmpdir: - split_bibtex(tmpdir, filename, categories) - cwd = os.getcwd() - os.chdir(tmpdir) - for (cat, name) in zip(categories, catnames): - (html, bib) = get_publications(tmpdir, cat, basename) - if html is None: - continue - outhtml.append('

{}

'.format(cat, name)) - outhtml.append(html) - outbibl.append(bib) - os.chdir(cwd) - - with open('{}.html'.format(basename), 'w') as f: - f.write('\n'.join(outhtml)) - with open('{}_bib.html'.format(basename), 'w') as f: - f.write('\n'.join(outbibl)) - - print('DONE! Now please copy {}.html and {}_bib.html to the webserver.'.format(basename, basename)) diff --git a/bibtex2mathhtml.py b/bibtex2mathhtml.py new file mode 100755 index 0000000..c2869b9 --- /dev/null +++ b/bibtex2mathhtml.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import os +import tempfile +import subprocess +import sys + +# Not used currently +# def split_bibtex(dirname, filename, categories): +# for cat in categories: +# cmd = ['bib2bib', '-q', '-c', '$type="{}"'.format(cat), '--warn-error', +# '-ob', os.path.join(dirname, '{}.bib'.format(cat)), +# filename] +# try: +# subprocess.check_call(cmd) +# except subprocess.CalledProcessError as e: +# if e.returncode != 2: +# raise e +# return tmpdir + +def get_publications(dirname, category, basename): + filename = '{}.bib'.format(category) + if not os.path.exists(filename): + return (None, None) + cmd = ['bibtex2html', '-q', '--no-doc', '--no-header', + filename] + subprocess.check_call(cmd) + with open('{}.html'.format(filename.rsplit('.', 1)[0]), 'r') as f: + html = f.read().split('
')[0].strip().replace('{}_bib.html'.format(category), '{}_bib.html'.format(basename)) + with open('{}_bib.html'.format(filename.rsplit('.', 1)[0]), 'r') as f: + bib = f.read().split('
')[0].strip().split('\n\n', 2)[2] + return (html, bib) + +if __name__ == '__main__': + if len(sys.argv) != 1: + print('Usage: {}'.format(sys.argv[0]), file=sys.stderr) + sys.exit(1) + + basename = 'pubs' + categories = ['preprints', 'articles', 'books', 'proceedings', 'theses'] + catnames = ['Preprints', 'ZeitschriftenartikelJournal articles', 'BücherBooks', 'Proceedings', 'AbschlussarbeitenTheses'] + + outhtml = [] + outbibl = [] + + outhtml.append('% use aam ueberschrift="Publikationen"') + outbibl.append('% use aam ueberschrift="BibTeX"') + outhtml.append('
') + outhtml.append('
') + outhtml.append('
    ') + + for (cat, name) in zip(categories, catnames): + outhtml.append('
  • {}'.format(cat, name)) + outhtml.append('
') + outhtml.append('
') + outhtml.append('
') + + for (cat, name) in zip(categories, catnames): + (html, bib) = get_publications(os.getcwd(), cat, basename) + if html is None: + continue + outhtml.append('

{}

'.format(cat, name)) + outhtml.append(html) + outbibl.append(bib) + + with open('{}.html'.format(basename), 'w') as f: + f.write('\n'.join(outhtml) + '\n') + with open('{}_bib.html'.format(basename), 'w') as f: + f.write('\n'.join(outbibl) + '\n') + + print('DONE! Now please copy {}.html and {}_bib.html to the webserver.'.format(basename, basename)) -- 2.11.0