From ec0d419c619709b37a2563ac6454e94b41872fc6 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 22 Nov 2011 20:29:46 -0500 Subject: [PATCH] Allow forcing of highlight type --- TODO | 2 +- lib/document_handler.js | 2 +- static/application.js | 25 ++++++++++++++++++++----- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/TODO b/TODO index 36b001b..9580ddb 100644 --- a/TODO +++ b/TODO @@ -3,7 +3,7 @@ tests add feedback for errors to UI - esp. too long fix that chrome bug where it loads the doc twice add link to about page -allow power users to force highlight lang - by supplying an extension!!!!!! +expand extension map maybe start serving highlighting on the other end to save transfer on highlight.js start using CDNs for most assets kick expiration back by increment on each view diff --git a/lib/document_handler.js b/lib/document_handler.js index ee78c50..8b7a18c 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -80,7 +80,7 @@ DocumentHandler.prototype.chooseKey = function(callback) { // Return a boolean indicating whether or not something can be a key DocumentHandler.potentialKey = function(key) { - return key.match(/^[a-zA-Z0-9]+$/); + return key.match(/^[a-zA-Z0-9]+(\.[a-zA-Z]+?)$/); }; // Generate a random key diff --git a/static/application.js b/static/application.js index 02c8dd3..393371c 100644 --- a/static/application.js +++ b/static/application.js @@ -5,7 +5,7 @@ var haste_document = function() { }; // Get this document from the server and lock it here -haste_document.prototype.load = function(key, callback) { +haste_document.prototype.load = function(key, callback, lang) { var _this = this; $.ajax('/documents/' + key, { type: 'get', @@ -14,11 +14,11 @@ haste_document.prototype.load = function(key, callback) { _this.locked = true; _this.key = key; _this.data = res.data; - var high = hljs.highlightAuto(res.data); + var high = lang ? hljs.highlight(lang, res.data) : hljs.highlightAuto(res.data); callback({ value: high.value, key: key, - language: high.language + language: lang || high.language }); }, error: function(err) { @@ -105,11 +105,26 @@ haste.prototype.newDocument = function(hideHistory) { }); }; +// Map of common extensions +haste.extensionMap = { + 'rb': 'ruby', + 'py': 'python' +}; + +// Map an extension to a language +haste.prototype.lookupExtension = function(ext) { + var match = haste.extensionMap[ext]; + return match; // if not found, will auto-detect +}; + // Load a document and show it haste.prototype.loadDocument = function(key) { + // Split the key up + var parts = key.split('.', 2); + // Ask for what we want var _this = this; _this.doc = new haste_document(); - _this.doc.load(key, function(ret) { + _this.doc.load(parts[0], function(ret) { if (ret) { _this.$code.html(ret.value); var title = ret.key; @@ -124,7 +139,7 @@ haste.prototype.loadDocument = function(key) { else { _this.newDocument(); } - }); + }, this.lookupExtension(parts[1])); }; // Duplicate the current document - only if locked