util.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. def flatten(l):
  2. if type(l) == list:
  3. return " ".join(l)
  4. else:
  5. return l
  6. def make_title_ascii(title):
  7. import unicodedata
  8. if type(title) != str :
  9. title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore')
  10. else :
  11. title = str(title)
  12. return title
  13. def dict_to_info(data):
  14. def sort_keys(key):
  15. if key[0] == "id":
  16. return 1
  17. elif key[0] == "author":
  18. return 2
  19. elif key[0] == "title":
  20. return 3
  21. elif key[0] == "issued":
  22. return 4
  23. elif key[0] == "abstract":
  24. return 5
  25. else:
  26. return -1
  27. try:
  28. from citeproc import CSLItem
  29. except:
  30. from .citeproc import CSLItem
  31. item = CSLItem(data)
  32. lines = []
  33. for i in data:
  34. if i in ("id", "author", "title", "issued", "abstract"):
  35. if i == "author":
  36. formatted_names = []
  37. for name in item.as_array(i):
  38. formatted_name = ", ".join([token for token in name if token != ''])
  39. formatted_names.append(formatted_name)
  40. lines.append((i, " & ".join(formatted_names)))
  41. else:
  42. lines.append((i, " ".join(map(flatten, item.as_array(i))).strip()))
  43. lines.sort(key=sort_keys)
  44. return "\n".join([l[0] + ":" + "\t" * 2 + l[1] for l in lines])