Skip to content

Commit

Permalink
Readme & tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam committed Aug 26, 2022
1 parent 8033112 commit a166ca3
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ src/version.h:
@printf '// Code generated by `make version.h`; DO NOT EDIT.\n\n#define APP_VERSION "%s"\n' "$(version)" > $@

test: ## Run tests
$(foreach test_dir,$(wildcard tests/*), $(MAKE) -C $(test_dir) test clean;)
@set -e; for n in $(wildcard tests/*); do \
$(MAKE) -C $$n test clean; \
done

clean: ## Cleaning
-rm $(TARGET) src/version.h obj/*.o obj/*.a
104 changes: 98 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ allowed (not $ENV_NAME).

### 🦾 What the `mustpl` can do

> For detailed information about the templating engine please refer to the following link: [mustache manual][mustache]
> For detailed information about the templating engine please refer to the following links - [mustache manual][mustache] and the [library repository](https://gitlab.com/jobol/mustach)
For example, you have the following template data (`data.json`):

Expand Down Expand Up @@ -253,26 +253,92 @@ Let's do the magic!
</html>
```

### ✅ Conditions

You can test the value of the selected key using the following operators:

- `key=value` (matching test)
- `key=!value` (not matching test)
- `key>value` (greater)
- `key>=value` (greater or equal)
- `key<value` (lesser)
- `key<=value` (lesser or equal)

```json
{
"person": {
"name": "Harry",
"age": 37
},
"lang": "fr",
"l10n": {
"en": "Hello",
"fr": "Salut"
}
}
```

```mustach
{{#person.name=Harry}}
Hello Harry!
{{/person.name=Harry}}
{{^person.name=John}}
The person's name is not John.
{{/person.name=John}}
{{#person.age>40}}
He's over 40 years old.
{{/person.age>40}}{{^person.age>40}}
He's definitely not more than 40 years old.
{{/person.age>40}}
{{#lang=fr}}{{ l10n.fr }}{{/lang=fr}}{{#lang=!fr}}{{ l10n.en }}{{/lang=!fr}} {{ person.name }}!
Render only if equals:
- {{ person.age=36 }}
- {{ person.age=37 }}
- {{ person.age=38 }}
```

Will be rendered as follows:

```text
Hello Harry!
The person's name is not John.
He's definitely not more than 40 years old.
Salut Harry!
Render only if equals:
-
- 37
-
```

### 🔄 Loops

Okay, but what about the **loops**? Here you go:
Okay, but what about the **loops**? Here you go (the value of the current field can be accessed using single dot `{{ . }}`):

```json
{
"servers": [
{
"listen": 8080,
"names": [
{"name": "example.com"}
"example.com"
],
"is_default": true,
"home": "/www/example.com"
},
{
"listen": 1088,
"names": [
{"name": "127-0-0-1.nip.io"},
{"name": "127-0-0-2.nip.io"}
"127-0-0-1.nip.io",
"127-0-0-2.nip.io"
],
"home": "/www/local"
}
Expand All @@ -284,7 +350,7 @@ Okay, but what about the **loops**? Here you go:
{{#servers}}
server { {{! just a comment, will not be rendered }}
listen {{ listen }};
server_name{{#names}} {{ name }}{{/names}}{{#is_default}} default_server{{/is_default}};
server_name{{#names}} {{ . }}{{/names}}{{#is_default}} default_server{{/is_default}};
location / {
root {{ home }};
Expand Down Expand Up @@ -316,6 +382,32 @@ server {
}
```

In addition, you can use the pattern `{{#X.*}} ... {{/X.*}}` to iterate on fields of `X` :

```json
{
"people": {
"John": 27,
"Mark": "32"
}
}
```

```mustach
{{#people.*}}
- {{ * }} is {{ . }} years old
{{/people.*}}
```

Produces:

```text
- John is 27 years old
- Mark is 32 years old
```

Here the single star `{{ * }}` is replaced by the iterated key and the single dot `{{ . }}` is replaced by its value.

### 🚩 Template data providing using options

You can provide your template data from cli using the `-d` (`--data`) flag:
Expand Down
12 changes: 12 additions & 0 deletions tests/readme-conditions/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/make

.PHONY: test clean

test:
../../mustpl -f ./give.data.json ./give.template.txt 2> ./stderr.out 1> ./stdout.out; \
exitCode=$$?; if [ $$exitCode -ne 0 ]; then echo "wrong exit code: $$exitCode"; exit 1; fi
diff -u ./want.stderr.txt ./stderr.out
diff -u ./want.stdout.txt ./stdout.out

clean:
-rm -v ./*.out
11 changes: 11 additions & 0 deletions tests/readme-conditions/give.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"person": {
"name": "Harry",
"age": 37
},
"lang": "fr",
"l10n": {
"en": "Hello",
"fr": "Salut"
}
}
20 changes: 20 additions & 0 deletions tests/readme-conditions/give.template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{#person.name=Harry}}
Hello Harry!
{{/person.name=Harry}}

{{^person.name=John}}
The person's name is not John.
{{/person.name=John}}

{{#person.age>40}}
He's over 40 years old.
{{/person.age>40}}{{^person.age>40}}
He's definitely not more than 40 years old.
{{/person.age>40}}

{{#lang=fr}}{{ l10n.fr }}{{/lang=fr}}{{#lang=!fr}}{{ l10n.en }}{{/lang=!fr}} {{ person.name }}!

Render only if equals:
- {{ person.age=36 }}
- {{ person.age=37 }}
- {{ person.age=38 }}
Empty file.
13 changes: 13 additions & 0 deletions tests/readme-conditions/want.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Hello Harry!

The person's name is not John.


He's definitely not more than 40 years old.

Salut Harry!

Render only if equals:
-
- 37
-
12 changes: 12 additions & 0 deletions tests/readme-loops/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/make

.PHONY: test clean

test:
../../mustpl -f ./give.data.json ./give.template.txt 2> ./stderr.out 1> ./stdout.out; \
exitCode=$$?; if [ $$exitCode -ne 0 ]; then echo "wrong exit code: $$exitCode"; exit 1; fi
diff -u ./want.stderr.txt ./stderr.out
diff -u ./want.stdout.txt ./stdout.out

clean:
-rm -v ./*.out
25 changes: 25 additions & 0 deletions tests/readme-loops/give.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"servers": [
{
"listen": 8080,
"names": [
"example.com"
],
"is_default": true,
"home": "/www/example.com"
},
{
"listen": 1088,
"names": [
"127-0-0-1.nip.io",
"127-0-0-2.nip.io"
],
"home": "/www/local"
}
],

"people": {
"John": 27,
"Mark": "32"
}
}
15 changes: 15 additions & 0 deletions tests/readme-loops/give.template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{#servers}}
server { {{! just a comment, will not be rendered }}
listen {{ listen }};
server_name{{#names}} {{ . }}{{/names}}{{#is_default}} default_server{{/is_default}};

location / {
root {{ home }};
index index.html index.htm;
}
}
{{/servers}}

{{#people.*}}
- {{ * }} is {{ . }} years old
{{/people.*}}
Empty file.
21 changes: 21 additions & 0 deletions tests/readme-loops/want.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server {
listen 8080;
server_name example.com default_server;

location / {
root /www/example.com;
index index.html index.htm;
}
}
server {
listen 1088;
server_name 127-0-0-1.nip.io 127-0-0-2.nip.io;

location / {
root /www/local;
index index.html index.htm;
}
}

- John is 27 years old
- Mark is 32 years old

0 comments on commit a166ca3

Please sign in to comment.