Skip to content

Commit

Permalink
Closes #47. Complete SpacePen preview rendering support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Aug 11, 2014
1 parent 9b4f722 commit c4ad78e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Or Settings/Preferences ➔ Packages ➔ Search for `preview`
- [x] Shows loading and error messages
- [x] [Updates on Tab Change](https://github.com/Glavin001/atom-coffeescript-preview/issues/3)
- [x] [Highlights using active Atom theme](https://github.com/Glavin001/atom-coffeescript-preview/issues/5)
- [x] [Users can manually change the default preview renderer](https://github.com/Glavin001/atom-preview/issues/22)
- [x] [SpacePen Views can live preview!](https://github.com/Glavin001/atom-preview/issues/47)

## Supported Languages

Expand Down
1 change: 0 additions & 1 deletion lib/options-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class OptionsView extends View
@div
class: "panel-body padded"
=>
@div "HAWT BODYYYY"
@button
class: 'btn btn-primary inline-block-tight'
click: 'selectRenderer'
Expand Down
67 changes: 30 additions & 37 deletions lib/renderer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ _ = require 'underscore-plus'
rCache = {}

module.exports =
findByGrammar: (grammar) ->
@grammars[grammar]
findAllByExtention: (extension) ->
allRenderers: ->
gs = []
# Map each renderer into an array
for r of @grammars
gs.push(@grammars[r])
return gs
findByGrammar: (grammar) ->
@grammars[grammar]
findAllByExtention: (extension) ->
gs = @allRenderers()
# Filter renderers
_.filter(gs, (renderer) ->
exts = renderer.exts
Expand Down Expand Up @@ -157,51 +160,41 @@ module.exports =
'SpacePen':
render: (text, filepath, cb) ->
try
console.log "File Path:", filepath
extension = path.extname(filepath)
temp.open {suffix: extension}, (err, info) ->
# Get a filename in the current directory that is unique
generateFilepath = (filepath, cb) ->
extension = path.extname(filepath)
cd = path.dirname(filepath)
newFilename = "preview-temp-file-#{+new Date()}#{extension}"
newFilepath = path.resolve cd, newFilename
return cb(null, newFilepath)
generateFilepath(filepath, (err, fp) ->
# console.log fp
if err?
return cb(err, null)
fs.write info.fd, text or "", (err) ->
# Write to file
fs.writeFile fp, text or "", (err) ->
if err?
return cb(err, null)
fs.close info.fd, (err) ->
if err?
return cb(err, null)
# Get the View class module
console.log info.path
# Patch the NODE_PATH
cd = path.dirname(filepath)
nodePath = process.env.NODE_PATH
deli = ":"
newNodePath = "#{nodePath}#{deli}#{cd}"
console.log newNodePath
process.env.NODE_PATH = newNodePath
module.paths.push cd
console.log module.paths
mFilename = module.filename
module.filename = cd
require('module').Module._initPaths();
View = null
try
View = require(info.path) # Get the View module
catch e
# Revert NODE_PATH
process.env.NODE_PATH = nodePath
module.filename = mFilename
require('module').Module._initPaths();
return cb(e, null)
# Revert NODE_PATH
process.env.NODE_PATH = nodePath
module.filename = mFilename
require('module').Module._initPaths();
# Get the View class module
try
View = require(fp) # Get the View module
view = new View() # Create new View
# Check if it is an instance of a Space-pen View
if view instanceof $
# Is Space-pen view
cb(null, view)
else
cb(new Error("Is not a SpacePen View"), null)
# Delete file
fs.unlink fp
return
catch e
# return error
cb(e, null)
# Delete file
fs.unlink fp
return
)
catch e
return cb(e, null)
exts: /^.*\.(coffee|js)$/
7 changes: 4 additions & 3 deletions lib/select-renderer-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ class SelectRendererView extends SelectListView
"<li>#{item}</li>"

confirmed: (item) ->
console.log("#{item} was selected")
# console.log("#{item} was selected")
@previewView.renderPreviewWithRenderer item
# Close
@detach()

attach: =>
@previewView.editorContents.append @
@previewView.hideMessage()
# @previewView.editorContents.append @
# @previewView.hideMessage()
atom.workspaceView.appendToTop @
toggle: =>
if @hasParent()
@detach()
Expand Down
1 change: 0 additions & 1 deletion menus/context.cson
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
'context-menu':
'.workspace .editor:not(.mini)':
'Toggle Preview': 'preview:toggle'
'.workspace .editor:not(.mini).preview-container':
'Toggle Preview Options': 'preview:toggle-options'
'Select Preview Renderer': 'preview:select-renderer'
68 changes: 68 additions & 0 deletions spec/samples/spacepen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var DeprecationView, View,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

View = require("atom").View;

module.exports = DeprecationView = (function(_super) {
__extends(DeprecationView, _super);

function DeprecationView() {
this.close = __bind(this.close, this);
return DeprecationView.__super__.constructor.apply(this, arguments);
}

DeprecationView.content = function() {
return this.div({
"class": 'coffeescript-preview deprecation-notice'
}, (function(_this) {
return function() {
return _this.div({
"class": 'overlay from-top'
}, function() {
return _this.div({
"class": "tool-panel panel-bottom"
}, function() {
return _this.div({
"class": "inset-panel"
}, function() {
_this.div({
"class": "panel-heading"
}, function() {
_this.div({
"class": 'btn-toolbar pull-right'
}, function() {
return _this.button({
"class": 'btn',
click: 'close'
}, 'Close');
});
return _this.span({
"class": 'text-error'
}, 'IMPORTANT: CoffeeScript Preview has been Depttrecated!');
});
return _this.div({
"class": "panel-body padded"
}, function() {
_this.span({
"class": 'text-warning'
}, 'CoffeeScript Preview has been deprecated. Please migrate to the Preview package for Atom. ');
return _this.a({
href: 'https://github.com/Glavin001/atom-preview'
}, "Click here to see the Preview package for Atom");
});
});
});
});
};
})(this));
};

DeprecationView.prototype.close = function(event, element) {
return this.detach();
};

return DeprecationView;

})(View);

0 comments on commit c4ad78e

Please sign in to comment.