Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.4 KiB

  1. 'use strict'
  2. var fs = require('fs')
  3. var path = require('path')
  4. var highlighter = require('..')
  5. var colors = require('ansicolors')
  6. var diffFile = path.join(__dirname, 'git-diff.txt')
  7. var diff = fs.readFileSync(diffFile, 'utf-8')
  8. // @@ is not a valid js token, so when we see it, we can be sure that we are dealing with a git or svn diff
  9. var diffRegex = /^@@[^@]+@@$/m
  10. var diffIndRegex = /^(@@[^@]+@@)(.*)$/
  11. var addRemRegex = /^[+-]/
  12. var lines = diff.split('\n')
  13. function isDiff(lines) {
  14. return !!lines
  15. .filter(function(line) {
  16. return diffRegex.test(line)
  17. })
  18. .length
  19. }
  20. diff = isDiff(lines)
  21. function tryHighlight(code) {
  22. // TODO: need to remove symbols added to get valid code
  23. // this should be done by getting the splits instead of the actual code from the highlighter
  24. // now we can remove first / last one after highlighting completed
  25. function tryAppending(appended, tryNext) {
  26. try {
  27. return highlighter.highlight(code + appended)
  28. } catch (e) {
  29. return tryNext(code)
  30. }
  31. }
  32. function tryRemoveLeadingComma(tryNext) {
  33. var success
  34. try {
  35. success = highlighter.highlight(code.replace(/^( +),(.+)$/, '$1 $2'))
  36. return success
  37. } catch (e) {
  38. return tryNext(code)
  39. }
  40. }
  41. function tryPlain() {
  42. try {
  43. return highlighter.highlight(code)
  44. } catch (e) {
  45. return tryCloseMustache()
  46. }
  47. }
  48. function tryCloseMustache() { return tryAppending('}', tryCloseParen) }
  49. function tryCloseParen() { return tryAppending('\\)', tryCloseMustacheParen) }
  50. function tryCloseMustacheParen() { return tryAppending('})', tryRemovingCommas) }
  51. function tryRemovingCommas() { return tryRemoveLeadingComma(giveUp) }
  52. function giveUp() { return code }
  53. return tryPlain()
  54. }
  55. function highlightDiffInd(line, matches) {
  56. var highlighted = colors.brightBlue(matches[1])
  57. var code = matches[2]
  58. return code ? highlighted + tryHighlight(code) : highlighted
  59. }
  60. function colorsAddRemove(c) {
  61. return addRemRegex.test(c) ? colors.yellow(c) : c
  62. }
  63. function highlightDiff(line) {
  64. var diffIndMatches = diffIndRegex.exec(line)
  65. return diffIndMatches
  66. ? highlightDiffInd(line, diffIndMatches)
  67. : colorsAddRemove(line[0]) + tryHighlight(line.slice(1))
  68. }
  69. var highlightFn = diff ? highlightDiff : tryHighlight
  70. var highlightedLines = lines.map(highlightFn)
  71. console.log(highlightedLines.join('\n'))