Commits

nemunemu committed 514af4b

Implement auto syntax highlight

* Add FiletypeDetectionPolicy. It detects file's filetype on its extname.

Comments (0)

Files changed (5)

app/api/models/file.coffee

 module.exports = ->
   fs = require('fs')
+  path = require('path')
   glass = this
 
   UuidModule = @requireUtil('UuidModule')
+  FiletypeDetectionPolicy = @requireComponent('api.policies.FiletypeDetectionPolicy')
 
   return class @Model.File extends UuidModule
     @findByUuid: (uuid) ->
     @property 'jsonKeys',
       get: -> @getJsonKeys()
 
+    @property 'extension',
+      get: -> path.extname(@path).split('.').pop()
+
+    @property 'filetype',
+      get: -> (new FiletypeDetectionPolicy(this)).filetype()
+

app/api/models/source.coffee

         @_content ||= fs.readFileSync(@path, 'utf8')
 
     toJsonWithContent: ->
-      @toJson(@jsonKeys.concat('content'))
+      @toJson(@jsonKeys.concat('content', 'filetype'))
 

app/api/policies/filetype_detection_policy.coffee

+
+module.exports = ->
+  class @Policies.FiletypeDetectionPolicy
+    constructor: (@file) ->
+
+    filetype: ->
+      switch @file.extension
+        when 'js' then 'javascript'
+        when 'coffee' then 'coffeescript'
+        when 'rb' then 'ruby'
+        when 'htm', 'html' then 'html'
+        else ''
+

app/assets/javascripts/backbone/views/viewer_view.js.coffee

 
   render: ->
     @editor.setValue(@model.content)
+    @editor.getSession().setMode("ace/mode/#{@model.get('filetype')}")
 
   onClick: ->
     return unless @editor

app/config/glass.coffee

     utils: {}
     Model: {}
     Controller: {}
+    Policies: {}
 
     request: (controllerName, method, params, req, res) ->
       controller = new (Glass.requireController(controllerName))()