Created
June 30, 2013 04:21
-
-
Save donpark/5893842 to your computer and use it in GitHub Desktop.
Sketchy coffee-script that generates precompiled Hogan templates from a HTML file containing script tags of type `text/x-mustache-template'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FS = require("fs") | |
| Path = require("path") | |
| Cheerio = require("cheerio") | |
| Hogan = require("hogan.js") | |
| htmlFile = process.argv[2] | |
| html = FS.readFileSync process.argv[2], encoding: "utf8" | |
| # console.log html | |
| $ = Cheerio.load html | |
| $templates = $("script[type='text/x-mustache-template']") | |
| templates = {} | |
| $templates.each (element) -> | |
| templates[@attr("id")] = Hogan.compile @text(), asString: true | |
| # console.log templates | |
| js = [] | |
| js.push "window.compiledTemplates = {}" | |
| for own name, code of templates | |
| js.push "compiledTemplates[\"#{name}\"] = new Hogan.Template(#{code});" | |
| js = js.join("\n") | |
| jsDir = Path.dirname(htmlFile) | |
| jsName = Path.basename(htmlFile, Path.extname(htmlFile)) | |
| jsFile = Path.join(jsDir, jsName + "-templates.js") | |
| FS.writeFileSync jsFile, js |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example input file
core-dialogs.jadewhich is used to generatemain.htmlfile: