forked from docpad/docpad
/
file.coffee
executable file
·541 lines (429 loc) · 11.3 KB
/
file.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# Requires
pathUtil = require('path')
balUtil = require('bal-util')
_ = require('underscore')
mime = require('mime')
# Local
{Backbone,Model} = require(__dirname+'/../base')
# ---------------------------------
# File Model
class FileModel extends Model
# ---------------------------------
# Properties
# The out directory path to put the file
outDirPath: null
# Model Type
type: 'file'
# Stat Object
stat: null
# The contents of the file, stored as a Buffer
data: null
# ---------------------------------
# Attributes
defaults:
# ---------------------------------
# Automaticly set variables
# The unique document identifier
id: null
# The file's name without the extension
basename: null
# The file's last extension
# "hello.md.eco" -> "eco"
extension: null
# The file's extensions as an array
# "hello.md.eco" -> ["md","eco"]
extensions: null # Array
# The file's name with the extension
filename: null
# The full path of our source file, only necessary if called by @load
fullPath: null
# The full directory path of our source file
fullDirPath: null
# The output path of our file
outPath: null
# The output path of our file's directory
outDirPath: null
# The relative output path of our file
relativeOutPath: null
# The relative output path of our file's directory
relativeOutDirPath: null
# The relative path of our source file (with extensions)
relativePath: null
# The relative directory path of our source file
relativeDirPath: null
# The relative base of our source file (no extension)
relativeBase: null
# The MIME content-type for the source document
contentType: null
# The date object for when this document was created
ctime: null
# The date object for when this document was last modified
mtime: null
# ---------------------------------
# Content variables
# The encoding of the file
encoding: null
# The contents of the file, stored as a String
content: null
# ---------------------------------
# User set variables
# The title for this document
title: null
# The date object for this document, defaults to mtime
date: null
# The generated slug (url safe seo title) for this document
slug: null
# The url for this document
url: null
# Alternative urls for this document
urls: null # Array
# Whether or not we ignore this document (do not render it)
ignored: false
# ---------------------------------
# Functions
# Initialize
initialize: (attrs,opts) ->
# Prepare
{outDirPath,stat,data} = opts
# Apply
@outDirPath = outDirPath if outDirPath
@setStat(stat) if stat
@setData(data) if data
@set({
extensions: []
urls: []
id: @cid
},{silent:true})
# Super
super
# Set Data
setData: (data) ->
@data = data
@
# Get Data
getData: ->
return @data
# Set Stat
setStat: (stat) ->
@stat = stat
@set(
ctime: new Date(stat.ctime)
mtime: new Date(stat.mtime)
)
@
# Get Attributes
getAttributes: ->
return @toJSON()
# Get Meta
getMeta: ->
return @meta
# Is Text?
isText: ->
return @get('encoding') isnt 'binary'
# Is Binary?
isBinary: ->
return @get('encoding') is 'binary'
# Get the arguments for the action
# Using this contains the transparency with using opts, and not using opts
getActionArgs: (opts,next) ->
if balUtil.isFunction(opts) and next? is false
next = opts
opts = {}
else
opts or= {}
next or= opts.next or null
return {next,opts}
# Load
# If the fullPath exists, load the file
# If it doesn't, then parse and normalize the file
load: (opts={},next) ->
# Prepare
{opts,next} = @getActionArgs(opts,next)
file = @
filePath = @get('relativePath') or @get('fullPath') or @get('filename')
fullPath = @get('fullPath') or filePath or null
data = @getData()
# Log
file.log('debug', "Loading the file: #{filePath}")
# Handler
complete = (err) ->
return next(err) if err
file.log('debug', "Loaded the file: #{filePath}")
next()
handlePath = ->
file.set({fullPath},{silent:true})
file.readFile(fullPath, complete)
handleData = ->
file.set({fullPath:null},{silent:true})
file.parseData data, (err) =>
return next(err) if err
file.normalize (err) =>
return next(err) if err
complete()
# Exists?
if fullPath
balUtil.exists fullPath, (exists) ->
# Read the file
if exists
handlePath()
else
handleData()
else if data
handleData()
else
err = new Error('Nothing to load')
return next(err)
# Chain
@
# Read File
# Reads in the source file and parses it
# next(err)
readFile: (fullPath,next) ->
# Prepare
file = @
fullPath = @get('fullPath')
# Log
file.log('debug', "Reading the file: #{fullPath}")
# Async
tasks = new balUtil.Group (err) =>
if err
file.log('err', "Failed to read the file: #{fullPath}")
return next(err)
else
@normalize (err) =>
return next(err) if err
file.log('debug', "Read the file: #{fullPath}")
next()
tasks.total = 2
# Stat the file
if file.stat
tasks.complete()
else
balUtil.stat fullPath, (err,fileStat) ->
return next(err) if err
file.stat = fileStat
tasks.complete()
# Read the file
balUtil.readFile fullPath, (err,data) ->
return next(err) if err
file.parseData(data, tasks.completer())
# Chain
@
# Get the encoding of a buffer
getEncoding: (buffer) ->
# Prepare
contentStartBinary = buffer.toString('binary',0,24)
contentStartUTF8 = buffer.toString('utf8',0,24)
encoding = 'utf8'
# Detect encoding
for i in [0...contentStartUTF8.length]
charCode = contentStartUTF8.charCodeAt(i)
if charCode is 65533 or charCode <= 8
# 8 and below are control characters (e.g. backspace, null, eof, etc.)
# 65533 is the unknown character
encoding = 'binary'
break
# Return encoding
return encoding
# Parse data
# Parses some data, and loads the meta data and content from it
# next(err)
parseData: (data,next) ->
# Prepare
encoding = 'utf8'
# Extract content from data
if data instanceof Buffer
encoding = @getEncoding(data)
if encoding is 'binary'
content = ''
else
content = data.toString(encoding)
else if balUtil.isString(data)
content = data
else
content = ''
# Trim the content
content = content.replace(/\r\n?/gm,'\n').replace(/\t/g,' ')
# Apply
@setData(data)
@set({content,encoding})
# Next
next()
@
# Set the url for the file
setUrl: (url) ->
@addUrl(url)
@set({url})
@
# Add a url
# Allows our file to support multiple urls
addUrl: (url) ->
# Multiple Urls
if url instanceof Array
for newUrl in url
@addUrl(newUrl)
# Single Url
else if url
found = false
urls = @get('urls')
for own existingUrl in urls
if existingUrl is url
found = true
break
urls.push(url) if not found
# Chain
@
# Remove a url
# Removes a url from our file
removeUrl: (userUrl) ->
urls = @get('urls')
for url,index in urls
if url is userUrl
urls.remove(index)
break
@
# Get a Path
# If the path starts with `.` then we get the path in relation to the document that is calling it
# Otherwise we just return it as normal
getPath: (relativePath, parentPath) ->
if /^\./.test(relativePath)
relativeDirPath = @get('relativeDirPath')
path = pathUtil.join(relativeDirPath, relativePath)
else
if parentPath
path = pathUtil.join(parentPath, relativePath)
else
path = relativePath
return path
# Normalize data
# Normalize any parsing we have done, as if a value has updates it may have consequences on another value. This will ensure everything is okay.
# next(err)
normalize: (opts={},next) ->
# Prepare
changes = {}
# Fetch
{opts,next} = @getActionArgs(opts,next)
basename = @get('basename')
filename = @get('filename')
fullPath = @get('fullPath')
extensions = @get('extensions')
relativePath = @get('relativePath')
mtime = @get('mtime')
# Filename
if fullPath
changes.filename = filename = pathUtil.basename(fullPath)
# Basename, extensions, extension
if filename
if filename[0] is '.'
basename = filename.replace(/^(\.[^\.]+)\..*$/, '$1')
else
basename = filename.replace(/\..*$/, '')
changes.basename = basename
# Extensions
if extensions? is false or extensions.length is 0
extensions = filename.split(/\./g)
extensions.shift() # ignore the first result, as that is our filename
changes.extensions = extensions
# determine the single extension that determine this file
if extensions.length
extension = extensions[extensions.length-1]
else
extension = null
changes.extension = extension
# fullDirPath, contentType
if fullPath
changes.fullDirPath = fullDirPath = pathUtil.dirname(fullPath) or ''
changes.contentType = contentType = mime.lookup(fullPath)
# relativeDirPath, relativeBase
if relativePath
changes.relativeDirPath = relativeDirPath = pathUtil.dirname(relativePath).replace(/^\.$/,'') or ''
changes.relativeBase = relativeBase =
if relativeDirPath
pathUtil.join(relativeDirPath, basename)
else
basename
changes.id = id = relativePath
# Date
if mtime
changes.date = date = mtime
# Apply
@set(changes)
# Next
next()
@
# Contextualize data
# Put our data into perspective of the bigger picture. For instance, generate the url for it's rendered equivalant.
# next(err)
contextualize: (opts={},next) ->
# Prepare
changes = {}
# Fetch
{opts,next} = @getActionArgs(opts,next)
relativePath = @get('relativePath') or null
relativeDirPath = @get('relativeDirPath') or null
relativeBase = @get('relativeBase') or null
filename = @get('filename') or null
outPath = @get('outPath') or null
outDirPath = @get('outDirPath') or null
# Create the URL for the file
if relativePath?
url = "/#{relativePath}"
@setUrl(url)
# Create a slug for the file
if relativeBase?
changes.slug = slug = balUtil.generateSlugSync(relativeBase)
# Set name if it doesn't exist already
if filename?
changes.name = name = filename
# Create the outPath if we have a outpute directory
if @outDirPath? and relativePath?
changes.relativeOutDirPath = relativeOutDirPath = relativeDirPath if relativeDirPath?
changes.relativeOutPath = relativeOutPath = relativePath
changes.outPath = outPath = pathUtil.join(@outDirPath,relativePath)
if outPath
changes.outDirPath = outDirPath = pathUtil.dirname(outPath)
# Apply
@set(changes)
# Forward
next()
@
# Write the rendered file
# next(err)
write: (next) ->
# Prepare
file = @
fileOutPath = @get('outPath')
content = @get('content') or @getData()
encoding = @get('encoding')
# Log
file.log 'debug', "Writing the file: #{fileOutPath}"
# Write data
balUtil.writeFile fileOutPath, content, encoding, (err) ->
# Check
return next(err) if err
# Log
file.log 'debug', "Wrote the file: #{fileOutPath}"
# Next
next()
# Chain
@
# Delete the file
# next(err)
delete: (next) ->
# Prepare
file = @
fileOutPath = @get('outPath')
# Log
file.log 'debug', "Delete the file: #{fileOutPath}"
# Write data
balUtil.unlink fileOutPath, (err) ->
# Check
return next(err) if err
# Log
file.log 'debug', "Deleted the file: #{fileOutPath}"
# Next
next()
# Chain
@
# Export
module.exports = FileModel