Skip to content

Instantly share code, notes, and snippets.

@jimlambie
Created August 16, 2017 13:32
Show Gist options
  • Save jimlambie/235ebeb97c40b26095e047883c6b6cc2 to your computer and use it in GitHub Desktop.
Save jimlambie/235ebeb97c40b26095e047883c6b6cc2 to your computer and use it in GitHub Desktop.
KirbyProvider for DADI Web
'use strict'
const _ = require('underscore')
const fs = require('fs')
const path = require('path')
const readDir = require('readdir')
const templateStore = require(path.join(__dirname, '/../templates/store'))
const walk = require('walkdir')
const visiblePageRe = /^\d+-/
const KirbyProvider = function () {
}
KirbyProvider.prototype.destroy = function () {
}
/**
* initialise - initialises the datasource provider
*
* @param {obj} datasource - the datasource to which this provider belongs
* @param {obj} schema - the schema that this provider works with
* @return {void}
*/
KirbyProvider.prototype.initialise = function (datasource, schema) {
this.datasource = datasource
this.schema = schema
}
/**
* load - loads data from the datasource
*
* @param {string} requestUrl - url of the web request (not used)
* @param {fn} done - callback on error or completion
* @return {void}
*/
KirbyProvider.prototype.load = function (requestUrl, done) {
let data = []
const contentPath = path.resolve(this.schema.datasource.source.path)
console.log('contentPath:', contentPath)
readDir.read(contentPath, ['*/'], readDir.ABSOLUTE_PATHS + readDir.INCLUDE_DIRECTORIES + readDir.NON_RECURSIVE, (err, contentParents) => {
console.log(contentParents)
contentParents.forEach((parent, index) => {
let emitter = walk(parent, {
follow_symlinks: false,
max_depth: 1
})
// emitter.on('directory', (directory, stat) => {
// console.log('d: ', directory)
// })
emitter.on('file', (file, stat) => {
console.log(file)
let page = buildPage(file, stat)
data.push(page)
})
emitter.on('end', () => {
// console.log(data)
console.log('END')
})
})
})
function buildPage (file, stat) {
console.log('f: ', file)
// read the page descriptor files (*.txt)
if (path.extname(file) === '.txt') {
let key = path.basename(path.dirname(file))
let directory = path.dirname(file)
let isVisible = visiblePageRe.test(key)
let url = '/' + key.replace(visiblePageRe, '')
let template = path.basename(file).replace(path.extname(file), '')
let page = {
attributes: {
directory: directory,
key: key,
template: template,
visible: isVisible
},
url: url,
children: []
}
let raw = fs.readFileSync(file).toString()
// explode all fields by the line separator
let fields = raw.split(/\n----\s*\n*/)
let attributes = {}
// loop through all fields and add them to the content
fields.forEach(field => {
let pos = field.indexOf(':')
let key = field.substring(0, pos).toLowerCase()
let value = field.substring(pos + 1) === '\n' ? null : field.substring(pos + 1).trim()
if (value) attributes[key] = value
})
page = Object.assign({}, page, attributes)
page.render = function (chunk, context, bodies, params) {
let template = templateStore.get(this.attributes.template)
let output = template.render(context)
return chunk.write(output)
}
console.log(directory)
readDir.read(directory, ['*/'], readDir.ABSOLUTE_PATHS + readDir.INCLUDE_DIRECTORIES + readDir.NON_RECURSIVE, (err, content) => {
console.log(content)
})
// let childEmitter = walk(directory, {
// follow_symlinks: false,
// max_depth: 2
// })
// childEmitter.on('directory', (directory, stat) => {
// // console.log('CHILD DIR: ', directory)
// // let page = buildPage(file, stat)
// // console.log(page)
// // page.children.push(page)
// })
//
// childEmitter.on('file', (file, stat) => {
// console.log('CHILD FILE: ', file)
// let p = buildPage(file, stat)
// console.log(p)
// // page.children.push(page)
// })
//
// childEmitter.on('end', () => {
// // return page
// console.log('CHILDREN END')
// })
return page
// if (index === contentParents.length - 1) {
// console.log(data)
// return done(null, { results: Array.isArray(data) ? data : [data] })
// }
}
}
// emitter.on('file', (filename, stat) => {
// console.log('file from emitter: ', filename)
// })
// if (Array.isArray(data)) {
// const sort = this.schema.datasource.sort
// const search = this.schema.datasource.search
// const count = this.schema.datasource.count
// const fields = this.schema.datasource.fields || []
//
// if (search) data = _.where(data, search)
//
// // apply a filter
// data = _.where(data, this.schema.datasource.filter)
//
// // Sort by field (with date support)
// if (sort && Object.keys(sort).length > 0) {
// Object.keys(sort).forEach(field => {
// data = _.sortBy(data, post => {
// const value = post[field]
// const valueAsDate = new Date(value)
// return valueAsDate.toString() !== 'Invalid Date'
// ? +valueAsDate
// : value
// })
// if (sort[field] === -1) {
// data = data.reverse()
// }
// })
// }
//
// if (count) data = _.first(data, count)
// if (fields && !_.isEmpty(fields)) {
// data = _.chain(data).selectFields(fields.join(',')).value()
// }
// }
// done(null, { results: Array.isArray(data) ? data : [data] })
}
module.exports = KirbyProvider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment