• ¶

    Geno

    A small static and opinionated site generator.

    Geno is a small and opinionated static site generator for easily generating a simple project site for your NPM modules.

    License: MIT
    Source: GitHub

  • ¶

    geno.js

  • ¶

    Modules

    Import the required modules.

    • nunjucks is the default template engine.
    var nunjucks = require('nunjucks'),
        Get      = require('./utils/get'),
        get      = new Get(),
        File     = require('./utils/file'),
        file     = new File(),
        Extend   = require('./utils/extend'),
        extend   = new Extend();
  • ¶

    Geno

    The constructor.

    var Geno = function() {};
  • ¶

    Geno.prototype.build

    Run through the configuration, the parsing and the compiling of the site.

    • done is a function.
    Geno.prototype.build = function(done) {
      done = (done || function() {});
      this.configure(function(config) {
        this.template(config, function(err, data) {
        if (err) { return done(err); }
          this.compile(config, data, function(err) {
            if (err) { return done(err); }
            return done();
          });
        }.bind(this));
      }.bind(this));
    };
  • ¶

    Geno.prototype.configure

    Return the config by reading the project's package.json file and the project's geno.json file, if it exists.

    • callback is a function.
    Geno.prototype.configure = function(callback) {
      var pkgPath = get.path(process.cwd(), 'package.json'),
          pkgInfo = require(pkgPath),
          genPath = get.path(process.cwd(), 'geno.json');
      file.exists(genPath, function(exists) {
        if (exists) {
          var genInfo = require(genPath),
              config  = extend.obj(pkgInfo, genInfo);
          return callback(config);
        } else {
          return callback(pkgInfo);
        }
      });
    };
  • ¶

    Geno.prototype.template

    Return the template file's data.

    • config is an object.
    • callback is a function.
    Geno.prototype.template = function(config, callback) {
      var index = (config.template || get.lib('template/index.html'));
      file.read(index, function(err, data) {
        if (err) { return callback(err); }
        return callback(null, data);
      });
    };
  • ¶

    Geno.prototype.compile

    Compile the content by running the data and config through nunjucks.

    • config is an object.
    • data is a string.
    • callback is a function.
    Geno.prototype.compile = function(config, data, callback) {
      var content = nunjucks.renderString(data, config),
          output  = (config.output || get.path(process.cwd(), 'site')),
          style   = (config.stylesheet || get.lib('template/style.css')),
          script  = (config.javascript || false);
      this.generate(content, output, style, script, function(err) {
          if (err) { return callback(err); }
          return callback(null);
      });
    };
  • ¶

    Geno.prototype.generate

    Generate the site by creating the output directory and saving the index.html file.

    • content is a string.
    • output is a string.
    • style is a string.
    • script is a string.
    • callback is a function.
    Geno.prototype.generate = function(content, output, style, script, callback) {
      var index = get.path(output, 'index.html');
      file.mkDir(output, function(err) {
        if (err) { return callback(err); }
        file.save(index, content, function() {
          if (err) { return callback(err); }
          this.assets(output, style, script, function(err) {
            if (err) { return callback(err); }
            return callback(null);
          });
        }.bind(this));
      }.bind(this));
    };
  • ¶

    Geno.prototype.assets

    Copy all of the assets, ie. the style and script files.

    • output is a string.
    • style is a string.
    • script is a string.
    • callback is a function.
    Geno.prototype.assets = function(output, style, script, callback) {
      var css = get.path(output, 'style.css'),
          src = get.name(script),
          js  = get.path(output, src);
      file.copy(style, css, function(err) {
        if (err) { return callback(err); }
        if (!script) {
          return callback(null);
        } else {
          file.copy(script, js, function(err) {
            if (err) { return callback(err); }
            return callback(null);
          });
        }
      });
    };
  • ¶

    Exports

    Export Geno.

    module.exports = Geno;
  • ¶

    utils/get.js

  • ¶

    Modules

    Import the required modules.

    var path = require('path');
  • ¶

    Get

    The constructor.

    var Get = function() {};
  • ¶

    Get.prototype.path

    Return the full path of the filename.

    • directory is a string.
    • filename is a string.
    Get.prototype.path = function(directory, filename) {
      return path.join(directory, '/', filename);
    };
  • ¶

    Get.prototype.lib

    Return the full path to the module library.

    • filename is a string.
    Get.prototype.lib = function(filename) {
      return path.join(__dirname, '../../', filename);
    };
  • ¶

    Get.prototype.name

    Return the base name of the filename.

    • filename is a string.
    Get.prototype.name = function(filename) {
      return path.basename(filename);
    };
  • ¶

    Exports

    Export Get.

    module.exports = Get;
  • ¶

    utils/extend.js

  • ¶

    Extend

    The constructor.

    var Extend = function() {};
  • ¶

    Extend.prototype.obj

    Return a single object by combining two into one.

    • target is an object.
    • options is an object.
    Extend.prototype.obj = function(target, options) {
      for (var key in options) {
        if (options.hasOwnProperty(key)) {
          if (options[key] !== undefined) {
            target[key] = options[key];
          }
        }
      }
      return target;
    };
  • ¶

    Exports

    Export Extend.

    module.exports = Extend;
  • ¶

    utils/file.js

  • ¶

    Modules

    Import the required modules.

    • fs-extra enables some extra fs goodies, like copy and mkdirp.
    var fs = require('fs-extra');
  • ¶

    File

    The constructor.

    var File = function() {};
  • ¶

    File.prototype.read

    Read the file (synchronously) and return the data.

    • file is a string.
    • callback is a function.
    File.prototype.read = function(file, callback) {
      try {
        var data = fs.readFileSync(file, { encoding: 'utf8' });
        return callback(null, data);
      } catch(err) {
        return callback(err);
      }
    };
  • ¶

    File.prototype.save

    Save the file and content (synchronously).

    • file is a string.
    • content is a string.
    • callback is a function.
    File.prototype.save = function(file, content, callback) {
      try {
        fs.writeFileSync(file, content, { encoding: 'utf8' });
        return callback(null);
      } catch(err) {
        return callback(err);
      }
    };
  • ¶

    File.prototype.copy

    Copy the target to the destination (synchronously).

    • target is a string.
    • destination is a string.
    • callback is a function.
    File.prototype.copy = function(target, destination, callback) {
      try {
        fs.copySync(target, destination);
        return callback(null);
      } catch(err) {
        return callback(err);
      }
    };
  • ¶

    File.prototype.exists

    Check if the path exists (synchronously).

    • path is a string.
    • callback is a function.
    File.prototype.exists = function(path, callback) {
      var exists = fs.existsSync(path);
      return callback(exists);
    };
  • ¶

    File.prototype.mkDir

    Create the path (synchronously).

    • path is a string.
    • callback is a function.
    File.prototype.mkDir = function(path, callback) {
      try {
        this.exists(path, function(exists) {
          if (!exists) { fs.mkdirpSync(path); }
          return callback(null);
        });
      } catch(err) {
        return callback(err);
      }
    };
  • ¶

    Exports

    Export File.

    module.exports = File;