MetaHandler.js 3.6 KB

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