#coding: utf-8
import workflow
import editor
# use input (for calling from Drafts) or current selection in Editorial
action_in = workflow.get_input()
if action_in == '':
action_in = editor.get_selected_text()
# following section is adapted from
# http://webstaff.itn.liu.se/~karlu/div/blog/2012-07-13_PythonBibTexParser/BibTeXParser.py
# Copyright 2012, Karljohan Lundin Palmerius
#
# Usage:
#
# import BibTeXParser
# parser = BibTeXParser()
# result = parser.parse("bibliography.bib")
#
# The result will be a list of one associative array for each bibtex
# record, containing the fields "type" and "key" with record type and
# bibtex-key, respectively, and the fields of each record.
#
import re
re_head_pattern = """\s*(\w+)\s*[({]\s*(\w*)\s*"""
re_var_pattern = """\s*(\w+)\s*=\s*(.*)\s*,?"""
class BibTeXParser:
def __init__(self):
pass
def parse(self,theText):
if theText is None:
return None
records = theText.split("@")
re_head = re.compile(re_head_pattern)
re_var = re.compile(re_var_pattern)
result = []
for record in records:
lines = record.splitlines()
if len(lines) < 2: continue
head_res = re_head.match(lines[0])
del lines[0]
res_rec = { "type": head_res.group(1), "key": head_res.group(2) }
for line in lines:
var_res = re_var.match(line)
if var_res is None: continue
res_rec[var_res.group(1)] = var_res.group(2).strip("""}"{,""")
result.append(res_rec)
return result
# end of adapted section
### formatting functions
def ital(s):
return '*'+s+'*'
### check for a field
def check(thePub,field):
if field in thePub:
return thePub[field]
else:
return ''
### handle fields in case-insensitive manner
def au(thePub):
return check(thePub,'Author')+check(thePub,'author')+''
def ti(thePub):
return check(thePub,'Title')+check(thePub,'title')+''
def yr(thePub):
return check(thePub,'Year')+check(thePub,'year')+''
def ad(thePub):
return check(thePub,'Address')+check(thePub,'address')+''
def pu(thePub):
return check(thePub,'Publisher')+check(thePub,'publisher')+''
def vol(thePub):
return check(thePub,'Volume')+check(thePub,'volume')+''
def no(thePub):
return check(thePub,'Number')+check(thePub,'number')+''
def pp(thePub):
return check(thePub,'Pages')+check(thePub,'pages')+''
def jo(thePub):
return check(thePub,'Journal')+check(thePub,'journal')+''
def bk(thePub):
return check(thePub,'Booktitle')+check(thePub,'booktitle')+''
def ed(thePub):
return check(thePub,'Editor')+check(thePub,'editor')+''
### handle various names for URL fields
def url(thePub):
u = ''
u = u + check(thePub,'URL')+check(thePub,'url')+check(thePub,'Url')
if check(thePub,'Bdsk-Url-1'):
u = u + '\n'
u = u + check(thePub,'Bdsk-Url-1')
if check(thePub,'Bdsk-Url-2'):
u = u + '\n'
u = u + check(thePub,'Bdsk-Url-2')
return u
### return a formatted citation
def cite(pub,style):
result=''
### formatting is coded here....add an elif for other styles
###....or rewrite to use a proper templating system
if style=='generic':
result = result+au(pub)+'. '+yr(pub)+'. '
if pub['type']=='book':
result = result+ital(ti(pub))+'. '
if ad(pub): result = result+ad(pub)+': '
result = result+pu(pub)
elif pub['type']=='article':
result = result+ti(pub)+'. '+ital(jo(pub))+'. '
if vol(pub): result = result+vol(pub)+':'
if no(pub): result = result+no(pub)+':'
result = result+pp(pub)
elif pub['type']=='inbook':
result = result+ti(pub)+'. '
result = result+'In '+ital(bk(pub))+' ed.'
if ed(pub): result = result+ed(pub)+'. '
if ad(pub): result = result+ad(pub)+': '
result = result+pu(pub)
if pp(pub): result = result+', pp. '+pp(pub)
result = result+'.\n'
if url(pub): result = result+url(pub)+'\n'
result = result+'\n'
return result
# parse the input
parser = BibTeXParser()
bib = parser.parse(action_in)
action_out=''
# generate the citations
for p in bib:
action_out=action_out+cite(p,'generic')
workflow.set_output(action_out)
There are no comments yet.