DocumentHelper.coffee 1.1 KB

12345678910111213141516171819202122232425
  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. ESCAPED_BRACES = /\\([{}\[\]])/g
  6. for line in DocumentHelper._getLinesFromContent(content, maxContentToScan)
  7. match = line.match(TITLE_WITH_SQUARE_BRACES) || line.match(TITLE_WITH_CURLY_BRACES)
  8. if match?
  9. return match[1].replace(ESCAPED_BRACES, (br)->br[1])
  10. return null
  11. contentHasDocumentclass: (content, maxContentToScan = 30000) ->
  12. for line in DocumentHelper._getLinesFromContent(content, maxContentToScan)
  13. # We've had problems with this regex locking up CPU.
  14. # Previously /.*\\documentclass/ would totally lock up on lines of 500kb (data text files :()
  15. # This regex will only look from the start of the line, including whitespace so will return quickly
  16. # regardless of line length.
  17. return true if line.match /^\s*\\documentclass/
  18. return false
  19. _getLinesFromContent: (content, maxContentToScan) ->
  20. return if typeof content is 'string' then content.substring(0, maxContentToScan).split("\n") else content