Skip to content

Commit

Permalink
Closes #2. Implement LESS support.
Browse files Browse the repository at this point in the history
Closes #11. Language renderers are now async.

See #5. Started work on TypeScript support.
  • Loading branch information
Glavin001 committed Jul 23, 2014
1 parent 7dbf5ac commit e995ab8
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
80 changes: 39 additions & 41 deletions lib/preview-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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 = $('<pre/>')
@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
Expand All @@ -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 = $('<pre/>')
@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'
Expand Down
21 changes: 17 additions & 4 deletions lib/renderer.coffee
Original file line number Diff line number Diff line change
@@ -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'
79 changes: 49 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"url": "https://github.com/Glavin001"
},
"contributors": [
{
"name": "ddavison",
"email": "[email protected]",
"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"
]
}
28 changes: 28 additions & 0 deletions spec/samples/simple_inheritance.ts
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 14 additions & 0 deletions spec/samples/test.less
Original file line number Diff line number Diff line change
@@ -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%) }
}

0 comments on commit e995ab8

Please sign in to comment.