DocumentHelper.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* eslint-disable
  2. max-len,
  3. no-cond-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let DocumentHelper
  15. module.exports = DocumentHelper = {
  16. getTitleFromTexContent(content, maxContentToScan) {
  17. if (maxContentToScan == null) {
  18. maxContentToScan = 30000
  19. }
  20. const TITLE_WITH_CURLY_BRACES = /\\[tT]itle\*?\s*{([^}]+)}/
  21. const TITLE_WITH_SQUARE_BRACES = /\\[tT]itle\s*\[([^\]]+)\]/
  22. for (const line of Array.from(
  23. DocumentHelper._getLinesFromContent(content, maxContentToScan)
  24. )) {
  25. let match
  26. if (
  27. (match =
  28. line.match(TITLE_WITH_CURLY_BRACES) ||
  29. line.match(TITLE_WITH_SQUARE_BRACES))
  30. ) {
  31. return DocumentHelper.detex(match[1])
  32. }
  33. }
  34. return null
  35. },
  36. contentHasDocumentclass(content, maxContentToScan) {
  37. if (maxContentToScan == null) {
  38. maxContentToScan = 30000
  39. }
  40. for (const line of Array.from(
  41. DocumentHelper._getLinesFromContent(content, maxContentToScan)
  42. )) {
  43. // We've had problems with this regex locking up CPU.
  44. // Previously /.*\\documentclass/ would totally lock up on lines of 500kb (data text files :()
  45. // This regex will only look from the start of the line, including whitespace so will return quickly
  46. // regardless of line length.
  47. if (line.match(/^\s*\\documentclass/)) {
  48. return true
  49. }
  50. }
  51. return false
  52. },
  53. detex(string) {
  54. return string
  55. .replace(/\\LaTeX/g, 'LaTeX')
  56. .replace(/\\TeX/g, 'TeX')
  57. .replace(/\\TikZ/g, 'TikZ')
  58. .replace(/\\BibTeX/g, 'BibTeX')
  59. .replace(/\\\[[A-Za-z0-9. ]*\]/g, ' ') // line spacing
  60. .replace(/\\(?:[a-zA-Z]+|.|)/g, '')
  61. .replace(/{}|~/g, ' ')
  62. .replace(/[${}]/g, '')
  63. .replace(/ +/g, ' ')
  64. .trim()
  65. },
  66. _getLinesFromContent(content, maxContentToScan) {
  67. if (typeof content === 'string') {
  68. return content.substring(0, maxContentToScan).split('\n')
  69. } else {
  70. return content
  71. }
  72. },
  73. }