-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.coffee
32 lines (24 loc) · 909 Bytes
/
plugin.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fs = require 'fs'
path = require 'path'
csv = require 'csv'
module.exports = (env, callback) ->
class CsvPlugin extends env.ContentPlugin
constructor: (@_filepath, @_text, @data) ->
getFilename: ->
@_filepath.relative
getView: ->
(env, locals, contents, templates, callback) ->
# return the plain CSV file
callback null, new Buffer @_text
CsvPlugin.fromFile = (filepath, callback) ->
fs.readFile filepath.full, (error, buffer) ->
if error
callback error
else
data = []
csv()
.from(buffer.toString(), columns: true)
.on('record', (row, index) -> data.push row)
.on('end', (count) -> callback null, new CsvPlugin(filepath, buffer.toString(), data))
env.registerContentPlugin 'csv', '**/*.csv', CsvPlugin
callback() # tell the plugin manager we are done