From: Nick Downing Date: Sun, 11 Nov 2018 04:47:39 +0000 (+1100) Subject: Change name from js_template to org scoped @ndcode/jst X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=c346fd43fd3b3e84b7438772f35ac2281b37953b;p=jst.git Change name from js_template to org scoped @ndcode/jst --- diff --git a/.npmignore b/.npmignore index c027963..204f5c8 100644 --- a/.npmignore +++ b/.npmignore @@ -5,7 +5,10 @@ /dist/bin.js /env.sh /js_template-*.tgz +/package-lock.json /rollup.config.* /src /test /test.js +/yarn.lock +/yarn-error.log diff --git a/README.md b/README.md index 1694545..832dafa 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ # JavaScript Template system +An NDCODE project. + ## Overview -The `js_template` package exports a single function `js_template(root, dirname, -pathname)`, which loads the given file from disk, parses it for a superset of -JavaScript (ES6) which can contain HTML-like constructs, and then generates -equivalent plain JavaScript code which in turn, can generate the desired HTML. +The `jst` package exports a single function `jst(root, dirname, pathname)`, +which loads the given file from disk, parses it for a superset of JavaScript +(ES6) which can contain HTML-like constructs, and then generates equivalent +plain JavaScript code which in turn, can generate the desired HTML. The system is inspired by Pug (formerly Jade) templates, but emphasizing the JavaScript, for instance you do not need a `-` sign at the start of each line, @@ -298,14 +300,14 @@ would be ignored, since the page template is expecting output to be in `_out`. ## File management -The `js_template()` function gives a "point and shoot" interface which makes it +The `jst()` function gives a "point and shoot" interface which makes it very easy to manage on-disk templates. You essentially just state the pathname to the template, which is normally a `*.jst` file, and the template will be parsed, converted to JavaScript, evaluated, and also cached for future use. -What the `js_template()` function returns depends on what the template returns. +What the `jst()` function returns depends on what the template returns. Template exports are similar but slightly different to CommonJS module exports, -as noted in examples above. Usually, `js_template()` returns a JavaScript +as noted in examples above. Usually, `jst()` returns a JavaScript function, which you call to generate HTML each time the page is to be served. The HTML-generating function is re-useable in this way for efficiency reasons, @@ -320,9 +322,9 @@ return (lang, name) => html(lang=lang) { } ``` Suppose the above is saved as `/hello/index.html.jst` under your document root -`/var/www/html`. You call `js_template(root, dirname, pathname)`, like this: +`/var/www/html`. You call `jst(root, dirname, pathname)`, like this: ``` -let template_func = js_template( +let template_func = jst( '/var/www/html', '/var/www/html/hello', 'index.jst' @@ -350,10 +352,10 @@ imports are taken relative to the importing module, similarly to in CommonJS. ## Memory vs disk caching of templates -Templates compiled using `js_template()` are cached in memory, as long as the +Templates compiled using `jst()` are cached in memory, as long as the same `node` interpreter is running, so that they can be retrieved using either -`js_template()`, or equivalently `_require()` inside a template, and they will -not be re-executed. In the above example, if you call `js_template()` twice, +`jst()`, or equivalently `_require()` inside a template, and they will +not be re-executed. In the above example, if you call `jst()` twice, you get the same object twice (it is a 2-argument function returning `String`). As well as this, the compiled templates are also cached on disk, which requires @@ -402,7 +404,7 @@ would probably be a webapp, although source code would also be made available. ## GIT repository The development version can be cloned, downloaded, or browsed with `gitweb` at: -https://git.ndcode.org/public/js_template.git +https://git.ndcode.org/public/jst.git ## License @@ -412,7 +414,7 @@ their JavaScript parser, which we've heavily modified to produce the JST parser. ## Contributions -We would greatly welcome your feedback and contributions. The `js_template` is +We would greatly welcome your feedback and contributions. The `jst` is under active development (and is part of a larger project that is also under development) and thus the API is considered tentative and subject to change. If this is undesirable, you could possibly pin the version in your `package.json`. diff --git a/js_template.js b/jst.js similarity index 94% rename from js_template.js rename to jst.js index d6a3507..d7474a5 100644 --- a/js_template.js +++ b/jst.js @@ -21,7 +21,7 @@ * IN THE SOFTWARE. */ -let BuildCache = require('build_cache') +let BuildCache = require('@ndcode/build_cache') let acorn = require('./dist/acorn') let astring = require('astring') let fs = require('fs') @@ -36,7 +36,7 @@ let fs_stat = util.promisify(fs.stat) let fs_writeFile = util.promisify(fs.writeFile) let build_cache = new BuildCache() -let js_template = async (root, dirname, pathname, args) => { +let jst = async (root, dirname, pathname, args) => { while (pathname.charAt(0) === '/') { dirname = root pathname = pathname.slice(1) @@ -49,7 +49,7 @@ let js_template = async (root, dirname, pathname, args) => { let arg_names = ['_require', '_html_escape', '_root', '_dirname'] let arg_values = [ - async pathname => js_template(root, dirname, pathname, args), + async pathname => jst(root, dirname, pathname, args), html_escape, root, dirname @@ -104,4 +104,4 @@ let js_template = async (root, dirname, pathname, args) => { ) } -module.exports = js_template +module.exports = jst diff --git a/package.json b/package.json index 2a0f028..f20ca52 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "js_template", + "name": "@ndcode/jst", "version": "0.1.0", - "description": "JavaScript HTML template engine", + "description": "JavaScript Templates for HTML.", "keywords": [ "template", "HTML", @@ -13,19 +13,19 @@ "homepage": "https://www.ndcode.org", "repository": { "type": "git", - "url": "https://git.ndcode.org/public/js_template.git" + "url": "https://git.ndcode.org/public/jst.git" }, "bugs": { "email": "nick@ndcode.org" }, - "main": "js_template.js", + "main": "jst.js", "engines": { "node": ">=0.4.0" }, "directories": {}, "dependencies": { + "@ndcode/build_cache": "^0.1.0", "astring": "^1.3.1", - "build_cache": "^0.1.0", "html-escape": "^2.0.0", "rollup": "^0.45.0", "rollup-plugin-buble": "^0.16.0" diff --git a/page.jst b/page.jst deleted file mode 100644 index 43ebe37..0000000 --- a/page.jst +++ /dev/null @@ -1,16 +0,0 @@ -async (_env, _out) => { - html.'cls-1'.cls-2#id-1(lang=_env.lang true=_env.val something-else="\"val\"") { - head {} - body { - `hello - & goodbye - ` - script { - for (let i = 0; i < 100; ++i) - console.log(i) - } - let page1 = await _req('test/page1.jst') - await page1(_env, _out) - } - } -} diff --git a/page3.jst b/page3.jst deleted file mode 100644 index 82428c1..0000000 --- a/page3.jst +++ /dev/null @@ -1,3 +0,0 @@ -async (_env, _out) => { - 'page3\n' -} diff --git a/test.js b/test.js deleted file mode 100644 index 6f05948..0000000 --- a/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2018 Nick Downing - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -let js_template = require('./js_template') - -;( - async () => { - let page = await js_template('.', 'page.jst') - out = [] - await page({lang: 'en', val: ''}, out) - console.log(out.join('')) - } -)() diff --git a/test/page1.jst b/test/page1.jst deleted file mode 100644 index d591f63..0000000 --- a/test/page1.jst +++ /dev/null @@ -1,7 +0,0 @@ -async (_env, _out) => { - let page2 = await _req('page2.jst') - let page3 = await _req('/page3.jst') - 'page1\n' - await page2(_env, _out) - await page3(_env, _out) -} diff --git a/test/page2.jst b/test/page2.jst deleted file mode 100644 index 78c2fa0..0000000 --- a/test/page2.jst +++ /dev/null @@ -1,3 +0,0 @@ -async (_env, _out) => { - 'page2\n' -}