Skip to content

Instantly share code, notes, and snippets.

@pwab
Created April 29, 2021 06:07
Show Gist options
  • Save pwab/3eccc6fa141ae83b2951eb4b565543e1 to your computer and use it in GitHub Desktop.
Save pwab/3eccc6fa141ae83b2951eb4b565543e1 to your computer and use it in GitHub Desktop.
This script helps to convert bibtex to markdown. It is just a start for now.
#!/usr/bin/env python
"""bib2md
This script helps to convert bibtex to markdown. It is just a start for now.
"""
# The MIT License (MIT)
# Copyright © 2021 Philipp Wabnitz
# https://pwab.mit-license.org
__author__ = "Philipp Wabnitz"
__copyright__ = "Copyright 2021, Philipp Wabnitz"
__license__ = "MIT"
__version__ = "0.1"
from pybtex.database import parse_file
def get_group(entry):
"""Will return the group(s) of an entry
Parameters
----------
entry : str
The entry of the bibtex file
Returns
-------
str
A string containing the group(s) (separated by comma)
"""
return entry.fields['groups']
def main():
# Keep a list of groups
groups = []
# Handle the output
output = ""
# Open file
# TODO: Convert to commandline parameter
bib = parse_file('test.bib')
# Search for groups
# TODO: Functionality to work with multiple or subgroups
for key in bib.entries.keys():
group = get_group(bib.entries[key])
if group not in groups:
groups.append(group)
# Go through the groups
# TODO: Functionality to work with multiple or subgroups
for current_group in groups:
# Set header for group
output += '## ' + str(current_group) + '\n'
# Go through all entries
for key in bib.entries.keys():
group = bib.entries[key].fields['groups']
if group == current_group:
# Entry is inside group so we should aggregate the data
# Authors
authors = bib.entries[key].persons['author']
output += ', '.join(map(str, authors)) + '; '
# Title
output += '**' + bib.entries[key].fields['title'] + '**'
# TODO: Functionality to work with tags, notes, date etc.
# Set a newline after each groups end
output += '\n'
# Do something with the output
print(output)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment