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"
74#include "llvm/ADT/ilist.h"
75#include "llvm/ADT/ilist_node.h"
78#include "llvm/Support/JSON.h"
80#include <functional>
81#include <vector>
82
83namespace llvm::mustache {
84
85using Lambda = std::function<llvm::json::Value()>;
86using SectionLambda = std::function<llvm::json::Value(std::string)>;
87
88class ASTNode;
89using AstPtr = ASTNode *;
92
103
104// A Template represents the container for the AST and the partials
105// and Lambdas that are registered with it.
106class Template {
107public:
108 LLVM_ABI Template(StringRef TemplateStr, MustacheContext &Ctx);
109
110 Template(const Template &) = delete;
111
112 Template &operator=(const Template &) = delete;
113
114 LLVM_ABI Template(Template &&Other) noexcept;
115
116 // Define this in the cpp file to work around ASTNode being an incomplete
117 // type.
119
121
123
124 LLVM_ABI void registerPartial(std::string Name, std::string Partial);
125
126 LLVM_ABI void registerLambda(std::string Name, Lambda Lambda);
127
128 LLVM_ABI void registerLambda(std::string Name, SectionLambda Lambda);
129
130 // By default the Mustache Spec Specifies that HTML special characters
131 // should be escaped. This function allows the user to specify which
132 // characters should be escaped.
134
135private:
136 MustacheContext &Ctx;
137 AstPtr Tree;
138};
139} // namespace llvm::mustache
140
141#endif // LLVM_SUPPORT_MUSTACHE
This file defines the StringMap class.
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition Compiler.h:213
This file supports working with JSON data.
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
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
An intrusive list with ownership and callbacks specified/controlled by ilist_traits,...
Definition ilist.h:328
A Value is an JSON value of unknown type.
Definition JSON.h:290
Template(const Template &)=delete
LLVM_ABI void registerPartial(std::string Name, std::string Partial)
Definition Mustache.cpp:910
Template & operator=(const Template &)=delete
LLVM_ABI void registerLambda(std::string Name, Lambda Lambda)
Definition Mustache.cpp:917
LLVM_ABI Template(StringRef TemplateStr, MustacheContext &Ctx)
Definition Mustache.cpp:929
Template & operator=(Template &&)=delete
LLVM_ABI void render(const llvm::json::Value &Data, llvm::raw_ostream &OS)
Definition Mustache.cpp:905
LLVM_ABI void overrideEscapeCharacters(DenseMap< char, std::string > Escapes)
Definition Mustache.cpp:925
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This file defines classes to implement an intrusive doubly linked list class (i.e.
This file defines the ilist_node class template, which is a convenient base class for creating classe...
iplist< ASTNode > ASTNodeList
Definition Mustache.h:91
std::function< llvm::json::Value(std::string)> SectionLambda
Definition Mustache.h:86
std::function< llvm::json::Value()> Lambda
Definition Mustache.h:85
ASTNode * AstPtr
Definition Mustache.h:89
DenseMap< char, std::string > EscapeMap
Definition Mustache.h:90
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
@ Other
Any other memory.
Definition ModRef.h:68
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
StringMap< Lambda > Lambdas
Definition Mustache.h:99
StringMap< SectionLambda > SectionLambdas
Definition Mustache.h:100
BumpPtrAllocator & Allocator
Definition Mustache.h:96
MustacheContext(BumpPtrAllocator &Allocator, StringSaver &Saver)
Definition Mustache.h:94
StringMap< AstPtr > Partials
Definition Mustache.h:98