From cd89e874ecb6cdaf30965d64322f4f111529e99d Mon Sep 17 00:00:00 2001 From: James Brooks Date: Fri, 28 Jun 2013 14:23:32 +0100 Subject: [PATCH] Changed JS language code. Created demo. Started making changes --- codepress.js | 69 +++++++++++++++++------------------ demo.html | 80 +++++++++++++++++++++++++++++++++++++++++ languages/javascript.js | 53 +++++++++++++-------------- 3 files changed, 142 insertions(+), 60 deletions(-) create mode 100644 demo.html diff --git a/codepress.js b/codepress.js index b3f563a..3b1e141 100644 --- a/codepress.js +++ b/codepress.js @@ -9,7 +9,9 @@ * Read the full licence: http://www.opensource.org/licenses/lgpl-license.php */ -CodePress = function(obj) { +var CodePress = function(obj) { + "use strict"; + var self = document.createElement('iframe'); self.textarea = obj; self.textarea.disabled = true; @@ -24,7 +26,8 @@ CodePress = function(obj) { self.options = self.textarea.className; self.initialize = function() { - self.editor = self.contentWindow.CodePress; + // self.editor = self.contentWindow.CodePress; + self.editor = self.contentWindow; self.editor.body = self.contentWindow.document.getElementsByTagName('body')[0]; self.editor.setCode(self.textarea.value); self.setOptions(); @@ -33,7 +36,7 @@ CodePress = function(obj) { self.style.position = 'static'; self.style.visibility = 'visible'; self.style.display = 'inline'; - } + }; // obj can by a textarea id or a string (code) self.edit = function(obj,language) { @@ -43,42 +46,42 @@ CodePress = function(obj) { self.src = CodePress.path+'codepress.html?language='+self.language+'&ts='+(new Date).getTime(); if(self.attachEvent) self.attachEvent('onload',self.initialize); else self.addEventListener('load',self.initialize,false); - } + }; self.getLanguage = function() { - for (language in CodePress.languages) + for (var language in CodePress.languages) if(self.options.match('\\b'+language+'\\b')) return CodePress.languages[language] ? language : 'generic'; - } + }; self.setOptions = function() { if(self.options.match('autocomplete-off')) self.toggleAutoComplete(); if(self.options.match('readonly-on')) self.toggleReadOnly(); if(self.options.match('linenumbers-off')) self.toggleLineNumbers(); - } + }; self.getCode = function() { return self.textarea.disabled ? self.editor.getCode() : self.textarea.value; - } + }; - self.setCode = function(code) { + self.editor.setCode = function(code) { self.textarea.disabled ? self.editor.setCode(code) : self.textarea.value = code; - } + }; self.toggleAutoComplete = function() { self.editor.autocomplete = (self.editor.autocomplete) ? false : true; - } + }; self.toggleReadOnly = function() { self.textarea.readOnly = (self.textarea.readOnly) ? false : true; if(self.style.display != 'none') // prevent exception on FF + iframe with display:none self.editor.readOnly(self.textarea.readOnly ? true : false); - } + }; self.toggleLineNumbers = function() { var cn = self.editor.body.className; self.editor.body.className = (cn==''||cn=='show-line-numbers') ? 'hide-line-numbers' : 'show-line-numbers'; - } + }; self.toggleEditor = function() { if(self.textarea.disabled) { @@ -86,8 +89,7 @@ CodePress = function(obj) { self.textarea.disabled = false; self.style.display = 'none'; self.textarea.style.display = 'inline'; - } - else { + } else { self.textarea.disabled = true; self.setCode(self.textarea.value); self.editor.syntaxHighlight('init'); @@ -98,26 +100,25 @@ CodePress = function(obj) { self.edit(); return self; -} +}; CodePress.languages = { - csharp : 'C#', - css : 'CSS', - generic : 'Generic', - html : 'HTML', - java : 'Java', - javascript : 'JavaScript', - perl : 'Perl', - ruby : 'Ruby', - php : 'PHP', - text : 'Text', - sql : 'SQL', - vbscript : 'VBScript' -} - + csharp: 'C#', + css: 'CSS', + generic: 'Generic', + html: 'HTML', + java: 'Java', + javascript: 'JavaScript', + perl: 'Perl', + ruby: 'Ruby', + php: 'PHP', + text: 'Text', + sql: 'SQL', + vbscript: 'VBScript' +}; CodePress.run = function() { - s = document.getElementsByTagName('script'); + var s = document.getElementsByTagName('script'); for(var i=0,n=s.length;i + + + CodePress - Real Time Syntax Highlighting Editor written in JavaScript + + + + + +
+ + + +

Demo

+ + + +

+ + + + + +

+
+ + + + diff --git a/languages/javascript.js b/languages/javascript.js index 66d0fc0..8b2b7c0 100644 --- a/languages/javascript.js +++ b/languages/javascript.js @@ -1,30 +1,31 @@ /* * CodePress regular expressions for JavaScript syntax highlighting */ - -// JavaScript -Language.syntax = [ - { input : /\"(.*?)(\"|
|<\/P>)/g, output : '"$1$2' }, // strings double quote - { input : /\'(.*?)(\'|
|<\/P>)/g, output : '\'$1$2' }, // strings single quote - { input : /\b(break|continue|do|for|new|this|void|case|default|else|function|return|typeof|while|if|label|switch|var|with|catch|boolean|int|try|false|throws|null|true|goto)\b/g, output : '$1' }, // reserved words - { input : /\b(alert|isNaN|parent|Array|parseFloat|parseInt|blur|clearTimeout|prompt|prototype|close|confirm|length|Date|location|Math|document|element|name|self|elements|setTimeout|navigator|status|String|escape|Number|submit|eval|Object|event|onblur|focus|onerror|onfocus|onclick|top|onload|toString|onunload|unescape|open|valueOf|window|onmouseover)\b/g, output : '$1' }, // special words - { input : /([^:]|^)\/\/(.*?)(//$2$3' }, // comments // - { input : /\/\*(.*?)\*\//g, output : '/*$1*/' } // comments /* */ -] - -Language.snippets = [ - { input : 'dw', output : 'document.write(\'$0\');' }, - { input : 'getid', output : 'document.getElementById(\'$0\')' }, - { input : 'fun', output : 'function $0(){\n\t\n}' }, - { input : 'func', output : 'function $0(){\n\t\n}' } -] -Language.complete = [ - { input : '\'',output : '\'$0\'' }, - { input : '"', output : '"$0"' }, - { input : '(', output : '\($0\)' }, - { input : '[', output : '\[$0\]' }, - { input : '{', output : '{\n\t$0\n}' } -] - -Language.shortcuts = [] +// JavaScript +var Language = (function() { + return { + syntax: [ + { input : /\"(.*?)(\"|
|<\/P>)/g, output : '"$1$2' }, // strings double quote + { input : /\'(.*?)(\'|
|<\/P>)/g, output : '\'$1$2' }, // strings single quote + { input : /\b(break|continue|do|for|new|this|void|case|default|else|function|return|typeof|while|if|label|switch|var|with|catch|boolean|int|try|false|throws|null|true|goto)\b/g, output : '$1' }, // reserved words + { input : /\b(alert|isNaN|parent|Array|parseFloat|parseInt|blur|clearTimeout|prompt|prototype|close|confirm|length|Date|location|Math|document|element|name|self|elements|setTimeout|navigator|status|String|escape|Number|submit|eval|Object|event|onblur|focus|onerror|onfocus|onclick|top|onload|toString|onunload|unescape|open|valueOf|window|onmouseover)\b/g, output : '$1' }, // special words + { input : /([^:]|^)\/\/(.*?)(//$2$3' }, // comments // + { input : /\/\*(.*?)\*\//g, output : '/*$1*/' } // comments /* */ + ], + snippets: [ + { input : 'dw', output : 'document.write(\'$0\');' }, + { input : 'getid', output : 'document.getElementById(\'$0\')' }, + { input : 'fun', output : 'function $0(){\n\t\n}' }, + { input : 'func', output : 'function $0(){\n\t\n}' } + ], + complete: [ + { input : '\'',output : '\'$0\'' }, + { input : '"', output : '"$0"' }, + { input : '(', output : '\($0\)' }, + { input : '[', output : '\[$0\]' }, + { input : '{', output : '{\n\t$0\n}' } + ], + shortcuts: [] + }; +}()); \ No newline at end of file