diff --git a/components/prism-git.js b/components/prism-git.js index 745966545f..812d509fee 100644 --- a/components/prism-git.js +++ b/components/prism-git.js @@ -1,68 +1,81 @@ -Prism.languages.git = { - /* - * A simple one line comment like in a git status command - * For instance: - * $ git status - * # On branch infinite-scroll - * # Your branch and 'origin/sharedBranches/frontendTeam/infinite-scroll' have diverged, - * # and have 1 and 2 different commits each, respectively. - * nothing to commit (working directory clean) - */ - 'comment': /^#.*$/m, - - /* - * a string (double and simple quote) - */ - 'string': /("|')(\\?.)*?\1/gm, - - /* - * a git command. It starts with a random prompt finishing by a $, then "git" then some other parameters - * For instance: - * $ git add file.txt - */ - 'command': { - pattern: /^.*\$ git .*$/m, +(function() { + // Exact copy of string token from prism-bash.js + var string_regexp = { + //allow multiline string + pattern: /("|')(\\?[\s\S])*?\1/g, inside: { - /* - * A git command can contain a parameter starting by a single or a double dash followed by a string - * For instance: - * $ git diff --cached - * $ git log -p - */ - 'parameter': /\s(--|-)\w+/m + //'property' class reused for bash variables + 'property': /\$([a-zA-Z0-9_#\?\-\*!@]+|\{[^\}]+\})/g } - }, + }; + + Prism.languages.git = { + /* + * A simple one line comment like in a git status command + * For instance: + * $ git status + * # On branch infinite-scroll + * # Your branch and 'origin/sharedBranches/frontendTeam/infinite-scroll' have diverged, + * # and have 1 and 2 different commits each, respectively. + * nothing to commit (working directory clean) + */ + comment: /^#.*$/m, + + /* + * a git command. It starts with a random prompt finishing by a $, then "git" then some other parameters + * For instance: + * $ git add file.txt + */ + command: { + pattern: /^.*\$ git .*$/m, + inside: { + /* + * A git command can contain a parameter starting by a single or a double dash followed by a string + * For instance: + * $ git diff --cached + * $ git log -p + */ + 'string': string_regexp, + 'parameter': /\s(--|-)\w+/m + } + }, + + /* + * Coordinates displayed in a git diff command + * For instance: + * $ git diff + * diff --git file.txt file.txt + * index 6214953..1d54a52 100644 + * --- file.txt + * +++ file.txt + * @@ -1 +1,2 @@ + * -Here's my tetx file + * +Here's my text file + * +And this is the second line + */ + coord: /^@@.*@@$/m, - /* - * Coordinates displayed in a git diff command - * For instance: - * $ git diff - * diff --git file.txt file.txt - * index 6214953..1d54a52 100644 - * --- file.txt - * +++ file.txt - * @@ -1 +1,2 @@ - * -Here's my tetx file - * +Here's my text file - * +And this is the second line - */ - 'coord': /^@@.*@@$/m, + /* + * Regexp to match the changed lines in a git diff output. Check the example above. + */ + 'deleted': /^-(?!-).+$/m, + 'inserted': /^\+(?!\+).+$/m, - /* - * Regexp to match the changed lines in a git diff output. Check the example above. - */ - 'deleted': /^-(?!-).+$/m, - 'inserted': /^\+(?!\+).+$/m, + /* + * a string (double and simple quote) + */ + string: string_regexp, - /* - * Match a "commit [SHA1]" line in a git log output. - * For instance: - * $ git log - * commit a11a14ef7e26f2ca62d4b35eac455ce636d0dc09 - * Author: lgiraudel - * Date: Mon Feb 17 11:18:34 2014 +0100 - * - * Add of a new line - */ - 'commit_sha1': /^commit \w{40}$/m -}; + /* + * Match a "commit [SHA1]" line in a git log output. + * For instance: + * $ git log + * commit a11a14ef7e26f2ca62d4b35eac455ce636d0dc09 + * Author: lgiraudel + * Date: Mon Feb 17 11:18:34 2014 +0100 + * + * Add of a new line + */ + 'commit_sha1': /^commit \w{40}$/m + }; +}()); diff --git a/components/prism-git.min.js b/components/prism-git.min.js index 7ba2782e89..1680de238c 100644 --- a/components/prism-git.min.js +++ b/components/prism-git.min.js @@ -1 +1 @@ -Prism.languages.git={comment:/^#.*$/m,string:/("|')(\\?.)*?\1/gm,command:{pattern:/^.*\$ git .*$/m,inside:{parameter:/\s(--|-)\w+/m}},coord:/^@@.*@@$/m,deleted:/^-(?!-).+$/m,inserted:/^\+(?!\+).+$/m,commit_sha1:/^commit \w{40}$/m} +(function(){var e={pattern:/("|')(\\?[\s\S])*?\1/g,inside:{property:/\$([a-zA-Z0-9_#\?\-\*!@]+|\{[^\}]+\})/g}};Prism.languages.git={comment:/^#.*$/m,command:{pattern:/^.*\$ git .*$/m,inside:{string:e,parameter:/\s(--|-)\w+/m}},coord:/^@@.*@@$/m,deleted:/^-(?!-).+$/m,inserted:/^\+(?!\+).+$/m,string:e,commit_sha1:/^commit \w{40}$/m}})() \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index 25de6e500d..654d09921b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,8 +1,9 @@ -var gulp = require('gulp'), - rename = require('gulp-rename'), - uglify = require('gulp-uglify'), - header = require('gulp-header'), - concat = require('gulp-concat'), +var gulp = require('gulp'), + rename = require('gulp-rename'), + uglify = require('gulp-uglify'), + header = require('gulp-header'), + concat = require('gulp-concat'), + jasmine = require('gulp-jasmine'), paths = { components: ['components/**/*.js', '!components/**/*.min.js'], @@ -14,7 +15,8 @@ var gulp = require('gulp'), 'components/prism-javascript.js', 'plugins/file-highlight/prism-file-highlight.js' ], - plugins: ['plugins/**/*.js', '!plugins/**/*.min.js'] + plugins: ['plugins/**/*.js', '!plugins/**/*.min.js'], + tests: ['prism.js', 'components/**/*.js', '!components/**/*.min.js', 'spec/**/*.js'] }; gulp.task('components', function() { @@ -43,6 +45,12 @@ gulp.task('plugins', function() { gulp.task('watch', function() { gulp.watch(paths.components, ['components', 'build']); gulp.watch(paths.plugins, ['plugins', 'build']); + gulp.watch(paths.tests, ['test']); +}); + +gulp.task('test', function() { + return gulp.src('spec/**/*.js') + .pipe(jasmine({verbose: false, includeStackTrace: false})); }); gulp.task('default', ['components', 'plugins', 'build']); diff --git a/package.json b/package.json index 06152b4fa8..9320fb8ea3 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "gulp-concat": "^2.3.4", "gulp-header": "^1.0.5", "gulp-rename": "^1.2.0", - "gulp-uglify": "^0.3.1" + "gulp-uglify": "^0.3.1", + "gulp-jasmine": "~1.0.0" } } diff --git a/spec/components/prism-git.spec.js b/spec/components/prism-git.spec.js new file mode 100644 index 0000000000..1f275212da --- /dev/null +++ b/spec/components/prism-git.spec.js @@ -0,0 +1,101 @@ +Prism = require('../../prism'); +require('../../components/prism-git'); + +describe('Prism-Git tests', function() { + it('parses git commands', function() { + var input = '$ git config --global core.editor "~/Sublime\ Text\ 3/sublime_text -w"'; + + var res = Prism.highlight(input, Prism.languages.git); + + expect(res).toBe('$ git config --global core.editor "~/Sublime Text 3/sublime_text -w"'); + }); + + + it('parses git comments', function() { + var input = + "$ git status\n" + + "# On branch infinite-scroll\n" + + "# Your branch and 'origin/sharedBranches/frontendTeam/infinite-scroll' have diverged,\n" + + "# and have 1 and 2 different commits each, respectively.\n" + + "nothing to commit (working directory clean)"; + + var res = Prism.highlight(input, Prism.languages.git); + + expect(res).toBe( + '$ git status\n' + + '# On branch infinite-scroll\n' + + '# Your branch and \'origin/sharedBranches/frontendTeam/infinite-scroll\' have diverged,\n' + + '# and have 1 and 2 different commits each, respectively.\n' + + 'nothing to commit (working directory clean)'); + }); + + it('parses diff', function() { + var input = + "$ git diff\n" + + "diff --git file.txt file.txt\n" + + "index 6214953..1d54a52 100644\n" + + "--- file.txt\n" + + "+++ file.txt\n" + + "@@ -1 +1,2 @@\n" + + "-Here's my tetx file\n" + + "+Here's my text file\n" + + "+And this is the second line"; + + var res = Prism.highlight(input, Prism.languages.git); + + expect(res).toBe( + '$ git diff\n' + + 'diff --git file.txt file.txt\n' + + 'index 6214953..1d54a52 100644\n' + + '--- file.txt\n' + + '+++ file.txt\n' + + '@@ -1 +1,2 @@\n' + + '-Here\'s my tetx file\n' + + '+Here\'s my text file\n' + + '+And this is the second line'); + }); + + it('parses a commit SHA1 (40 char)', function() { + var input = + "$ git log\n" + + "commit a11a14ef7e26f2ca62d4b35eac455ce636d0dc09\n" + + "Author: lgiraudel\n" + + "Date: Mon Feb 17 11:18:34 2014 +0100\n" + + "\n" + + " Add of a new line\n" + + "\n" + + "commit 87edc4ad8c71b95f6e46f736eb98b742859abd95\n" + + "Author: lgiraudel\n" + + "Date: Mon Feb 17 11:18:15 2014 +0100\n" + + "\n" + + " Typo fix\n" + + "\n" + + "commit 3102416a90c431400d2e2a14e707fb7fd6d9e06d\n" + + "Author: lgiraudel\n" + + "Date: Mon Feb 17 10:58:11 2014 +0100\n" + + "\n" + + " Initial commit"; + + var res = Prism.highlight(input, Prism.languages.git); + + expect(res).toBe( + '$ git log\n' + + 'commit a11a14ef7e26f2ca62d4b35eac455ce636d0dc09\n' + + 'Author: lgiraudel\n' + + 'Date: Mon Feb 17 11:18:34 2014 +0100\n' + + '\n' + + ' Add of a new line\n' + + '\n' + + 'commit 87edc4ad8c71b95f6e46f736eb98b742859abd95\n' + + 'Author: lgiraudel\n' + + 'Date: Mon Feb 17 11:18:15 2014 +0100\n' + + '\n' + + ' Typo fix\n' + + '\n' + + 'commit 3102416a90c431400d2e2a14e707fb7fd6d9e06d\n' + + 'Author: lgiraudel\n' + + 'Date: Mon Feb 17 10:58:11 2014 +0100\n' + + '\n' + + ' Initial commit'); + }); +}); \ No newline at end of file