Commit bdb8fae8449b2031d4025f1e051917e71406264b

Lea Verou 2012-07-13T15:11:03

Added hooks system, moved entity tooltips to the Markup language, as a plugin

diff --git a/components/prism-core.js b/components/prism-core.js
index 9f09628..7654466 100644
--- a/components/prism-core.js
+++ b/components/prism-core.js
@@ -145,28 +145,52 @@ var _ = self.Prism = {
 	},
 	
 	wrap: function(token, content) {
-		var tag = 'span',
-		    classes = ['token', token],
-		    attributes = {};
+		var env = {
+			token: token,
+			content: content
+		};
 		
-		if (token === 'comment') {
-			attributes['spellcheck'] = 'true';
-		}
+		env.tag = 'span';
+		env.classes = ['token', token];
+		env.attributes = {};
 		
-		// Example plugin - Will be removed
-		if (token === 'entity') {
-			attributes['title'] = content.replace(/&/, '&');
+		if (token === 'comment') {
+			env.attributes['spellcheck'] = 'true';
 		}
 		
-		// TODO Add hook here
+		_.hooks.run('wrap', env);
 		
 		var attributesSerialized = '';
 		
-		for (var name in attributes) {
-			attributesSerialized += name + '="' + (attributes[name] || '') + '"';
+		for (var name in env.attributes) {
+			attributesSerialized += name + '="' + (env.attributes[name] || '') + '"';
 		}
 		
-		return '<' + tag + ' class="' + classes.join(' ') + '" ' + attributesSerialized + '>' + content + '</' + tag + '>';
+		return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attributesSerialized + '>' + env.content + '</' + env.tag + '>';
+	},
+	
+	hooks: {
+		all: {},
+		
+		add: function (name, callback) {
+			var hooks = _.hooks.all;
+			
+			hooks[name] = hooks[name] || [];
+			
+			hooks[name].push(callback);
+		},
+		
+		run: function (name, env) {
+			var callbacks = _.hooks.all[name];
+			
+			if (!callbacks || !callbacks.length) {
+				return;
+			}
+			
+			for (var i=0, callback; callback = callbacks[i++];) {
+				callback(env);
+			}
+		}
 	}
 };
 
diff --git a/components/prism-markup.js b/components/prism-markup.js
index 6f55fd4..a56b797 100644
--- a/components/prism-markup.js
+++ b/components/prism-markup.js
@@ -64,4 +64,12 @@ if (Prism.languages.css) {
 }
 else {
 	delete Prism.languages.markup.style;
-}
\ No newline at end of file
+}
+
+// Plugin to make entity title show the real entity
+Prism.hooks.add('wrap', function(env) {
+
+	if (env.token === 'entity') {
+		env.attributes['title'] = env.content.replace(/&amp;/, '&');
+	}
+});
\ No newline at end of file
diff --git a/prism.js b/prism.js
index 402f62b..f3d1481 100644
--- a/prism.js
+++ b/prism.js
@@ -145,28 +145,52 @@ var _ = self.Prism = {
 	},
 	
 	wrap: function(token, content) {
-		var tag = 'span',
-		    classes = ['token', token],
-		    attributes = {};
+		var env = {
+			token: token,
+			content: content
+		};
 		
-		if (token === 'comment') {
-			attributes['spellcheck'] = 'true';
-		}
+		env.tag = 'span';
+		env.classes = ['token', token];
+		env.attributes = {};
 		
-		// Example plugin - Will be removed
-		if (token === 'entity') {
-			attributes['title'] = content.replace(/&amp;/, '&');
+		if (token === 'comment') {
+			env.attributes['spellcheck'] = 'true';
 		}
 		
-		// TODO Add hook here
+		_.hooks.run('wrap', env);
 		
 		var attributesSerialized = '';
 		
-		for (var name in attributes) {
-			attributesSerialized += name + '="' + (attributes[name] || '') + '"';
+		for (var name in env.attributes) {
+			attributesSerialized += name + '="' + (env.attributes[name] || '') + '"';
 		}
 		
-		return '<' + tag + ' class="' + classes.join(' ') + '" ' + attributesSerialized + '>' + content + '</' + tag + '>';
+		return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attributesSerialized + '>' + env.content + '</' + env.tag + '>';
+	},
+	
+	hooks: {
+		all: {},
+		
+		add: function (name, callback) {
+			var hooks = _.hooks.all;
+			
+			hooks[name] = hooks[name] || [];
+			
+			hooks[name].push(callback);
+		},
+		
+		run: function (name, env) {
+			var callbacks = _.hooks.all[name];
+			
+			if (!callbacks || !callbacks.length) {
+				return;
+			}
+			
+			for (var i=0, callback; callback = callbacks[i++];) {
+				callback(env);
+			}
+		}
 	}
 };
 
@@ -304,4 +328,12 @@ if (Prism.languages.css) {
 }
 else {
 	delete Prism.languages.markup.style;
-}
\ No newline at end of file
+}
+
+// Plugin to make entity title show the real entity
+Prism.hooks.add('wrap', function(env) {
+
+	if (env.token === 'entity') {
+		env.attributes['title'] = env.content.replace(/&amp;/, '&');
+	}
+});
\ No newline at end of file