Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/8702265 to your computer and use it in GitHub Desktop.
Save ryanflorence/8702265 to your computer and use it in GitHub Desktop.
// this file is designed to freak you out
const fs = require('fs')
const path = require('path')
const hbs = require('handlebars')
const cheerio = require('cheerio')
!function main() {
return process.stdout.write(
template()(
templateLocals()))}()
function defaultFilesPath() {
return 'data'}
function filesPath() {
return path.resolve(
defaultFilesPath())}
function filePath(file) {
return ''+filesPath()+'/'+file}
function makeFileObject(fileName) {
return {
id: fileName.replace(/\.html$/, ''),
content: fs.readFileSync(
filePath(fileName)).toString() }}
function files() {
return fs.readdirSync(
filesPath())}
function processFile(fileName) {
return parseFile(
makeFileObject(fileName))}
function templatePath() {
return path.join(__dirname, 'template.hbs')}
function template() {
return hbs.compile(
fs.readFileSync(
templatePath()).toString())}
function parseFile(fileObject) {
return templateFileObject(
cheerio.load(
fileObject.content))}
function templateFileObject($) {
return {
name: $('title').html(),
id: fileObject.id,
introduction: $('introduction').html(),
textIntroduction: $('introduction').text().trim().replace(/\s+/g, ' '),
incorrect: {
description: $('incorrect description').html(),
demo: $('incorrect demo').html().trim(),
},
correct: {
description: $('correct description').html(),
demo: $('correct demo').html().trim() }}}
function readCss() {
return fs.readFileSync(
path.resolve(__dirname, 'styles.css')).toString()}
function templateLocals() {
return {
files: files().sort().map(processFile),
css: readCss()}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment