diff --git a/README.md b/README.md index 755ef98..59c2362 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Or Settings/Preferences ➔ Packages ➔ Search for `preview` ## Supported Languages - [x] [CoffeeScript](https://github.com/Glavin001/atom-preview/issues/1) -- [ ] [LESS](https://github.com/Glavin001/atom-preview/issues/2) +- [x] [LESS](https://github.com/Glavin001/atom-preview/issues/2) - [ ] [SASS](https://github.com/Glavin001/atom-preview/issues/3) - [ ] [TypeScript](https://github.com/Glavin001/atom-preview/issues/5) - [ ] [Markdown](https://github.com/Glavin001/atom-preview/issues/7) diff --git a/lib/preview-view.coffee b/lib/preview-view.coffee index 69c6dbe..8ccb5f6 100644 --- a/lib/preview-view.coffee +++ b/lib/preview-view.coffee @@ -82,19 +82,15 @@ class PreviewView extends ScrollView if updateOnTabChange currEditor = atom.workspace.getActiveEditor() if currEditor? - lang = currEditor.getGrammar().name - renderer = renderers[lang] - # Check if supported - if renderer? - # Stop watching for events on current Editor - @unsubscribe() - # Switch to new editor - @editor = currEditor - @editorId = @editor.id - # Start watching editors on new editor - @handleEvents() - # Trigger update - @changeHandler() + # Stop watching for events on current Editor + @unsubscribe() + # Switch to new editor + @editor = currEditor + @editorId = @editor.id + # Start watching editors on new editor + @handleEvents() + # Trigger update + @changeHandler() handleEvents: () -> if @editor? @@ -123,6 +119,32 @@ class PreviewView extends ScrollView @showLoading() # Update Title @trigger 'title-changed' + # Create Callback + callback = (error, result) => + outLang = renderer.lang() + grammar = atom.syntax.selectGrammar("source.#{outLang}", result) + # Get codeBlock + codeBlock = @codeBlock.find('pre') + if codeBlock.length is 0 + codeBlock = $('
')
+        @codeBlock.append(codeBlock)
+      # Reset codeBlock
+      codeBlock.empty()
+      codeBlock.addClass('editor-colors')
+      # Render the JavaScript as HTML with syntax Highlighting
+      htmlEolInvisibles = ''
+      for tokens in grammar.tokenizeLines(result).slice(0, -1)
+        lineText = _.pluck(tokens, 'value').join('')
+        codeBlock.append \
+        EditorView.buildLineHtml {tokens, text: lineText, htmlEolInvisibles}
+      # Clear message display
+      @message.empty()
+      # Display the new rendered HTML
+      @trigger 'preview:html-changed'
+      # Set font-size from Editor to the Preview
+      fontSize = atom.config.get('editor.fontSize')
+      if fontSize?
+        codeBlock.css('font-size', fontSize)
     # Start preview processing
     text = @editor.getText()
     try
@@ -131,37 +153,13 @@ class PreviewView extends ScrollView
       if not text?
         return @showError new Error "Nothing to render."
       if renderer?
-        result = renderer.render text
+        renderer.render text, callback
       else
-        return @showError new Error \
-        "Can not find renderer for grammar #{grammar}."
+        return @showError(new Error \
+        "Can not find renderer for grammar #{grammar}.")
     catch e
       return @showError e
-    outLang = renderer.lang()
-
-    grammar = atom.syntax.selectGrammar("source.#{outLang}", result)
-    # Get codeBlock
-    codeBlock = @codeBlock.find('pre')
-    if codeBlock.length is 0
-      codeBlock = $('
')
-      @codeBlock.append(codeBlock)
-    # Reset codeBlock
-    codeBlock.empty()
-    codeBlock.addClass('editor-colors')
-    # Render the JavaScript as HTML with syntax Highlighting
-    htmlEolInvisibles = ''
-    for tokens in grammar.tokenizeLines(result).slice(0, -1)
-      lineText = _.pluck(tokens, 'value').join('')
-      codeBlock.append \
-      EditorView.buildLineHtml {tokens, text: lineText, htmlEolInvisibles}
-    # Clear message display
-    @message.empty()
-    # Display the new rendered HTML
-    @trigger 'preview:html-changed'
-    # Set font-size from Editor to the Preview
-    fontSize = atom.config.get('editor.fontSize')
-    if fontSize?
-      codeBlock.css('font-size', fontSize)
+
 
   syncScroll: ->
     console.log 'Sync scroll'
diff --git a/lib/renderer.coffee b/lib/renderer.coffee
index fd51d7e..3ba4785 100644
--- a/lib/renderer.coffee
+++ b/lib/renderer.coffee
@@ -1,11 +1,24 @@
 module.exports =
   'CoffeeScript':
-    render: (text) ->
+    render: (text, cb) ->
       coffeescript = require 'coffee-script'
-      coffeescript.compile text
+      result = coffeescript.compile text
+      cb null, result
     lang: -> 'js'
   'CoffeeScript (Literate)':
-    render: (text) ->
+    render: (text, cb) ->
       coffeescript = require 'coffee-script'
-      coffeescript.compile text
+      result = coffeescript.compile text
+      cb null, result
     lang: -> 'js'
+  #'TypeScript':
+  #  render: (text) ->
+  #    typescript = require 'typescript'
+  #    # FIXME: Add support for compiling TypeScript
+  #  lang: -> 'js'
+  'LESS':
+    render: (text, cb) ->
+      less = require 'less'
+      less.render text, (e, css) ->
+        cb e, css
+    lang: -> 'css'
diff --git a/package.json b/package.json
index 0cd6db9..817cb46 100644
--- a/package.json
+++ b/package.json
@@ -1,33 +1,52 @@
 {
-    "name": "preview",
-    "main": "lib/preview",
-    "version": "0.0.0",
-    "description": "Ultimate previewer of source code in Atom.",
-    "license": "MIT",
-    "private": true,
-    "repository": {
-        "type": "git",
-        "url": "https://github.com/Glavin001/atom-preview.git"
-    },
-    "homepage": "https://github.com/Glavin001/atom-preview",
-    "bugs": {
-        "url": "https://github.com/Glavin001/atom-preview/issues"
-    },
-    "engines": {
-        "atom": ">0.50.0"
-    },
-    "scripts": {
-        "test": "grunt"
-    },
-    "dependencies": {
-        "underscore-plus": "^1.5.0",
-        "coffee-script": "^1.7.1"
-    },
-    "devDependencies": {
-        "grunt": "~0.4.2",
-        "grunt-coffeelint": "0.0.8",
-        "grunt-contrib-watch": "~0.5.3",
-        "grunt-lesslint": "~1.0.0",
-        "grunt-apm": "0.0.1"
+  "name": "preview",
+  "main": "lib/preview",
+  "version": "0.0.0",
+  "description": "Ultimate previewer of source code in Atom.",
+  "license": "MIT",
+  "private": true,
+  "author": {
+    "name": "Glavin Wiechert",
+    "email": "glavin.wiechert@gmail.com",
+    "url": "https://github.com/Glavin001"
+  },
+  "contributors": [
+    {
+      "name": "ddavison",
+      "email": "sircapsalot@gmail.com",
+      "url": "https://github.com/ddavison"
     }
+  ],
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/Glavin001/atom-preview.git"
+  },
+  "homepage": "https://github.com/Glavin001/atom-preview",
+  "bugs": {
+    "url": "https://github.com/Glavin001/atom-preview/issues"
+  },
+  "engines": {
+    "atom": ">0.50.0"
+  },
+  "scripts": {
+    "test": "grunt"
+  },
+  "dependencies": {
+    "underscore-plus": "^1.5.0",
+    "coffee-script": "^1.7.1",
+    "typescript": "^1.0.1",
+    "less": "^1.7.3"
+  },
+  "devDependencies": {
+    "grunt": "~0.4.2",
+    "grunt-coffeelint": "0.0.8",
+    "grunt-contrib-watch": "~0.5.3",
+    "grunt-lesslint": "~1.0.0",
+    "grunt-apm": "0.0.1"
+  },
+  "keywords": [
+    "preview",
+    "coffeescript",
+    "less"
+  ]
 }
diff --git a/spec/samples/simple_inheritance.ts b/spec/samples/simple_inheritance.ts
new file mode 100644
index 0000000..25b236c
--- /dev/null
+++ b/spec/samples/simple_inheritance.ts
@@ -0,0 +1,28 @@
+class Animal {
+    constructor(public name: string) { }
+    move(meters: number) {
+        alert(this.name + " moved " + meters + "m.");
+    }
+}
+
+class Snake extends Animal {
+    constructor(name: string) { super(name); }
+    move() {
+        alert("Slithering...");
+        super.move(5);
+    }
+}
+
+class Horse extends Animal {
+    constructor(name: string) { super(name); }
+    move() {
+        alert("Galloping...");
+        super.move(45);
+    }
+}
+
+var sam = new Snake("Sammy the Python");
+var tom: Animal = new Horse("Tommy the Palomino");
+
+sam.move();
+tom.move(34);
diff --git a/spec/samples/test.less b/spec/samples/test.less
new file mode 100644
index 0000000..564f8f4
--- /dev/null
+++ b/spec/samples/test.less
@@ -0,0 +1,14 @@
+@base: #f938ab;
+
+.box-shadow(@style, @c) when (iscolor(@c)) {
+  -webkit-box-shadow: @style @c;
+  box-shadow:         @style @c;
+}
+.box-shadow(@style, @alpha: 50%) when (isnumber(@alpha)) {
+  .box-shadow(@style, rgba(0, 0, 0, @alpha));
+}
+.box {
+  color: saturate(@base, 5%);
+  border-color: lighten(@base, 30%);
+  div { .box-shadow(0 0 5px, 30%) }
+}