Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arafatahmedyasser/fb5820452d95413a0185feeee7ceadee to your computer and use it in GitHub Desktop.
Save arafatahmedyasser/fb5820452d95413a0185feeee7ceadee to your computer and use it in GitHub Desktop.

Revisions

  1. @selvakn selvakn revised this gist May 7, 2012. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions rhino-handlebars-precompiler.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,9 @@ importPackage(java.io);
    console = {
    log: print
    },
    showUsage = function() {
    console.log('Usage: java -jar <rhino.jar> rhino-handlebars-compiler.js --handlebars <handlebars library path> --templates <templates directory> --output <output file>');
    },
    handlebarsLibrary,
    templatesDirectory,
    outputFile,
    @@ -43,7 +46,7 @@ importPackage(java.io);
    outStream = new BufferedWriter(new FileWriter(outputFile));

    if (undefined === handlebarsLibrary || undefined === templatesDirectory) {
    console.log('Usage: java -jar <rhino.jar> rhino-handlebars-compiler.js --handlebars <handlebars library path> --templates <templates directory> --output <output file>');
    showUsage();
    java.lang.System.exit(1);
    }
    load(handlebarsLibrary);
    @@ -66,4 +69,4 @@ importPackage(java.io);

    outStream.write(output.join(''));
    outStream.close();
    }(arguments));
    }(arguments));
  2. @selvakn selvakn created this gist May 7, 2012.
    69 changes: 69 additions & 0 deletions rhino-handlebars-precompiler.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    importPackage(java.io);

    (function(args) {
    var templateFileExtension = 'handlebars',
    output = ['// This file is auto-generated and should be ignored from version control.\n'],
    console = {
    log: print
    },
    handlebarsLibrary,
    templatesDirectory,
    outputFile,
    templateFiles,
    outStream,
    index,
    templateFile,
    templateContents,
    argumentsParser,
    options;

    argumentsParser = function() {
    var arg, parse = function(args) {
    var options = {};
    args = Array.prototype.slice.call(args);
    arg = args.shift();
    while (arg) {
    if (arg.indexOf("--") === 0) {
    options[arg.substring(2)] = args.shift();
    }
    arg = args.shift();
    }
    return options;
    };

    return { parse: parse };
    };

    options = new argumentsParser().parse(args);
    handlebarsLibrary = options.handlebars;
    templatesDirectory = options.templates;
    outputFile = options.output;

    templateFiles = new File(templatesDirectory).listFiles();
    outStream = new BufferedWriter(new FileWriter(outputFile));

    if (undefined === handlebarsLibrary || undefined === templatesDirectory) {
    console.log('Usage: java -jar <rhino.jar> rhino-handlebars-compiler.js --handlebars <handlebars library path> --templates <templates directory> --output <output file>');
    java.lang.System.exit(1);
    }
    load(handlebarsLibrary);

    output.push('(function(){');
    output.push('\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n');

    templateFiles = templateFiles.filter(function(fileName) {
    return(fileName.getName().substr(-11) === ("." + templateFileExtension));
    });

    for (index = 0; index < templateFiles.length; index++) {
    templateFile = templateFiles[index];
    templateName = templateFile.getName().replaceAll(('\\.' + templateFileExtension + '$'), '');
    templateContents = Handlebars.precompile(readFile(templateFile.getAbsolutePath()));
    output.push(' templates[\'' + templateName + '\'] = template(' + templateContents + ');\n');
    }

    output.push('}());');

    outStream.write(output.join(''));
    outStream.close();
    }(arguments));