Skip to content

Commit

Permalink
Merge pull request #1 from floatplane/library
Browse files Browse the repository at this point in the history
Do the things to build an Arduino library
  • Loading branch information
floatplane authored Mar 16, 2024
2 parents 9e3cdd4 + 735aed2 commit 906c04d
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 4 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/arduino_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Arduino Lint

on:
push:
branches:
- main
pull_request:

# The concurrency spec means that we'll only run one set of jobs per pull request or push to main.
# If a new push or pull request comes in while a job is running, all jobs in the concurrency group will be cancelled.
concurrency:
group: arduino-lint-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: arduino/arduino-lint-action@v1
with:
compliance: strict
library-manager: submit
project-type: library
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[![.github/workflows/arduino_lint.yml](https://github.com/floatplane/ministache/actions/workflows/arduino_lint.yml/badge.svg)](https://github.com/floatplane/ministache/actions/workflows/arduino_lint.yml)
[![.github/workflows/build.yml](https://github.com/floatplane/ministache/actions/workflows/build.yml/badge.svg)](https://github.com/floatplane/ministache/actions/workflows/build.yml)
[![.github/workflows/test.yml](https://github.com/floatplane/ministache/actions/workflows/test.yml/badge.svg)](https://github.com/floatplane/ministache/actions/workflows/test.yml)
[![.github/workflows/static_analysis.yml](https://github.com/floatplane/ministache/actions/workflows/static_analysis.yml/badge.svg)](https://github.com/floatplane/ministache/actions/workflows/static_analysis.yml)
[![.github/workflows/clangformat.yml](https://github.com/floatplane/ministache/actions/workflows/clangformat.yml/badge.svg)](https://github.com/floatplane/ministache/actions/workflows/clangformat.yml)

# Ministache

A spec-complete implementation of the [Mustache](https://mustache.github.io/) templating language for Arduino.
A spec-complete implementation of the [Mustache](https://mustache.github.io/) templating language for Arduino. A sane alternative to building up complex strings via concatenation and custom code. Very useful for embedded web servers!

## Features

Expand All @@ -20,11 +21,21 @@ Complete support for all elements of the [Mustache core specification](https://g

See the [mustache documentation](https://mustache.github.io/mustache.5.html) for more details on these features.

## Example
## Basics

```c++
ArduinoJson::JsonDocument data;
data[F("subject")] = F("world");
const String output = ministache::render(F("Hello, {{subject}}!"), data);
Serial.println(output); // Hello, world!
```

See [basic.ino](examples/basic/basic.ino) for a sketch demonstrating basic Ministache usage.

## Partials

Partials are a powerful Mustache feature allow you to define a chunk of template code and use it in a loop. See [partials.ino](examples/partials/partials.ino) for a sketch demonstrating how to use partials with Ministache.

## Projects using Ministache

- [floatplane/mitsubishi2MQTT](https://github.com/floatplane/mitsubishi2MQTT)
37 changes: 37 additions & 0 deletions examples/basic/basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <Ministache.h>

/***************************************************
This is a very basic example for the ministache library
(https://github.com/floatplane/ministache).
It shows how the library can be used to render a Mustache template with a JSON object.
For more details on Mustache syntax, see http://mustache.github.io/mustache.5.html
****************************************************/

void setup() {
Serial.begin(115200);
Serial.println("");

// Create a JsonDocument instance to hold the data that we'll use in our template
JsonDocument data;
data["name"] = "World";
data["value"] = 42;

// Create a template string
String templateString = "Hello, {{name}}! The answer is {{value}}.";

// Render the template with the data
String output = ministache::render(templateString, data);

// Print the result
Serial.println(output);

// Expected output:
//
// Hello, World! The answer is 42.
}

void loop() {
delay(500);
}
57 changes: 57 additions & 0 deletions examples/partials/partials.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <Ministache.h>

/***************************************************
This is an example of how to use Mustache partials with the Ministache library
(https://github.com/floatplane/ministache).
For more details on Mustache syntax, see http://mustache.github.io/mustache.5.html
****************************************************/

void setup() {
Serial.begin(115200);
Serial.println("");

// Create a JsonDocument instance to hold the data that we'll use in our template
const char* json = R"""(
{
"people": [
{
"name": "Alice",
"role": "Engineer"
},
{
"name": "Bob",
"role": "Intern"
}
]
}
)""";
JsonDocument data;
deserializeJson(data, json);

// Create a template string that renders the data for a single person. This is a *partial*.
String personString = "Name: {{name}}\tRole: {{role}}\n";

// Create a template string that renders the data for all people. This is the main template.
// Note that it loops over a field called "people" and includes the partial "person" for each of
// them.
String reportString = "People report:\n{{#people}}{{> person}}{{/people}}";

// Render the template with the data. The third argument is the partials list. This
// defines how to map a partial reference like "person" to a particular template
// ("personString").
String output = ministache::render(reportString, data, {{"person", personString}});

// Print the result
Serial.println(output);

// Expected output:
//
// People report:
// Name: Alice Role: Engineer
// Name: Bob Role: Intern
}

void loop() {
delay(500);
}
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ministache KEYWORD1
render KEYWORD2
39 changes: 39 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "Ministache",
"version": "1.0.0",
"description": "Ministache is a small, fast and spec-complete implementation of the Mustache templating language for Arduino. All core Mustache tags are supported: interpolation, partials, sections, inverted sections, custom delimiters, and comments.",
"keywords": ["mustache", "moustache", "text", "text processor", "template", "logic-less", "html"],
"repository": {
"type": "git",
"url": "https://github.com/floatplane/ministache"
},
"authors": [
{
"name": "Brian Sharon",
"url": "https://github.com/floatplane",
"maintainer": true
}
],
"license": "MIT",
"platforms": "*",
"headers": "Ministache.h",
"examples": [
{
"name": "Basic",
"base": "examples/basic",
"files": ["basic.ino"]
},
{
"name": "Partials",
"base": "examples/partials",
"files": ["partials.ino"]
}
],
"dependencies": {
"bblanchon/ArduinoJson": "^7.0.0"
},
"build": {
"unflags": "-std=gnu++11",
"flags": "-std=gnu++17"
}
}
12 changes: 12 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name=Ministache
version=1.0.0
author=Brian Sharon <[email protected]>
maintainer=Brian Sharon <[email protected]>
sentence=Implementation of the Mustache templating language for Arduino
paragraph=Ministache is a small, fast and spec-complete implementation of the Mustache templating language for Arduino.
category=Data Processing
url=https://github.com/floatplane/ministache
architectures=*
includes=Ministache.h
depends=ArduinoJson (>=7.0.0)

2 changes: 1 addition & 1 deletion src/ministache.cpp → src/Ministache.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include "ministache.hpp"
#include "Ministache.h"

#include <Arduino.h>
#include <ArduinoJson.h>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/ministache.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define DOCTEST_CONFIG_IMPLEMENT // REQUIRED: Enable custom main()

#include "ministache.hpp"
#include "Ministache.h"

#include <Arduino.h>
#include <ArduinoJson.h>
Expand Down

0 comments on commit 906c04d

Please sign in to comment.