Add support for supplying multiple templates to the dustjs shim to support testing partials.
This commit is contained in:
parent
608c55575e
commit
7e0a617ba6
@ -14,3 +14,5 @@ path = "src/bin.rs"
|
||||
|
||||
[dependencies]
|
||||
nom = { git = "https://github.com/tomalexander/nom.git", branch = "take_until_parser_matches" }
|
||||
serde = "1.0.106"
|
||||
serde_json = "1.0.51"
|
||||
|
@ -4,4 +4,4 @@ An implementation of the [LinkedIn fork of DustJS](https://www.dustjs.com/) writ
|
||||
|
||||
**NOT RECOMMENDED FOR PUBLIC USE**
|
||||
|
||||
This code is available free and open source under the [0BSD](https://choosealicense.com/licenses/0bsd/), but it is a very early-stage project. You're welcome to use it, fork it, print it out and fold it into a hat, etc... but you will find that this project is not yet polished nor feature complete. While this repository uses the 0BSD license which does not require the inclusion of a copyright notice/text in any distribution, it depends on [nom](https://github.com/Geal/nom) which is under the MIT license and the Rust standard library which is [dual licensed](https://github.com/rust-lang/rust/issues/67014).
|
||||
This code is available free and open source under the [0BSD](https://choosealicense.com/licenses/0bsd/), but it is a very early-stage project. You're welcome to use it, fork it, print it out and fold it into a hat, etc... but you will find that this project is not yet polished nor feature complete. While this repository uses the 0BSD license which does not require the inclusion of a copyright notice/text in any distribution, it depends on [nom](https://github.com/Geal/nom) which is under the MIT license, the Rust standard library which is [dual licensed](https://github.com/rust-lang/rust/issues/67014), serde_json which is [dual licensed](https://github.com/serde-rs/json), and serde which is [dual licensed](https://github.com/serde-rs/serde).
|
||||
|
19
src/bin.rs
19
src/bin.rs
@ -1,9 +1,24 @@
|
||||
extern crate nom;
|
||||
|
||||
use parser::template;
|
||||
use std::io::{self, Read};
|
||||
|
||||
mod parser;
|
||||
|
||||
fn main() {
|
||||
let parsed_template = template("{#foo.bar}hello {name}{/foo.bar}!");
|
||||
println!("{:?}", parsed_template);
|
||||
let context = read_context_from_stdin();
|
||||
println!("{:?}", context);
|
||||
}
|
||||
|
||||
fn read_context_from_stdin() -> serde_json::map::Map<String, serde_json::Value> {
|
||||
let mut buffer = String::new();
|
||||
io::stdin()
|
||||
.read_to_string(&mut buffer)
|
||||
.expect("Failed to read stdin");
|
||||
|
||||
let parsed: serde_json::Value = serde_json::from_str(&buffer).expect("Failed to parse json");
|
||||
match parsed {
|
||||
serde_json::Value::Object(obj) => obj,
|
||||
_ => panic!("Expected context to be an object"),
|
||||
}
|
||||
}
|
||||
|
@ -5,5 +5,7 @@ Install LinkedIn's DustJS in order to rust the compliance tests
|
||||
** Running
|
||||
The dustjs_shim expects a path to a template file as the first parameter, and it expects a json context over stdin. For example, to invoke it on the command line you could run:
|
||||
```sh
|
||||
cat test_cases/hello_world/input1.json | node dustjs_shim.js test_cases/hello_world/template.dust
|
||||
cat test_cases/hello_world/input1.json | node dustjs_shim.js test_cases/hello_world/main.dust
|
||||
```
|
||||
|
||||
Each template will be compiled with the same name as the file without the file extension (so main.dust will be compiled as "main"). THe shim renders the first template passed into it (all the others can be accessed as partials).
|
||||
|
@ -1,24 +1,31 @@
|
||||
var dust = require('dustjs-linkedin');
|
||||
var fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
var argv = process.argv.slice(2);
|
||||
if (argv.length != 1) {
|
||||
if (argv.length < 1) {
|
||||
console.error("Expecting only 1 argument (a path to a template)");
|
||||
process.exit(1);
|
||||
}
|
||||
var context = JSON.parse(fs.readFileSync(0, 'utf-8'));
|
||||
var main_template = path.parse(argv[0])["name"];
|
||||
|
||||
for (var i = 0, len = argv.length; i < len; ++i) {
|
||||
var filename = path.parse(argv[i])["name"];
|
||||
|
||||
try {
|
||||
var template_source = fs.readFileSync(argv[i], 'utf-8');
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
var compiled_template = dust.compile(template_source, filename);
|
||||
dust.loadSource(compiled_template);
|
||||
|
||||
try {
|
||||
var template_source = fs.readFileSync(argv[0], 'utf-8');
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
var compiled_template = dust.compile(template_source, "tmpl");
|
||||
dust.loadSource(compiled_template);
|
||||
|
||||
dust.render("tmpl", context, function(err, out) {
|
||||
dust.render(main_template, context, function(err, out) {
|
||||
if(err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
|
Loading…
Reference in New Issue
Block a user