Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 81 lines (60 sloc) 1.952 kB
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
1 {join} = require 'path'
2 {existsSync} = require 'path'
3
28d1b90 @quangv HulkHogan refactored.
authored
4 class HulkHogan
5 parsePartials : (source)-> # Return list of patial filenames in the source.
6 {scan} = require 'hogan.js'
7 result = []
8 scanned = scan source
9 for item in scanned
10 if item.tag is '>'
11 result.push item.n
12 return result
f186fed @quangv Partials on sub-view folders working. #2
authored
13
28d1b90 @quangv HulkHogan refactored.
authored
14
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
15 _readFile : (path)->
28d1b90 @quangv HulkHogan refactored.
authored
16 {readFileSync} = require 'fs'
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
17 return readFileSync(path).toString() # TODO better way to read file? especially large ones.
e4a6886 @quangv Refactored, and ignore missing Partials
authored
18
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
19 getSourcePath : (file)-> # Get full filepath.
20 join @root, file
21
22 getSource : (file)-> # Return source file.
23 filepath = @getSourcePath file
e4a6886 @quangv Refactored, and ignore missing Partials
authored
24
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
25 if existsSync filepath
26 return @_readFile filepath
27 else if @ext and existsSync filepath+=".#{@ext}"
28 return @_readFile filepath
29
30 return ''
31
32 _makePartials : (partials, list)->
28d1b90 @quangv HulkHogan refactored.
authored
33 for file in list
34 sublist = []
35 source = null
c53dc49 @quangv Refactored hulk.coffee
authored
36
28d1b90 @quangv HulkHogan refactored.
authored
37 if file not of partials
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
38 source = @getSource file
edc617e @quangv Sub-Partials (Partials, in partials) support added.
authored
39
28d1b90 @quangv HulkHogan refactored.
authored
40 if source
41 partials[file] = source
edc617e @quangv Sub-Partials (Partials, in partials) support added.
authored
42
28d1b90 @quangv HulkHogan refactored.
authored
43 sublist = @parsePartials source
edc617e @quangv Sub-Partials (Partials, in partials) support added.
authored
44
28d1b90 @quangv HulkHogan refactored.
authored
45 if sublist.length
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
46 @_makePartials partials, sublist
28d1b90 @quangv HulkHogan refactored.
authored
47
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
48 render : (source, context={})-> # Shortcut for @compile(source, options)()
49 do @compile source, context
50
51 compile : (source='', options={})->
52 @root = options.root if options.root
53 @ext = options.defaultEngine if options.defaultEngine
54
28d1b90 @quangv HulkHogan refactored.
authored
55 hogan = require 'hogan.js'
56 compiled = hogan.compile source
c53dc49 @quangv Refactored hulk.coffee
authored
57
28d1b90 @quangv HulkHogan refactored.
authored
58 partials = {}
59 # Get Partials #
60 partial_files = @parsePartials source
1688334 @quangv Implementation of partials compile test.
authored
61
28d1b90 @quangv HulkHogan refactored.
authored
62 if partial_files.length
baaf61c @quangv Refactored and added render(), getSource() & getSourcePath()
authored
63 @_makePartials partials, partial_files
c53dc49 @quangv Refactored hulk.coffee
authored
64
28d1b90 @quangv HulkHogan refactored.
authored
65 return ->
66 return compiled.render options, partials
3b52088 @christopherscott add support for Express 3.0 templating
christopherscott authored
67
fb42463 @christopherscott change to using fat-arrow for __express method
christopherscott authored
68 __express : (filename, options, callback) =>
28d1b90 @quangv HulkHogan refactored.
authored
69 # add support for Express 3.x templating scheme
70 # in Express use like this:
71 #
72 # app.engine 'html', hogan.__express
73 # app.set 'view engine', 'html'
74 #
75 source = @_readFile(filename)
76 # pass options to compile method to support partials
77 compiled = @compile(source, options)
78 callback(null, compiled(options))
3b52088 @christopherscott add support for Express 3.0 templating
christopherscott authored
79
28d1b90 @quangv HulkHogan refactored.
authored
80 module.exports = new HulkHogan
Something went wrong with that request. Please try again.