MetaHandler.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* eslint-disable
  2. camelcase,
  3. n/handle-callback-err,
  4. max-len,
  5. no-cond-assign,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. let MetaHandler
  17. const ProjectEntityHandler = require('../Project/ProjectEntityHandler')
  18. const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
  19. const packageMapping = require('./packageMapping')
  20. module.exports = MetaHandler = {
  21. labelRegex() {
  22. return /\\label{(.{0,80}?)}/g
  23. },
  24. usepackageRegex() {
  25. return /^\\usepackage(?:\[.{0,80}?])?{(.{0,80}?)}/g
  26. },
  27. ReqPackageRegex() {
  28. return /^\\RequirePackage(?:\[.{0,80}?])?{(.{0,80}?)}/g
  29. },
  30. getAllMetaForProject(projectId, callback) {
  31. if (callback == null) {
  32. callback = function () {}
  33. }
  34. return DocumentUpdaterHandler.flushProjectToMongo(
  35. projectId,
  36. function (err) {
  37. if (err != null) {
  38. return callback(err)
  39. }
  40. return ProjectEntityHandler.getAllDocs(projectId, function (err, docs) {
  41. if (err != null) {
  42. return callback(err)
  43. }
  44. const projectMeta = MetaHandler.extractMetaFromProjectDocs(docs)
  45. return callback(null, projectMeta)
  46. })
  47. }
  48. )
  49. },
  50. getMetaForDoc(projectId, docId, callback) {
  51. if (callback == null) {
  52. callback = function () {}
  53. }
  54. return DocumentUpdaterHandler.flushDocToMongo(
  55. projectId,
  56. docId,
  57. function (err) {
  58. if (err != null) {
  59. return callback(err)
  60. }
  61. return ProjectEntityHandler.getDoc(
  62. projectId,
  63. docId,
  64. function (err, lines) {
  65. if (err != null) {
  66. return callback(err)
  67. }
  68. const docMeta = MetaHandler.extractMetaFromDoc(lines)
  69. return callback(null, docMeta)
  70. }
  71. )
  72. }
  73. )
  74. },
  75. extractMetaFromDoc(lines) {
  76. let pkg
  77. const docMeta = { labels: [], packages: {} }
  78. const packages = []
  79. const label_re = MetaHandler.labelRegex()
  80. const package_re = MetaHandler.usepackageRegex()
  81. const req_package_re = MetaHandler.ReqPackageRegex()
  82. for (const line of Array.from(lines)) {
  83. let labelMatch
  84. let clean, messy, packageMatch
  85. while ((labelMatch = label_re.exec(line))) {
  86. let label
  87. if ((label = labelMatch[1])) {
  88. docMeta.labels.push(label)
  89. }
  90. }
  91. while ((packageMatch = package_re.exec(line))) {
  92. if ((messy = packageMatch[1])) {
  93. for (pkg of Array.from(messy.split(','))) {
  94. if ((clean = pkg.trim())) {
  95. packages.push(clean)
  96. }
  97. }
  98. }
  99. }
  100. while ((packageMatch = req_package_re.exec(line))) {
  101. if ((messy = packageMatch[1])) {
  102. for (pkg of Array.from(messy.split(','))) {
  103. if ((clean = pkg.trim())) {
  104. packages.push(clean)
  105. }
  106. }
  107. }
  108. }
  109. }
  110. for (pkg of Array.from(packages)) {
  111. if (packageMapping[pkg] != null) {
  112. docMeta.packages[pkg] = packageMapping[pkg]
  113. }
  114. }
  115. return docMeta
  116. },
  117. extractMetaFromProjectDocs(projectDocs) {
  118. const projectMeta = {}
  119. for (const _path in projectDocs) {
  120. const doc = projectDocs[_path]
  121. projectMeta[doc._id] = MetaHandler.extractMetaFromDoc(doc.lines)
  122. }
  123. return projectMeta
  124. },
  125. }