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
82namespace llvm::mustache {
83
84using Lambda = std::function<llvm::json::Value()>;
85using SectionLambda = std::function<llvm::json::Value(std::string)>;
86
87class ASTNode;
88using AstPtr = ASTNode *;
91
102
103// A Template represents the container for the AST and the partials
104// and Lambdas that are registered with it.
105class Template {
106public:
107 LLVM_ABI Template(StringRef TemplateStr, MustacheContext &Ctx);
108
109 Template(const Template &) = delete;
110
111 Template &operator=(const Template &) = delete;
112
113 LLVM_ABI Template(Template &&Other) noexcept;
114
115 // Define this in the cpp file to work around ASTNode being an incomplete
116 // type.
118
120
122
123 LLVM_ABI void registerPartial(std::string Name, std::string Partial);
124
125 LLVM_ABI void registerLambda(std::string Name, Lambda Lambda);
126
127 LLVM_ABI void registerLambda(std::string Name, SectionLambda Lambda);
128
129 // By default the Mustache Spec Specifies that HTML special characters
130 // should be escaped. This function allows the user to specify which
131 // characters should be escaped.
133
134private:
135 MustacheContext &Ctx;
136 AstPtr Tree;
137};
138} // namespace llvm::mustache
139
140#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:893
Template & operator=(const Template &)=delete
LLVM_ABI void registerLambda(std::string Name, Lambda Lambda)
Definition Mustache.cpp:900
LLVM_ABI Template(StringRef TemplateStr, MustacheContext &Ctx)
Definition Mustache.cpp:912
Template & operator=(Template &&)=delete
LLVM_ABI void render(const llvm::json::Value &Data, llvm::raw_ostream &OS)
Definition Mustache.cpp:888
LLVM_ABI void overrideEscapeCharacters(DenseMap< char, std::string > Escapes)
Definition Mustache.cpp:908
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:90
std::function< llvm::json::Value(std::string)> SectionLambda
Definition Mustache.h:85
std::function< llvm::json::Value()> Lambda
Definition Mustache.h:84
ASTNode * AstPtr
Definition Mustache.h:88
DenseMap< char, std::string > EscapeMap
Definition Mustache.h:89
@ Other
Any other memory.
Definition ModRef.h:68
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
StringMap< Lambda > Lambdas
Definition Mustache.h:98
StringMap< SectionLambda > SectionLambdas
Definition Mustache.h:99
BumpPtrAllocator & Allocator
Definition Mustache.h:95
MustacheContext(BumpPtrAllocator &Allocator, StringSaver &Saver)
Definition Mustache.h:93
StringMap< AstPtr > Partials
Definition Mustache.h:97