Line data Source code
1 : /**
2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef EXTRA_WEBSERVER_PUG_SPPUGTEMPLATE_H_
24 : #define EXTRA_WEBSERVER_PUG_SPPUGTEMPLATE_H_
25 :
26 : #include "SPPugLexer.h"
27 :
28 : namespace STAPPLER_VERSIONIZED stappler::pug {
29 :
30 : class Template : public memory::AllocPool {
31 : public:
32 : using OutStream = Callback<void(StringView)>;
33 :
34 : enum ChunkType {
35 : Block,
36 : HtmlTag,
37 : HtmlInlineTag,
38 : HtmlEntity,
39 : OutputEscaped,
40 : OutputUnescaped,
41 : AttributeEscaped,
42 : AttributeUnescaped,
43 : AttributeList,
44 : Code,
45 :
46 : ControlCase,
47 : ControlWhen,
48 : ControlDefault,
49 :
50 : ControlIf,
51 : ControlUnless,
52 : ControlElseIf,
53 : ControlElse,
54 :
55 : ControlEach,
56 : ControlEachPair,
57 :
58 : ControlWhile,
59 :
60 : Include,
61 :
62 : ControlMixin,
63 :
64 : MixinCall,
65 :
66 : VirtualTag,
67 : };
68 :
69 : struct Chunk {
70 : ChunkType type = Block;
71 : String value;
72 : Expression *expr = nullptr;
73 : size_t indent = 0;
74 : Vector<Chunk *> chunks;
75 : };
76 :
77 : struct Options {
78 : enum Flags {
79 : Pretty,
80 : StopOnError,
81 : LineFeeds,
82 : };
83 :
84 : static Options getDefault();
85 : static Options getPretty();
86 :
87 : Options &setFlags(std::initializer_list<Flags> &&);
88 : Options &clearFlags(std::initializer_list<Flags> &&);
89 :
90 : bool hasFlag(Flags) const;
91 :
92 : std::bitset<toInt(Flags::LineFeeds) + 1> flags;
93 : };
94 :
95 : struct RunContext {
96 : Vector<const Template *> templateStack;
97 : Vector<Template::Chunk *> tagStack;
98 : bool withinHead = false;
99 : bool withinBody = false;
100 : Options opts;
101 : };
102 :
103 : static Template *read(const StringView &, const Options & = Options::getDefault(),
104 : const Callback<void(StringView)> &err = nullptr);
105 :
106 : static Template *read(memory::pool_t *, const StringView &, const Options & = Options::getDefault(),
107 : const Callback<void(StringView)> &err = nullptr);
108 :
109 400 : Options getOptions() const { return _opts; }
110 :
111 : bool run(Context &, const OutStream &) const;
112 : bool run(Context &, const OutStream &, const Options &opts) const;
113 : bool run(Context &, const OutStream &, RunContext &opts) const;
114 :
115 : void describe(const OutStream &stream, bool tokens = false) const;
116 :
117 : protected:
118 : Template(memory::pool_t *, const StringView &, const Options &opts, const OutStream &err);
119 :
120 : bool runChunk(const Chunk &chunk, Context &, const OutStream &, RunContext &) const;
121 : bool runCase(const Chunk &chunk, Context &, const OutStream &, RunContext &) const;
122 :
123 : void pushWithPrettyFilter(StringView, size_t indent, const OutStream &) const;
124 :
125 : memory::pool_t *_pool;
126 : Lexer _lexer;
127 : Time _mtime;
128 : Chunk _root;
129 : Options _opts;
130 :
131 : Vector<StringView> _includes;
132 : };
133 :
134 : }
135 :
136 : #endif /* EXTRA_WEBSERVER_PUG_SPPUGTEMPLATE_H_ */
|