LLVM 22.0.0git
Mustache.h
Go to the documentation of this file.
1//===--- Mustache.h ---------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of the Mustache templating language supports version 1.4.2
10// currently relies on llvm::json::Value for data input.
11// See the Mustache spec for more information
12// (https://mustache.github.io/mustache.5.html).
13//
14// Current Features Supported:
15// - Variables
16// - Sections
17// - Inverted Sections
18// - Partials
19// - Comments
20// - Lambdas
21// - Unescaped Variables
22//
23// Features Not Supported:
24// - Set Delimiter
25// - Blocks
26// - Parents
27// - Dynamic Names
28//
29// The Template class is a container class that outputs the Mustache template
30// string and is the main class for users. It stores all the lambdas and the
31// ASTNode Tree. When the Template is instantiated it tokenizes the Template
32// String and creates a vector of Tokens. Then it calls a basic recursive
33// descent parser to construct the ASTNode Tree. The ASTNodes are all stored
34// in an arena allocator which is freed once the template class goes out of
35// scope.
36//
37// Usage:
38// \code
39// // Creating a simple template and rendering it
40// auto Template = Template("Hello, {{name}}!");
41// Value Data = {{"name", "World"}};
42// std::string Out;
43// raw_string_ostream OS(Out);
44// T.render(Data, OS);
45// // Out == "Hello, World!"
46//
47// // Creating a template with a partial and rendering it
48// auto Template = Template("{{>partial}}");
49// Template.registerPartial("partial", "Hello, {{name}}!");
50// Value Data = {{"name", "World"}};
51// std::string Out;
52// raw_string_ostream OS(Out);
53// T.render(Data, OS);
54// // Out == "Hello, World!"
55//
56// // Creating a template with a lambda and rendering it
57// Value D = Object{};
58// auto T = Template("Hello, {{lambda}}!");
59// Lambda L = []() -> llvm::json::Value { return "World"; };
60// T.registerLambda("lambda", L);
61// std::string Out;
62// raw_string_ostream OS(Out);
63// T.render(D, OS);
64// // Out == "Hello, World!"
65// \endcode
66//
67//===----------------------------------------------------------------------===//
68
69#ifndef LLVM_SUPPORT_MUSTACHE
70#define LLVM_SUPPORT_MUSTACHE
71
72#include "Error.h"
73#include "llvm/ADT/StringMap.h"
76#include "llvm/Support/JSON.h"
78#include <functional>
79#include <vector>
80
81namespace llvm::mustache {
82
83using Lambda = std::function<llvm::json::Value()>;
84using SectionLambda = std::function<llvm::json::Value(std::string)>;
85
86class ASTNode;
87using AstPtr = std::unique_ptr<ASTNode>;
88
89// A Template represents the container for the AST and the partials
90// and Lambdas that are registered with it.
91class Template {
92public:
93 LLVM_ABI Template(StringRef TemplateStr);
94
95 Template(const Template &) = delete;
96
97 Template &operator=(const Template &) = delete;
98
99 LLVM_ABI Template(Template &&Other) noexcept;
100
101 // Define this in the cpp file to work around ASTNode being an incomplete
102 // type.
104
106
108
109 LLVM_ABI void registerPartial(std::string Name, std::string Partial);
110
111 LLVM_ABI void registerLambda(std::string Name, Lambda Lambda);
112
114
115 // By default the Mustache Spec Specifies that HTML special characters
116 // should be escaped. This function allows the user to specify which
117 // characters should be escaped.
119
120private:
121 StringMap<AstPtr> Partials;
122 StringMap<Lambda> Lambdas;
123 StringMap<SectionLambda> SectionLambdas;
125 AstPtr Tree;
126};
127} // namespace llvm::mustache
128
129#endif // LLVM_SUPPORT_MUSTACHE
This file defines the StringMap class.
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition: Compiler.h:213
std::string Name
This file supports working with JSON data.
raw_pwrite_stream & OS
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A Value is an JSON value of unknown type.
Definition: JSON.h:288
Template(const Template &)=delete
LLVM_ABI void registerPartial(std::string Name, std::string Partial)
Definition: Mustache.cpp:724
Template & operator=(const Template &)=delete
LLVM_ABI void registerLambda(std::string Name, Lambda Lambda)
Definition: Mustache.cpp:730
LLVM_ABI void render(const llvm::json::Value &Data, llvm::raw_ostream &OS)
Definition: Mustache.cpp:720
LLVM_ABI void overrideEscapeCharacters(DenseMap< char, std::string > Escapes)
Definition: Mustache.cpp:736
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
std::function< llvm::json::Value()> Lambda
Definition: Mustache.h:83
std::function< llvm::json::Value(std::string)> SectionLambda
Definition: Mustache.h:84
std::unique_ptr< ASTNode > AstPtr
Definition: Mustache.h:87
@ Other
Any other memory.