parse/modules/bibtex/json.js

  1. /**
  2. * @module input/bibtex
  3. */
  4. import parseBibTeXProp from './prop'
  5. import parseBibTeXType from './type'
  6. /**
  7. * Format BibTeX JSON data
  8. *
  9. * @access protected
  10. * @method parseBibTeXJSON
  11. *
  12. * @param {Object|Array<Object>} data - The input data
  13. *
  14. * @return {Array<CSL>} The formatted input data
  15. */
  16. const parseBibTeXJSON = function (data) {
  17. return [].concat(data).map(entry => {
  18. const newEntry = {}
  19. let toMerge = []
  20. for (let prop in entry.properties) {
  21. const oldValue = entry.properties[prop]
  22. const [cslField, cslValue] = parseBibTeXProp(prop, oldValue) || []
  23. if (cslField) {
  24. if (/^[^:\s]+?:[^.\s]+(\.[^.\s]+)*$/.test(cslField)) {
  25. toMerge.push([cslField, cslValue])
  26. } else {
  27. newEntry[cslField] = cslValue
  28. }
  29. }
  30. }
  31. newEntry.type = parseBibTeXType(entry.type)
  32. newEntry.id = newEntry['citation-label'] = entry.label
  33. if (/\d(\D+)$/.test(entry.label)) {
  34. newEntry['year-suffix'] = entry.label.match(/\d(\D+)$/)[1]
  35. }
  36. toMerge.forEach(([cslField, value]) => {
  37. const props = cslField.split(/:|\./g)
  38. let cursor = newEntry
  39. while (props.length > 0) {
  40. const prop = props.shift()
  41. cursor = cursor[prop] || (cursor[prop] = !props.length ? value : isNaN(+props[0]) ? {} : [])
  42. }
  43. })
  44. return newEntry
  45. })
  46. }
  47. export {
  48. parseBibTeXJSON as parse,
  49. parseBibTeXJSON as default
  50. }