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 e0198a5 commit 6a02e7e
Showing 1 changed file with 59 additions and 37 deletions.
96 changes: 59 additions & 37 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 All @@ -35,9 +33,9 @@
'surface'.
The '--permissive' option allows certain recoverable parsing errors (e.g.
duplicate transport data) to be ignored. Further, the '--name=<name>' option
is used to override default phase names (i.e. 'gas'). The '--bibtex=<filename>'
option attaches references to the yaml output, where fields are based on BibTeX
duplicate transport data) to be ignored. Further, the '--name=<name>' option
is used to override default phase names (i.e. 'gas'). The '--bibtex=<filename>'
option attaches references to the yaml output, where fields are based on BibTeX
style input.
"""

Expand Down Expand Up @@ -1338,32 +1336,50 @@ def load_bibtex_file(self, path):
"""
Load and separate BibTeX-formatted entries from ``path`` on disk.
"""
with open(path, 'r', errors='ignore') as bib_file:
def parsecitation(entry):
# split entry into tagged key/value blocks and retrieve entry type
blocks = '\n'.join([e.strip() for e in entry[1:]]).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 = []
for t in block.split('\n'):
value.append(' '.join([v.strip(' "{}') for v in t.split('#')]))

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

def readcitation(bib_file):
entry = []
count = 0
while not(entry) or count > 0:
line = strip_nonascii(bib_file.readline()).rstrip()
count += line.count('{') - line.count('}')
count -= line.count('\{') - line.count('\}')
entry.append(line)
if line == "":
break

def readcitation():
block = []
count = 0
while not(block) or count > 0:
line = strip_nonascii(bib_file.readline()).rstrip()
count += line.count('{') - line.count('}')
count -= line.count('\{') - line.count('\}')
block.append(line)
if line == "":
break

if block[0]:
regex = r"^@+([a-zA-z]+){+(\w+)[ ,]"
bib = re.findall(regex, block[0].strip())
if bib and block[-1].rstrip()[-1] == '}' and count == 0:
return bib[0][1], block
else:
raise InputError("Encountered invalid syntax in "
"BibTeX entry:\n{}".format('\n'.join(block)))
if entry[0]:
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)
else:
return None, None
raise InputError("Encountered invalid syntax in "
"BibTeX entry:\n{}".format('\n'.join(entry)))
else:
return None, None

with open(path, 'r', errors='ignore') as bib_file:

while True:
label, entry = readcitation()
label, entry = readcitation(bib_file)
if label is not None:
self.bibtex[label] = entry
else:
Expand Down Expand Up @@ -1888,18 +1904,24 @@ 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 isinstance(v, list):
desc = '\n'.join(v)
desc = yaml.scalarstring.PreservedScalarString(desc)
elif v.isdigit():
desc = int(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',
after='references use BibTeX format'
)
bib.yaml_set_comment_before_after_key('references', before='\n')
emitter.dump(bib, dest)

# Additional information regarding conversion
Expand Down

0 comments on commit 6a02e7e

Please sign in to comment.