Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

hello world

  • Loading branch information...
commit ba98ed8b95cd4f965f08eaaea56162755d41f7ea 0 parents
Larry Ng authored

Showing 4 changed files with 280 additions and 0 deletions. Show diff stats Hide diff stats

  1. +13 0 LICENSE
  2. +83 0 index.html
  3. +8 0 js/coffee-script.js
  4. +176 0 js/main.js
13 LICENSE
... ... @@ -0,0 +1,13 @@
  1 +Copyright (c) 2012, Larry Ng <ng.larry@gmail.com>
  2 +
  3 +Permission to use, copy, modify, and/or distribute this software for any
  4 +purpose with or without fee is hereby granted, provided that the above
  5 +copyright notice and this permission notice appear in all copies.
  6 +
  7 +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  8 +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9 +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  10 +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11 +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  12 +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13 +PERFORMANCE OF THIS SOFTWARE.
83 index.html
... ... @@ -0,0 +1,83 @@
  1 +<!DOCTYPE html>
  2 +<html>
  3 + <title>In-browser CoffeeScript REPL</title>
  4 + <head>
  5 + <style>
  6 +
  7 + pre {
  8 + margin: 0;
  9 + padding: 0;
  10 + }
  11 +
  12 + body {
  13 + background-color: #222;
  14 + color: #fff;
  15 + font-size: 1em;
  16 + }
  17 +
  18 + #container {
  19 + width: 100%;
  20 + height: 100%;
  21 + font-family: monospace;
  22 + }
  23 +
  24 + #output {
  25 + word-wrap: break-word;
  26 + }
  27 +
  28 + #inputdiv {
  29 + width: 100%;
  30 + }
  31 +
  32 + #inputl {
  33 + display: inline;
  34 + }
  35 +
  36 + #inputr {
  37 + display: inline;
  38 + }
  39 +
  40 + #input {
  41 + border: none;
  42 + outline: none;
  43 + background-color: #222;
  44 + color: #fff;
  45 + font-family: monospace;
  46 + margin: 0;
  47 + padding: 0;
  48 + font-size: 1em;
  49 + resize: none;
  50 + position: absolute;
  51 + overflow: hidden;
  52 + }
  53 +
  54 + #inputcopy {
  55 + display: none;
  56 + white-space: pre-wrap;
  57 + min-height: 2em;
  58 + padding: 0;
  59 + margin: 0;
  60 + word-wrap: break-word;
  61 + }
  62 +
  63 + </style>
  64 + </head>
  65 + <body>
  66 + <div id="container">
  67 + <div id="output"></div>
  68 + <div id="inputdiv">
  69 + <div id="inputl">
  70 + <code id="prompt">coffee&gt;&nbsp;</code>
  71 + </div>
  72 + <div id="inputr">
  73 + <textarea id="input"></textarea>
  74 + <div id="inputcopy"></div>
  75 + </div>
  76 + </div>
  77 + </div>
  78 +
  79 + <script src="js/coffee-script.js"></script>
  80 + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  81 + <script src="js/main.js"></script>
  82 + </body>
  83 +</html>
8 js/coffee-script.js
8 additions, 0 deletions not shown
176 js/main.js
... ... @@ -0,0 +1,176 @@
  1 +/*
  2 + * In-browser CoffeeScript REPL
  3 + *
  4 + * written by Larry Ng
  5 + */
  6 +
  7 +$(function () {
  8 +
  9 + var $output = $('#output');
  10 + var $input = $('#input');
  11 + var $prompt = $('#prompt');
  12 + var $inputdiv = $('#inputdiv');
  13 + var $inputl = $('#inputl');
  14 + var $inputr = $('#inputr');
  15 + var $inputcopy = $('#inputcopy');
  16 +
  17 + var HISTORY = [];
  18 + var HISTORY_I = -1;
  19 + var SAVED = "";
  20 + var MULTILINE = false;
  21 +
  22 + var print = function (s) {
  23 + s = s || " ";
  24 + $output[0].innerHTML += '<pre>' + s + '</pre>';
  25 + }
  26 +
  27 + var grabInput = function () {
  28 + var tmp = $input.val();
  29 + $input.val('');
  30 + return tmp;
  31 + }
  32 +
  33 + var processSaved = function () {
  34 + try {
  35 + compiled = CoffeeScript.compile(SAVED);
  36 + compiled = compiled.slice(14, compiled.length-17);
  37 + value = eval.call(window, compiled);
  38 + window['$_'] = value;
  39 + output = String(value);
  40 + } catch (e) {
  41 + if (e.stack) {
  42 + output = e.stack;
  43 +
  44 + // FF doesn't have Error.toString() as the first line of Error.stack
  45 + // while Chrome does.
  46 + if (output.split('\n')[0] !== e.toString()) {
  47 + output = e.toString() + '\n' + e.stack;
  48 + }
  49 + } else {
  50 + output = e.toString();
  51 + }
  52 + }
  53 + SAVED = "";
  54 + print(output);
  55 + }
  56 +
  57 + var setPrompt = function (multi) {
  58 + var s = multi ? "------" : "coffee";
  59 + $prompt.html(s + '&gt;&nbsp;');
  60 + }
  61 +
  62 + var addToSaved = function (s) {
  63 + if (s[s.length-1] === '\\') {
  64 + SAVED += s.substring(0, s.length-1);
  65 + } else {
  66 + SAVED += s;
  67 + }
  68 + addToHistory(s);
  69 + SAVED += '\n';
  70 + }
  71 +
  72 + var addToHistory = function (s) {
  73 + HISTORY.unshift(s);
  74 + HISTORY_I = -1;
  75 + }
  76 +
  77 + var resizeInput = function (e) {
  78 + // using the invisible div trick
  79 + // see: http://www.impressivewebs.com/textarea-auto-resize/
  80 +
  81 + var width = $inputdiv.width() - $inputl.width();
  82 + var content = $input.val();
  83 + content.replace(/\n/g, '<br/>');
  84 + $inputcopy.html(content);
  85 +
  86 + $inputcopy.width(width);
  87 + $input.width(width);
  88 + $input.height($inputcopy.height());
  89 + }
  90 +
  91 + var scrollToBottom = function () {
  92 + window.scrollTo(0, $prompt[0].offsetTop);
  93 + }
  94 +
  95 + var inputKeypress = function (e) {
  96 + var compiled;
  97 + var input;
  98 + var value;
  99 + var output;
  100 +
  101 + if (e.which === 13) {
  102 + e.preventDefault();
  103 + input = grabInput();
  104 +
  105 + print($prompt.html() + input);
  106 +
  107 + if (input) {
  108 + addToSaved(input);
  109 + if (input[input.length-1] !== '\\' && !MULTILINE) {
  110 + processSaved();
  111 + }
  112 + }
  113 + } else if (e.which === 27) {
  114 + e.preventDefault();
  115 + input = $input.val();
  116 +
  117 + if (input) {
  118 + if (MULTILINE) {
  119 + if (SAVED) {
  120 + input = grabInput();
  121 +
  122 + print($prompt.html() + input);
  123 + addToSaved(input);
  124 + processSaved();
  125 + }
  126 + }
  127 + } else {
  128 + if (MULTILINE) {
  129 + if (SAVED) {
  130 + processSaved();
  131 + }
  132 + }
  133 + }
  134 +
  135 + MULTILINE = !MULTILINE;
  136 + setPrompt(MULTILINE);
  137 + } else if (e.which === 38) {
  138 + e.preventDefault();
  139 +
  140 + if (HISTORY_I < HISTORY.length-1) {
  141 + HISTORY_I += 1;
  142 + $input.val(HISTORY[HISTORY_I]);
  143 + }
  144 + } else if (e.which === 40) {
  145 + e.preventDefault();
  146 +
  147 + if (HISTORY_I > 0) {
  148 + HISTORY_I += -1;
  149 + $input.val(HISTORY[HISTORY_I]);
  150 + }
  151 + }
  152 +
  153 + scrollToBottom();
  154 + };
  155 +
  156 + var printKeyPressed = function (e) {
  157 + print(e.which);
  158 + }
  159 +
  160 + // bind handlers
  161 + $input.keydown(inputKeypress);
  162 + $(window).resize(resizeInput);
  163 + $input.keyup(resizeInput);
  164 + $input.change(resizeInput);
  165 +
  166 + // initialize
  167 + resizeInput();
  168 + $input.focus();
  169 +
  170 + // header
  171 + print("// CoffeeScript v" + CoffeeScript.VERSION + " REPL")
  172 + print("//");
  173 + print("// Press Esc to toggle multiline mode.");
  174 + print();
  175 +
  176 +})

0 comments on commit ba98ed8

Please sign in to comment.
Something went wrong with that request. Please try again.