DocumentHelper.coffee 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. module.exports = DocumentHelper =
  2. getTitleFromTexContent: (content, maxContentToScan = 30000) ->
  3. TITLE_WITH_CURLY_BRACES = /\\[tT]itle\*?\s*{([^}]+)}/
  4. TITLE_WITH_SQUARE_BRACES = /\\[tT]itle\s*\[([^\]]+)\]/
  5. for line in DocumentHelper._getLinesFromContent(content, maxContentToScan)
  6. if match = line.match(TITLE_WITH_CURLY_BRACES) || line.match(TITLE_WITH_SQUARE_BRACES)
  7. return DocumentHelper.detex(match[1])
  8. return null
  9. contentHasDocumentclass: (content, maxContentToScan = 30000) ->
  10. for line in DocumentHelper._getLinesFromContent(content, maxContentToScan)
  11. # We've had problems with this regex locking up CPU.
  12. # Previously /.*\\documentclass/ would totally lock up on lines of 500kb (data text files :()
  13. # This regex will only look from the start of the line, including whitespace so will return quickly
  14. # regardless of line length.
  15. return true if line.match /^\s*\\documentclass/
  16. return false
  17. detex: (string) ->
  18. return string.replace(/\\LaTeX/g, 'LaTeX')
  19. .replace(/\\TeX/g, 'TeX')
  20. .replace(/\\TikZ/g, 'TikZ')
  21. .replace(/\\BibTeX/g, 'BibTeX')
  22. .replace(/\\\[[A-Za-z0-9. ]*\]/g, ' ') # line spacing
  23. .replace(/\\(?:[a-zA-Z]+|.|)/g, '')
  24. .replace(/{}|~/g, ' ')
  25. .replace(/[${}]/g, '')
  26. .replace(/ +/g, ' ')
  27. .trim()
  28. _getLinesFromContent: (content, maxContentToScan) ->
  29. return if typeof content is 'string' then content.substring(0, maxContentToScan).split("\n") else content