| 1 |
|
| 2 |
import sys |
| 3 |
import unicodedata |
| 4 |
|
| 5 |
from . import BibliographySource, Reference |
| 6 |
from . import Name, Date, DateRange, LiteralDate |
| 7 |
from ..string import String, MixedString, NoCase |
| 8 |
from .. import NAMES, DATES |
| 9 |
|
| 10 |
|
| 11 |
class CiteProcJSON(BibliographySource): |
| 12 |
def __init__(self, json_data): |
| 13 |
for ref in json_data: |
| 14 |
ref_data = {} |
| 15 |
for key, value in ref.items(): |
| 16 |
python_key = key.replace('-', '_') |
| 17 |
if python_key == 'id': |
| 18 |
ref_key = str(value).lower() |
| 19 |
continue |
| 20 |
elif python_key == 'type': |
| 21 |
ref_type = value |
| 22 |
continue |
| 23 |
elif python_key == 'key': |
| 24 |
# conflicts with the ref_key, so ignore |
| 25 |
continue |
| 26 |
if python_key == 'shortTitle': |
| 27 |
python_key = 'title_short' |
| 28 |
|
| 29 |
if python_key in NAMES: |
| 30 |
value = self.parse_names(value) |
| 31 |
elif python_key in DATES: |
| 32 |
value = self.parse_date(value) |
| 33 |
elif key not in ('language', ): |
| 34 |
value = self.parse_string(value) |
| 35 |
|
| 36 |
ref_data[python_key] = value |
| 37 |
self.add(Reference(ref_key, ref_type, **ref_data)) |
| 38 |
|
| 39 |
start_tag = '<span class="nocase">' |
| 40 |
end_tag = '</span>' |
| 41 |
|
| 42 |
def parse_string(self, string): |
| 43 |
string = str(string) |
| 44 |
lower_string = string.lower() |
| 45 |
end = 0 |
| 46 |
output = MixedString() |
| 47 |
try: |
| 48 |
while True: |
| 49 |
start = lower_string.index(self.start_tag, end) |
| 50 |
regular = string[end:start] |
| 51 |
if regular: |
| 52 |
output += String(regular) |
| 53 |
end = lower_string.index(self.end_tag, start + 1) |
| 54 |
start += len(self.start_tag) |
| 55 |
no_case = string[start:end] |
| 56 |
output += NoCase(no_case) |
| 57 |
end += len(self.end_tag) |
| 58 |
except ValueError: |
| 59 |
regular = string[end:] |
| 60 |
if regular: |
| 61 |
output += String(regular) |
| 62 |
return output |
| 63 |
|
| 64 |
def parse_names(self, json_data): |
| 65 |
names = [] |
| 66 |
for name_data in json_data: |
| 67 |
name = Name(**name_data) |
| 68 |
names.append(name) |
| 69 |
return names |
| 70 |
|
| 71 |
def parse_date(self, json_data): |
| 72 |
def parse_single_date(json_date): |
| 73 |
date_data = {} |
| 74 |
try: |
| 75 |
for i, part in enumerate(('year', 'month', 'day')): |
| 76 |
date_data[part] = json_date[i] |
| 77 |
except IndexError: |
| 78 |
pass |
| 79 |
return date_data |
| 80 |
|
| 81 |
dates = [] |
| 82 |
for json_date in json_data.get('date-parts', []): |
| 83 |
date = parse_single_date(json_date) |
| 84 |
if 'season' in json_data: |
| 85 |
date['season'] = json_data['season'] |
| 86 |
dates.append(date) |
| 87 |
|
| 88 |
circa = json_data.get('circa', 0) != 0 |
| 89 |
|
| 90 |
if len(dates) == 1: |
| 91 |
return Date(circa=circa, **dates[0]) |
| 92 |
elif len(dates) > 1: |
| 93 |
return DateRange(begin=Date(**dates[0]), end=Date(**dates[1]), |
| 94 |
circa=circa) |
| 95 |
elif 'literal' in json_data: |
| 96 |
return LiteralDate(json_data['literal'], circa=circa) |
| 97 |
else: |
| 98 |
return None |