Add syntax highlighting for c and bash

master
Buddy Sandidge 10 years ago
parent c5829b65ea
commit f7c472d762

@ -12,7 +12,7 @@
return function (prefix) { return function (prefix) {
prefix = prefix || ''; prefix = prefix || '';
var langPrefix = prefix + '/vendor/prism/components/prism-';
return { return {
paths: { paths: {
backbone: prefix + '/vendor/backbone/backbone', backbone: prefix + '/vendor/backbone/backbone',
@ -21,11 +21,20 @@
jquery: prefix + '/vendor/jquery/dist/jquery', jquery: prefix + '/vendor/jquery/dist/jquery',
marionette: prefix + '/vendor/marionette/lib/backbone.marionette', marionette: prefix + '/vendor/marionette/lib/backbone.marionette',
prism: prefix + '/vendor/prism/prism', prism: prefix + '/vendor/prism/prism',
'prism-bash': langPrefix + 'bash',
'prism-c': langPrefix + 'c',
'prism-clike': langPrefix + 'clike',
'prism-javascript': langPrefix + 'javascript',
underscore: prefix + '/vendor/underscore/underscore' underscore: prefix + '/vendor/underscore/underscore'
}, },
shim: { shim: {
handlebars: {exports: 'Handlebars'}, handlebars: {exports: 'Handlebars'},
prism: {exports: 'Prism'}, prism: {exports: 'Prism'},
'prism-bash': {deps: ['prism', 'prism-clike'], exports: 'Prism'},
'prism-c': {deps: ['prism', 'prism-clike'], exports: 'Prism'},
'prism-clike': {deps: ['prism'], exports: 'Prism'},
'prism-javascript': {deps: ['prism', 'prism-clike'], exports: 'Prism'},
underscore: {exports: '_'} underscore: {exports: '_'}
} }
}; };

@ -1,6 +1,8 @@
define(['prism'], function (Prism) { define(['prism', 'prism-javascript'], function (Prism) {
return function (text, language) { return function (text, language, cb) {
language = language || 'javascript'; language = language || 'javascript';
return Prism.highlight(text, Prism.languages[language]); require(['prism-' + language], function () {
return cb(Prism.highlight(text, Prism.languages[language]));
});
}; };
}); });

@ -39,10 +39,19 @@ define([
if (copy) { if (copy) {
this.ui.copy.html(markdown(copy)); this.ui.copy.html(markdown(copy));
} }
var code = this.model.get('code');
if (code) { this.ui.copy.find('pre > code').each(function (index, el) {
this.ui.code.html(syntax(code)); var language = _.reduce(el.classList, function (memo, className) {
} if (/language-/.test(className)) {
return className.replace('language-', '');
}
return memo;
}, '');
var $el = $(el);
syntax($el.text(), language, function (html) {
$el.html(html);
});
});
}, },
onRequestResult: function onRequestResult(event) { onRequestResult: function onRequestResult(event) {

@ -2,6 +2,8 @@
```bash ```bash
$ gcc -xc - <<APP $ gcc -xc - <<APP
```
```c
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int *p = NULL; int *p = NULL;
@ -10,6 +12,8 @@ int main() {
printf("After pointer dereference\n"); printf("After pointer dereference\n");
return 0; return 0;
} }
```
```bash
APP APP
$ ./a.out $ ./a.out
Before pointer dereference Before pointer dereference

@ -1,9 +1,11 @@
define(['jquery', 'lib/syntax'], function ($, syntax) { define(['jquery', 'lib/syntax'], function ($, syntax) {
describe('syntax', function () { describe('syntax', function () {
it('→ converts javascript to html', function () { it('→ converts javascript to html', function (done) {
var src = 'function () {console.log("hi");}'; var src = 'function () {console.log("hi");}';
var actual = syntax(src); syntax(src, null, function (actual) {
expect($(actual).text()).toBe(src); expect($(actual).text()).toBe(src);
done();
});
}); });
}); });
}); });

Loading…
Cancel
Save