Skip to content

Commit

Permalink
[YAML] Parse BibTeX entries to YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingmar Schoegl committed Jan 12, 2020
1 parent 8e148e8 commit 9e1bead
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions interfaces/cython/cantera/ck2yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
[--bibtex=<filename>]
[--output=<filename>]
[--permissive]
[--quiet]
[--no-validate]
[-d | --debug]
Example:
Expand Down Expand Up @@ -1338,7 +1336,45 @@ def load_bibtex_file(self, path):
"""
Load and separate BibTeX-formatted entries from ``path`` on disk.
"""
def readcitation(bib_file):
def text_strip(text):
text = text.strip()
if text[0] == '"' and text[-1] == '"':
return text.strip('"')
elif text[0] == '{' and text[-1] == '}':
return text.strip('{}')
elif text[0] == r'\ '[0] and text[-1] == '}' or text.isdigit():
return text
else:
raise NotImplementedError(
"Cannot parse @string definition '{}'".format(text))

def parse_citation(entry):
# split entry into tagged key/value blocks and retrieve entry type
blocks = '\n'.join([e.strip() for e in entry[1:]])
blocks = blocks.rstrip('}').rstrip('\n ').split(',\n')
out = {'entry_type': entry[0].split('{')[0].strip(' ').lower()}

for block in blocks:
# retrieve key
parts = block.split('=')
key = parts[0].strip().lower()
block = '='.join(parts[1:])

# build value
value = ' '.join([text_strip(b)
for b in block.replace('\n', ' ').split('#')])

# reformat author list
if key == 'author':
authors = [a.strip().replace('{', '').replace('}', '')
for a in value.split(' and ')]
if len(authors) > 1:
value = ', '.join(authors[:-1]) + ' and ' + authors[-1]

out[key] = value if len(value) > 1 else value[0]
return out

def read_citation(bib_file):
entry = []
count = 0
while not(entry) or count > 0:
Expand All @@ -1353,7 +1389,7 @@ def readcitation(bib_file):
regex = r"^@+([a-zA-z]+){+(\w+)[ ,]"
bib = re.findall(regex, entry[0].strip())
if bib and entry[-1].rstrip()[-1] == '}' and count == 0:
return bib[0][1], parsecitation(entry)
return bib[0][1], parse_citation(entry)
else:
raise InputError("Encountered invalid syntax in "
"BibTeX entry:\n{}".format('\n'.join(entry)))
Expand All @@ -1363,7 +1399,7 @@ def readcitation(bib_file):
with open(path, 'r', errors='ignore') as bib_file:

while True:
label, entry = readcitation(bib_file)
label, entry = read_citation(bib_file)
if label is not None:
self.bibtex[label] = entry
else:
Expand Down Expand Up @@ -1888,12 +1924,21 @@ def write_yaml(self, name='gas', out_name='mech.yaml'):

# bibtex entries
if self.bibtex:
entries = []
entries = BlockMap()
for key, val in self.bibtex.items():
desc = '\n'.join(val)
desc = yaml.scalarstring.PreservedScalarString(desc)
entries.append((key, desc))
bib = BlockMap([('references', BlockMap(entries))])
tags = []
comment = val.pop('entry_type')
for k, v in val.items():
if v.isdigit():
desc = int(v)
elif not v.count(" ") and len(v) > 50:
desc = yaml.scalarstring.PreservedScalarString(v)
else:
desc = v
tags.append((k, desc))
entries[key] = BlockMap(tags)
entries.yaml_add_eol_comment(comment, key)
bib = BlockMap([('references', entries)])
if desc.strip():
bib.yaml_set_comment_before_after_key('references', before='\n')
emitter.dump(bib, dest)
Expand Down

0 comments on commit 9e1bead

Please sign in to comment.