MetaHandler.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* eslint-disable
  2. camelcase,
  3. 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(err, projectMeta) {}
  33. }
  34. return DocumentUpdaterHandler.flushProjectToMongo(projectId, function(err) {
  35. if (err != null) {
  36. return callback(err)
  37. }
  38. return ProjectEntityHandler.getAllDocs(projectId, function(err, docs) {
  39. if (err != null) {
  40. return callback(err)
  41. }
  42. const projectMeta = MetaHandler.extractMetaFromProjectDocs(docs)
  43. return callback(null, projectMeta)
  44. })
  45. })
  46. },
  47. getMetaForDoc(projectId, docId, callback) {
  48. if (callback == null) {
  49. callback = function(err, docMeta) {}
  50. }
  51. return DocumentUpdaterHandler.flushDocToMongo(projectId, docId, function(
  52. err
  53. ) {
  54. if (err != null) {
  55. return callback(err)
  56. }
  57. return ProjectEntityHandler.getDoc(projectId, docId, function(
  58. err,
  59. lines
  60. ) {
  61. if (err != null) {
  62. return callback(err)
  63. }
  64. const docMeta = MetaHandler.extractMetaFromDoc(lines)
  65. return callback(null, docMeta)
  66. })
  67. })
  68. },
  69. extractMetaFromDoc(lines) {
  70. let pkg
  71. const docMeta = { labels: [], packages: {} }
  72. const packages = []
  73. const label_re = MetaHandler.labelRegex()
  74. const package_re = MetaHandler.usepackageRegex()
  75. const req_package_re = MetaHandler.ReqPackageRegex()
  76. for (let line of Array.from(lines)) {
  77. var labelMatch
  78. var clean, messy, packageMatch
  79. while ((labelMatch = label_re.exec(line))) {
  80. var label
  81. if ((label = labelMatch[1])) {
  82. docMeta.labels.push(label)
  83. }
  84. }
  85. while ((packageMatch = package_re.exec(line))) {
  86. if ((messy = packageMatch[1])) {
  87. for (pkg of Array.from(messy.split(','))) {
  88. if ((clean = pkg.trim())) {
  89. packages.push(clean)
  90. }
  91. }
  92. }
  93. }
  94. while ((packageMatch = req_package_re.exec(line))) {
  95. if ((messy = packageMatch[1])) {
  96. for (pkg of Array.from(messy.split(','))) {
  97. if ((clean = pkg.trim())) {
  98. packages.push(clean)
  99. }
  100. }
  101. }
  102. }
  103. }
  104. for (pkg of Array.from(packages)) {
  105. if (packageMapping[pkg] != null) {
  106. docMeta.packages[pkg] = packageMapping[pkg]
  107. }
  108. }
  109. return docMeta
  110. },
  111. extractMetaFromProjectDocs(projectDocs) {
  112. const projectMeta = {}
  113. for (let _path in projectDocs) {
  114. const doc = projectDocs[_path]
  115. projectMeta[doc._id] = MetaHandler.extractMetaFromDoc(doc.lines)
  116. }
  117. return projectMeta
  118. }
  119. }