Skip to content

Instantly share code, notes, and snippets.

@odashi
Last active August 29, 2015 14:01
Show Gist options
  • Save odashi/2395210deed16fa4d188 to your computer and use it in GitHub Desktop.
Save odashi/2395210deed16fa4d188 to your computer and use it in GitHub Desktop.
JSONで書いた文献一覧をBibTeXで吐き出す。
#!/path/to/python3
# -*- coding: utf-8 -*-
import cgi
import cgitb
import io
import json
import os
import sys
def main():
cgitb.enable()
query = cgi.parse_qs(os.environ['QUERY_STRING'])
refs = open('references.json', encoding='utf-8').read()
refs = json.loads(refs)
checklist = [
('title', lambda x: x),
('author', lambda x: ' and '.join(x)),
('journal', lambda x: x),
('booktitle', lambda x: x),
('publisher', lambda x: x),
('address', lambda x: x),
('pages', lambda x: str(x) if len(x) == 1 else str(x[0])+'--'+str(x[1])),
('month', lambda x: str(x)),
('year', lambda x: str(x)),
]
if 'name' in query:
print('Content-Type: text/plain; charset=UTF-8')
print()
name = query['name'][0]
if name in refs:
name = query['name'][0]
ref = refs[name]
info = []
print('@%s {%s,' % (ref['type'], name))
for k, f in checklist:
if k in ref:
info.append(' %s = {%s}' % (k, f(ref[k])))
print(',\n'.join(info))
print('}')
else:
print('No reference')
else:
print('Content-Type: text/html; charset=UTF-8')
print()
print('<html><head><title>References</title></head><body><ul>')
for k in sorted(refs.keys(), reverse=True):
print('<li><a href="/bibtex.cgi?name=%s">%s</a></li>' % (k, k))
print('</ul></body></html>')
if __name__ == '__main__':
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
main()
{
"oda201406acl": {
"type": "inproceedings",
"title": "Optimizing Segmentation Strategies for Simultaneous Speech Translation",
"author": ["Yusuke Oda", "Others"],
"booktitle": "The 52nd Annual Meeting of The Association for Computational Linguistics (ACL 2014) Short Paper Track",
"address": "Baltimore, USA",
"month": "June",
"year": 2014
},
"oda201405infosoc": {
"type": "inproceedings",
"title": "オンラインコミュニティの盛衰と挨拶表現の関連性の調査",
"author": ["小田 悠介", "他"],
"booktitle": "情報社会学会2014年度年次研究発表大会",
"address": "神奈川",
"month": "5月",
"year": 2014
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment