From 9378f2b2e4be41ecca175ce815466e783ed7a279 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 29 Nov 2011 10:39:12 -0500 Subject: [PATCH] Automatically add file extensions --- TODO.md | 1 - static/application.js | 51 +++++++++++++++++++++++++-------------- static/application.min.js | 2 +- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/TODO.md b/TODO.md index 46a7e2b..77f0551 100644 --- a/TODO.md +++ b/TODO.md @@ -1,2 +1 @@ -* Add file extensions ourselves to push state * add feedback for errors to UI - esp. too long diff --git a/static/application.js b/static/application.js index bfdb862..b6a5da1 100644 --- a/static/application.js +++ b/static/application.js @@ -120,19 +120,31 @@ haste.prototype.newDocument = function(hideHistory) { }; // Map of common extensions +// Note: this list does not need to include anything that IS its extension, +// due to the behavior of lookupTypeByExtension and lookupExtensionByType +// Note: optimized for lookupTypeByExtension haste.extensionMap = { - rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go', - xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript', - lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec', - vala: 'vala', cs: 'cs', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini', - diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell', - md: 'markdown' + rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go', + xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript', + lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec', + vala: 'vala', cs: 'cs', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini', + diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell', + md: 'markdown' }; -// Map an extension to a language -haste.prototype.lookupExtension = function(ext) { - var match = haste.extensionMap[ext]; - return match; // if not found, will auto-detect +// Look up the extension preferred for a type +// If not found, return the type itself - which we'll place as the extension +haste.prototype.lookupExtensionByType = function(type) { + for (var key in haste.extensionMap) { + if (haste.extensionMap[key] === type) return key; + } + return type; +}; + +// Look up the type for a given extension +// If not found, return the extension - which we'll attempt to use as the type +haste.prototype.lookupTypeByExtension = function(ext) { + return haste.extensionMap[ext] || ext; }; // Load a document and show it @@ -145,11 +157,14 @@ haste.prototype.loadDocument = function(key) { _this.doc.load(parts[0], function(ret) { if (ret) { _this.$code.html(ret.value); - var title = ret.key; + _this.setTitle(ret.key); + var file = '/' + ret.key; if (ret.language) { - title += ' - ' + ret.language; + file += '.' + _this.lookupExtensionByType(ret.language); + } + if (window.location.path != file) { + window.history.pushState(null, _this.appName + '-' + ret.key, file); } - _this.setTitle(title); _this.fullKey(); _this.$textarea.val('').hide(); _this.$box.show().focus(); @@ -157,7 +172,7 @@ haste.prototype.loadDocument = function(key) { else { _this.newDocument(); } - }, this.lookupExtension(parts[1])); + }, this.lookupTypeByExtension(parts[1])); }; // Duplicate the current document - only if locked @@ -175,12 +190,12 @@ haste.prototype.lockDocument = function() { this.doc.save(this.$textarea.val(), function(ret) { if (ret) { _this.$code.html(ret.value); - var title = ret.key; + _this.setTitle(ret.key); + var file = '/' + ret.key; if (ret.language) { - title += ' - ' + ret.language; + file += '.' + _this.lookupExtensionByType(ret.language); } - _this.setTitle(title); - window.history.pushState(null, _this.appName + '-' + ret.key, '/' + ret.key); + window.history.pushState(null, _this.appName + '-' + ret.key, file); _this.fullKey(); _this.$textarea.val('').hide(); _this.$box.show().focus(); diff --git a/static/application.min.js b/static/application.min.js index 20a293f..4176001 100644 --- a/static/application.min.js +++ b/static/application.min.js @@ -1 +1 @@ -var haste_document=function(){this.locked=!1};haste_document.prototype.load=function(a,b,c){var d=this;$.ajax("/documents/"+a,{type:"get",dataType:"json",success:function(e){d.locked=!0,d.key=a,d.data=e.data;try{var f=c?hljs.highlight(c,e.data):hljs.highlightAuto(e.data)}catch(g){f=hljs.highlightAuto(e.data)}b({value:f.value,key:a,language:f.language||c})},error:function(a){b(!1)}})},haste_document.prototype.save=function(a,b){if(this.locked)return!1;this.data=a;var c=this;$.ajax("/documents",{type:"post",data:a,dataType:"json",success:function(d){c.locked=!0,c.key=d.key;var e=hljs.highlightAuto(a);b({value:e.value,key:d.key,language:e.language})}})};var haste=function(a,b){this.appName=a,this.baseUrl=window.location.href,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.options=b,this.configureShortcuts(),this.configureButtons(),b.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(a){var b=a?this.appName+" - "+a:this.appName;document.title=b},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","link"])},haste.prototype.configureKey=function(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c